Search in sources :

Example 36 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method testInference.

// inference ////////////////////////////////////////////////////////////////
public void testInference() throws Exception {
    if (null != inferencer) {
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI classX = sail.getValueFactory().createURI("http://example.org/classX");
        URI classY = sail.getValueFactory().createURI("http://example.org/classY");
        SailConnection sc = inferencer.getConnection();
        try {
            sc.clear();
            sc.addStatement(classX, RDFS.SUBCLASSOF, classY);
            sc.addStatement(uriA, RDF.TYPE, classX);
            sc.addStatement(uriB, RDF.TYPE, classY);
            sc.commit();
            //showStatements(sc, uriA, RDF.TYPE, null);
            assertEquals(3, countStatements(sc, uriA, RDF.TYPE, null, true));
            assertEquals(1, countStatements(sc, uriA, RDF.TYPE, null, false));
            assertEquals(2, countStatements(sc, uriB, RDF.TYPE, null, true));
            assertEquals(1, countStatements(sc, uriB, RDF.TYPE, null, false));
            if (uniqueStatements) {
                sc.addStatement(uriA, RDF.TYPE, classY);
                sc.commit();
                //showStatements(sc, uriA, RDF.TYPE, null);
                assertEquals(3, countStatements(sc, uriA, RDF.TYPE, null, true));
                assertEquals(2, countStatements(sc, uriA, RDF.TYPE, null, false));
                sc.removeStatements(uriA, RDF.TYPE, classY);
                sc.commit();
                assertEquals(3, countStatements(sc, uriA, RDF.TYPE, null, true));
                assertEquals(2, countStatements(sc, uriA, RDF.TYPE, null, false));
            //sc.removeStatements(uriA, RDF.TYPE, classX);
            //sc.commit();
            //assertEquals(1, countStatements(sc, uriA, RDF.TYPE, null));
            }
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) URI(org.openrdf.model.URI)

Example 37 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method testClear.

@Test
public void testClear() throws Exception {
    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");
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        sc.clear();
        assertEquals(0L, sc.size());
        sc.addStatement(uriA, uriB, uriC, uriA);
        sc.addStatement(uriC, uriA, uriB, uriA);
        sc.addStatement(uriB, uriC, uriA, uriA);
        assertEquals(3L, sc.size(uriA));
        sc.addStatement(uriA, uriB, uriC, uriB);
        sc.addStatement(uriB, uriC, uriA, uriB);
        assertEquals(2L, sc.size(uriB));
        sc.addStatement(uriA, uriB, uriC);
        assertEquals(1L, sc.size((Resource) null));
        sc.addStatement(uriA, uriB, uriC, uriC);
        sc.addStatement(uriB, uriC, uriA, uriC);
        sc.addStatement(uriC, uriA, uriB, uriC);
        sc.addStatement(uriA, uriB, uriB, uriC);
        assertEquals(4L, sc.size(uriC));
        assertEquals(10L, sc.size());
        sc.clear(uriA, uriC);
        assertEquals(1L, sc.size((Resource) null));
        assertEquals(0L, sc.size(uriA));
        assertEquals(2L, sc.size(uriB));
        assertEquals(0L, sc.size(uriC));
        assertEquals(3L, sc.size());
        sc.clear();
        assertEquals(0L, sc.size());
        sc.commit();
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 38 with SailConnection

use of org.openrdf.sail.SailConnection 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 39 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method testGetStatementsP_SOG.

@Test
public void testGetStatementsP_SOG() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/test/P_SOG#a");
        URI uriB = sail.getValueFactory().createURI("http://example.org/test/P_SOG#b");
        URI uriC = sail.getValueFactory().createURI("http://example.org/test/P_SOG#c");
        URI foo = sail.getValueFactory().createURI("http://example.org/ns#foo");
        URI firstName = sail.getValueFactory().createURI("http://example.org/ns#firstName");
        Literal plainLitA = sail.getValueFactory().createLiteral("arbitrary plain literal 238445");
        Literal fooLabel = sail.getValueFactory().createLiteral("foo", XMLSchema.STRING);
        int before, after;
        // Add statement to the implicit null context.
        sc.removeStatements(null, uriA, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, null, false);
        sc.addStatement(uriB, uriA, uriC);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, null, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add plain literal statement to the default context.
        sc.removeStatements(null, uriA, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, null, false);
        sc.addStatement(uriA, uriA, plainLitA);
        sc.addStatement(uriA, uriB, plainLitA);
        sc.addStatement(uriB, uriB, plainLitA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, null, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add string-typed literal statement to the default context.
        sc.removeStatements(null, firstName, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, firstName, null, false);
        sc.addStatement(foo, firstName, fooLabel);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, firstName, null, false);
        assertEquals(0, before);
        assertEquals(1, after);
        assertEquals(foo, toSet(sc.getStatements(null, firstName, null, false)).iterator().next().getSubject());
        // Add statement to a non-null context.
        sc.removeStatements(null, uriA, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, null, false);
        sc.addStatement(uriB, uriA, uriC, uriA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, null, false);
        assertEquals(0, before);
        assertEquals(1, after);
        sc.removeStatements(null, uriA, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, null, false);
        sc.addStatement(uriB, uriA, uriC, uriC);
        sc.addStatement(uriC, uriA, uriA, uriA);
        sc.commit();
        sc.begin();
        sc.addStatement(uriA, uriA, uriB, uriB);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, null, false);
        assertEquals(0, before);
        assertEquals(3, after);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 40 with SailConnection

use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.

the class SailTest method testGetStatementsSPO_G.

@Test
public void testGetStatementsSPO_G() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/test/S_POG#a");
        URI uriB = sail.getValueFactory().createURI("http://example.org/test/S_POG#b");
        URI uriC = sail.getValueFactory().createURI("http://example.org/test/S_POG#c");
        URI uriD = sail.getValueFactory().createURI("http://example.org/test/S_POG#d");
        int before, after;
        // default context, different S,P,O
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, uriA, uriB, uriC, false);
        sc.addStatement(uriA, uriB, uriC);
        sc.commit();
        sc.begin();
        after = countStatements(sc, uriA, uriB, uriC, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // default context, same S,P,O
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        before = countStatements(sc, uriA, uriB, uriC, false);
        sc.addStatement(uriA, uriB, uriC);
        sc.commit();
        sc.begin();
        after = countStatements(sc, uriA, uriB, uriC, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // one specific context, different S,P,O
        sc.removeStatements(uriA, null, null, uriD);
        sc.commit();
        sc.begin();
        before = countStatements(sc, uriA, uriB, uriC, false, uriD);
        sc.addStatement(uriA, uriB, uriC, uriD);
        sc.commit();
        sc.begin();
        after = countStatements(sc, uriA, uriB, uriC, false, uriD);
        assertEquals(0, before);
        assertEquals(1, after);
        // one specific context, same S,P,O,G
        sc.removeStatements(uriA, null, null, uriA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, uriA, uriB, uriC, false, uriA);
        sc.addStatement(uriA, uriB, uriC, uriA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, uriA, uriB, uriC, false, uriA);
        assertEquals(0, before);
        assertEquals(1, after);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

SailConnection (org.openrdf.sail.SailConnection)55 Test (org.junit.Test)34 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)28 URI (org.openrdf.model.URI)25 SailException (org.openrdf.sail.SailException)14 Sail (org.openrdf.sail.Sail)10 ValueFactory (org.openrdf.model.ValueFactory)9 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)8 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)5 RyaClient (org.apache.rya.api.client.RyaClient)5 Literal (org.openrdf.model.Literal)5 Resource (org.openrdf.model.Resource)5 Statement (org.openrdf.model.Statement)5 ParsedQuery (org.openrdf.query.parser.ParsedQuery)5 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)5 URIImpl (org.openrdf.model.impl.URIImpl)4 BindingSet (org.openrdf.query.BindingSet)4 Vertex (com.tinkerpop.blueprints.Vertex)3 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3