Search in sources :

Example 31 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class OpTriple method equivalent.

public boolean equivalent(OpBGP opBGP) {
    BasicPattern bgp = opBGP.getPattern();
    if (bgp.size() != 1)
        return false;
    Triple t = bgp.get(0);
    return triple.equals(t);
}
Also used : Triple(org.apache.jena.graph.Triple) BasicPattern(org.apache.jena.sparql.core.BasicPattern)

Example 32 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class TestFmtUtils method testFormatBGP_1.

@Test
public void testFormatBGP_1() {
    IndentedLineBuffer b = new IndentedLineBuffer();
    BasicPattern bgp = SSE.parseBGP("(prefix ((zz: <" + aUri + ">)) (bgp (zz:s zz:p zz:o)))");
    FmtUtils.formatPattern(b, bgp, getContext());
    assertEquals("zz:s zz:p zz:o .", b.toString());
}
Also used : BasicPattern(org.apache.jena.sparql.core.BasicPattern) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer) Test(org.junit.Test)

Example 33 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class TestFmtUtils method testFormatBGP_2.

@Test
public void testFormatBGP_2() {
    IndentedLineBuffer b = new IndentedLineBuffer();
    BasicPattern bgp = SSE.parseBGP("(prefix ((zz: <" + aUri + ">)) (bgp (zz:s zz:p zz:o) (zz:s zz:p 123) ))");
    FmtUtils.formatPattern(b, bgp, getContext());
    assertEquals("zz:s zz:p zz:o .\nzz:s zz:p 123 .", b.toString());
}
Also used : BasicPattern(org.apache.jena.sparql.core.BasicPattern) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer) Test(org.junit.Test)

Example 34 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern in project jena by apache.

the class TestFmtUtils method formatPattern_2_triples.

@Test
public void formatPattern_2_triples() {
    BasicPattern basicPattern = new BasicPattern();
    basicPattern.add(getTriple());
    basicPattern.add(getTriple2());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (IndentedWriter iw = new IndentedWriter(os)) {
        SerializationContext sc = new SerializationContext();
        FmtUtils.formatPattern(iw, basicPattern, sc);
    }
    assertEquals("<n1> <n2> \"l3\" .\n" + "<nb1> <nb2> \"lb3\" .", new String(os.toByteArray()));
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) BasicPattern(org.apache.jena.sparql.core.BasicPattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 35 with BasicPattern

use of org.apache.jena.sparql.core.BasicPattern 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

BasicPattern (org.apache.jena.sparql.core.BasicPattern)44 Node (org.apache.jena.graph.Node)15 Triple (org.apache.jena.graph.Triple)13 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)13 Op (org.apache.jena.sparql.algebra.Op)11 Test (org.junit.Test)11 Var (org.apache.jena.sparql.core.Var)5 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)5 Binding (org.apache.jena.sparql.engine.binding.Binding)5 ArrayList (java.util.ArrayList)4 BaseTest (org.apache.jena.atlas.junit.BaseTest)4 OpQuadPattern (org.apache.jena.sparql.algebra.op.OpQuadPattern)4 Quad (org.apache.jena.sparql.core.Quad)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 OpService (org.apache.jena.sparql.algebra.op.OpService)3 QueryExceptionHTTP (org.apache.jena.sparql.engine.http.QueryExceptionHTTP)3 Context (org.apache.jena.sparql.util.Context)3 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)2