Search in sources :

Example 1 with EmptyBindingSet

use of org.openrdf.query.impl.EmptyBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testNoVariableSP.

@Test
public void testNoVariableSP() throws Exception {
    // Insert data
    insert(OWL.THING, RDF.TYPE, OWL.CLASS);
    insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
    insert(VF.createURI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT * WHERE {\n" + "  owl:Thing a owl:Class .\n" + "}";
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new EmptyBindingSet());
    // Execute pipeline and verify results
    QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Projection);
    Projection projection = (Projection) queryTree.getArg();
    Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
    Multiset<BindingSet> solutions = HashMultiset.create();
    CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) QueryRoot(org.openrdf.query.algebra.QueryRoot) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Projection(org.openrdf.query.algebra.Projection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 2 with EmptyBindingSet

use of org.openrdf.query.impl.EmptyBindingSet in project blueprints by tinkerpop.

the class PropertyGraphSailTest method testSPARQL.

@Test
public void testSPARQL() throws Exception {
    int count;
    String queryStr = "PREFIX pgm: <" + PropertyGraphSail.ONTOLOGY_NS + ">\n" + "PREFIX prop: <" + PropertyGraphSail.PROPERTY_NS + ">\n" + "SELECT ?project ?name WHERE {\n" + "   ?marko prop:name \"marko\".\n" + "   ?e1 pgm:label \"knows\".\n" + "   ?e1 pgm:tail ?marko.\n" + "   ?e1 pgm:head ?friend.\n" + "   ?e2 pgm:label \"created\".\n" + "   ?e2 pgm:tail ?friend.\n" + "   ?e2 pgm:head ?project.\n" + "   ?project prop:name ?name.\n" + "}";
    System.out.println(queryStr);
    ParsedQuery query = new SPARQLParser().parseQuery(queryStr, "http://example.org/bogus/");
    CloseableIteration<? extends BindingSet, QueryEvaluationException> results = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);
    try {
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI project = (URI) set.getValue("project");
            Literal name = (Literal) set.getValue("name");
            assertNotNull(project);
            assertNotNull(name);
            System.out.println("project = " + project + ", name = " + name);
        }
    } finally {
        results.close();
    }
    assertEquals(2, count);
}
Also used : BindingSet(org.openrdf.query.BindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 3 with EmptyBindingSet

use of org.openrdf.query.impl.EmptyBindingSet in project blueprints by tinkerpop.

the class SailTest method testJoins.

@Test
public void testJoins() throws Exception {
    SPARQLParser parser = new SPARQLParser();
    BindingSet bindings = new EmptyBindingSet();
    String baseURI = "http://example.org/bogus/";
    SailConnection sc = sail.getConnection();
    try {
        CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
        int count;
        String queryStr = "PREFIX : <urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/>\n" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?foaf WHERE {\n" + "    :ford foaf:knows ?friend .\n" + "    ?friend foaf:knows ?foaf .\n" + "}";
        ParsedQuery query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI foaf = (URI) set.getValue("foaf");
            assertTrue(foaf.stringValue().startsWith("urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/"));
        }
        results.close();
        assertEquals(4, count);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ParsedQuery(org.openrdf.query.parser.ParsedQuery) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 4 with EmptyBindingSet

use of org.openrdf.query.impl.EmptyBindingSet in project blueprints by tinkerpop.

the class SailTest method testEvaluate.

// tuple queries ///////////////////////////////////////////////////////////
@Test
public void testEvaluate() throws Exception {
    Set<String> languages;
    String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
    URI thorUri = sail.getValueFactory().createURI(prefix + "thor");
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        sc.addStatement(uriA, uriB, uriC);
        sc.commit();
        sc.begin();
        SPARQLParser parser = new SPARQLParser();
        BindingSet bindings = new EmptyBindingSet();
        String baseURI = "http://example.org/bogus/";
        String queryStr;
        ParsedQuery query;
        CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
        int count;
        // s ?p ?o SELECT
        queryStr = "SELECT ?y ?z WHERE { <http://example.org/uriA> ?y ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI y = (URI) set.getValue("y");
            Value z = (Value) set.getValue("z");
            assertNotNull(y);
            assertNotNull(z);
        // System.out.println("y = " + y + ", z = " + z);
        }
        results.close();
        assertTrue(count > 0);
        // s p ?o SELECT using a namespace prefix
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        languages = new HashSet<String>();
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            Literal z = (Literal) set.getValue("z");
            assertNotNull(z);
            languages.add(z.getLanguage());
        }
        results.close();
        assertTrue(count > 0);
        assertEquals(2, languages.size());
        assertTrue(languages.contains("en"));
        assertTrue(languages.contains("is"));
        // ?s p o SELECT using a plain literal value with no language tag
        queryStr = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?s WHERE { ?s rdfs:comment \"he really knows where his towel is\" }";
        URI fordUri = sail.getValueFactory().createURI(prefix + "ford");
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, fordUri);
        }
        results.close();
        assertTrue(count > 0);
        // ?s p o SELECT using a language-specific literal value
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\"@en }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, thorUri);
        }
        results.close();
        assertTrue(count > 0);
        // The language tag is necessary
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\" }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
        // ?s p o SELECT using a typed literal value
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\"^^xsd:string }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, thorUri);
        }
        results.close();
        assertTrue(count > 0);
        // The data type is necessary
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\" }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
        // s ?p o SELECT
        // TODO: commented out languages for now
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?p WHERE { <" + prefix + "thor> ?p \"Thor\"@en }";
        query = parser.parseQuery(queryStr, baseURI);
        URI foafNameUri = sail.getValueFactory().createURI("http://xmlns.com/foaf/0.1/name");
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI p = (URI) set.getValue("p");
            assertNotNull(p);
            assertEquals(p, foafNameUri);
        }
        results.close();
        assertTrue(count > 0);
        // context-specific SELECT
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <" + prefix + "ctx1>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        languages = new HashSet<String>();
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            Literal z = (Literal) set.getValue("z");
            assertNotNull(z);
            languages.add(z.getLanguage());
        }
        results.close();
        assertTrue(count > 0);
        assertEquals(2, languages.size());
        assertTrue(languages.contains("en"));
        assertTrue(languages.contains("is"));
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <http://example.org/emptycontext>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
    // s p o? select without and with inferencing
    // TODO commented out waiting for inferencing
    // queryStr =
    // "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
    // + "SELECT ?o\n"
    // + "WHERE { <" + prefix + "instance1> rdf:type ?o }";
    // query = parser.parseQuery(queryStr, baseURI);
    // results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
    // bindings, false);
    // count = 0;
    // while (results.hasNext()) {
    // count++;
    // BindingSet set = results.next();
    // URI o = (URI) set.getValue("o");
    // assertEquals(prefix + "classB", o.toString());
    // }
    // results.close();
    // assertEquals(1, count);
    // results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
    // bindings, true);
    // count = 0;
    // boolean foundA = false, foundB = false;
    // while (results.hasNext()) {
    // count++;
    // BindingSet set = results.next();
    // URI o = (URI) set.getValue("o");
    // String s = o.toString();
    // if (s.equals(prefix + "classA")) {
    // foundA = true;
    // } else if (s.equals(prefix + "classB")) {
    // foundB = true;
    // }
    // }
    // results.close();
    // assertEquals(2, count);
    // assertTrue(foundA);
    // assertTrue(foundB);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) URI(org.openrdf.model.URI) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) Test(org.junit.Test)

Example 5 with EmptyBindingSet

use of org.openrdf.query.impl.EmptyBindingSet in project blueprints by tinkerpop.

the class GraphSailTest method testCodePlay.

@Test
public void testCodePlay() throws Exception {
    Sail sail = new GraphSail(new TinkerGraph());
    sail.initialize();
    try {
        SailConnection sc = sail.getConnection();
        try {
            sc.begin();
            ValueFactory vf = sail.getValueFactory();
            sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#knows"), vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com"));
            sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("marko"), vf.createURI("http://tinkerpop.com"));
            sc.addStatement(vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("josh"), vf.createURI("http://tinkerpop.com"));
            CloseableIteration<? extends Statement, SailException> results = sc.getStatements(null, null, null, false);
            try {
                System.out.println("get statements: ?s ?p ?o ?g");
                while (results.hasNext()) {
                    System.out.println(results.next());
                }
            } finally {
                results.close();
            }
            System.out.println("\nget statements: http://tinkerpop.com#3 ?p ?o ?g");
            results = sc.getStatements(vf.createURI("http://tinkerpop.com#3"), null, null, false);
            try {
                while (results.hasNext()) {
                    System.out.println(results.next());
                }
            } finally {
                results.close();
            }
            SPARQLParser parser = new SPARQLParser();
            CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults;
            String queryString = "SELECT ?x ?y WHERE { ?x <http://tinkerpop.com#knows> ?y }";
            ParsedQuery query = parser.parseQuery(queryString, "http://tinkerPop.com");
            System.out.println("\nSPARQL: " + queryString);
            sparqlResults = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);
            try {
                while (sparqlResults.hasNext()) {
                    System.out.println(sparqlResults.next());
                }
            } finally {
                sparqlResults.close();
            }
            Graph graph = ((GraphSail) sail).getBaseGraph();
            System.out.println();
            for (Vertex v : graph.getVertices()) {
                System.out.println("------");
                System.out.println(v);
                for (String key : v.getPropertyKeys()) {
                    System.out.println(key + "=" + v.getProperty(key));
                }
            }
            for (Edge e : graph.getEdges()) {
                System.out.println("------");
                System.out.println(e);
                for (String key : e.getPropertyKeys()) {
                    System.out.println(key + "=" + e.getProperty(key));
                }
            }
        } finally {
            sc.rollback();
            sc.close();
        }
    } finally {
        sail.shutDown();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) ParsedQuery(org.openrdf.query.parser.ParsedQuery) ValueFactory(org.openrdf.model.ValueFactory) SailException(org.openrdf.sail.SailException) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SailConnection(org.openrdf.sail.SailConnection) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) KeyIndexableGraph(com.tinkerpop.blueprints.KeyIndexableGraph) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)5 BindingSet (org.openrdf.query.BindingSet)4 ParsedQuery (org.openrdf.query.parser.ParsedQuery)4 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)4 URI (org.openrdf.model.URI)3 SailConnection (org.openrdf.sail.SailConnection)3 Literal (org.openrdf.model.Literal)2 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)2 Edge (com.tinkerpop.blueprints.Edge)1 Graph (com.tinkerpop.blueprints.Graph)1 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)1 Vertex (com.tinkerpop.blueprints.Vertex)1 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 Value (org.openrdf.model.Value)1 ValueFactory (org.openrdf.model.ValueFactory)1 Projection (org.openrdf.query.algebra.Projection)1 QueryRoot (org.openrdf.query.algebra.QueryRoot)1 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)1