Search in sources :

Example 1 with NullableStatementImpl

use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.

the class AccumuloQueryRuleset method getRange.

/**
 * Turn a single StatementPattern into a Range.
 * @param conf
 * @throws IOException if the range can't be resolved
 */
private Map.Entry<TABLE_LAYOUT, ByteRange> getRange(final StatementPattern sp) throws IOException {
    final Var context = sp.getContextVar();
    final Statement stmt = new NullableStatementImpl((Resource) sp.getSubjectVar().getValue(), (URI) sp.getPredicateVar().getValue(), sp.getObjectVar().getValue(), context == null ? null : (Resource) context.getValue());
    final RyaStatement rs = RdfToRyaConversions.convertStatement(stmt);
    final TriplePatternStrategy strategy = ryaContext.retrieveStrategy(rs);
    final Map.Entry<TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(rs.getSubject(), rs.getPredicate(), rs.getObject(), rs.getContext(), conf);
    return entry;
}
Also used : TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) TriplePatternStrategy(org.apache.rya.api.query.strategy.TriplePatternStrategy) ByteRange(org.apache.rya.api.query.strategy.ByteRange) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) Resource(org.openrdf.model.Resource) RyaStatement(org.apache.rya.api.domain.RyaStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NullableStatementImpl

use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.

the class SameAsVisitor method getReplaceJoin.

private InferJoin getReplaceJoin(Set<Resource> uris, boolean subSubj, Var subjVar, Var objVar, Var predVar, Var cntxtVar) {
    String s = UUID.randomUUID().toString();
    Var dummyVar = new Var(s);
    StatementPattern origStatement;
    Var subVar;
    if (subSubj) {
        subVar = subjVar;
        origStatement = new DoNotExpandSP(dummyVar, predVar, objVar, cntxtVar);
    } else {
        subVar = objVar;
        origStatement = new DoNotExpandSP(subjVar, predVar, dummyVar, cntxtVar);
    }
    FixedStatementPattern fsp = new FixedStatementPattern(dummyVar, new Var("c-" + s, OWL.SAMEAS), subVar, cntxtVar);
    for (Resource sameAs : uris) {
        NullableStatementImpl newStatement = new NullableStatementImpl(sameAs, OWL.SAMEAS, (Resource) subVar.getValue(), getVarValue(cntxtVar));
        fsp.statements.add(newStatement);
    }
    InferJoin join = new InferJoin(fsp, origStatement);
    join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
    return 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) Resource(org.openrdf.model.Resource) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern)

Example 3 with NullableStatementImpl

use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.

the class SubClassOfVisitor method meetSP.

@Override
protected void meetSP(final StatementPattern node) throws Exception {
    final StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();
    final Var objVar = sp.getObjectVar();
    final Var conVar = sp.getContextVar();
    if (predVar != null && objVar != null && objVar.getValue() != null && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
        /**
         * ?type sesame:directSubClassOf ub:Student . ?student rdf:type ?type +
         */
        // String s = UUID.randomUUID().toString();
        // Var typeVar = new Var(s);
        // StatementPattern subClassOf = new StatementPattern(typeVar, new Var("c-" + s, SESAME.DIRECTSUBCLASSOF), objVar, SUBCLASS_EXPANDED);
        // StatementPattern rdfType = new StatementPattern(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, SUBCLASS_EXPANDED);
        // InferJoin join = new InferJoin(subClassOf, rdfType);
        // join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
        // node.replaceWith(join);
        final URI subclassof_uri = (URI) objVar.getValue();
        final Collection<URI> parents = InferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), subclassof_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.SUBCLASSOF), objVar, conVar);
            parents.add(subclassof_uri);
            for (final URI u : parents) {
                fsp.statements.add(new NullableStatementImpl(u, RDFS.SUBCLASSOF, subclassof_uri));
            }
            final StatementPattern rdfType = new DoNotExpandSP(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, conVar);
            final InferJoin join = new InferJoin(fsp, rdfType);
            join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
            node.replaceWith(join);
        }
    // 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, predVar, new Var(objVar.getName(), u)));
    // }
    // node.replaceWith(statementPatterns);
    // }
    // if (parents != null && parents.size() > 0) {
    // VarCollection vc = new VarCollection();
    // vc.setName(objVar.getName());
    // vc.values.add(objVar);
    // for (URI u : parents) {
    // vc.values.add(new Var(objVar.getName(), u));
    // }
    // Var subjVar = node.getSubjectVar();
    // node.replaceWith(new StatementPattern(subjVar, predVar, vc, node.getContextVar()));
    // }
    }
}
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 4 with NullableStatementImpl

use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.

the class AllValuesFromVisitorTest method testRewriteTypePattern.

@Test
public void testRewriteTypePattern() throws Exception {
    // Configure a mock instance engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<Resource, Set<URI>> personAVF = new HashMap<>();
    personAVF.put(parentsAreTallPeople, new HashSet<>());
    personAVF.put(parentsArePeople, new HashSet<>());
    personAVF.put(relativesArePeople, new HashSet<>());
    personAVF.get(parentsAreTallPeople).add(parent);
    personAVF.get(parentsArePeople).add(parent);
    personAVF.get(relativesArePeople).add(relative);
    personAVF.get(relativesArePeople).add(parent);
    Map<Resource, Set<URI>> dogAVF = new HashMap<>();
    dogAVF.put(parentsAreDogs, new HashSet<>());
    dogAVF.get(parentsAreDogs).add(parent);
    when(inferenceEngine.getAllValuesFromByValueType(person)).thenReturn(personAVF);
    when(inferenceEngine.getAllValuesFromByValueType(dog)).thenReturn(dogAVF);
    // 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 AllValuesFromVisitor(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/restriction type combinations
    // with another join querying for values of that predicate for members of that type.
    Assert.assertTrue(query.getArg() instanceof Union);
    TupleExpr left = ((Union) query.getArg()).getLeftArg();
    TupleExpr right = ((Union) query.getArg()).getRightArg();
    final Join join;
    if (left instanceof StatementPattern) {
        Assert.assertEquals(originalSP, left);
        Assert.assertTrue(right instanceof Join);
        join = (Join) right;
    } else {
        Assert.assertEquals(originalSP, right);
        Assert.assertTrue(left instanceof Join);
        join = (Join) left;
    }
    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/restriction pairs
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsArePeople, OWL.ONPROPERTY, parent)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, relative)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, parent)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsAreTallPeople, OWL.ONPROPERTY, parent)));
    Assert.assertEquals(4, fsp.statements.size());
    // Verify general pattern for matching instances of each pair: Join on unknown subject; left
    // triple states it belongs to the restriction while right triple relates it to the original
    // subject variable by the relevant property. Restriction and property variables are given
    // by the FixedStatementPattern.
    StatementPattern leftSP = (StatementPattern) left;
    StatementPattern rightSP = (StatementPattern) right;
    Assert.assertEquals(rightSP.getSubjectVar(), leftSP.getSubjectVar());
    Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
    Assert.assertEquals(fsp.getSubjectVar(), leftSP.getObjectVar());
    Assert.assertEquals(fsp.getObjectVar(), rightSP.getPredicateVar());
    Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getObjectVar());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) 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 5 with NullableStatementImpl

use of org.apache.rya.api.utils.NullableStatementImpl 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)

Aggregations

NullableStatementImpl (org.apache.rya.api.utils.NullableStatementImpl)12 Var (org.openrdf.query.algebra.Var)12 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)11 Resource (org.openrdf.model.Resource)9 StatementPattern (org.openrdf.query.algebra.StatementPattern)9 URI (org.openrdf.model.URI)8 Set (java.util.Set)7 TupleExpr (org.openrdf.query.algebra.TupleExpr)4 HashMap (java.util.HashMap)3 Value (org.openrdf.model.Value)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Test (org.junit.Test)2 Statement (org.openrdf.model.Statement)2 Join (org.openrdf.query.algebra.Join)2 Projection (org.openrdf.query.algebra.Projection)2 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)2 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)2 Union (org.openrdf.query.algebra.Union)2 ConvertingIteration (info.aduna.iteration.ConvertingIteration)1