Search in sources :

Example 41 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.

the class AlgebraExec method main.

public static void main(String[] argv) {
    String BASE = "http://example/";
    BasicPattern bp = new BasicPattern();
    Var var_x = Var.alloc("x");
    Var var_z = Var.alloc("z");
    // ---- Build expression
    bp.add(new Triple(var_x, NodeFactory.createURI(BASE + "p"), var_z));
    Op op = new OpBGP(bp);
    // Expr expr = ExprUtils.parse("?z < 2 ") ;
    Expr expr = new E_LessThan(new ExprVar(var_z), NodeValue.makeNodeInteger(2));
    op = OpFilter.filter(expr, op);
    // ---- Example setup
    Model m = makeModel();
    m.write(System.out, "TTL");
    System.out.println("--------------");
    System.out.print(op);
    System.out.println("--------------");
    // ---- Execute expression
    QueryIterator qIter = Algebra.exec(op, m.getGraph());
    // -------- Either read the query iterator directly ...
    if (false) {
        for (; qIter.hasNext(); ) {
            Binding b = qIter.nextBinding();
            Node n = b.get(var_x);
            System.out.println(NodeFmtLib.displayStr(n));
            System.out.println(b);
        }
        qIter.close();
    } else {
        // -------- Or make ResultSet from it (but not both - reading an
        // iterator consumes the current solution)
        List<String> varNames = new ArrayList<>();
        varNames.add("x");
        varNames.add("z");
        ResultSet rs = ResultSetStream.create(varNames, m, qIter);
        ResultSetFormatter.out(rs);
        qIter.close();
    }
    System.exit(0);
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) Binding(org.apache.jena.sparql.engine.binding.Binding) Op(org.apache.jena.sparql.algebra.Op) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BasicPattern(org.apache.jena.sparql.core.BasicPattern) E_LessThan(org.apache.jena.sparql.expr.E_LessThan) Triple(org.apache.jena.graph.Triple) Expr(org.apache.jena.sparql.expr.Expr) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet)

Example 42 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.

the class labelSearch method buildSyntax.

// Build SPARQL syntax and compile it.
// Not recommended.
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt) {
    Var var2 = createNewVar();
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2);
    elementBGP.addTriple(t);
    // Regular expression for  regex(?hiddenVar, "pattern", "i")
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i");
    ElementGroup elementGroup = new ElementGroup();
    elementGroup.addElement(elementBGP);
    elementGroup.addElement(new ElementFilter(regex));
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup);
    op = Algebra.optimize(op, execCxt.getContext());
    return QC.execute(op, 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) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 43 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar 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)

Example 44 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.

the class ExProg2 method main.

public static void main(String[] args) {
    Model model = createModel();
    Query query = QueryFactory.make();
    query.setQuerySelectType();
    // See also ExProg1
    ElementGroup elg = new ElementGroup();
    Var varTitle = Var.alloc("title");
    Var varX = Var.alloc("x");
    Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
    elg.addTriplePattern(t1);
    // Adds a filter.  Need to wrap variable in a NodeVar.
    Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i");
    ElementFilter filter = new ElementFilter(expr);
    elg.addElementFilter(filter);
    // Attach the group to query.
    query.setQueryPattern(elg);
    // Choose what we want - SELECT ?title
    query.addResultVar(varTitle);
    // Print query with line numbers
    // Prefix mapping just helps serialization
    query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
    query.serialize(new IndentedWriter(System.out, true));
    System.out.println();
    try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        // Assumption: it's a SELECT query.
        ResultSet rs = qexec.execSelect();
        // The order of results is undefined.
        System.out.println("Titles: ");
        for (; rs.hasNext(); ) {
            QuerySolution rb = rs.nextSolution();
            // Get title - variable names do not include the '?' (or '$')
            RDFNode x = rb.get("title");
            // Check the type of the result value
            if (x instanceof Literal) {
                Literal titleStr = (Literal) x;
                System.out.println("    " + titleStr);
            } else
                System.out.println("Strange - not a literal: " + x);
        }
    }
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Expr(org.apache.jena.sparql.expr.Expr) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter)

Example 45 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar in project jena by apache.

the class TestUserDefinedFunctionFactory method test_user_defined_function_factory_add_01.

@Test
public void test_user_defined_function_factory_add_01() {
    Expr e = new E_Multiply(new ExprVar("x"), new ExprVar("x"));
    UserDefinedFunctionFactory.getFactory().add("http://example/square", e, new ArrayList<>(e.getVarsMentioned()));
    Assert.assertTrue(UserDefinedFunctionFactory.getFactory().isRegistered("http://example/square"));
    Assert.assertEquals(e, UserDefinedFunctionFactory.getFactory().get("http://example/square").getBaseExpr());
}
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) Test(org.junit.Test)

Aggregations

ExprVar (org.apache.jena.sparql.expr.ExprVar)51 Expr (org.apache.jena.sparql.expr.Expr)34 Test (org.junit.Test)17 ContractTest (org.xenei.junit.contract.ContractTest)15 Var (org.apache.jena.sparql.core.Var)12 SortCondition (org.apache.jena.query.SortCondition)11 Node (org.apache.jena.graph.Node)9 Op (org.apache.jena.sparql.algebra.Op)7 Triple (org.apache.jena.graph.Triple)6 Query (org.apache.jena.query.Query)6 E_LessThan (org.apache.jena.sparql.expr.E_LessThan)5 E_Random (org.apache.jena.sparql.expr.E_Random)5 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)5 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)4 BasicPattern (org.apache.jena.sparql.core.BasicPattern)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)4 ArrayList (java.util.ArrayList)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 Random (java.util.Random)2