Search in sources :

Example 1 with Template

use of org.apache.jena.sparql.syntax.Template in project jena by apache.

the class ParserSPARQL10 method parseTemplate.

public static Template parseTemplate(String string) {
    final Query query = new Query();
    Action action = new Action() {

        @Override
        public void exec(SPARQLParser10 parser) throws Exception {
            Template t = parser.ConstructTemplate();
            query.setConstructTemplate(t);
        }
    };
    perform(query, string, action);
    return query.getConstructTemplate();
}
Also used : Query(org.apache.jena.query.Query) SPARQLParser10(org.apache.jena.sparql.lang.sparql_10.SPARQLParser10) Template(org.apache.jena.sparql.syntax.Template)

Example 2 with Template

use of org.apache.jena.sparql.syntax.Template in project jena by apache.

the class ConstructHandler method setVars.

@Override
public void setVars(Map<Var, Node> values) {
    if (values.isEmpty()) {
        return;
    }
    AbstractRewriter<Node> rw = new AbstractRewriter<Node>(values) {
    };
    query.setConstructTemplate(new Template(BasicPattern.wrap(rw.rewrite(constructs))));
}
Also used : Node(org.apache.jena.graph.Node) AbstractRewriter(org.apache.jena.arq.querybuilder.rewriters.AbstractRewriter) Template(org.apache.jena.sparql.syntax.Template)

Example 3 with Template

use of org.apache.jena.sparql.syntax.Template in project jena by apache.

the class ConstructHandlerTest method testSetVars.

@Test
public void testSetVars() {
    Var v = Var.alloc("v");
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v);
    handler.addConstruct(t);
    Template template = query.getConstructTemplate();
    assertNotNull(template);
    List<Triple> lst = template.getTriples();
    assertEquals(1, lst.size());
    assertEquals(t, lst.get(0));
    Map<Var, Node> values = new HashMap<>();
    values.put(v, NodeFactory.createURI("three"));
    handler.setVars(values);
    template = query.getConstructTemplate();
    assertNotNull(template);
    lst = template.getTriples();
    assertEquals(1, lst.size());
    t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    assertEquals(t, lst.get(0));
}
Also used : Triple(org.apache.jena.graph.Triple) HashMap(java.util.HashMap) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Template(org.apache.jena.sparql.syntax.Template) Test(org.junit.Test)

Example 4 with Template

use of org.apache.jena.sparql.syntax.Template in project jena by apache.

the class QueryExecutionBase method execConstructTriples.

@Override
public Iterator<Triple> execConstructTriples() {
    checkNotClosed();
    if (!query.isConstructType())
        throw new QueryExecException("Attempt to get a CONSTRUCT model from a " + labelForQuery(query) + " query");
    // This causes there to be no PROJECT around the pattern.
    // That in turn, exposes the initial bindings.  
    query.setQueryResultStar(true);
    startQueryIterator();
    Template template = query.getConstructTemplate();
    return TemplateLib.calcTriples(template.getTriples(), queryIterator);
}
Also used : Template(org.apache.jena.sparql.syntax.Template)

Example 5 with Template

use of org.apache.jena.sparql.syntax.Template in project jena by apache.

the class QueryExecutionBase method execConstructQuads.

@Override
public Iterator<Quad> execConstructQuads() {
    checkNotClosed();
    if (!query.isConstructType())
        throw new QueryExecException("Attempt to get a CONSTRUCT model from a " + labelForQuery(query) + " query");
    // This causes there to be no PROJECT around the pattern.
    // That in turn, exposes the initial bindings.  
    query.setQueryResultStar(true);
    startQueryIterator();
    Template template = query.getConstructTemplate();
    return TemplateLib.calcQuads(template.getQuads(), queryIterator);
}
Also used : Template(org.apache.jena.sparql.syntax.Template)

Aggregations

Template (org.apache.jena.sparql.syntax.Template)13 Query (org.apache.jena.query.Query)4 Triple (org.apache.jena.graph.Triple)3 Test (org.junit.Test)3 Node (org.apache.jena.graph.Node)2 QueryExecException (org.apache.jena.query.QueryExecException)2 HashMap (java.util.HashMap)1 AbstractRewriter (org.apache.jena.arq.querybuilder.rewriters.AbstractRewriter)1 Var (org.apache.jena.sparql.core.Var)1 ARQParser (org.apache.jena.sparql.lang.arq.ARQParser)1 SPARQLParser10 (org.apache.jena.sparql.lang.sparql_10.SPARQLParser10)1 SPARQLParser11 (org.apache.jena.sparql.lang.sparql_11.SPARQLParser11)1