Search in sources :

Example 1 with E_Regex

use of org.apache.jena.sparql.expr.E_Regex 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 2 with E_Regex

use of org.apache.jena.sparql.expr.E_Regex 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 3 with E_Regex

use of org.apache.jena.sparql.expr.E_Regex 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 4 with E_Regex

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

E_Regex (org.apache.jena.sparql.expr.E_Regex)4 Expr (org.apache.jena.sparql.expr.Expr)4 Triple (org.apache.jena.graph.Triple)3 Var (org.apache.jena.sparql.core.Var)3 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 Op (org.apache.jena.sparql.algebra.Op)2 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)2 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)2 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)1 Node (org.apache.jena.graph.Node)1 Table (org.apache.jena.sparql.algebra.Table)1 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)1 OpTable (org.apache.jena.sparql.algebra.op.OpTable)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 NodeValue (org.apache.jena.sparql.expr.NodeValue)1 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)1