Search in sources :

Example 76 with Query

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

the class TestClassify method classifyLJ.

/**
     * Test left join classification
     * @param pattern WHERE clause for the query as a string
     * @param expected Whether the join should be classified as linear
     */
private void classifyLJ(String pattern, boolean expected) {
    String qs1 = "PREFIX : <http://example/>\n";
    String qs = qs1 + "SELECT * " + pattern;
    Query query = QueryFactory.create(qs, Syntax.syntaxARQ);
    Op op = Algebra.compile(query.getQueryPattern());
    if (!(op instanceof OpLeftJoin))
        fail("Not a leftjoin: " + pattern);
    boolean nonLinear = LeftJoinClassifier.isLinear((OpLeftJoin) op);
    assertEquals("LeftJoin: " + pattern, expected, nonLinear);
}
Also used : Query(org.apache.jena.query.Query) OpLeftJoin(org.apache.jena.sparql.algebra.op.OpLeftJoin)

Example 77 with Query

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

the class TestSerialization method test.

private void test(String qs1, String qs2, boolean result) {
    Query q1 = null;
    Query q2 = null;
    try {
        q1 = QueryFactory.create(qs1, Syntax.syntaxSPARQL);
    } catch (Exception ex) {
        fail("Building query 1");
    }
    try {
        q2 = QueryFactory.create(qs2, Syntax.syntaxSPARQL);
    } catch (Exception ex) {
        fail("Building query 2");
    }
    boolean b = false;
    try {
        b = q1.equals(q2);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        fail("Evaluating .equals");
    }
    if (result)
        assertTrue(b);
    else
        assertFalse(b);
}
Also used : OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) Query(org.apache.jena.query.Query)

Example 78 with Query

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

the class TestFlattenSyntax method test.

private static void test(String input, String expected) {
    if (expected == null)
        expected = input;
    String qs = gen(PRE, input);
    String qsExpected = gen(PRE, expected);
    Query query = QueryFactory.create(qs, Syntax.syntaxARQ);
    Query query2 = QueryTransformOps.transform(query, new ElementTransformCleanGroupsOfOne());
    Query queryExpected = QueryFactory.create(qsExpected, Syntax.syntaxARQ);
    Op op1 = Algebra.compile(query);
    Op op2 = Algebra.compile(query2);
    assertEquals("Algebra different", op1, op2);
    boolean modified = !query.equals(query2);
    boolean expectModification = !queryExpected.equals(query);
    assertEquals("Expect query modifed?", expectModification, modified);
}
Also used : Op(org.apache.jena.sparql.algebra.Op) Query(org.apache.jena.query.Query) ElementTransformCleanGroupsOfOne(org.apache.jena.sparql.syntax.syntaxtransform.ElementTransformCleanGroupsOfOne)

Example 79 with Query

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

the class TestService method query_service_context_application_02.

@SuppressWarnings("unchecked")
@Test
public void query_service_context_application_02() {
    // This test requires us to set some authentication credentials for the
    // service
    Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
    if (serviceContextMap == null) {
        ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
        serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
    }
    if (serviceContextMap.get(SERVICE) == null) {
        serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
    }
    Context serviceContext = serviceContextMap.get(SERVICE);
    try {
        HttpClient testClient = HttpClients.custom().build();
        serviceContext.put(Service.queryClient, testClient);
        Query q = QueryFactory.create("ASK { }");
        QueryEngineHTTP engine = QueryExecutionFactory.createServiceRequest(SERVICE, q);
        Assert.assertNotNull(engine);
        // Check that no settings were changed
        Assert.assertEquals(-1, engine.getTimeout1());
        Assert.assertEquals(-1, engine.getTimeout2());
        Assert.assertTrue(engine.getAllowCompression());
        Assert.assertEquals(testClient, engine.getClient());
    } finally {
        serviceContext.remove(Service.queryClient);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) Query(org.apache.jena.query.Query) HttpClient(org.apache.http.client.HttpClient) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 80 with Query

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

the class TestService method query_service_context_application_05.

@SuppressWarnings("unchecked")
@Test
public void query_service_context_application_05() {
    // This test requires us to set that GZip and Deflate are permitted
    Map<String, Context> serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
    if (serviceContextMap == null) {
        ARQ.getContext().put(Service.serviceContext, new HashMap<String, Context>());
        serviceContextMap = (Map<String, Context>) ARQ.getContext().get(Service.serviceContext);
    }
    if (serviceContextMap.get(SERVICE) == null) {
        serviceContextMap.put(SERVICE, new Context(ARQ.getContext()));
    }
    Context serviceContext = serviceContextMap.get(SERVICE);
    try {
        serviceContext.put(Service.queryCompression, false);
        Query q = QueryFactory.create("ASK { }");
        QueryEngineHTTP engine = QueryExecutionFactory.createServiceRequest(SERVICE, q);
        Assert.assertNotNull(engine);
        // Check that no settings were changed
        Assert.assertEquals(-1, engine.getTimeout1());
        Assert.assertEquals(-1, engine.getTimeout2());
        Assert.assertFalse(engine.getAllowCompression());
        Assert.assertNull(engine.getClient());
    } finally {
        serviceContext.remove(Service.queryCompression);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

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