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;
}
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();
}
}
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);
}
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);
}
}
}
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;
}
Aggregations