Search in sources :

Example 46 with Query

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

the class ValuesHandlerTest method testAddSquare.

@Test
public void testAddSquare() {
    Node n = NodeFactory.createLiteral("hello");
    Node nn = NodeFactory.createLiteral("hola");
    Node n2 = NodeFactory.createLiteral("there");
    Node nn2 = NodeFactory.createLiteral("aqui");
    Var v = Var.alloc("x");
    Var v2 = Var.alloc("y");
    handler.addValueVar(v, Arrays.asList(n, n2));
    handler.addValueVar(v2, Arrays.asList(nn, nn2));
    ValuesHandler handler2 = new ValuesHandler(new Query());
    Node n3 = NodeFactory.createLiteral("why");
    Node nn3 = NodeFactory.createLiteral("quando");
    handler2.addValueVar(v, Arrays.asList(n3));
    handler2.addValueVar(v2, Arrays.asList(nn3));
    handler.addAll(handler2);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(2, vars.size());
    assertTrue(vars.contains(v));
    assertTrue(vars.contains(v2));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(3, lb.size());
    List<Node> ln = new ArrayList<Node>();
    ln.add(n);
    ln.add(n2);
    ln.add(n3);
    List<Node> ln2 = new ArrayList<Node>();
    ln2.add(nn);
    ln2.add(nn2);
    ln2.add(nn3);
    for (Binding b : lb) {
        assertTrue(b.contains(v));
        assertTrue(ln.contains(b.get(v)));
        ln.remove(b.get(v));
        assertTrue(b.contains(v2));
        assertTrue(ln2.contains(b.get(v2)));
        ln2.remove(b.get(v2));
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 47 with Query

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

the class WhereHandlerTest method testAddAllPopulatedEmpty.

@Test
public void testAddAllPopulatedEmpty() {
    handler.addWhere(new TriplePath(Triple.ANY));
    Query query2 = new Query();
    WhereHandler handler2 = new WhereHandler(query2);
    handler2.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
    handler.addAll(handler2);
    assertContainsRegex(WHERE + OPEN_CURLY + "ANY" + SPACE + "ANY" + SPACE + "ANY" + DOT + SPACE + uri("one") + SPACE + uri("two") + SPACE + quote("three") + OPT_SPACE + CLOSE_CURLY, query.toString());
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) TriplePath(org.apache.jena.sparql.core.TriplePath) Test(org.junit.Test)

Example 48 with Query

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

the class WhereHandlerTest method testAddAllOnEmpty.

@Test
public void testAddAllOnEmpty() {
    Query query2 = new Query();
    WhereHandler handler2 = new WhereHandler(query2);
    handler2.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
    handler.addAll(handler2);
    assertContainsRegex(WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + quote("three") + OPT_SPACE + CLOSE_CURLY, query.toString());
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) TriplePath(org.apache.jena.sparql.core.TriplePath) Test(org.junit.Test)

Example 49 with Query

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

the class ExTDB4 method main.

public static void main(String... argv) {
    // Direct way: Make a TDB-back Jena model in the named directory.
    String directory = "MyDatabases/DB1";
    Dataset dataset = TDBFactory.createDataset(directory);
    // Potentially expensive query.
    String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
    // See http://incubator.apache.org/jena/documentation/query/app_api.html
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(results);
    qexec.close();
    dataset.close();
}
Also used : Query(org.apache.jena.query.Query) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 50 with Query

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

the class ExTDB_Txn1 method execQuery.

public static void execQuery(String sparqlQueryString, Dataset dataset) {
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
    try {
        ResultSet results = qexec.execSelect();
        for (; results.hasNext(); ) {
            QuerySolution soln = results.nextSolution();
            int count = soln.getLiteral("count").getInt();
            System.out.println("count = " + count);
        }
    } finally {
        qexec.close();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Aggregations

Query (org.apache.jena.query.Query)106 Test (org.junit.Test)22 Op (org.apache.jena.sparql.algebra.Op)17 QueryExecution (org.apache.jena.query.QueryExecution)13 ResultSet (org.apache.jena.query.ResultSet)11 Var (org.apache.jena.sparql.core.Var)11 HashMap (java.util.HashMap)9 QuerySolution (org.apache.jena.query.QuerySolution)9 ContractTest (org.xenei.junit.contract.ContractTest)9 Node (org.apache.jena.graph.Node)8 WhereHandler (org.apache.jena.arq.querybuilder.handlers.WhereHandler)7 VarExprList (org.apache.jena.sparql.core.VarExprList)7 Element (org.apache.jena.sparql.syntax.Element)7 Triple (org.apache.jena.graph.Triple)6 Before (org.junit.Before)6 Map (java.util.Map)5 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)5 Context (org.apache.jena.sparql.util.Context)5 StringReader (java.io.StringReader)4 NodeAlignment (mom.trd.opentheso.bdd.helper.nodes.NodeAlignment)4