Search in sources :

Example 6 with Extension

use of org.openrdf.query.algebra.Extension in project incubator-rya by apache.

the class HasSelfVisitor method meetSP.

@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 (RDF.TYPE.equals(pred) && obj.isConstant()) {
        // for property in getHasSelfImplyingType(C1):
        if (obj.getValue() instanceof URI) {
            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(property.stringValue(), property), clone.getSubjectVar()));
                // originalSP.replaceWith(node)
                node.replaceWith(union);
            }
        }
    // else if originalSP like (s :p o):  where p is not a variable and at least one of s and o are variables
    } else if (node.getPredicateVar().isConstant() && (!node.getSubjectVar().isConstant() || !node.getObjectVar().isConstant())) {
        // for type in getHasSelfImplyingProperty(p):
        for (final Resource type : inferenceEngine.getHasSelfImplyingProperty(pred)) {
            final Extension extension;
            if (obj.isConstant()) {
                // subject is the variable
                // Extension(StatementPattern(o, rdf:type, type), ExtensionElem(o, "s"))
                extension = new Extension(new StatementPattern(obj, TYPE_VAR, new Var(type.stringValue(), type)), new ExtensionElem(obj, node.getSubjectVar().getName()));
            } else {
                // o is a variable and s may either be defined or a variable
                // Extension(StatementPattern(s, rdf:type, type), ExtensionElem(s, "o"))
                extension = new Extension(new StatementPattern(node.getSubjectVar(), TYPE_VAR, new Var(type.stringValue(), type)), new ExtensionElem(node.getSubjectVar(), obj.getName()));
            }
            // node <- InferUnion(node, newNode)
            final InferUnion union = new InferUnion(extension, clone);
            node.replaceWith(union);
        }
    }
}
Also used : Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) URI(org.openrdf.model.URI)

Example 7 with Extension

use of org.openrdf.query.algebra.Extension in project incubator-rya by apache.

the class ConstructConsequentVisitorTest method testConcreteSP.

@Test
public void testConcreteSP() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"), new ExtensionElem(new ValueConstant(OWL.CLASS), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "subject"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(FOAF.PERSON), p(RDF.TYPE), o(OWL.CLASS)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) SingletonSet(org.openrdf.query.algebra.SingletonSet) ValueConstant(org.openrdf.query.algebra.ValueConstant) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 8 with Extension

use of org.openrdf.query.algebra.Extension in project incubator-rya by apache.

the class ConstructConsequentVisitorTest method testMissingVariables.

@Test
public void testMissingVariables() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(FOAF.PERSON), "x"), new ExtensionElem(new ValueConstant(RDF.TYPE), "y"));
    Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "s"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(RDF.TYPE), o(null)));
    Assert.assertEquals(expected, visitor.getConsequents());
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) SingletonSet(org.openrdf.query.algebra.SingletonSet) ValueConstant(org.openrdf.query.algebra.ValueConstant) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 9 with Extension

use of org.openrdf.query.algebra.Extension in project incubator-rya by apache.

the class MultiProjectionEvaluator method make.

/**
 * Make a {@link MultiProjectionEvaluator} that processes the logic of a {@link MultiProjection}.
 *
 * @param multiProjection - Defines the projections that will be processed. (not null)
 * @param bNodeIdFactory - Creates the IDs for Blank Nodes. (not null)
 * @return A {@link MultiProjectionEvaluator} for the provided {@link MultiProjection}.
 */
public static MultiProjectionEvaluator make(final MultiProjection multiProjection, final BNodeIdFactory bNodeIdFactory) {
    requireNonNull(multiProjection);
    // Figure out if there are extensions.
    final TupleExpr arg = multiProjection.getArg();
    final Optional<Extension> extension = (arg instanceof Extension) ? Optional.of((Extension) arg) : Optional.empty();
    // If there are, iterate through them and find any blank node source names.
    final Set<String> blankNodeSourceNames = new HashSet<>();
    if (extension.isPresent()) {
        for (final ExtensionElem elem : extension.get().getElements()) {
            if (elem.getExpr() instanceof BNodeGenerator) {
                blankNodeSourceNames.add(elem.getName());
            }
        }
    }
    // Create a ProjectionEvaluator for each projection that is part of the multi.
    final Set<ProjectionEvaluator> projections = new HashSet<>();
    for (final ProjectionElemList projectionElemList : multiProjection.getProjections()) {
        projections.add(new ProjectionEvaluator(projectionElemList, extension));
    }
    return new MultiProjectionEvaluator(projections, blankNodeSourceNames, bNodeIdFactory);
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) BNodeGenerator(org.openrdf.query.algebra.BNodeGenerator) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) TupleExpr(org.openrdf.query.algebra.TupleExpr) HashSet(java.util.HashSet)

Example 10 with Extension

use of org.openrdf.query.algebra.Extension in project incubator-rya by apache.

the class ConstructConsequentVisitorTest method testBNode.

@Test
public void testBNode() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new Var("x"), "x"), new ExtensionElem(new BNodeGenerator(), "z"));
    Projection projection = new Projection(extension, new ProjectionElemList(new ProjectionElem("x", "subject"), new ProjectionElem("y", "predicate"), new ProjectionElem("z", "object")));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(null), anon(o(null))));
    Assert.assertEquals(expected, visitor.getConsequents());
}
Also used : Extension(org.openrdf.query.algebra.Extension) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) BNodeGenerator(org.openrdf.query.algebra.BNodeGenerator) SingletonSet(org.openrdf.query.algebra.SingletonSet) Var(org.openrdf.query.algebra.Var) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Aggregations

Extension (org.openrdf.query.algebra.Extension)12 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)11 StatementPattern (org.openrdf.query.algebra.StatementPattern)10 Test (org.junit.Test)9 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)9 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)7 Var (org.openrdf.query.algebra.Var)7 Projection (org.openrdf.query.algebra.Projection)6 MultiProjection (org.openrdf.query.algebra.MultiProjection)5 SingletonSet (org.openrdf.query.algebra.SingletonSet)5 ValueConstant (org.openrdf.query.algebra.ValueConstant)5 HashSet (java.util.HashSet)3 Resource (org.openrdf.model.Resource)3 TupleExpr (org.openrdf.query.algebra.TupleExpr)3 BNodeGenerator (org.openrdf.query.algebra.BNodeGenerator)2 QueryRoot (org.openrdf.query.algebra.QueryRoot)2 Union (org.openrdf.query.algebra.Union)2 URI (org.openrdf.model.URI)1 Not (org.openrdf.query.algebra.Not)1