Search in sources :

Example 66 with Expr

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()));
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Expr(org.apache.jena.sparql.expr.Expr) E_Multiply(org.apache.jena.sparql.expr.E_Multiply) BeforeClass(org.junit.BeforeClass)

Example 67 with Expr

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);
}
Also used : E_Regex(org.apache.jena.sparql.expr.E_Regex) NodeValue(org.apache.jena.sparql.expr.NodeValue) Expr(org.apache.jena.sparql.expr.Expr)

Example 68 with Expr

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());
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) E_Random(org.apache.jena.sparql.expr.E_Random) ContractTest(org.xenei.junit.contract.ContractTest)

Example 69 with Expr

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();
}
Also used : Expr(org.apache.jena.sparql.expr.Expr) WriterExpr(org.apache.jena.sparql.sse.writers.WriterExpr) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer)

Example 70 with Expr

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);
}
Also used : Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) ExprVar(org.apache.jena.sparql.expr.ExprVar) Op(org.apache.jena.sparql.algebra.Op) Table(org.apache.jena.sparql.algebra.Table) OpTable(org.apache.jena.sparql.algebra.op.OpTable) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Aggregations

Expr (org.apache.jena.sparql.expr.Expr)83 Var (org.apache.jena.sparql.core.Var)28 ExprList (org.apache.jena.sparql.expr.ExprList)15 NodeValue (org.apache.jena.sparql.expr.NodeValue)15 Op (org.apache.jena.sparql.algebra.Op)13 ExprVar (org.apache.jena.sparql.expr.ExprVar)12 FunctionEnvBase (org.apache.jena.sparql.function.FunctionEnvBase)9 Test (org.junit.Test)9 VarExprList (org.apache.jena.sparql.core.VarExprList)8 Node (org.apache.jena.graph.Node)7 E_Random (org.apache.jena.sparql.expr.E_Random)7 ArrayList (java.util.ArrayList)6 Triple (org.apache.jena.graph.Triple)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 E_Regex (org.apache.jena.sparql.expr.E_Regex)4 ContractTest (org.xenei.junit.contract.ContractTest)4 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)3 Query (org.apache.jena.query.Query)3 SortCondition (org.apache.jena.query.SortCondition)3 ExprAggregator (org.apache.jena.sparql.expr.ExprAggregator)3