Skip to content

Commit

Permalink
GH-3332: DirectTypeHierarchyInferencer queries are broken and tests a…
Browse files Browse the repository at this point in the history
…re missing (#4698)
  • Loading branch information
hmottestad authored Jul 29, 2023
2 parents a9abf6e + 77853bf commit bc1ce6c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ public class DirectTypeHierarchyInferencer extends NotifyingSailWrapper {
DIRECT_SUBCLASSOF_QUERY = QueryParserUtil.parseGraphQuery(QueryLanguage.SPARQL,
"CONSTRUCT { ?X sesame:directSubClassOf ?Y } "
+ "WHERE { ?X rdfs:subClassOf ?Y . "
+ "FILTER X != Y AND "
+ "NOT EXISTS { SELECT ?Z WHERE { ?X rdfs:subClassOf ?Z. ?Z rdfs:subClassOf ?Y . FILTER ?X != ?Z AND ?Z != ?Y }}}",
+ "FILTER (?X != ?Y && "
+ "NOT EXISTS { SELECT ?Z WHERE { ?X rdfs:subClassOf ?Z. ?Z rdfs:subClassOf ?Y . FILTER (?X != ?Z && ?Z != ?Y) }})}",
null);

DIRECT_SUBPROPERTYOF_QUERY = QueryParserUtil.parseGraphQuery(QueryLanguage.SPARQL,
"CONSTRUCT { ?X sesame:directSubPropertyOf ?Y } "
+ "WHERE { ?X rdfs:subPropertyOf ?Y . "
+ "FILTER X != Y AND "
+ "NOT EXISTS { SELECT ?Z WHERE { ?X rdfs:subPropertyOf ?Z. ?Z rdfs:subPropertyOf ?Y . FILTER ?X != ?Z AND ?Z != ?Y }}}",
+ "FILTER (?X != ?Y && "
+ "NOT EXISTS { SELECT ?Z WHERE { ?X rdfs:subPropertyOf ?Z. ?Z rdfs:subPropertyOf ?Y . FILTER (?X != ?Z && ?Z != ?Y) }})}",
null);

DIRECT_TYPE_QUERY = QueryParserUtil.parseGraphQuery(QueryLanguage.SPARQL,
"CONSTRUCT { ?X sesame:directType ?Y } WHERE { ?X rdf:type ?Y . \n "
+ "FILTER NOT EXISTS { SELECT ?Z WHERE { ?X rdf:type ?Z. ?Z rdfs:subClassOf ?Y . FILTER ?Z != ?Y }}}",
+ "FILTER NOT EXISTS { SELECT ?Z WHERE { ?X rdf:type ?Z. ?Z rdfs:subClassOf ?Y . FILTER (?Z != ?Y) }}}",
null);
} catch (MalformedQueryException e) {
// Can only occur due to a bug in this code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 2023 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
package org.eclipse.rdf4j.sail.inferencer.fc;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.StringReader;

import org.assertj.core.api.Assertions;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.query.QueryResults;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.repository.util.RDFLoader;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class DirectTypeHierarchyInferencerTest {

@BeforeAll
public static void setUpClass() throws Exception {
System.setProperty("org.eclipse.rdf4j.repository.debug", "true");
}

@AfterAll
public static void afterClass() throws Exception {
System.setProperty("org.eclipse.rdf4j.repository.debug", "false");
}

@Test
public void testDirectTypeHierarchyInference() throws IOException {
SailRepository rep = new SailRepository(new DirectTypeHierarchyInferencer(new MemoryStore()));
try {
rep.init();

Model inputModel = Rio.parse(this.getClass().getResourceAsStream("direct-type-hierarchy-test-in.nt"),
RDFFormat.NTRIPLES);
Model expectedEntailedStatements = Rio.parse(
this.getClass().getResourceAsStream("direct-type-hierarchy-test-out.nt"),
RDFFormat.NTRIPLES);

try (RepositoryConnection con = rep.getConnection()) {
con.begin();
con.add(inputModel);
con.commit();
}

Model entailedStatements;
try (RepositoryConnection con = rep.getConnection()) {
entailedStatements = QueryResults.asModel(con.getStatements(null, null, null,
true));
entailedStatements.removeAll(inputModel);
}

assertThat(entailedStatements).hasSameElementsAs(expectedEntailedStatements);

} finally {
rep.shutDown();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<http://example.org/ClassA> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassA> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassA> .
<http://example.org/ClassB> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassB> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassA> .
<http://example.org/ClassB> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassB> .
<http://example.org/ClassC> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassC> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassA> .
<http://example.org/ClassC> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassB> .
<http://example.org/ClassC> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/ClassC> .
<http://example.org/propA> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propA> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propA> .
<http://example.org/propB> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propB> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propA> .
<http://example.org/propB> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propB> .
<http://example.org/propC> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propC> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propA> .
<http://example.org/propC> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propB> .
<http://example.org/propC> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> <http://example.org/propC> .
<http://example.org/instA> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassA> .
<http://example.org/instB> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassA> .
<http://example.org/instB> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassB> .
<http://example.org/instC> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassA> .
<http://example.org/instC> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassB> .
<http://example.org/instC> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ClassC> .
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<http://example.org/ClassA> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassB> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassB> <http://www.openrdf.org/schema/sesame#directSubClassOf> <http://example.org/ClassA> .
<http://example.org/ClassC> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://example.org/ClassC> <http://www.openrdf.org/schema/sesame#directSubClassOf> <http://example.org/ClassB> .
<http://example.org/propA> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propB> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propB> <http://www.openrdf.org/schema/sesame#directSubPropertyOf> <http://example.org/propA> .
<http://example.org/propC> <http://www.openrdf.org/schema/sesame#directType> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.org/propC> <http://www.openrdf.org/schema/sesame#directSubPropertyOf> <http://example.org/propB> .
<http://example.org/instA> <http://www.openrdf.org/schema/sesame#directType> <http://example.org/ClassA> .
<http://example.org/instB> <http://www.openrdf.org/schema/sesame#directType> <http://example.org/ClassB> .
<http://example.org/instC> <http://www.openrdf.org/schema/sesame#directType> <http://example.org/ClassC> .

0 comments on commit bc1ce6c

Please sign in to comment.