Search in sources :

Example 71 with URI

use of org.openrdf.model.URI 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 72 with URI

use of org.openrdf.model.URI 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 73 with URI

use of org.openrdf.model.URI 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)

Example 74 with URI

use of org.openrdf.model.URI in project blueprints by tinkerpop.

the class GraphSailTest method testBlankNodesUnique.

@Test
public void testBlankNodesUnique() throws Exception {
    String ex = "http://example.org/ns#";
    URI class1 = new URIImpl(ex + "Class1");
    clear();
    int edgesBefore, verticesBefore;
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        edgesBefore = countEdges();
        verticesBefore = countVertices();
    } finally {
        sc.rollback();
        sc.close();
    }
    // Load a file once.
    addFile(SailTest.class.getResourceAsStream("graph-example-bnodes.trig"), RDFFormat.TRIG);
    sc = sail.getConnection();
    try {
        assertEquals(3, countStatements(sc, class1, RDFS.SUBCLASSOF, null, false));
        assertEquals(edgesBefore + 14, countEdges());
        assertEquals(verticesBefore + 10, countVertices());
    } finally {
        sc.rollback();
        sc.close();
    }
    // Load the file again.
    // Loading the same file twice results in extra vertices and edges,
    // since blank nodes assume different identities on each load.
    addFile(SailTest.class.getResourceAsStream("graph-example-bnodes.trig"), RDFFormat.TRIG);
    sc = sail.getConnection();
    try {
        assertEquals(5, countStatements(sc, class1, RDFS.SUBCLASSOF, null, false));
        assertEquals(edgesBefore + 23, countEdges());
        assertEquals(verticesBefore + 12, countVertices());
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 75 with URI

use of org.openrdf.model.URI in project backstage by zepheira.

the class DefaultLens method render.

@Override
public void render(URI item, Scriptable result, Database database, SailRepositoryConnection connection) {
    result.put("label", result, database.getItemLabel(database.getItemId(item)));
    try {
        RepositoryResult<Statement> r = connection.getStatements(item, null, null, true, SailUtilities.noContext);
        Map<URI, Set<Value>> propertyToValues = new HashMap<URI, Set<Value>>();
        try {
            while (r.hasNext()) {
                Statement s = r.next();
                URI predicate = s.getPredicate();
                Set<Value> values = propertyToValues.get(predicate);
                if (values == null) {
                    values = new HashSet<Value>();
                    propertyToValues.put(predicate, values);
                }
                values.add(s.getObject());
            }
        } finally {
            r.close();
        }
        DefaultScriptableObject propertiesO = new DefaultScriptableObject();
        for (URI property : propertyToValues.keySet()) {
            String propertyID = database.getPropertyId(property);
            PropertyRecord propertyRecord = database.getPropertyRecord(propertyID);
            DefaultScriptableObject o = new DefaultScriptableObject();
            o.put("propertyLabel", o, propertyRecord.label);
            o.put("valueType", o, propertyRecord.valueType);
            ScriptableArrayBuilder valueArrayBuilder = new ScriptableArrayBuilder();
            Set<Value> values = propertyToValues.get(property);
            if ("item".equals(propertyRecord.valueType)) {
                for (Value value : values) {
                    if (value instanceof URI) {
                        URI itemURI = (URI) value;
                        String itemID = database.getItemId(itemURI);
                        DefaultScriptableObject v = new DefaultScriptableObject();
                        v.put("id", v, itemID);
                        v.put("label", v, database.getItemLabel(itemID));
                        valueArrayBuilder.add(v);
                    } else {
                        valueArrayBuilder.add(((Resource) value).stringValue());
                    }
                }
            } else {
                for (Value value : values) {
                    valueArrayBuilder.add(Utilities.valueToString(value));
                }
            }
            o.put("values", o, valueArrayBuilder.toArray());
            propertiesO.put(propertyID, propertiesO, o);
        }
        result.put("propertyValues", result, propertiesO);
    } catch (Exception e) {
        _logger.error("Failed to generate default lens for item " + item, e);
    }
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Statement(org.openrdf.model.Statement) URI(org.openrdf.model.URI) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) PropertyRecord(edu.mit.simile.backstage.model.data.Database.PropertyRecord) Value(org.openrdf.model.Value)

Aggregations

URI (org.openrdf.model.URI)111 Test (org.junit.Test)28 SailConnection (org.openrdf.sail.SailConnection)25 Value (org.openrdf.model.Value)22 Statement (org.openrdf.model.Statement)21 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)21 Resource (org.openrdf.model.Resource)20 Literal (org.openrdf.model.Literal)14 ValueFactory (org.openrdf.model.ValueFactory)12 RepositoryException (org.openrdf.repository.RepositoryException)11 SailException (org.openrdf.sail.SailException)10 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)8 Vertex (com.tinkerpop.blueprints.Vertex)7 HashSet (java.util.HashSet)7 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)7 Model (org.openrdf.model.Model)7 URIImpl (org.openrdf.model.impl.URIImpl)7 Edge (com.tinkerpop.blueprints.Edge)6 BNode (org.openrdf.model.BNode)6 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)6