use of org.apache.jena.sparql.expr.NodeValue in project jena by apache.
the class TestScriptFunction method script_7.
// Narrow to integer
@Test
public void script_7() {
NodeValue nv = eval("combine", "2.5", "3.5");
NodeValue nvx = nv("6");
assertDatatype(nv, XSDDatatype.XSDinteger);
assertEquals(nvx, nv);
}
use of org.apache.jena.sparql.expr.NodeValue in project jena by apache.
the class TestFnFunctionsDateTimeDuration method testException.
private static void testException(String exprStr) {
Expr expr = ExprUtils.parse(exprStr);
try {
NodeValue rExpected = expr.eval(null, LibTestExpr.createTest());
fail("Expected exception: " + exprStr);
} catch (ExprEvalException ex) {
}
}
use of org.apache.jena.sparql.expr.NodeValue in project jena by apache.
the class QueryIterGroup method calc.
private static Iterator<Binding> calc(final QueryIterator iter, final VarExprList groupVarExpr, final List<ExprAggregator> aggregators, final ExecutionContext execCxt) {
return new IteratorDelayedInitialization<Binding>() {
@Override
protected Iterator<Binding> initializeIterator() {
boolean hasAggregators = (aggregators != null && !aggregators.isEmpty());
boolean hasGroupBy = !groupVarExpr.isEmpty();
boolean noInput = !iter.hasNext();
// 2/ No GROUP BY, e.g. COUNT=0, the results is one row always and not handled here.
if (noInput) {
if (hasGroupBy)
// GROUP
return Iter.nullIterator();
if (!hasAggregators) {
// No GROUP BY, no aggregators. One result row of no columns.
return Iter.singleton(BindingFactory.binding());
}
// No GROUP BY, has aggregators. Insert default values.
BindingBuilder builder = Binding.builder();
for (ExprAggregator agg : aggregators) {
Node value = agg.getAggregator().getValueEmpty();
if (value == null)
continue;
Var v = agg.getVar();
builder.add(v, value);
}
return Iter.singleton(builder.build());
}
// Case: there is input.
// Phase 1 : Create keys and aggregators per key, and pump bindings through the aggregators.
Multimap<Binding, Pair<Var, Accumulator>> accumulators = MultimapBuilder.hashKeys().arrayListValues().build();
while (iter.hasNext()) {
Binding b = iter.nextBinding();
Binding key = genKey(groupVarExpr, b, execCxt);
if (!hasAggregators) {
// Put in a dummy to remember the input.
accumulators.put(key, placeholder);
continue;
}
// Create if does not exist.
if (!accumulators.containsKey(key)) {
for (ExprAggregator agg : aggregators) {
Accumulator x = agg.getAggregator().createAccumulator();
Var v = agg.getVar();
accumulators.put(key, Pair.create(v, x));
}
}
// Do the per-accumulator calculation.
for (Pair<Var, Accumulator> pair : accumulators.get(key)) pair.getRight().accumulate(b, execCxt);
}
if (!hasAggregators)
// We used placeholder so there are always the key.
return accumulators.keySet().iterator();
List<Binding> results = new ArrayList<>();
for (Binding k : accumulators.keySet()) {
BindingBuilder builder2 = Binding.builder(k);
Collection<Pair<Var, Accumulator>> accs = accumulators.get(k);
for (Pair<Var, Accumulator> pair : accs) {
NodeValue value = pair.getRight().getValue();
if (value == null)
continue;
Var v = pair.getLeft();
builder2.add(v, value.asNode());
}
results.add(builder2.build());
}
return results.iterator();
}
};
}
use of org.apache.jena.sparql.expr.NodeValue in project jena by apache.
the class FunctionBase2 method exec.
@Override
public final NodeValue exec(List<NodeValue> args) {
if (args == null)
// The contract on the function interface is that this should not happen.
throw new ARQInternalErrorException(Lib.className(this) + ": Null args list");
if (args.size() != 2)
throw new ExprEvalException(Lib.className(this) + ": Wrong number of arguments: Wanted 2, got " + args.size());
NodeValue v1 = args.get(0);
NodeValue v2 = args.get(1);
return exec(v1, v2);
}
use of org.apache.jena.sparql.expr.NodeValue in project jena by apache.
the class FunctionBase4 method exec.
@Override
public final NodeValue exec(List<NodeValue> args) {
if (args == null)
// The contract on the function interface is that this should not happen.
throw new ARQInternalErrorException(Lib.className(this) + ": Null args list");
if (args.size() != 4)
throw new ExprEvalException(Lib.className(this) + ": Wrong number of arguments: Wanted 4, got " + args.size());
NodeValue v1 = args.get(0);
NodeValue v2 = args.get(1);
NodeValue v3 = args.get(2);
NodeValue v4 = args.get(3);
return exec(v1, v2, v3, v4);
}
Aggregations