Search in sources :

Example 11 with ExtensionElem

use of org.openrdf.query.algebra.ExtensionElem 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)

Example 12 with ExtensionElem

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

the class ConstructConsequentVisitorTest method testGenericSP.

@Test
public void testGenericSP() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new Var("z"), "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), 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) 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)

Example 13 with ExtensionElem

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

the class ConstructConsequentVisitorTest method testMultiProjection.

@Test
public void testMultiProjection() {
    Extension extension = new Extension(new SingletonSet(), new ExtensionElem(new ValueConstant(RDF.TYPE), "rdftype"), new ExtensionElem(new ValueConstant(OWL.OBJECTPROPERTY), "owlprop"), new ExtensionElem(new ValueConstant(OWL.EQUIVALENTCLASS), "owleqcls"), new ExtensionElem(new ValueConstant(OWL.CLASS), "owlclass"));
    MultiProjection projection = new MultiProjection(extension, Arrays.asList(new ProjectionElemList(new ProjectionElem("cls", "subject"), new ProjectionElem("rdftype", "predicate"), new ProjectionElem("owlclass", "object")), new ProjectionElemList(new ProjectionElem("prop", "subject"), new ProjectionElem("rdftype", "predicate"), new ProjectionElem("owlprop", "object")), new ProjectionElemList(new ProjectionElem("owleqcls", "predicate"), new ProjectionElem("cls", "object"))));
    ConstructConsequentVisitor visitor = new ConstructConsequentVisitor();
    projection.visit(visitor);
    Set<StatementPattern> expected = Sets.newHashSet(new StatementPattern(s(null), p(RDF.TYPE), o(OWL.CLASS)), new StatementPattern(s(null), p(RDF.TYPE), o(OWL.OBJECTPROPERTY)), new StatementPattern(s(null), p(OWL.EQUIVALENTCLASS), 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) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 14 with ExtensionElem

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

the class AggregationPipelineQueryNode method extend.

/**
 * Add a SPARQL extension to the pipeline, if possible. An extension adds
 * some number of variables to the result. Adds a "$project" step to the
 * pipeline, but differs from the SPARQL project operation in that
 * 1) pre-existing variables are always kept, and 2) values of new variables
 * are defined by expressions, which may be more complex than simply
 * variable names. Not all expressions are supported. If unsupported
 * expression types are used in the extension, the pipeline will remain
 * unchanged and this method will return false.
 * @param extensionElements A list of new variables and their expressions
 * @return True if the extension was successfully converted into a pipeline
 *  step, false otherwise.
 */
public boolean extend(Iterable<ExtensionElem> extensionElements) {
    List<Bson> valueFields = new LinkedList<>();
    List<Bson> hashFields = new LinkedList<>();
    List<Bson> typeFields = new LinkedList<>();
    for (String varName : bindingNames) {
        valueFields.add(Projections.include(varName));
        hashFields.add(Projections.include(varName));
        typeFields.add(Projections.include(varName));
    }
    Set<String> newVarNames = new HashSet<>();
    for (ExtensionElem elem : extensionElements) {
        String name = elem.getName();
        if (!isValidFieldName(name)) {
            // If the field name is invalid, replace it internally
            name = replace(name);
        }
        // We can only handle certain kinds of value expressions; return
        // failure for any others.
        ValueExpr expr = elem.getExpr();
        final Object valueField;
        final Object hashField;
        final Object typeField;
        if (expr instanceof Var) {
            String varName = ((Var) expr).getName();
            valueField = "$" + varName;
            hashField = "$" + varName;
            typeField = "$" + varName;
        } else if (expr instanceof ValueConstant) {
            Value val = ((ValueConstant) expr).getValue();
            valueField = new Document("$literal", val.stringValue());
            hashField = new Document("$literal", SimpleMongoDBStorageStrategy.hash(val.stringValue()));
            if (val instanceof Literal) {
                typeField = new Document("$literal", ((Literal) val).getDatatype().stringValue());
            } else {
                typeField = null;
            }
        } else {
            // if not understood, return failure
            return false;
        }
        valueFields.add(Projections.computed(name, valueField));
        hashFields.add(Projections.computed(name, hashField));
        if (typeField != null) {
            typeFields.add(Projections.computed(name, typeField));
        }
        newVarNames.add(name);
    }
    assuredBindingNames.addAll(newVarNames);
    bindingNames.addAll(newVarNames);
    Bson projectOpts = Projections.fields(Projections.computed(VALUES, Projections.fields(valueFields)), Projections.computed(HASHES, Projections.fields(hashFields)), Projections.computed(TYPES, Projections.fields(typeFields)), Projections.include(LEVEL), Projections.include(TIMESTAMP));
    pipeline.add(Aggregates.project(projectOpts));
    return true;
}
Also used : ValueExpr(org.openrdf.query.algebra.ValueExpr) Var(org.openrdf.query.algebra.Var) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Document(org.bson.Document) LinkedList(java.util.LinkedList) Bson(org.bson.conversions.Bson) ValueConstant(org.openrdf.query.algebra.ValueConstant) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashSet(java.util.HashSet)

Aggregations

ExtensionElem (org.openrdf.query.algebra.ExtensionElem)14 Extension (org.openrdf.query.algebra.Extension)11 StatementPattern (org.openrdf.query.algebra.StatementPattern)11 Test (org.junit.Test)10 Var (org.openrdf.query.algebra.Var)10 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)8 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)8 ValueConstant (org.openrdf.query.algebra.ValueConstant)8 Projection (org.openrdf.query.algebra.Projection)6 HashSet (java.util.HashSet)5 MultiProjection (org.openrdf.query.algebra.MultiProjection)5 SingletonSet (org.openrdf.query.algebra.SingletonSet)5 Resource (org.openrdf.model.Resource)3 BNodeGenerator (org.openrdf.query.algebra.BNodeGenerator)3 Value (org.openrdf.model.Value)2 Not (org.openrdf.query.algebra.Not)2 QueryRoot (org.openrdf.query.algebra.QueryRoot)2 TupleExpr (org.openrdf.query.algebra.TupleExpr)2 Union (org.openrdf.query.algebra.Union)2 BasicDBObject (com.mongodb.BasicDBObject)1