use of org.openrdf.model.BNode 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.BNode in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serializeValueComposite.
private void serializeValueComposite(Resource subject, URI predicate, ValueComposite value, ValueType valueType, Graph graph, String baseUri, boolean includeNonQueryable) {
final ValueFactory valueFactory = graph.getValueFactory();
BNode collection = valueFactory.createBNode();
graph.add(subject, predicate, collection);
for (PropertyDescriptor persistentProperty : ((ValueCompositeType) valueType).properties()) {
Object propertyValue = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((Composite) value).state().propertyFor(persistentProperty.accessor()).get();
if (propertyValue == null) {
// Skip null values
continue;
}
ValueType type = persistentProperty.valueType();
if (type instanceof ValueCompositeType) {
URI pred = valueFactory.createURI(baseUri, persistentProperty.qualifiedName().name());
serializeValueComposite(collection, pred, (ValueComposite) propertyValue, type, graph, baseUri + persistentProperty.qualifiedName().name() + "/", includeNonQueryable);
} else {
serializeProperty(persistentProperty, propertyValue, collection, graph, includeNonQueryable);
}
}
}
use of org.openrdf.model.BNode in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serializeManyAssociations.
private void serializeManyAssociations(final EntityState entityState, final Graph graph, final URI entityUri, final Iterable<? extends AssociationDescriptor> associations, final boolean includeNonQueryable) {
ValueFactory values = graph.getValueFactory();
// Many-Associations
for (AssociationDescriptor associationType : associations) {
if (!(includeNonQueryable || associationType.queryable())) {
// Skip non-queryable
continue;
}
BNode collection = values.createBNode();
graph.add(entityUri, values.createURI(associationType.qualifiedName().toURI()), collection);
graph.add(collection, Rdfs.TYPE, Rdfs.SEQ);
ManyAssociationState associatedIds = entityState.manyAssociationValueOf(associationType.qualifiedName());
for (EntityReference associatedId : associatedIds) {
URI assocEntityURI = values.createURI(associatedId.toURI());
graph.add(collection, Rdfs.LIST_ITEM, assocEntityURI);
}
}
}
use of org.openrdf.model.BNode in project stanbol by apache.
the class RdfRepresentationTest method testBNodeFiltering.
/**
* Test for STANBOL-1301
*/
@Test
public void testBNodeFiltering() {
URI concept = new URIImpl("http://example.org/mySkos#Concept123");
Representation r = createRepresentation(concept.stringValue());
assertTrue(r instanceof RdfRepresentation);
RdfRepresentation rep = (RdfRepresentation) r;
//add the example as listed in STANBOL-1301 to directly to the
//Sesame Model backing the created Representation
Model m = rep.getModel();
m.add(concept, RDF.TYPE, SKOS.CONCEPT);
m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
m.add(concept, SKOS.SCOPE_NOTE, note1);
m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
m.add(concept, SKOS.SCOPE_NOTE, note2);
m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
//now assert that BNodes are not reported via the Representation API
Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNotes.hasNext());
Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
assertFalse(scopeNoteRefs.hasNext());
}
use of org.openrdf.model.BNode in project stanbol by apache.
the class RdfIndexingSource method extractRepresentation.
/**
* Extracts all {@link Statement}s part of the Representation. If
* {@link #followBNodeState} this is called recursively for {@link Statement}s
* where the value is an {@link BNode}.
*/
protected void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
//we need all the outgoing relations and also want to follow bNodes until
//the next UriRef. However we are not interested in incoming relations!
RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
Statement statement;
Set<BNode> bnodes = followBNodeState ? new HashSet<BNode>() : null;
while (outgoing.hasNext()) {
statement = outgoing.next();
model.add(statement);
if (followBNodeState) {
Value object = statement.getObject();
if (object instanceof BNode && !visited.contains(object)) {
bnodes.add((BNode) object);
}
}
//else do not follow values beeing BNodes
}
outgoing.close();
if (followBNodeState) {
for (BNode bnode : bnodes) {
visited.add(bnode);
//TODO: recursive calls could cause stackoverflows with wired graphs
extractRepresentation(con, model, bnode, visited);
}
}
}
Aggregations