Search in sources :

Example 1 with ParsedQuery

use of org.openrdf.query.parser.ParsedQuery 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 2 with ParsedQuery

use of org.openrdf.query.parser.ParsedQuery in project blueprints by tinkerpop.

the class SailGraph method executeSparql.

/**
     * Evaluate a SPARQL query against the SailGraph (http://www.w3.org/TR/rdf-sparql-query/). The result is a mapping between the ?-bindings and the bound URI, blank node, or literal represented as a Vertex.
     *
     * @param sparqlQuery the SPARQL query to evaluate
     * @return the mapping between a ?-binding and the URI, blank node, or literal as a Vertex
     * @throws RuntimeException if an error occurs in the SPARQL query engine
     */
public List<Map<String, Vertex>> executeSparql(String sparqlQuery) throws RuntimeException {
    try {
        sparqlQuery = getPrefixes() + sparqlQuery;
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery query = parser.parseQuery(sparqlQuery, null);
        boolean includeInferred = false;
        final CloseableIteration<? extends BindingSet, QueryEvaluationException> results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred);
        final List<Map<String, Vertex>> returnList = new ArrayList<Map<String, Vertex>>();
        try {
            while (results.hasNext()) {
                BindingSet bs = results.next();
                Map<String, Vertex> returnMap = new HashMap<String, Vertex>();
                for (Binding b : bs) {
                    returnMap.put(b.getName(), this.getVertex(b.getValue().toString()));
                }
                returnList.add(returnMap);
            }
        } finally {
            results.close();
        }
        return returnList;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : Binding(org.openrdf.query.Binding) Vertex(com.tinkerpop.blueprints.Vertex) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ParsedQuery

use of org.openrdf.query.parser.ParsedQuery 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 ParsedQuery

use of org.openrdf.query.parser.ParsedQuery 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 ParsedQuery

use of org.openrdf.query.parser.ParsedQuery 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

QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 ParsedQuery (org.openrdf.query.parser.ParsedQuery)5 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)5 Test (org.junit.Test)4 BindingSet (org.openrdf.query.BindingSet)4 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)4 URI (org.openrdf.model.URI)3 SailConnection (org.openrdf.sail.SailConnection)3 Vertex (com.tinkerpop.blueprints.Vertex)2 Literal (org.openrdf.model.Literal)2 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)2 SailException (org.openrdf.sail.SailException)2 Edge (com.tinkerpop.blueprints.Edge)1 Graph (com.tinkerpop.blueprints.Graph)1 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)1 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Value (org.openrdf.model.Value)1