Search in sources :

Example 11 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project jena by apache.

the class TestAPI method testInitialBindings5.

/**
 * Initial binding substitution happens before optimization so initial bindings can make a semantically always false query into one that can return true
 */
@Test
public void testInitialBindings5() {
    // From JENA-500
    Query query = QueryFactory.create("ASK\n" + "WHERE {\n" + "    FILTER (?a = <http://constant>) .\n" + "}");
    // System.out.println(Algebra.optimize(Algebra.compile(query)).toString());
    Model model = ModelFactory.createDefaultModel();
    model.add(OWL.Thing, RDF.type, OWL.Class);
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    initialBinding.add("a", ResourceFactory.createResource("http://constant"));
    try (QueryExecution qExec = QueryExecution.model(model).query(query).initialBinding(initialBinding).build()) {
        boolean result = qExec.execAsk();
        assertTrue(result);
    }
}
Also used : Query(org.apache.jena.query.Query) Model(org.apache.jena.rdf.model.Model) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 12 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project rdf2neo by Rothamsted.

the class RdfDataManager method getCyNode.

/**
 * Uses the underlining TDB and mapping queries to create a new {@link CyNode} instance.
 *
 * @param nodeRes the RDF/Jena resource correspnding to the Cypher node. This provides the ?iri paramter in the queries below.
 * @param labelsSparql the node labels query, which is usually taken from {@link CyNodeLoadingHandler#getLabelsSparql()}.
 * @param propsSparql the node properties query, which is usually taken from {@link CyNodeLoadingHandler#getNodePropsSparql()}.
 */
public CyNode getCyNode(Resource nodeRes, String labelsSparql, String propsSparql) {
    ensureOpen();
    Model model = this.dataSet.getDefaultModel();
    QuerySolutionMap params = new QuerySolutionMap();
    params.add("iri", nodeRes);
    CyNode cyNode = new CyNode(nodeRes.getURI());
    // The node's labels
    if (labelsSparql != null) {
        // If it's omitted, it will get the default label.
        Query qry = this.queryCache.getUnchecked(labelsSparql);
        Function<String, String> labelIdConverter = this.getCyNodeLabelIdConverter();
        boolean wasInTnx = dataSet.isInTransaction();
        if (!wasInTnx)
            dataSet.begin(ReadWrite.READ);
        try {
            QueryExecution qx = QueryExecutionFactory.create(qry, model, params);
            qx.execSelect().forEachRemaining(row -> cyNode.addLabel(this.getCypherId(row.get("label"), labelIdConverter)));
        } finally {
            if (!wasInTnx && dataSet.isInTransaction())
                dataSet.end();
        }
    }
    // and the properties
    this.addCypherProps(cyNode, propsSparql);
    return cyNode;
}
Also used : Query(org.apache.jena.query.Query) Model(org.apache.jena.rdf.model.Model) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 13 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project jena by apache.

the class TestAPI method testInitialBindingsConstruct2.

@Test
public void testInitialBindingsConstruct2() {
    QuerySolutionMap init = new QuerySolutionMap();
    init.add("o", m.createLiteral("x1"));
    String qs = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
    try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
        Model r = qExec.execConstruct();
        assertTrue("Empty model", r.size() > 0);
        Property p1 = m.createProperty(ns + "p1");
        assertTrue("Empty model", r.contains(null, p1, init.get("x1")));
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) Property(org.apache.jena.rdf.model.Property) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 14 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project jena by apache.

the class TestAPI method testInitialBindings1.

@Test
public void testInitialBindings1() {
    QuerySolutionMap init = new QuerySolutionMap();
    init.add("o", m.createLiteral("y1"));
    String qs = "SELECT * {?s ?p ?o}";
    try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
        int count = queryAndCount(qExec);
        assertEquals("Initial binding didn't restrict query properly", 1, count);
    }
}
Also used : QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 15 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project jena by apache.

the class TestAPI method testInitialBindings2.

@Test
public void testInitialBindings2() {
    QuerySolutionMap init = new QuerySolutionMap();
    init.add("z", m.createLiteral("zzz"));
    String qs = "SELECT * {?s ?p ?o}";
    try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
        int count = queryAndCount(qExec);
        assertEquals("Initial binding restricted query improperly", 3, count);
    }
}
Also used : QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Aggregations

QuerySolutionMap (org.apache.jena.query.QuerySolutionMap)17 QueryExecution (org.apache.jena.query.QueryExecution)13 Test (org.junit.Test)10 Query (org.apache.jena.query.Query)8 Model (org.apache.jena.rdf.model.Model)7 QuerySolution (org.apache.jena.query.QuerySolution)3 ResultSet (org.apache.jena.query.ResultSet)3 Node (org.apache.jena.graph.Node)2 Dataset (org.apache.jena.query.Dataset)2 Property (org.apache.jena.rdf.model.Property)2 ArrayList (java.util.ArrayList)1 SortCondition (org.apache.jena.query.SortCondition)1 ResourceImpl (org.apache.jena.rdf.model.impl.ResourceImpl)1 ShaclException (org.apache.jena.shacl.ShaclException)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1 Var (org.apache.jena.sparql.core.Var)1 Expr (org.apache.jena.sparql.expr.Expr)1