Search in sources :

Example 66 with Query

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

the class ValuesHandlerTest method testAddNotSquare.

@Test
public void testAddNotSquare() {
    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");
    Var v3 = Var.alloc("z");
    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(v2, Arrays.asList(n3));
    handler2.addValueVar(v3, Arrays.asList(nn3));
    handler.addAll(handler2);
    handler.build();
    List<Var> vars = query.getValuesVariables();
    assertEquals(3, vars.size());
    assertTrue(vars.contains(v));
    assertTrue(vars.contains(v2));
    assertTrue(vars.contains(v3));
    assertNotNull(query.getValuesData());
    List<Binding> lb = query.getValuesData();
    assertEquals(3, lb.size());
    for (Binding b : lb) {
        assertTrue(b.contains(v2));
        Node node = b.get(v2);
        if (node.equals(nn)) {
            assertEquals(n, b.get(v));
            assertFalse(b.contains(v3));
        } else if (node.equals(nn2)) {
            assertEquals(n2, b.get(v));
            assertFalse(b.contains(v3));
        } else if (node.equals(n3)) {
            assertFalse(b.contains(v));
            assertEquals(nn3, b.get(v3));
        } else {
            fail("Wrong data in table");
        }
    }
}
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) Test(org.junit.Test)

Example 67 with Query

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

the class ExTDB5 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);
    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();
    }
    // Close the dataset.
    dataset.close();
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 68 with Query

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

the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiteralsMultiple.

protected List<Node> doTestSearchWithLiteralsMultiple(String turtle, String queryString, String expectedEntityURI) {
    List<Node> literals = new LinkedList<>();
    Model model = dataset.getDefaultModel();
    Reader reader = new StringReader(turtle);
    dataset.begin(ReadWrite.WRITE);
    model.read(reader, "", "TURTLE");
    dataset.commit();
    Query query = QueryFactory.create(queryString);
    dataset.begin(ReadWrite.READ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        int count;
        for (count = 0; results.hasNext(); count++) {
            QuerySolution soln = results.nextSolution();
            String entityURI = soln.getResource("s").getURI();
            assertEquals(expectedEntityURI, entityURI);
            Literal literal = soln.getLiteral("literal");
            assertNotNull(literal);
            literals.add(literal.asNode());
        }
    } finally {
        dataset.end();
    }
    return literals;
}
Also used : Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) Reader(java.io.Reader) StringReader(java.io.StringReader) LinkedList(java.util.LinkedList) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) ResultSet(org.apache.jena.query.ResultSet)

Example 69 with Query

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

the class AbstractTestTransform method testQuery.

public void testQuery(String input, Transform transform, String... output) {
    Query q = QueryFactory.create(input);
    Op op = Algebra.compile(q);
    test(op, transform, output);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query)

Example 70 with Query

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

the class TestOpAsQuery method test_roundTripAlegbra.

// Compare A1 and A2 where 
//  query[Q1]->algebra[A1]->OpAsQuery->query[Q2]->algebra[A2]
// Sometimes Q1 and Q2 are equivalent but not .equals.  
public void test_roundTripAlegbra(String query) {
    Query[] r = roundTripQuery(query);
    // Even if the strings come out as non-equal because of the translation from algebra to query
    // the algebras should be equal
    // i.e. the queries should remain semantically equivalent
    Op a1 = Algebra.compile(r[0]);
    Op a2 = Algebra.compile(r[1]);
    Assert.assertEquals(a1, a2);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query)

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