Search in sources :

Example 16 with ExprVar

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

the class TestSortedDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.

@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
    List<Binding> unsorted = randomBindings(500);
    List<SortCondition> conditions = new ArrayList<>();
    conditions.add(new SortCondition(new ExprVar("8"), Query.ORDER_ASCENDING));
    BindingComparator comparator = new BindingComparator(conditions);
    SortedDataBag<Binding> db = new SortedDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), comparator);
    List<File> spillFiles = new ArrayList<>();
    try {
        db.addAll(unsorted);
        spillFiles.addAll(db.getSpillFiles());
        int count = 0;
        for (File file : spillFiles) {
            if (file.exists()) {
                count++;
            }
        }
        // 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
        assertEquals(49, count);
        Iterator<Binding> iter = db.iterator();
        while (iter.hasNext()) {
            iter.next();
        }
        Iter.close(iter);
    } finally {
        db.close();
    }
    int count = 0;
    for (File file : spillFiles) {
        if (file.exists()) {
            count++;
        }
    }
    assertEquals(0, count);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ExprVar(org.apache.jena.sparql.expr.ExprVar) SortCondition(org.apache.jena.query.SortCondition) BindingComparator(org.apache.jena.sparql.engine.binding.BindingComparator) ArrayList(java.util.ArrayList) SortedDataBag(org.apache.jena.atlas.data.SortedDataBag) File(java.io.File) Test(org.junit.Test)

Example 17 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 18 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 19 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 = new ResultSetStream(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) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) 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 20 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)

Aggregations

ExprVar (org.apache.jena.sparql.expr.ExprVar)20 Expr (org.apache.jena.sparql.expr.Expr)12 Var (org.apache.jena.sparql.core.Var)8 Test (org.junit.Test)7 Op (org.apache.jena.sparql.algebra.Op)5 ArrayList (java.util.ArrayList)4 Node (org.apache.jena.graph.Node)4 Triple (org.apache.jena.graph.Triple)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 SortCondition (org.apache.jena.query.SortCondition)3 Binding (org.apache.jena.sparql.engine.binding.Binding)3 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 SortedDataBag (org.apache.jena.atlas.data.SortedDataBag)2 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 VarExprList (org.apache.jena.sparql.core.VarExprList)2 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)2 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)2