Search in sources :

Example 11 with Statement

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

the class EntitySerializerTest method testEntitySerializer.

@Test
public void testEntitySerializer() throws RDFHandlerException {
    EntityReference entityReference = new EntityReference("test2");
    EntityState entityState = entityStore.newUnitOfWork(UsecaseBuilder.newUsecase("Test"), module, System.currentTimeMillis()).entityStateOf(entityReference);
    Iterable<Statement> graph = serializer.serialize(entityState);
    String[] prefixes = new String[] { "rdf", "dc", " vc" };
    String[] namespaces = new String[] { Rdfs.RDF, DcRdf.NAMESPACE, "http://www.w3.org/2001/vcard-rdf/3.0#" };
    new RdfXmlSerializer().serialize(graph, new PrintWriter(System.out), prefixes, namespaces);
}
Also used : Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) EntityReference(org.qi4j.api.entity.EntityReference) EntityState(org.qi4j.spi.entity.EntityState) PrintWriter(java.io.PrintWriter) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 12 with Statement

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

the class EntityTypeSerializerTest method testEntityTypeSerializer.

@Test
public void testEntityTypeSerializer() throws RDFHandlerException {
    EntityDescriptor entityDescriptor = module.entityDescriptor(TestEntity.class.getName());
    Iterable<Statement> graph = serializer.serialize(entityDescriptor);
    String[] prefixes = new String[] { "rdf", "dc", " vc", "qi4j" };
    String[] namespaces = new String[] { Rdfs.RDF, DcRdf.NAMESPACE, "http://www.w3.org/2001/vcard-rdf/3.0#", Qi4jEntityType.NAMESPACE };
    new RdfXmlSerializer().serialize(graph, new PrintWriter(System.out), prefixes, namespaces);
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) PrintWriter(java.io.PrintWriter) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 13 with Statement

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

the class EntityResource method representRdfXml.

private Representation representRdfXml(final EntityState entity) throws ResourceException {
    Representation representation = new WriterRepresentation(MediaType.APPLICATION_RDF_XML) {

        @Override
        public void write(Writer writer) throws IOException {
            try {
                Iterable<Statement> statements = entitySerializer.serialize(entity);
                new RdfXmlSerializer().serialize(statements, writer);
            } catch (RDFHandlerException e) {
                throw new IOException(e);
            }
        }
    };
    representation.setCharacterSet(CharacterSet.UTF_8);
    return representation;
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) WriterRepresentation(org.restlet.representation.WriterRepresentation) Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) Representation(org.restlet.representation.Representation) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer)

Example 14 with Statement

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

the class Database method internalBuildPropertyRecords.

private void internalBuildPropertyRecords(SailConnection sc) {
    _propertyRecords = new LinkedList<PropertyRecord>();
    _propertyIDToRecord = new HashMap<String, PropertyRecord>();
    CloseableIteration<? extends Statement, SailException> i;
    try {
        i = sc.getStatements(null, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get property records", e);
        return;
    }
    Set<URI> predicates = new HashSet<URI>();
    try {
        while (i.hasNext()) {
            Statement s = i.next();
            predicates.add(s.getPredicate());
        }
    } 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 predicate : predicates) {
        PropertyRecord r = createPropertyRecord(predicate, sc);
        if (r != null) {
            _propertyRecords.add(r);
            _propertyIDToRecord.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 15 with Statement

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

the class Database method createPropertyRecord.

private PropertyRecord createPropertyRecord(URI predicate, SailConnection sc) {
    String id = getPropertyId(predicate, sc);
    String label = SailUtilities.getStringObject(sc, predicate, RDFS.LABEL, id);
    String valueType = SailUtilities.getStringObject(sc, predicate, ExhibitOntology.VALUE_TYPE, "text");
    Properties properties = new Properties();
    CloseableIteration<? extends Statement, SailException> i = null;
    try {
        i = sc.getStatements(predicate, null, null, true);
    } catch (SailException e) {
        _logger.error("Failed to get all statements in order to get property 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) && !p.equals(ExhibitOntology.VALUE_TYPE)) {
                    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 PropertyRecord(predicate, id, label, valueType, 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)

Aggregations

Statement (org.openrdf.model.Statement)67 Resource (org.openrdf.model.Resource)22 Value (org.openrdf.model.Value)22 URI (org.openrdf.model.URI)21 HashSet (java.util.HashSet)16 SailException (org.openrdf.sail.SailException)10 Collection (java.util.Collection)9 Literal (org.openrdf.model.Literal)9 Graph (org.openrdf.model.Graph)7 Edge (com.tinkerpop.blueprints.Edge)6 Test (org.junit.Test)6 Vertex (com.tinkerpop.blueprints.Vertex)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 PrintWriter (java.io.PrintWriter)3 Set (java.util.Set)3 BNode (org.openrdf.model.BNode)3 ValueFactory (org.openrdf.model.ValueFactory)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3 RdfXmlSerializer (org.qi4j.library.rdf.serializer.RdfXmlSerializer)3