Examples of SailConnection


Examples of org.openrdf.sail.SailConnection

    }

    protected void addFile(final InputStream in,
                           final RDFFormat format) throws Exception {
        try {
            SailConnection sc = sail.getConnection();
            sc.begin();
            try {
                RDFHandler h = new SailAdder(sc);
                RDFParser p = Rio.createParser(format);
                p.setRDFHandler(h);
                p.parse(in, "http://example.org/bogusBaseURI/");
                sc.commit();
            } finally {
                sc.rollback();
                sc.close();
            }
        } finally {
            in.close();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    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();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    public void testDuplicateStatements() throws Exception {
        if (uniqueStatements) {
            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(0, countStatements(sc, uriA, uriB, uriC, false));
                sc.addStatement(uriA, uriB, uriC);
                assertEquals(1, countStatements(sc, uriA, uriB, uriC, false));
                sc.addStatement(uriA, uriB, uriC);
                assertEquals(1, countStatements(sc, uriA, uriB, uriC, false));

                sc.addStatement(uriA, uriB, uriC, uriC);
                assertEquals(2, countStatements(sc, uriA, uriB, uriC, false));
                assertEquals(1, countStatements(sc, uriA, uriB, uriC, false, uriC));
                sc.commit();
            } finally {
                sc.rollback();
                sc.close();
            }
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    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();
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    @Test
    public void testBlankNodes() throws Throwable {
        URI uriA = sail.getValueFactory().createURI("http://example.org/test/S_POG#a");
        URI uriB = sail.getValueFactory().createURI("http://example.org/test/S_POG#b");
        SailConnection sc = sail.getConnection();
        try {
            sc.begin();
            ValueFactory factory = sail.getValueFactory();
            BNode bNode = factory.createBNode();
            try {
                sc.addStatement(uriA, uriA, bNode);
            } catch (SailException se) {
                // FIXME: not supporting blank nodes ATM
                assertTrue(se.getCause() instanceof UnsupportedOperationException);
            }
            sc.commit();
        } finally {
            sc.rollback();
            sc.close();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    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();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    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();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

            };
            ((NotifyingSail) sail).addSailChangedListener(listener);
            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();
                sc.commit();
                sc.begin();
                events.clear();
                assertEquals(0, events.size());
                sc.addStatement(uriA, uriB, uriC, uriA);
                sc.addStatement(uriB, uriC, uriA, uriA);
                // Events are buffered until the commit
                assertEquals(0, events.size());
                sc.commit();
                sc.begin();
                // Only one SailChangedEvent per commit
                assertEquals(1, events.size());
                SailChangedEvent event = events.iterator().next();
                assertTrue(event.statementsAdded());
                assertFalse(event.statementsRemoved());
                events.clear();
                assertEquals(0, events.size());
                sc.removeStatements(uriA, uriB, uriC, uriA);
                sc.commit();
                sc.begin();
                assertEquals(1, events.size());
                event = events.iterator().next();
                assertFalse(event.statementsAdded());
                assertTrue(event.statementsRemoved());
                events.clear();
                assertEquals(0, events.size());
                sc.clear();
                sc.commit();
                sc.begin();
                assertEquals(1, events.size());
                event = events.iterator().next();
                assertFalse(event.statementsAdded());
                assertTrue(event.statementsRemoved());
            } finally {
                sc.rollback();
                sc.close();
            }
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

    // namespaces //////////////////////////////////////////////////////////////

    @Test
    public void testClearNamespaces() throws Exception {
        SailConnection sc = sail.getConnection();
        try {
            CloseableIteration<? extends Namespace, SailException> namespaces;
            int count;
            count = 0;
            namespaces = sc.getNamespaces();
            while (namespaces.hasNext()) {
                namespaces.next();
                count++;
            }
            namespaces.close();
            assertTrue(count > 0);
            // TODO: actually clear namespaces (but this wipes them out for
            // subsequent tests)
        } finally {
            sc.rollback();
            sc.close();
        }
    }
View Full Code Here

Examples of org.openrdf.sail.SailConnection

        }
    }

    @Test
    public void testGetNamespace() throws Exception {
        SailConnection sc = sail.getConnection();
        try {
            // FIXME: temporary
            //sc.setNamespace("foo", "http://example.org/foo/");
            //showNamespaces(sc);

            String name;
            name = sc.getNamespace("bogus");
            assertNull(name);
            // assertEquals(name, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            name = sc.getNamespace("rdfs");
            //sc.commit();
            assertEquals(name, "http://www.w3.org/2000/01/rdf-schema#");
        } finally {
            sc.rollback();
            sc.close();
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.