Search in sources :

Example 31 with SailConnection

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

the class SailTest method testGetLiteralsFromTripleStore.

@Test
public void testGetLiteralsFromTripleStore() throws Exception {
    Literal l;
    String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
    XMLGregorianCalendar calendar;
    ValueFactory vf = sail.getValueFactory();
    SailConnection sc = sail.getConnection();
    // Get an actual plain literal from the triple store.
    URI ford = vf.createURI(prefix + "ford");
    l = (Literal) toSet(sc.getStatements(ford, RDFS.COMMENT, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("he really knows where his towel is", l.getLabel());
    assertNull(l.getDatatype());
    URI thor = vf.createURI(prefix + "thor");
    // Get an actual language-tagged literal from the triple store.
    URI foafName = vf.createURI("http://xmlns.com/foaf/0.1/name");
    Iterator<Statement> iter = toSet(sc.getStatements(thor, foafName, null, false)).iterator();
    boolean found = false;
    while (iter.hasNext()) {
        l = (Literal) iter.next().getObject();
        if (l.getLanguage().equals("en")) {
            found = true;
            assertEquals("Thor", l.getLabel());
            assertNull(l.getDatatype());
        }
    // if (l.getLanguage().equals("is")) {
    // found = true;
    // assertEquals("?�r", l.getLabel());
    // }
    }
    assertTrue(found);
    // Get an actual data-typed literal from the triple-store.
    URI msnChatID = vf.createURI("http://xmlns.com/foaf/0.1/msnChatID");
    l = (Literal) toSet(sc.getStatements(thor, msnChatID, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("Thorster123", l.getLabel());
    assertEquals(XMLSchema.STRING, l.getDatatype());
    // Test Literal.xxxValue() methods for Literals read from the triple
    // store
    URI valueUri, hasValueUri;
    hasValueUri = vf.createURI(prefix + "hasValue");
    valueUri = vf.createURI(prefix + "stringValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("foo", l.getLabel());
    assertEquals(XMLSchema.STRING, l.getDatatype());
    valueUri = vf.createURI(prefix + "byteValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("99", l.getLabel());
    assertEquals(XMLSchema.BYTE, l.getDatatype());
    assertEquals((byte) 'c', l.byteValue());
    valueUri = vf.createURI(prefix + "booleanValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("false", l.getLabel());
    assertEquals(XMLSchema.BOOLEAN, l.getDatatype());
    assertEquals(false, l.booleanValue());
    valueUri = vf.createURI(prefix + "intValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("42", l.getLabel());
    assertEquals(XMLSchema.INT, l.getDatatype());
    assertEquals(42, l.intValue());
    valueUri = vf.createURI(prefix + "shortValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("42", l.getLabel());
    assertEquals(XMLSchema.SHORT, l.getDatatype());
    assertEquals((short) 42, l.shortValue());
    valueUri = vf.createURI(prefix + "longValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("42", l.getLabel());
    assertEquals(XMLSchema.LONG, l.getDatatype());
    assertEquals(42l, l.longValue());
    valueUri = vf.createURI(prefix + "floatValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("3.1415926", l.getLabel());
    assertEquals(XMLSchema.FLOAT, l.getDatatype());
    assertEquals((float) 3.1415926, l.floatValue());
    valueUri = vf.createURI(prefix + "doubleValue");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("3.1415926", l.getLabel());
    assertEquals(XMLSchema.DOUBLE, l.getDatatype());
    assertEquals(3.1415926, l.doubleValue());
    valueUri = vf.createURI(prefix + "dateTimeValue");
    calendar = XMLDatatypeUtil.parseCalendar("2002-10-10T12:00:00-05:00");
    l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
    assertNotNull(l);
    assertNull(l.getLanguage());
    assertEquals("2002-10-10T12:00:00-05:00", l.getLabel());
    assertEquals(XMLSchema.DATETIME, l.getDatatype());
    assertEquals(calendar, l.calendarValue());
    sc.rollback();
    sc.close();
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Statement(org.openrdf.model.Statement) Literal(org.openrdf.model.Literal) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 32 with SailConnection

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

the class SailTest method testSize.

@Test
public void testSize() 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.removeStatements(null, null, null);
        assertEquals(0L, sc.size());
        sc.addStatement(uriA, uriB, uriC, uriA);
        // sc.commit();
        assertEquals(1L, sc.size());
        sc.addStatement(uriA, uriB, uriC, uriB);
        // sc.commit();
        assertEquals(2L, sc.size());
        sc.addStatement(uriB, uriB, uriC, uriB);
        // sc.commit();
        assertEquals(3L, sc.size());
        sc.addStatement(uriC, uriB, uriA);
        // sc.commit();
        assertEquals(4L, sc.size());
        assertEquals(1L, sc.size(uriA));
        assertEquals(2L, sc.size(uriB));
        assertEquals(0L, sc.size(uriC));
        assertEquals(1L, sc.size((URI) null));
        assertEquals(3L, sc.size(uriB, null));
        assertEquals(3L, sc.size(uriB, uriC, null));
        assertEquals(4L, sc.size(uriA, uriB, null));
        assertEquals(4L, sc.size(uriA, uriB, uriC, null));
        assertEquals(3L, sc.size(uriA, uriB));
        sc.commit();
    } 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)

Example 33 with SailConnection

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

the class SailTest method clear.

protected void clear() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        sc.clear();
        sc.commit();
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection)

Example 34 with SailConnection

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

the class SailTest method setUp.

@Before
public final void setUp() throws Exception {
    before();
    this.sail = createSail();
    sail.initialize();
    if (sail instanceof NotifyingSail) {
        SailConnection sc = sail.getConnection();
        try {
            if (sc instanceof InferencerConnection) {
                inferencer = new ForwardChainingRDFSInferencer((NotifyingSail) sail);
            }
        } finally {
            sc.rollback();
            sc.close();
        }
    }
    addFile(SailTest.class.getResourceAsStream("graph-example-sail-test.trig"), RDFFormat.TRIG);
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) InferencerConnection(org.openrdf.sail.inferencer.InferencerConnection) ForwardChainingRDFSInferencer(org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer) Before(org.junit.Before)

Example 35 with SailConnection

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

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