use of org.apache.jena.sparql.expr.Expr in project jena by apache.
the class TestUserFunctionsInSparql method setup.
@BeforeClass
public static void setup() {
UserDefinedFunctionFactory.getFactory().clear();
//Define a square function
Expr square = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
UserDefinedFunctionFactory.getFactory().add("http://example/square", square, new ArrayList<>(square.getVarsMentioned()));
}
use of org.apache.jena.sparql.expr.Expr in project jena by apache.
the class TestRegex method regexTest.
public void regexTest(String value, String pattern, String flags, boolean expected) {
Expr s = NodeValue.makeString(value);
E_Regex r = new E_Regex(s, pattern, flags);
NodeValue nv = r.eval(BindingFactory.binding(), null);
boolean b = nv.getBoolean();
if (b != expected)
fail(fmtTest(value, pattern, flags) + " ==> " + b + " expected " + expected);
}
use of org.apache.jena.sparql.expr.Expr in project jena by apache.
the class SolutionModifierTest method testAddOrderByExprDescending.
@ContractTest
public void testAddOrderByExprDescending() {
SolutionModifierClause<?> solutionModifier = getProducer().newInstance();
Expr e = new E_Random();
AbstractQueryBuilder<?> builder = solutionModifier.addOrderBy(e, Order.DESCENDING);
assertContainsRegex(ORDER_BY + "DESC" + OPEN_PAREN + "rand" + OPEN_PAREN + CLOSE_PAREN + CLOSE_PAREN, builder.buildString());
builder = solutionModifier.addOrderBy("bar");
assertContainsRegex(ORDER_BY + "DESC" + OPEN_PAREN + "rand" + OPEN_PAREN + CLOSE_PAREN + CLOSE_PAREN + SPACE + var("bar"), builder.buildString());
}
use of org.apache.jena.sparql.expr.Expr in project jena by apache.
the class AggCustom method toPrefixString.
@Override
public String toPrefixString() {
IndentedLineBuffer x = new IndentedLineBuffer();
x.append("(");
x.append(getName().toLowerCase(Locale.ROOT));
x.append(" <");
x.append(iri);
x.append("> ");
x.incIndent();
if (isDistinct)
x.append("distinct ");
boolean first = true;
for (Expr e : getExprList()) {
if (!first)
x.append(" ");
first = false;
WriterExpr.output(x, e, null);
first = false;
}
x.decIndent();
x.append(")");
return x.asString();
}
use of org.apache.jena.sparql.expr.Expr in project jena by apache.
the class labelSearch method exec.
/* This be called once, with unevaluated arguments.
* To do a rewrite of part of a query, we must use the fundamental PropertyFunction
* interface to be called once with the input iterator.
* Must not return null nor throw an exception. Instead, return a QueryIterNullIterator
* indicating no matches.
*/
@Override
public QueryIterator exec(QueryIterator input, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
// No real need to check the pattern arguments because
// the replacement triple pattern and regex will cope
// but we illustrate testing here.
Node nodeVar = argSubject.getArg();
String pattern = NodeUtils.stringLiteral(argObject.getArg());
if (pattern == null) {
Log.warn(this, "Pattern must be a plain literal or xsd:string: " + argObject.getArg());
return QueryIterNullIterator.create(execCxt);
}
if (false)
// Old (ARQ 1) way - not recommended.
return buildSyntax(input, nodeVar, pattern, execCxt);
// Better
// Build a SPARQL algebra expression
// Hidden variable
Var var2 = createNewVar();
BasicPattern bp = new BasicPattern();
Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2);
bp.add(t);
OpBGP op = new OpBGP(bp);
Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i");
Op filter = OpFilter.filter(regex, op);
// ---- Evaluation
if (true) {
// Use the reference query engine
// Create a table for the input stream (so it uses working memory at this point,
// which is why this is not the preferred way).
// Then join to expression for this stage.
Table table = TableFactory.create(input);
Op op2 = OpJoin.create(OpTable.create(table), filter);
return Algebra.exec(op2, execCxt.getDataset());
}
// Use the default, optimizing query engine.
return QC.execute(filter, input, execCxt);
}
Aggregations