Search in sources :

Example 6 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class SomeValuesFromVisitor method meetSP.

/**
 * Checks whether the StatementPattern is a type query whose solutions could be inferred by
 * someValuesFrom inference, and if so, replaces the node with a union of itself and any
 * possible inference.
 */
@Override
protected void meetSP(StatementPattern node) throws Exception {
    final Var subjVar = node.getSubjectVar();
    final Var predVar = node.getPredicateVar();
    final Var objVar = node.getObjectVar();
    // Only applies to type queries where the type is defined
    if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof Resource) {
        final Resource typeToInfer = (Resource) objVar.getValue();
        Map<Resource, Set<URI>> relevantSvfRestrictions = inferenceEngine.getSomeValuesFromByRestrictionType(typeToInfer);
        if (!relevantSvfRestrictions.isEmpty()) {
            // We can infer the queried type if it is to a someValuesFrom restriction (or a
            // supertype of one), and the node in question (subjVar) is the subject of a triple
            // whose predicate is the restriction's property and whose object is an arbitrary
            // node of the restriction's value type.
            final Var valueTypeVar = new Var("t-" + UUID.randomUUID());
            final Var svfPredVar = new Var("p-" + UUID.randomUUID());
            final Var neighborVar = new Var("n-" + UUID.randomUUID());
            neighborVar.setAnonymous(true);
            final StatementPattern membershipPattern = new DoNotExpandSP(neighborVar, new Var(RDF.TYPE.stringValue(), RDF.TYPE), valueTypeVar);
            final StatementPattern valuePattern = new StatementPattern(subjVar, svfPredVar, neighborVar);
            final InferJoin svfPattern = new InferJoin(membershipPattern, valuePattern);
            // Use a FixedStatementPattern to contain the appropriate (predicate, value type)
            // pairs, and check each one against the general pattern.
            final FixedStatementPattern svfPropertyTypes = new FixedStatementPattern(svfPredVar, new Var(OWL.SOMEVALUESFROM.stringValue(), OWL.SOMEVALUESFROM), valueTypeVar);
            for (Resource svfValueType : relevantSvfRestrictions.keySet()) {
                for (URI svfProperty : relevantSvfRestrictions.get(svfValueType)) {
                    svfPropertyTypes.statements.add(new NullableStatementImpl(svfProperty, OWL.SOMEVALUESFROM, svfValueType));
                }
            }
            final InferJoin svfInferenceQuery = new InferJoin(svfPropertyTypes, svfPattern);
            node.replaceWith(new InferUnion(node.clone(), svfInferenceQuery));
        }
    }
}
Also used : FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Set(java.util.Set) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 7 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class SomeValuesFromVisitorTest method testSomeValuesFrom.

@Test
public void testSomeValuesFrom() throws Exception {
    // Configure a mock instance engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<Resource, Set<URI>> personSVF = new HashMap<>();
    personSVF.put(gradCourse, Sets.newHashSet(takesCourse));
    personSVF.put(course, Sets.newHashSet(takesCourse));
    personSVF.put(department, Sets.newHashSet(headOf));
    personSVF.put(organization, Sets.newHashSet(worksFor, headOf));
    when(inferenceEngine.getSomeValuesFromByRestrictionType(person)).thenReturn(personSVF);
    // Query for a specific type and rewrite using the visitor:
    StatementPattern originalSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", person));
    final Projection query = new Projection(originalSP, new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new SomeValuesFromVisitor(conf, inferenceEngine));
    // Expected structure: a union of two elements: one is equal to the original statement
    // pattern, and the other one joins a list of predicate/value type combinations
    // with another join querying for any nodes who are the subject of a triple with that
    // predicate and with an object of that type.
    // 
    // Union(
    // SP(?node a :impliedType),
    // Join(
    // FSP(<?property someValuesFrom ?valueType> {
    // takesCourse/Course;
    // takesCourse/GraduateCourse;
    // headOf/Department;
    // headOf/Organization;
    // worksFor/Organization;
    // }),
    // Join(
    // SP(_:object a ?valueType),
    // SP(?node ?property _:object)
    // )
    // )
    Assert.assertTrue(query.getArg() instanceof Union);
    TupleExpr left = ((Union) query.getArg()).getLeftArg();
    TupleExpr right = ((Union) query.getArg()).getRightArg();
    Assert.assertEquals(originalSP, left);
    Assert.assertTrue(right instanceof Join);
    final Join join = (Join) right;
    Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
    Assert.assertTrue(join.getRightArg() instanceof Join);
    FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
    left = ((Join) join.getRightArg()).getLeftArg();
    right = ((Join) join.getRightArg()).getRightArg();
    Assert.assertTrue(left instanceof StatementPattern);
    Assert.assertTrue(right instanceof StatementPattern);
    // Verify expected predicate/type pairs
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, course)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, gradCourse)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, department)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, organization)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(worksFor, OWL.SOMEVALUESFROM, organization)));
    Assert.assertEquals(5, fsp.statements.size());
    // Verify pattern for matching instances of each pair: a Join of <_:x rdf:type ?t> and
    // <?s ?p _:x> where p and t are the predicate/type pair and s is the original subject
    // variable.
    StatementPattern leftSP = (StatementPattern) left;
    StatementPattern rightSP = (StatementPattern) right;
    Assert.assertEquals(rightSP.getObjectVar(), leftSP.getSubjectVar());
    Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
    Assert.assertEquals(fsp.getObjectVar(), leftSP.getObjectVar());
    Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getSubjectVar());
    Assert.assertEquals(fsp.getSubjectVar(), rightSP.getPredicateVar());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Set(java.util.Set) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) Test(org.junit.Test)

Example 8 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class SubPropertyOfVisitor method meetSP.

@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();
    final URI pred = (URI) predVar.getValue();
    final String predNamespace = pred.getNamespace();
    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    if (objVar != null && !RDF.NAMESPACE.equals(predNamespace) && !SESAME.NAMESPACE.equals(predNamespace) && !RDFS.NAMESPACE.equals(predNamespace) && !EXPANDED.equals(cntxtVar)) {
        /**
         * { ?subProp rdfs:subPropertyOf ub:worksFor . ?y ?subProp <http://www.Department0.University0.edu> }\n" +
         *             "       UNION " +
         *             "      { ?y ub:worksFor <http://www.Department0.University0.edu> }
         */
        // String s = UUID.randomUUID().toString();
        // Var subPropVar = new Var(s);
        // StatementPattern subPropOf = new StatementPattern(subPropVar, new Var("c-" + s, SESAME.DIRECTSUBPROPERTYOF), predVar, EXPANDED);
        // StatementPattern subPropOf2 = new StatementPattern(sp.getSubjectVar(), subPropVar, objVar, EXPANDED);
        // InferJoin join = new InferJoin(subPropOf, subPropOf2);
        // join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
        // node.replaceWith(join);
        // Collection<URI> parents = inferenceEngine.findParents(inferenceEngine.subPropertyOfGraph, (URI) predVar.getValue());
        // if (parents != null && parents.size() > 0) {
        // StatementPatterns statementPatterns = new StatementPatterns();
        // statementPatterns.patterns.add(node);
        // Var subjVar = node.getSubjectVar();
        // for (URI u : parents) {
        // statementPatterns.patterns.add(new StatementPattern(subjVar, new Var(predVar.getName(), u), objVar));
        // }
        // node.replaceWith(statementPatterns);
        // }
        // if (parents != null && parents.size() > 0) {
        // VarCollection vc = new VarCollection();
        // vc.setName(predVar.getName());
        // vc.values.add(predVar);
        // for (URI u : parents) {
        // vc.values.add(new Var(predVar.getName(), u));
        // }
        // Var subjVar = node.getSubjectVar();
        // node.replaceWith(new StatementPattern(subjVar, vc, objVar, node.getContextVar()));
        // }
        final URI subprop_uri = (URI) predVar.getValue();
        final Set<URI> parents = InferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), subprop_uri);
        if (parents != null && parents.size() > 0) {
            final String s = UUID.randomUUID().toString();
            final Var typeVar = new Var(s);
            final FixedStatementPattern fsp = new FixedStatementPattern(typeVar, new Var("c-" + s, RDFS.SUBPROPERTYOF), predVar, cntxtVar);
            // fsp.statements.add(new NullableStatementImpl(subprop_uri, RDFS.SUBPROPERTYOF, subprop_uri));
            // add self
            parents.add(subprop_uri);
            for (final URI u : parents) {
                fsp.statements.add(new NullableStatementImpl(u, RDFS.SUBPROPERTYOF, subprop_uri));
            }
            final StatementPattern rdfType = new DoNotExpandSP(sp.getSubjectVar(), typeVar, sp.getObjectVar(), cntxtVar);
            final InferJoin join = new InferJoin(fsp, rdfType);
            join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
            node.replaceWith(join);
        }
    }
}
Also used : FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Var(org.openrdf.query.algebra.Var) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 9 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class SameAsVisitor method meet.

public void meet(StatementPattern sp) throws Exception {
    if (!include) {
        return;
    }
    if (sp instanceof FixedStatementPattern || sp instanceof TransitivePropertySP || sp instanceof DoNotExpandSP) {
        // already inferred somewhere else
        return;
    }
    final Var predVar = sp.getPredicateVar();
    // do not know when things are null
    if (predVar == null) {
        return;
    }
    meetSP(sp);
}
Also used : Var(org.openrdf.query.algebra.Var) TransitivePropertySP(org.apache.rya.rdftriplestore.utils.TransitivePropertySP) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 10 with FixedStatementPattern

use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.

the class SameAsVisitor method meetSP.

@Override
protected void meetSP(StatementPattern node) throws Exception {
    StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();
    boolean shouldExpand = true;
    if (predVar.hasValue()) {
        URI pred = (URI) predVar.getValue();
        String predNamespace = pred.getNamespace();
        shouldExpand = !pred.equals(OWL.SAMEAS) && !RDF.NAMESPACE.equals(predNamespace) && !SESAME.NAMESPACE.equals(predNamespace) && !RDFS.NAMESPACE.equals(predNamespace);
    }
    final Var objVar = sp.getObjectVar();
    final Var subjVar = sp.getSubjectVar();
    final Var cntxtVar = sp.getContextVar();
    if (shouldExpand && !EXPANDED.equals(cntxtVar) && !(objVar == null) && !(subjVar == null)) {
        if (objVar.getValue() == null) {
            Value subjVarValue = subjVar.getValue();
            if (subjVarValue instanceof Resource) {
                Set<Resource> uris = inferenceEngine.findSameAs((Resource) subjVar.getValue(), getVarValue(cntxtVar));
                if (uris.size() > 1) {
                    InferJoin join = getReplaceJoin(uris, true, subjVar, objVar, predVar, cntxtVar);
                    node.replaceWith(join);
                }
            }
        } else if (subjVar.getValue() == null) {
            Value objVarValue = objVar.getValue();
            if (objVarValue instanceof Resource) {
                Set<Resource> uris = inferenceEngine.findSameAs((Resource) objVar.getValue(), getVarValue(cntxtVar));
                if (uris.size() > 1) {
                    InferJoin join = getReplaceJoin(uris, false, subjVar, objVar, predVar, cntxtVar);
                    node.replaceWith(join);
                }
            }
        } else {
            // both subj and pred are set and should be expanded
            Set<Resource> subjURIs = new HashSet<Resource>();
            Set<Resource> objURIs = new HashSet<Resource>();
            // TODO I don't like these checks -- is there a better way to do this?
            Value objVarValue = objVar.getValue();
            if (objVarValue instanceof Resource) {
                objURIs = inferenceEngine.findSameAs((Resource) objVar.getValue(), getVarValue(cntxtVar));
            }
            Value subjVarValue = subjVar.getValue();
            if (subjVarValue instanceof Resource) {
                subjURIs = inferenceEngine.findSameAs((Resource) subjVar.getValue(), getVarValue(cntxtVar));
            }
            InferJoin finalJoin = null;
            // expand subj first
            if (subjURIs.size() > 1) {
                finalJoin = getReplaceJoin(subjURIs, true, subjVar, objVar, predVar, cntxtVar);
            }
            // now expand the obj
            if (objURIs.size() > 1) {
                // if we already expanded the subj
                if (finalJoin != null) {
                    // we know what this is since we created it
                    DoNotExpandSP origStatement = (DoNotExpandSP) finalJoin.getRightArg();
                    String s = UUID.randomUUID().toString();
                    Var dummyVar = new Var(s);
                    StatementPattern origDummyStatement = new DoNotExpandSP(origStatement.getSubjectVar(), origStatement.getPredicateVar(), dummyVar, cntxtVar);
                    FixedStatementPattern fsp = new FixedStatementPattern(dummyVar, new Var("c-" + s, OWL.SAMEAS), objVar, cntxtVar);
                    for (Resource sameAs : objURIs) {
                        NullableStatementImpl newStatement = new NullableStatementImpl(sameAs, OWL.SAMEAS, (Resource) objVar.getValue(), getVarValue(cntxtVar));
                        fsp.statements.add(newStatement);
                    }
                    InferJoin interimJoin = new InferJoin(fsp, origDummyStatement);
                    finalJoin = new InferJoin(finalJoin.getLeftArg(), interimJoin);
                } else {
                    finalJoin = getReplaceJoin(objURIs, false, subjVar, objVar, predVar, cntxtVar);
                }
            }
            if (finalJoin != null) {
                node.replaceWith(finalJoin);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) Value(org.openrdf.model.Value) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Aggregations

FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)19 Var (org.openrdf.query.algebra.Var)16 StatementPattern (org.openrdf.query.algebra.StatementPattern)12 NullableStatementImpl (org.apache.rya.api.utils.NullableStatementImpl)11 URI (org.openrdf.model.URI)11 Resource (org.openrdf.model.Resource)10 Set (java.util.Set)9 Join (org.openrdf.query.algebra.Join)8 TupleExpr (org.openrdf.query.algebra.TupleExpr)6 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 Projection (org.openrdf.query.algebra.Projection)5 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)5 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)5 Union (org.openrdf.query.algebra.Union)5 HashMap (java.util.HashMap)4 Value (org.openrdf.model.Value)4 DoNotExpandSP (org.apache.rya.rdftriplestore.inference.DoNotExpandSP)3 TransitivePropertySP (org.apache.rya.rdftriplestore.utils.TransitivePropertySP)3 Statement (org.openrdf.model.Statement)3