Github user jessehatfield commented on a diff in the pull request: https://github.com/apache/incubator-rya/pull/209#discussion_r134620096 --- Diff: sail/src/main/java/org/apache/rya/rdftriplestore/inference/HasSelfVisitor.java --- @@ -0,0 +1,81 @@ +package org.apache.rya.rdftriplestore.inference; + +import org.apache.rya.api.RdfCloudTripleStoreConfiguration; +import org.openrdf.model.Resource; +import org.openrdf.model.URI; +import org.openrdf.model.vocabulary.RDF; +import org.openrdf.query.algebra.Extension; +import org.openrdf.query.algebra.ExtensionElem; +import org.openrdf.query.algebra.StatementPattern; +import org.openrdf.query.algebra.Var; + +/** + * Expands the query tree to account for any relevant has-self class expressions + * in the ontology known to the {@link InferenceEngine}. + * + * Only operates on {@link StatementPattern} nodes, and only those including a + * defined type or defined predicate which is relevant to a has-self expression + * in the ontology. When applicable, replaces the node with one or more + * {@link InferUnion}s, one of whose leaves is the original StatementPattern. + * + * A has-self restriction defines the set of resources that are connected to + * themselves by a property. If the ontology states that a type is a resource + * that has a self referencing property, then the inference engine should: + * <li>1. Rewrite queries of the from ?x rdf:type :T to find all resources + * matching ?x :P ?x (as well as anything explicitly stated to be of type :T) + * </li> + * <li>2. Rewrite queries of the from :CONST :P ?o or ?subj :P :CONST to match + * :CONST if :CONST is known to have the type :T + * </li> + */ +public class HasSelfVisitor extends AbstractInferVisitor { + private static final Var TYPE_VAR = new Var("p", RDF.TYPE); + + /** + * Creates a new {@link HasSelfVisitor}, which is enabled by default. + * @param conf The {@link RdfCloudTripleStoreConfiguration}. + * @param inferenceEngine The InferenceEngine containing the relevant ontology. + */ + public HasSelfVisitor(final RdfCloudTripleStoreConfiguration conf, final InferenceEngine inferenceEngine) { + super(conf, inferenceEngine); + include = conf.hasSelf(); + } + + @Override + protected void meetSP(final StatementPattern node) throws Exception { + final URI pred = (URI) node.getPredicateVar().getValue(); + final Var obj = node.getObjectVar(); + //if originalSP like (?s rdf:type :C1): require that C1 is defined, i.e. not a variable + // node <- originalSP + final StatementPattern clone = node.clone(); + if(pred.equals(RDF.TYPE) && obj.isConstant()) { + //for property in getHasSelfImplyingType(C1): + for (final URI property : inferenceEngine.getHasSelfImplyingType((URI) obj.getValue())) { + //node <- InferUnion(node, StatementPattern(?s, property, ?s)). + final InferUnion union = new InferUnion(clone, new StatementPattern(clone.getSubjectVar(), new Var("p", property), clone.getSubjectVar())); --- End diff -- Similar comment to TYPE_VAR, might rename from "p" to something like the property's actual URI.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---