use of org.openrdf.query.algebra.ValueConstant in project incubator-rya by apache.
the class GeoTemporalMongoDBStorageStrategyTest method extractArguments.
private Value[] extractArguments(final String matchName, final FunctionCall call) {
final Value[] args = new Value[call.getArgs().size() - 1];
int argI = 0;
for (int i = 0; i != call.getArgs().size(); ++i) {
final ValueExpr arg = call.getArgs().get(i);
if (argI == i && arg instanceof Var && matchName.equals(((Var) arg).getName())) {
continue;
}
if (arg instanceof ValueConstant) {
args[argI] = ((ValueConstant) arg).getValue();
} else if (arg instanceof Var && ((Var) arg).hasValue()) {
args[argI] = ((Var) arg).getValue();
} else {
throw new IllegalArgumentException("Query error: Found " + arg + ", expected a Literal, BNode or URI");
}
++argI;
}
return args;
}
use of org.openrdf.query.algebra.ValueConstant 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