Search in sources :

Example 76 with URI

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

the class Database method createTypeRecord.

private TypeRecord createTypeRecord(URI type, SailConnection sc) {
    String id = getTypeId(type, sc);
    String label = SailUtilities.getStringObject(sc, type, RDFS.LABEL, id);
    Properties properties = new Properties();
    CloseableIteration<? extends Statement, SailException> i = null;
    try {
        i = sc.getStatements(type, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get type record", e);
        return null;
    }
    if (i != null) {
        try {
            while (i.hasNext()) {
                Statement s = i.next();
                URI p = s.getPredicate();
                Value o = s.getObject();
                if (!p.equals(RDFS.LABEL) && !p.equals(ExhibitOntology.ID)) {
                    properties.put(p.getLocalName(), SailUtilities.valueToString(o));
                }
            }
        } catch (SailException e) {
            _logger.warn("Failed to iterate through statements", e);
        } finally {
            try {
                i.close();
            } catch (SailException e) {
                _logger.warn("Failed to close statement iterator", e);
            }
        }
    }
    return new TypeRecord(type, id, label, properties);
}
Also used : Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) SailException(org.openrdf.sail.SailException) Properties(java.util.Properties) URI(org.openrdf.model.URI)

Example 77 with URI

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

the class Database method abbreviateItems.

protected synchronized void abbreviateItems() {
    if (_abbreviatedItems) {
        return;
    }
    _abbreviatedItems = true;
    getRepository();
    SailConnection sc = null;
    try {
        sc = _sail.getConnection();
    } catch (SailException e) {
        _logger.error("Failed to open sail connection in order to compute cached information", e);
    }
    if (sc != null) {
        try {
            CloseableIteration<? extends Statement, SailException> i;
            try {
                i = sc.getStatements(null, RDF.TYPE, null, true);
            } catch (SailException e) {
                _logger.error("Failed to get all statements in order to abbreviate items", e);
                return;
            }
            try {
                while (i.hasNext()) {
                    Statement s = i.next();
                    Resource r = s.getSubject();
                    if (r instanceof URI) {
                        getItemId((URI) r, sc);
                    }
                }
            } catch (SailException e) {
                _logger.warn("Failed to iterate through statements", e);
            } finally {
                try {
                    i.close();
                } catch (SailException e) {
                    _logger.warn("Failed to close statement iterator", e);
                }
            }
        } finally {
            try {
                sc.close();
            } catch (SailException e) {
                _logger.warn("Failed to close sail connection", e);
            }
        }
    }
}
Also used : SailConnection(org.openrdf.sail.SailConnection) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI)

Example 78 with URI

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

the class Database method internalBuildTypeRecords.

private void internalBuildTypeRecords(SailConnection sc) {
    _typeRecords = new LinkedList<TypeRecord>();
    _typeIDToRecord = new HashMap<String, TypeRecord>();
    getRepository();
    CloseableIteration<? extends Statement, SailException> i;
    try {
        i = sc.getStatements(null, RDF.TYPE, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get type records", e);
        return;
    }
    Set<URI> types = new HashSet<URI>();
    try {
        while (i.hasNext()) {
            Statement s = i.next();
            types.add((URI) s.getObject());
        }
    } catch (SailException e) {
        _logger.warn("Failed to iterate through statements", e);
    } finally {
        try {
            i.close();
        } catch (SailException e) {
            _logger.warn("Failed to close statement iterator", e);
        }
    }
    for (URI type : types) {
        TypeRecord r = createTypeRecord(type, sc);
        if (r != null) {
            _typeRecords.add(r);
            _typeIDToRecord.put(r.id, r);
        }
    }
}
Also used : Statement(org.openrdf.model.Statement) SailException(org.openrdf.sail.SailException) URI(org.openrdf.model.URI) HashSet(java.util.HashSet)

Example 79 with URI

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

the class EntityStateSerializer method serialize.

public void serialize(final EntityState entityState, final boolean includeNonQueryable, final Graph graph) {
    ValueFactory values = graph.getValueFactory();
    EntityReference identity = entityState.identity();
    URI entityUri = createEntityURI(values, identity);
    graph.add(entityUri, Rdfs.TYPE, values.createURI(Classes.toURI(first(entityState.entityDescriptor().types()))));
    serializeProperties(entityState, graph, entityUri, entityState.entityDescriptor(), includeNonQueryable);
    serializeAssociations(entityState, graph, entityUri, entityState.entityDescriptor().state().associations(), includeNonQueryable);
    serializeManyAssociations(entityState, graph, entityUri, entityState.entityDescriptor().state().manyAssociations(), includeNonQueryable);
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI)

Example 80 with URI

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

the class EntityStateSerializer method serializeAssociations.

private void serializeAssociations(final EntityState entityState, final Graph graph, URI entityUri, final Iterable<? extends AssociationDescriptor> associations, final boolean includeNonQueryable) {
    ValueFactory values = graph.getValueFactory();
    // Associations
    for (AssociationDescriptor associationType : associations) {
        if (!(includeNonQueryable || associationType.queryable())) {
            // Skip non-queryable
            continue;
        }
        EntityReference associatedId = entityState.associationValueOf(associationType.qualifiedName());
        if (associatedId != null) {
            URI assocURI = values.createURI(associationType.qualifiedName().toURI());
            URI assocEntityURI = values.createURI(associatedId.toURI());
            graph.add(entityUri, assocURI, assocEntityURI);
        }
    }
}
Also used : EntityReference(org.qi4j.api.entity.EntityReference) ValueFactory(org.openrdf.model.ValueFactory) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) URI(org.openrdf.model.URI)

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