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());
}
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());
}
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());
}
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;
}
Aggregations