Search in sources :

Example 1 with Literal

use of org.openrdf.model.Literal in project qi4j-sdk by Qi4j.

the class SolrEntityIndexerMixin method indexEntityState.

private SolrInputDocument indexEntityState(final EntityState entityState, final SolrServer server) throws IOException, SolrServerException, JSONException {
    Graph graph = new GraphImpl();
    stateSerializer.serialize(entityState, false, graph);
    SolrInputDocument input = new SolrInputDocument();
    input.addField("id", entityState.identity().identity());
    input.addField("type", first(entityState.entityDescriptor().types()).getName());
    input.addField("lastModified", new Date(entityState.lastModified()));
    for (Statement statement : graph) {
        SchemaField field = indexedFields.get(statement.getPredicate().getLocalName());
        if (field != null) {
            if (statement.getObject() instanceof Literal) {
                String value = statement.getObject().stringValue();
                if (field.getType().getTypeName().equals("json")) {
                    if (value.charAt(0) == '[') {
                        JSONArray array = new JSONArray(value);
                        indexJson(input, array);
                    } else if (value.charAt(0) == '{') {
                        JSONObject object = new JSONObject(value);
                        indexJson(input, object);
                    }
                } else {
                    input.addField(field.getName(), value);
                }
            } else if (statement.getObject() instanceof URI && !"type".equals(field.getName())) {
                String value = statement.getObject().stringValue();
                value = value.substring(value.lastIndexOf(':') + 1, value.length());
                String name = field.getName();
                input.addField(name, value);
            } else if (statement.getObject() instanceof BNode) {
                Iterator<Statement> seq = graph.match((Resource) statement.getObject(), new URIImpl("http://www.w3.org/1999/02/22-rdf-syntax-ns#li"), null, (Resource) null);
                while (seq.hasNext()) {
                    Statement seqStatement = seq.next();
                    String value = seqStatement.getObject().stringValue();
                    value = value.substring(value.lastIndexOf(':') + 1, value.length());
                    input.addField(field.getName(), value);
                }
            }
        }
    }
    return input;
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) JSONArray(org.json.JSONArray) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) Date(java.util.Date) SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Graph(org.openrdf.model.Graph) JSONObject(org.json.JSONObject) GraphImpl(org.openrdf.model.impl.GraphImpl) Literal(org.openrdf.model.Literal)

Example 2 with Literal

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

the class SailTest method testGetStatementsO_SPG.

@Test
public void testGetStatementsO_SPG() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/test/O_SPG#a");
        URI uriB = sail.getValueFactory().createURI("http://example.org/test/O_SPG#b");
        URI uriC = sail.getValueFactory().createURI("http://example.org/test/O_SPG#c");
        Literal plainLitA = sail.getValueFactory().createLiteral("arbitrary plain literal 9548734867");
        Literal stringLitA = sail.getValueFactory().createLiteral("arbitrary string literal 8765", XMLSchema.STRING);
        int before, after;
        // Add statement to a specific context.
        sc.removeStatements(null, null, uriA, uriA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, null, uriA, false);
        sc.addStatement(uriB, uriC, uriA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, null, uriA, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add plain literal statement to the default context.
        sc.removeStatements(null, null, plainLitA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, null, plainLitA, false);
        sc.addStatement(uriA, uriA, plainLitA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, null, plainLitA, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add string-typed literal statement to the default context.
        sc.removeStatements(null, null, plainLitA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, null, stringLitA, false);
        sc.addStatement(uriA, uriA, stringLitA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, null, stringLitA, false);
        assertEquals(0, before);
        assertEquals(1, 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 3 with Literal

use of org.openrdf.model.Literal 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 4 with Literal

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

the class SailVertex method setProperty.

public void setProperty(final String key, final Object value) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        boolean update = false;
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), new URIImpl(this.graph.expandPrefix(value.toString())));
            update = true;
        } else if (key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), value.toString());
            update = true;
        }
        if (update) {
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
    }
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource) URIImpl(org.openrdf.model.impl.URIImpl)

Example 5 with Literal

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

the class SailVertex method removeProperty.

public <T> T removeProperty(final String key) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE) || key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel());
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
        if (key.equals(SailTokens.DATATYPE)) {
            return (T) oldLiteral.getDatatype().toString();
        } else if (key.equals(SailTokens.LANGUAGE)) {
            return (T) oldLiteral.getLanguage();
        }
    }
    return null;
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource)

Aggregations

Literal (org.openrdf.model.Literal)23 URI (org.openrdf.model.URI)12 Test (org.junit.Test)10 Statement (org.openrdf.model.Statement)5 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)5 SailConnection (org.openrdf.sail.SailConnection)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)3 ValueFactory (org.openrdf.model.ValueFactory)3 BindingSet (org.openrdf.query.BindingSet)3 Edge (com.tinkerpop.blueprints.Edge)2 Vertex (com.tinkerpop.blueprints.Vertex)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)2 Resource (org.openrdf.model.Resource)2 Value (org.openrdf.model.Value)2 LiteralImpl (org.openrdf.model.impl.LiteralImpl)2