Search in sources :

Example 1 with BNode

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;
}
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 BNode

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);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueComposite(org.qi4j.api.value.ValueComposite) Composite(org.qi4j.api.composite.Composite) ValueType(org.qi4j.api.type.ValueType) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 3 with BNode

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);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) EntityReference(org.qi4j.api.entity.EntityReference) ValueFactory(org.openrdf.model.ValueFactory) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) URI(org.openrdf.model.URI)

Example 4 with BNode

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());
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) BNode(org.openrdf.model.BNode) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Model(org.openrdf.model.Model) URIImpl(org.openrdf.model.impl.URIImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) URI(org.openrdf.model.URI) BNodeImpl(org.openrdf.model.impl.BNodeImpl) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 5 with BNode

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);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value)

Aggregations

BNode (org.openrdf.model.BNode)9 URI (org.openrdf.model.URI)6 Test (org.junit.Test)3 Model (org.openrdf.model.Model)3 Statement (org.openrdf.model.Statement)3 ValueFactory (org.openrdf.model.ValueFactory)3 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)2 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)2 Value (org.openrdf.model.Value)2 URIImpl (org.openrdf.model.impl.URIImpl)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 SchemaField (org.apache.solr.schema.SchemaField)1 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)1 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)1 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)1 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)1 SesameYard (org.apache.stanbol.entityhub.yard.sesame.SesameYard)1 JSONArray (org.json.JSONArray)1