Search in sources :

Example 41 with Query

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

the class TestAPI method testARQConstructQuad_Prefix.

// Allow duplicated template quads in execConstructQuads()
@Test
public void testARQConstructQuad_Prefix() {
    String queryString = "PREFIX :   <http://example/ns#> CONSTRUCT { GRAPH :g1 { ?s :p ?o} } WHERE { ?s ?p ?o }";
    Query q = QueryFactory.create(queryString, Syntax.syntaxARQ);
    QueryExecution qExec = QueryExecutionFactory.create(q, d);
    Iterator<Quad> quads = qExec.execConstructQuads();
    DatasetGraph result = DatasetGraphFactory.create();
    long count = 0;
    while (quads.hasNext()) {
        count++;
        Quad qd = quads.next();
        result.add(qd);
    }
    DatasetGraph expected = DatasetGraphFactory.create();
    expected.add(g1.asNode(), s.asNode(), p.asNode(), o.asNode());
    assertEquals(1, count);
    assertTrue(IsoMatcher.isomorphic(expected, result));
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Query(org.apache.jena.query.Query) QueryExecution(org.apache.jena.query.QueryExecution) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 42 with Query

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

the class TestAPI method testARQConstructQuad_a_1.

// // Execute a test both with and without regex optimization enabled
// // Check the number of results
// private void XexecRegexTest(int expected, String queryString)
// {
// Object b = ARQ.getContext().get(ARQ.enableRegexConstraintsOpt) ;
// try {
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, "false") ;
// int count1 = queryAndCount(queryString) ;
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, "true") ;
// int count2 = queryAndCount(queryString) ;
// assertEquals("Different number of results", count1, count2) ;
// if ( expected >= 0 )
// assertEquals("Unexpected number of results", expected, count1) ;
// } finally {
// ARQ.getContext().set(ARQ.enableRegexConstraintsOpt, b) ;
// }
// }
// ARQ Construct Quad Tests:
// Two types of query strings: a) construct triple string; b) construct quad string;
// Two kinds of query methods: 1) execTriples(); 2) execQuads();
// Test a)+1)
@Test
public void testARQConstructQuad_a_1() {
    String queryString = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH ?g { ?s ?p ?o } }";
    Query q = QueryFactory.create(queryString, Syntax.syntaxARQ);
    QueryExecution qExec = QueryExecutionFactory.create(q, d);
    Iterator<Triple> ts = qExec.execConstructTriples();
    Model result = ModelFactory.createDefaultModel();
    while (ts.hasNext()) {
        Triple t = ts.next();
        Statement stmt = ModelUtils.tripleToStatement(result, t);
        if (stmt != null)
            result.add(stmt);
    }
    assertEquals(3, result.size());
    assertTrue(m.isIsomorphicWith(result));
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 43 with Query

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

the class TestAPI method testExecJson.

/**
 * Test that a JSON query returns an array with the correct size, given a pre-populated model.
 */
@Test
public void testExecJson() {
    // JENA-632
    Query query = QueryFactory.create("JSON { \"s\": ?s , \"p\": ?p , \"o\" : ?o } " + "WHERE { ?s ?p ?o }", Syntax.syntaxARQ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, m)) {
        JsonArray jsonArray = qexec.execJson();
        assertNotNull(jsonArray);
        assertEquals(3, jsonArray.size());
    }
}
Also used : JsonArray(org.apache.jena.atlas.json.JsonArray) Query(org.apache.jena.query.Query) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 44 with Query

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

the class TestAPI method testInitialBindings7.

@Test
public void testInitialBindings7() {
    // JENA-1354
    Query query = QueryFactory.create("SELECT DISTINCT ?x WHERE {}");
    Dataset ds = DatasetFactory.create();
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    initialBinding.add("a", ResourceFactory.createTypedLiteral(Boolean.TRUE));
    try (QueryExecution qexec = QueryExecutionFactory.create(query, ds, initialBinding)) {
        assertFalse(qexec.execSelect().next().contains("a"));
    }
}
Also used : Query(org.apache.jena.query.Query) Dataset(org.apache.jena.query.Dataset) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap) Test(org.junit.Test)

Example 45 with Query

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

the class TestAPI method testARQConstructQuad_Duplicate_1.

// Allow duplicated quads in execConstructQuads()
@Test
public void testARQConstructQuad_Duplicate_1() {
    String queryString = "CONSTRUCT { GRAPH ?g1 {?s ?p ?o} } WHERE { ?s ?p ?o. GRAPH ?g1 {?s1 ?p1 ?o1} }";
    Query q = QueryFactory.create(queryString, Syntax.syntaxARQ);
    QueryExecution qExec = QueryExecutionFactory.create(q, d);
    Iterator<Quad> ts = qExec.execConstructQuads();
    long count = 0;
    Quad expected = Quad.create(g1.asNode(), s.asNode(), p.asNode(), o.asNode());
    while (ts.hasNext()) {
        count++;
        Quad qd = ts.next();
        assertEquals(expected, qd);
    }
    // 3 duplicated quads
    assertEquals(3, count);
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Query(org.apache.jena.query.Query) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Aggregations

Query (org.apache.jena.query.Query)265 Test (org.junit.Test)78 ContractTest (org.xenei.junit.contract.ContractTest)65 QueryExecution (org.apache.jena.query.QueryExecution)63 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)49 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)42 Var (org.apache.jena.sparql.core.Var)42 Triple (org.apache.jena.graph.Triple)38 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)32 ResultSet (org.apache.jena.query.ResultSet)29 Node (org.apache.jena.graph.Node)28 TriplePath (org.apache.jena.sparql.core.TriplePath)28 QuerySolution (org.apache.jena.query.QuerySolution)26 ExprVar (org.apache.jena.sparql.expr.ExprVar)24 Binding (org.apache.jena.sparql.engine.binding.Binding)22 Model (org.apache.jena.rdf.model.Model)20 HashMap (java.util.HashMap)19 Dataset (org.apache.jena.query.Dataset)15 Op (org.apache.jena.sparql.algebra.Op)15 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)14