From ed223f63f6bbc5b96e8b5562fcc2de69e983902b Mon Sep 17 00:00:00 2001 From: "eric.white" Date: Thu, 17 Aug 2017 16:52:49 -0400 Subject: [PATCH] RYA-292 PR Update #2 --- .../api/RdfCloudTripleStoreConfiguration.java | 155 ++++++++++-------- .../inference/InferenceEngine.java | 38 ++++- .../inference/IntersectionOfVisitor.java | 2 +- .../inference/InferenceEngineTest.java | 94 +++++------ .../inference/IntersectionOfVisitorTest.java | 47 ++++++ 5 files changed, 217 insertions(+), 119 deletions(-) diff --git a/common/rya.api/src/main/java/org/apache/rya/api/RdfCloudTripleStoreConfiguration.java b/common/rya.api/src/main/java/org/apache/rya/api/RdfCloudTripleStoreConfiguration.java index c72b85a92..ee91e6b03 100644 --- a/common/rya.api/src/main/java/org/apache/rya/api/RdfCloudTripleStoreConfiguration.java +++ b/common/rya.api/src/main/java/org/apache/rya/api/RdfCloudTripleStoreConfiguration.java @@ -26,12 +26,11 @@ import java.util.List; import java.util.Set; +import org.apache.hadoop.conf.Configuration; import org.apache.rya.api.domain.RyaURI; import org.apache.rya.api.layout.TableLayoutStrategy; import org.apache.rya.api.layout.TablePrefixLayoutStrategy; import org.apache.rya.api.persist.RdfEvalStatsDAO; - -import org.apache.hadoop.conf.Configuration; import org.openrdf.query.algebra.evaluation.QueryOptimizer; import com.google.common.base.Joiner; @@ -59,7 +58,7 @@ public abstract class RdfCloudTripleStoreConfiguration extends Configuration { public static final String CONF_LIMIT = "query.limit"; public static final String CONF_QUERYPLAN_FLAG = "query.printqueryplan"; public static final String CONF_QUERY_AUTH = "query.auth"; - public static final String CONF_RESULT_FORMAT = "query.resultformat"; + public static final String CONF_RESULT_FORMAT = "query.resultformat"; public static final String CONF_CV = "conf.cv"; public static final String CONF_TBL_SPO = "tbl.spo"; public static final String CONF_TBL_PO = "tbl.po"; @@ -73,6 +72,7 @@ public abstract class RdfCloudTripleStoreConfiguration extends Configuration { public static final String CONF_STATEMENT_METADATA_PROPERTIES = "statement.metadata.properites"; public static final String CONF_USE_STATEMENT_METADATA = "use.statement.metadata"; public static final String STATS_PUSH_EMPTY_RDFTYPE_DOWN = "conf.stats.rdftype.down"; + public static final String INFER_INCLUDE_INTERSECTION_OF = "infer.include.intersectionof"; public static final String INFER_INCLUDE_INVERSEOF = "infer.include.inverseof"; public static final String INFER_INCLUDE_SUBCLASSOF = "infer.include.subclassof"; public static final String INFER_INCLUDE_SUBPROPOF = "infer.include.subpropof"; @@ -92,7 +92,7 @@ public abstract class RdfCloudTripleStoreConfiguration extends Configuration { public RdfCloudTripleStoreConfiguration() { } - public RdfCloudTripleStoreConfiguration(Configuration other) { + public RdfCloudTripleStoreConfiguration(final Configuration other) { super(other); if (other instanceof RdfCloudTripleStoreConfiguration) { setTableLayoutStrategy(((RdfCloudTripleStoreConfiguration) other).getTableLayoutStrategy()); @@ -100,13 +100,13 @@ public RdfCloudTripleStoreConfiguration(Configuration other) { } @Override - public abstract RdfCloudTripleStoreConfiguration clone(); + public abstract RdfCloudTripleStoreConfiguration clone(); public TableLayoutStrategy getTableLayoutStrategy() { return tableLayoutStrategy; } - public void setTableLayoutStrategy(TableLayoutStrategy tableLayoutStrategy) { + public void setTableLayoutStrategy(final TableLayoutStrategy tableLayoutStrategy) { if (tableLayoutStrategy != null) { this.tableLayoutStrategy = tableLayoutStrategy; } else { @@ -120,28 +120,28 @@ public void setTableLayoutStrategy(TableLayoutStrategy tableLayoutStrategy) { } public Long getTtl() { - String val = get(CONF_TTL); + final String val = get(CONF_TTL); if (val != null) { return Long.valueOf(val); } return null; } - public void setTtl(Long ttl) { + public void setTtl(final Long ttl) { Preconditions.checkNotNull(ttl); Preconditions.checkArgument(ttl >= 0, "ttl must be non negative"); set(CONF_TTL, ttl.toString()); } public Long getStartTime() { - String val = get(CONF_STARTTIME); + final String val = get(CONF_STARTTIME); if (val != null) { return Long.valueOf(val); } return null; } - public void setStartTime(Long startTime) { + public void setStartTime(final Long startTime) { Preconditions.checkNotNull(startTime); Preconditions.checkArgument(startTime >= 0, "startTime must be non negative"); set(CONF_STARTTIME, startTime.toString()); @@ -151,7 +151,7 @@ public Integer getNumThreads() { return getInt(CONF_NUM_THREADS, 2); } - public void setNumThreads(Integer numThreads) { + public void setNumThreads(final Integer numThreads) { Preconditions.checkNotNull(numThreads); Preconditions.checkArgument(numThreads > 0, "numThreads must be greater than 0"); setInt(CONF_NUM_THREADS, numThreads); @@ -161,7 +161,7 @@ public Boolean isPerformant() { return getBoolean(CONF_PERFORMANT, true); } - public void setPerformant(Boolean val) { + public void setPerformant(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_PERFORMANT, val); } @@ -170,7 +170,7 @@ public Boolean isInfer() { return getBoolean(CONF_INFER, false); } - public void setInfer(Boolean val) { + public void setInfer(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_INFER, val); } @@ -179,7 +179,7 @@ public Boolean isUseStats() { return getBoolean(CONF_USE_STATS, false); } - public void setUseStats(Boolean val) { + public void setUseStats(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_USE_STATS, val); } @@ -188,7 +188,7 @@ public Boolean isUseSelectivity() { return getBoolean(CONF_USE_SELECTIVITY, false); } - public void setUseSelectivity(Boolean val) { + public void setUseSelectivity(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_USE_SELECTIVITY, val); } @@ -197,7 +197,7 @@ public Boolean isPrefixRowsWithHash() { return getBoolean(CONF_PREFIX_ROW_WITH_HASH, false); } - public void setPrefixRowsWithHash(Boolean val) { + public void setPrefixRowsWithHash(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_PREFIX_ROW_WITH_HASH, val); } @@ -206,49 +206,49 @@ public String getTablePrefix() { return get(CONF_TBL_PREFIX, RdfCloudTripleStoreConstants.TBL_PRFX_DEF); } - public void setTablePrefix(String tablePrefix) { + public void setTablePrefix(final String tablePrefix) { Preconditions.checkNotNull(tablePrefix); set(CONF_TBL_PREFIX, tablePrefix); setTableLayoutStrategy(new TablePrefixLayoutStrategy(tablePrefix)); //TODO: Should we change the layout strategy } public Integer getBatchSize() { - String val = get(CONF_BATCH_SIZE); + final String val = get(CONF_BATCH_SIZE); if (val != null) { return Integer.valueOf(val); } return null; } - public void setBatchSize(Long batchSize) { + public void setBatchSize(final Long batchSize) { Preconditions.checkNotNull(batchSize); Preconditions.checkArgument(batchSize > 0, "Batch Size must be greater than 0"); setLong(CONF_BATCH_SIZE, batchSize); } public Long getOffset() { - String val = get(CONF_OFFSET); + final String val = get(CONF_OFFSET); if (val != null) { return Long.valueOf(val); } return null; } - public void setOffset(Long offset) { + public void setOffset(final Long offset) { Preconditions.checkNotNull(offset); Preconditions.checkArgument(offset >= 0, "offset must be positive"); setLong(CONF_OFFSET, offset); } public Long getLimit() { - String val = get(CONF_LIMIT); + final String val = get(CONF_LIMIT); if (val != null) { return Long.valueOf(val); } return null; } - public void setLimit(Long limit) { + public void setLimit(final Long limit) { Preconditions.checkNotNull(limit); Preconditions.checkArgument(limit >= 0, "limit must be positive"); setLong(CONF_LIMIT, limit); @@ -259,7 +259,7 @@ public Boolean isDisplayQueryPlan() { return getBoolean(CONF_QUERYPLAN_FLAG, false); } - public void setDisplayQueryPlan(Boolean val) { + public void setDisplayQueryPlan(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_QUERYPLAN_FLAG, val); } @@ -269,7 +269,7 @@ public void setDisplayQueryPlan(Boolean val) { * @deprecated */ @Deprecated - public String getAuth() { + public String getAuth() { return Joiner.on(",").join(getAuths()); } @@ -278,7 +278,7 @@ public String getAuth() { * @deprecated */ @Deprecated - public void setAuth(String auth) { + public void setAuth(final String auth) { Preconditions.checkNotNull(auth); setStrings(CONF_QUERY_AUTH, auth); } @@ -287,25 +287,25 @@ public String[] getAuths() { return getStrings(CONF_QUERY_AUTH, EMPTY_STR_ARR); } - public void setAuths(String... auths) { + public void setAuths(final String... auths) { Preconditions.checkNotNull(auths); setStrings(CONF_QUERY_AUTH, auths); } - public String getEmit() { - return get(CONF_RESULT_FORMAT); + public String getEmit() { + return get(CONF_RESULT_FORMAT); } - public void setEmit(String emit) { - Preconditions.checkNotNull(emit); - set(CONF_RESULT_FORMAT, emit); + public void setEmit(final String emit) { + Preconditions.checkNotNull(emit); + set(CONF_RESULT_FORMAT, emit); } public String getCv() { return get(CONF_CV); } - public void setCv(String cv) { + public void setCv(final String cv) { Preconditions.checkNotNull(cv); set(CONF_CV, cv); } @@ -315,7 +315,7 @@ public Boolean isUseCompositeCardinality() { return getBoolean(CONF_USE_COMPOSITE, true); } - public void setCompositeCardinality(Boolean val) { + public void setCompositeCardinality(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(CONF_USE_COMPOSITE, val); } @@ -325,16 +325,35 @@ public Boolean isStatsPushEmptyRdftypeDown() { return getBoolean(STATS_PUSH_EMPTY_RDFTYPE_DOWN, true); } - public void setStatsPushEmptyRdftypeDown(Boolean val) { + public void setStatsPushEmptyRdftypeDown(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(STATS_PUSH_EMPTY_RDFTYPE_DOWN, val); } + /** + * @return {@code true} if owl:intersectionOf inferencing is enabled. + * {@code false} otherwise. Defaults to {@code true} if nothing is + * specified. + */ + public Boolean isInferIntersectionOf() { + return getBoolean(INFER_INCLUDE_INTERSECTION_OF, true); + } + + /** + * Sets whether owl:intersectionOf inferencing is enabled or disabled. + * @param value {@code true} if owl:intersectionOf inferencing is enabled. + * {@code false} otherwise. + */ + public void setInferIntersectionOf(final Boolean value) { + Preconditions.checkNotNull(value); + setBoolean(INFER_INCLUDE_INTERSECTION_OF, value); + } + public Boolean isInferInverseOf() { return getBoolean(INFER_INCLUDE_INVERSEOF, true); } - public void setInferInverseOf(Boolean val) { + public void setInferInverseOf(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(INFER_INCLUDE_INVERSEOF, val); } @@ -343,7 +362,7 @@ public Boolean isInferSubClassOf() { return getBoolean(INFER_INCLUDE_SUBCLASSOF, true); } - public void setInferSubClassOf(Boolean val) { + public void setInferSubClassOf(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(INFER_INCLUDE_SUBCLASSOF, val); } @@ -352,7 +371,7 @@ public Boolean isInferSubPropertyOf() { return getBoolean(INFER_INCLUDE_SUBPROPOF, true); } - public void setInferSubPropertyOf(Boolean val) { + public void setInferSubPropertyOf(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(INFER_INCLUDE_SUBPROPOF, val); } @@ -361,7 +380,7 @@ public Boolean isInferSymmetricProperty() { return getBoolean(INFER_INCLUDE_SYMMPROP, true); } - public void setInferSymmetricProperty(Boolean val) { + public void setInferSymmetricProperty(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(INFER_INCLUDE_SYMMPROP, val); } @@ -370,12 +389,12 @@ public Boolean isInferTransitiveProperty() { return getBoolean(INFER_INCLUDE_TRANSITIVEPROP, true); } - public void setInferTransitiveProperty(Boolean val) { + public void setInferTransitiveProperty(final Boolean val) { Preconditions.checkNotNull(val); setBoolean(INFER_INCLUDE_TRANSITIVEPROP, val); } - public void setRdfEvalStatsDaoClass(Class rdfEvalStatsDaoClass) { + public void setRdfEvalStatsDaoClass(final Class rdfEvalStatsDaoClass) { Preconditions.checkNotNull(rdfEvalStatsDaoClass); setClass(RDF_EVAL_STATS_DAO_CLASS, rdfEvalStatsDaoClass, RdfEvalStatsDAO.class); } @@ -385,50 +404,50 @@ public Class getRdfEvalStatsDaoClass() { } - public void setPcjTables(List indexTables) { + public void setPcjTables(final List indexTables) { Preconditions.checkNotNull(indexTables); setStrings(CONF_PCJ_TABLES, indexTables.toArray(new String[]{})); } public List getPcjTables() { - List pcjTables = Lists.newArrayList(); - String[] tables = getStrings(CONF_PCJ_TABLES); + final List pcjTables = Lists.newArrayList(); + final String[] tables = getStrings(CONF_PCJ_TABLES); if(tables == null) { return pcjTables; } - for(String table: tables) { + for(final String table: tables) { Preconditions.checkNotNull(table); pcjTables.add(table); } return pcjTables; } - - public void setUseStatementMetadata(boolean useMetadata) { + + public void setUseStatementMetadata(final boolean useMetadata) { setBoolean(CONF_USE_STATEMENT_METADATA, useMetadata); } - + public boolean getUseStatementMetadata() { return getBoolean(CONF_USE_STATEMENT_METADATA, false); } - - public void setStatementMetadataProperties(Set metadataProperties) { - - String[] propArray = new String[metadataProperties.size()]; + + public void setStatementMetadataProperties(final Set metadataProperties) { + + final String[] propArray = new String[metadataProperties.size()]; int i = 0; - for(RyaURI uri: metadataProperties) { + for(final RyaURI uri: metadataProperties) { propArray[i] = uri.getData(); i++; } setStrings(CONF_STATEMENT_METADATA_PROPERTIES, propArray); } - - + + public Set getStatementMetadataProperties() { - Set uriSet = new HashSet<>(); - String[] uriStrings = getStrings(CONF_STATEMENT_METADATA_PROPERTIES); + final Set uriSet = new HashSet<>(); + final String[] uriStrings = getStrings(CONF_STATEMENT_METADATA_PROPERTIES); if (uriStrings != null) { - for (String s : uriStrings) { + for (final String s : uriStrings) { uriSet.add(new RyaURI(s)); } } @@ -436,13 +455,13 @@ public Set getStatementMetadataProperties() { } - public void setPcjOptimizer(Class optimizer) { + public void setPcjOptimizer(final Class optimizer) { Preconditions.checkNotNull(optimizer); setClass(CONF_PCJ_OPTIMIZER, optimizer, QueryOptimizer.class); } public Class getPcjOptimizer() { - Class opt = getClass(CONF_PCJ_OPTIMIZER, null, QueryOptimizer.class); + final Class opt = getClass(CONF_PCJ_OPTIMIZER, null, QueryOptimizer.class); if (opt != null) { Preconditions.checkArgument(QueryOptimizer.class.isAssignableFrom(opt)); return (Class) opt; @@ -452,10 +471,10 @@ public Class getPcjOptimizer() { } - public void setOptimizers(List> optimizers) { + public void setOptimizers(final List> optimizers) { Preconditions.checkNotNull(optimizers); - List strs = Lists.newArrayList(); - for (Class ai : optimizers){ + final List strs = Lists.newArrayList(); + for (final Class ai : optimizers){ Preconditions.checkNotNull(ai); strs.add(ai.getName()); } @@ -464,8 +483,8 @@ public void setOptimizers(List> optimizers) { } public List> getOptimizers() { - List> opts = Lists.newArrayList(); - for (Class clazz : getClasses(CONF_OPTIMIZERS)){ + final List> opts = Lists.newArrayList(); + for (final Class clazz : getClasses(CONF_OPTIMIZERS)){ Preconditions.checkArgument(QueryOptimizer.class.isAssignableFrom(clazz)); opts.add((Class) clazz); } @@ -479,7 +498,7 @@ public String getRegexSubject() { return get(REGEX_SUBJECT); } - public void setRegexSubject(String regexSubject) { + public void setRegexSubject(final String regexSubject) { Preconditions.checkNotNull(regexSubject); set(REGEX_SUBJECT, regexSubject); } @@ -488,7 +507,7 @@ public String getRegexPredicate() { return get(REGEX_PREDICATE); } - public void setRegexPredicate(String regex) { + public void setRegexPredicate(final String regex) { Preconditions.checkNotNull(regex); set(REGEX_PREDICATE, regex); } @@ -497,7 +516,7 @@ public String getRegexObject() { return get(REGEX_OBJECT); } - public void setRegexObject(String regex) { + public void setRegexObject(final String regex) { Preconditions.checkNotNull(regex); set(REGEX_OBJECT, regex); } diff --git a/sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java b/sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java index 43a54d620..6663ebc7c 100644 --- a/sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java +++ b/sail/src/main/java/org/apache/rya/rdftriplestore/inference/InferenceEngine.java @@ -28,6 +28,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.Stack; import java.util.Timer; @@ -669,7 +670,7 @@ public void handleStatement(final Statement statement) throws RDFHandlerExceptio }); intersections.clear(); - for (final Map.Entry>> entry : intersectionsProp.entrySet()) { + for (final Entry>> entry : intersectionsProp.entrySet()) { final Resource type = entry.getKey(); final List> intersectionList = entry.getValue(); final Set otherTypes = new HashSet<>(); @@ -690,11 +691,40 @@ public void handleStatement(final Statement statement) throws RDFHandlerExceptio } } } + + final List typeStatements = new ArrayList<>(); + ryaDaoQueryWrapper.queryAll(type, OWL.INTERSECTIONOF, null, new RDFHandlerBase() { + @Override + public void handleStatement(final Statement statement) throws RDFHandlerException { + typeStatements.add(statement); + } + }); + + final Set superClasses = getSuperClasses((URI) type); + for (final Set intersection : intersectionList) { + addIntersection(intersection, type); + for (final URI superClass : superClasses) { + // Add intersections to super classes if applicable. + // IF: + // :A intersectionOf[:B, :C] + // AND + // :A subclassOf :D + // Then we can infer: + // intersectionOf[:B, :C] subclassOf :D + for (final Statement statement : typeStatements) { + final Resource intersectionOfBnode = (Resource) statement.getObject(); + addSubClassOf(intersectionOfBnode, superClass); + } + } + } + } + for (final Entry>> entry : intersectionsProp.entrySet()) { + final Resource type = entry.getKey(); + final List> intersectionList = entry.getValue(); for (final Set intersection : intersectionList) { addIntersection(intersection, type); } } - intersections.putAll(intersectionsProp); } /** @@ -761,7 +791,9 @@ private void addIntersection(final Set intersection, final Resource ty if (intersectionList == null) { intersectionList = new ArrayList<>(); } - intersectionList.add(intersection); + if (!intersectionList.contains(intersection)) { + intersectionList.add(intersection); + } intersections.put(type, intersectionList); } } diff --git a/sail/src/main/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitor.java b/sail/src/main/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitor.java index bb73c8708..b4853c0df 100644 --- a/sail/src/main/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitor.java +++ b/sail/src/main/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitor.java @@ -47,7 +47,7 @@ public class IntersectionOfVisitor extends AbstractInferVisitor { */ public IntersectionOfVisitor(final RdfCloudTripleStoreConfiguration conf, final InferenceEngine inferenceEngine) { super(conf, inferenceEngine); - include = true; + include = conf.isInferIntersectionOf(); } @Override diff --git a/sail/src/test/java/org/apache/rya/rdftriplestore/inference/InferenceEngineTest.java b/sail/src/test/java/org/apache/rya/rdftriplestore/inference/InferenceEngineTest.java index 885bd03fe..f02ff44be 100644 --- a/sail/src/test/java/org/apache/rya/rdftriplestore/inference/InferenceEngineTest.java +++ b/sail/src/test/java/org/apache/rya/rdftriplestore/inference/InferenceEngineTest.java @@ -91,7 +91,7 @@ public void tearDown() throws Exception { @Test public void testSubClassGraph() throws Exception { - String insert = "INSERT DATA { GRAPH {\n" + final String insert = "INSERT DATA { GRAPH {\n" + " rdfs:subClassOf . \n" + " rdfs:subClassOf . \n" + " rdfs:subClassOf . \n" @@ -102,20 +102,20 @@ public void testSubClassGraph() throws Exception { + "}}"; conn.prepareUpdate(QueryLanguage.SPARQL, insert).execute(); inferenceEngine.refreshGraph(); - Graph graph = inferenceEngine.getSubClassOfGraph(); - URI a = vf.createURI("urn:A"); - URI b = vf.createURI("urn:B"); - URI c = vf.createURI("urn:C"); - URI d = vf.createURI("urn:D"); - URI e = vf.createURI("urn:E"); - URI f = vf.createURI("urn:F"); - URI g = vf.createURI("urn:G"); - URI z = vf.createURI("urn:Z"); - URI missing = vf.createURI("urn:Missing"); - Set empty = new HashSet<>(); - Set belowLevel2 = new HashSet<>(Arrays.asList(new URI[] { a, b })); - Set belowLevel3 = new HashSet<>(Arrays.asList(new URI[] { a, b, c, d, e })); - Set belowLevel4 = new HashSet<>(Arrays.asList(new URI[] { a, b, c, d, e, f, g })); + final Graph graph = inferenceEngine.getSubClassOfGraph(); + final URI a = vf.createURI("urn:A"); + final URI b = vf.createURI("urn:B"); + final URI c = vf.createURI("urn:C"); + final URI d = vf.createURI("urn:D"); + final URI e = vf.createURI("urn:E"); + final URI f = vf.createURI("urn:F"); + final URI g = vf.createURI("urn:G"); + final URI z = vf.createURI("urn:Z"); + final URI missing = vf.createURI("urn:Missing"); + final Set empty = new HashSet<>(); + final Set belowLevel2 = new HashSet<>(Arrays.asList(new URI[] { a, b })); + final Set belowLevel3 = new HashSet<>(Arrays.asList(new URI[] { a, b, c, d, e })); + final Set belowLevel4 = new HashSet<>(Arrays.asList(new URI[] { a, b, c, d, e, f, g })); Assert.assertEquals(empty, inferenceEngine.findParents(graph, a)); Assert.assertEquals(empty, inferenceEngine.findParents(graph, b)); Assert.assertEquals(empty, inferenceEngine.findParents(graph, z)); @@ -129,7 +129,7 @@ public void testSubClassGraph() throws Exception { @Test public void testSubPropertyGraph() throws Exception { - String insert = "INSERT DATA { GRAPH {\n" + final String insert = "INSERT DATA { GRAPH {\n" + " rdfs:subPropertyOf . \n" + " rdfs:subPropertyOf . \n" + " owl:equivalentProperty . \n" @@ -141,21 +141,21 @@ public void testSubPropertyGraph() throws Exception { + "}}"; conn.prepareUpdate(QueryLanguage.SPARQL, insert).execute(); inferenceEngine.refreshGraph(); - Graph graph = inferenceEngine.getSubPropertyOfGraph(); - URI p = vf.createURI("urn:p"); - URI q = vf.createURI("urn:q"); - URI r = vf.createURI("urn:r"); - URI s = vf.createURI("urn:s"); - URI t = vf.createURI("urn:t"); - URI u = vf.createURI("urn:u"); - URI v = vf.createURI("urn:v"); - URI w = vf.createURI("urn:w"); - URI missing = vf.createURI("urn:Missing"); - Set empty = new HashSet<>(); - Set belowQ = new HashSet<>(Arrays.asList(new URI[] { p })); - Set belowR = new HashSet<>(Arrays.asList(new URI[] { p, r, s })); - Set belowT = new HashSet<>(Arrays.asList(new URI[] { p, q })); - Set belowU = new HashSet<>(Arrays.asList(new URI[] { p, q, r, s, t, u, v })); + final Graph graph = inferenceEngine.getSubPropertyOfGraph(); + final URI p = vf.createURI("urn:p"); + final URI q = vf.createURI("urn:q"); + final URI r = vf.createURI("urn:r"); + final URI s = vf.createURI("urn:s"); + final URI t = vf.createURI("urn:t"); + final URI u = vf.createURI("urn:u"); + final URI v = vf.createURI("urn:v"); + final URI w = vf.createURI("urn:w"); + final URI missing = vf.createURI("urn:Missing"); + final Set empty = new HashSet<>(); + final Set belowQ = new HashSet<>(Arrays.asList(new URI[] { p })); + final Set belowR = new HashSet<>(Arrays.asList(new URI[] { p, r, s })); + final Set belowT = new HashSet<>(Arrays.asList(new URI[] { p, q })); + final Set belowU = new HashSet<>(Arrays.asList(new URI[] { p, q, r, s, t, u, v })); Assert.assertEquals(empty, inferenceEngine.findParents(graph, p)); Assert.assertEquals(empty, inferenceEngine.findParents(graph, w)); Assert.assertEquals(empty, inferenceEngine.findParents(graph, missing)); @@ -169,7 +169,7 @@ public void testSubPropertyGraph() throws Exception { @Test public void testDomainRange() throws Exception { - String insert = "INSERT DATA { GRAPH {\n" + final String insert = "INSERT DATA { GRAPH {\n" + " rdfs:subPropertyOf . \n" + " rdfs:subPropertyOf . \n" + " rdfs:subPropertyOf . \n" @@ -190,25 +190,25 @@ public void testDomainRange() throws Exception { + "}}"; conn.prepareUpdate(QueryLanguage.SPARQL, insert).execute(); inferenceEngine.refreshGraph(); - Set hasDomainD1 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D1")); - Set hasDomainD2 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D2")); - Set hasDomainD3 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D3")); - Set hasRangeD1 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D1")); - Set hasRangeD2 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D2")); - Set hasRangeD3 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D3")); - Set hasDomainR1 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R1")); - Set hasDomainR2 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R2")); - Set hasDomainR3 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R3")); - Set hasRangeR1 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R1")); - Set hasRangeR2 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R2")); - Set hasRangeR3 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R3")); - Set empty = new HashSet<>(); - Set expectedForward = new HashSet<>(); + final Set hasDomainD1 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D1")); + final Set hasDomainD2 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D2")); + final Set hasDomainD3 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:D3")); + final Set hasRangeD1 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D1")); + final Set hasRangeD2 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D2")); + final Set hasRangeD3 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:D3")); + final Set hasDomainR1 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R1")); + final Set hasDomainR2 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R2")); + final Set hasDomainR3 = inferenceEngine.getPropertiesWithDomain(vf.createURI("urn:R3")); + final Set hasRangeR1 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R1")); + final Set hasRangeR2 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R2")); + final Set hasRangeR3 = inferenceEngine.getPropertiesWithRange(vf.createURI("urn:R3")); + final Set empty = new HashSet<>(); + final Set expectedForward = new HashSet<>(); expectedForward.add(vf.createURI("urn:p2")); expectedForward.add(vf.createURI("urn:p1")); expectedForward.add(vf.createURI("urn:q2")); expectedForward.add(vf.createURI("urn:q1")); - Set expectedInverse = new HashSet<>(); + final Set expectedInverse = new HashSet<>(); expectedInverse.add(vf.createURI("urn:i1")); expectedInverse.add(vf.createURI("urn:i2")); expectedInverse.add(vf.createURI("urn:j1")); diff --git a/sail/src/test/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitorTest.java b/sail/src/test/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitorTest.java index 0f55ab59a..58551a5bb 100644 --- a/sail/src/test/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitorTest.java +++ b/sail/src/test/java/org/apache/rya/rdftriplestore/inference/IntersectionOfVisitorTest.java @@ -145,6 +145,7 @@ public void testIntersectionOf() throws Exception { final StatementPattern nunSp = (StatementPattern) join4.getRightArg(); assertStatementPattern(nunSp, NUN); + assertTrue(union1.getRightArg() instanceof StatementPattern); final StatementPattern actualMotherSp = (StatementPattern) union1.getRightArg(); final StatementPattern expectedMotherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", MOTHER)); assertEquals(expectedMotherSp, actualMotherSp); @@ -174,11 +175,57 @@ public void testIntersectionOf() throws Exception { final StatementPattern parentSp2 = (StatementPattern) join5.getRightArg(); assertStatementPattern(parentSp2, PARENT); + assertTrue(union3.getRightArg() instanceof StatementPattern); final StatementPattern actualFatherSp = (StatementPattern) union3.getRightArg(); final StatementPattern expectedFatherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER)); assertEquals(expectedFatherSp, actualFatherSp); } + @Test + public void testIntersectionOfDisabled() throws Exception { + // Configure a mock instance engine with an ontology: + final InferenceEngine inferenceEngine = mock(InferenceEngine.class); + final Map>> intersections = new HashMap<>(); + final List> motherIntersections = Arrays.asList( + Sets.newHashSet(ANIMAL, FEMALE, PARENT), + Sets.newHashSet(FEMALE, LEADER, NUN) + ); + final List> fatherIntersections = Arrays.asList( + Sets.newHashSet(MAN, PARENT) + ); + intersections.put(MOTHER, motherIntersections); + when(inferenceEngine.getIntersectionsImplying(MOTHER)).thenReturn(motherIntersections); + when(inferenceEngine.getIntersectionsImplying(FATHER)).thenReturn(fatherIntersections); + // Query for a specific type and rewrite using the visitor: + final Projection query = new Projection( + new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", MOTHER)), + new ProjectionElemList(new ProjectionElem("s", "subject"))); + + final AccumuloRdfConfiguration disabledConf = conf.clone(); + disabledConf.setInferIntersectionOf(false); + + query.visit(new IntersectionOfVisitor(disabledConf, inferenceEngine)); + + // Expected structure: the original statement: + assertTrue(query.getArg() instanceof StatementPattern); + final StatementPattern actualMotherSp = (StatementPattern) query.getArg(); + final StatementPattern expectedMotherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", MOTHER)); + assertEquals(expectedMotherSp, actualMotherSp); + + + // Query for a specific type and rewrite using the visitor: + final Projection query2 = new Projection( + new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER)), + new ProjectionElemList(new ProjectionElem("s", "subject"))); + query2.visit(new IntersectionOfVisitor(disabledConf, inferenceEngine)); + + // Expected structure: the original statement: + assertTrue(query2.getArg() instanceof StatementPattern); + final StatementPattern actualFatherSp = (StatementPattern) query2.getArg(); + final StatementPattern expectedFatherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER)); + assertEquals(expectedFatherSp, actualFatherSp); + } + private static void assertStatementPattern(final StatementPattern statementPattern, final URI uri) { assertNotNull(statementPattern.getPredicateVar()); assertEquals(RDF.TYPE, statementPattern.getPredicateVar().getValue());