Search in sources :

Example 1 with SerializingProvider

use of org.apache.clerezza.rdf.core.serializedform.SerializingProvider in project stanbol by apache.

the class JenaToClerezzaConverter method clerezzaGraphToJenaModel.

/**
	 * Converts a Clerezza {@link Graph} to a Jena {@link Model}.
	 * 
	 * @param mGraph {@link Graph}
	 * @return the equivalent Jena {@link Model}.
	 */
public static Model clerezzaGraphToJenaModel(org.apache.clerezza.commons.rdf.Graph mGraph) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SerializingProvider serializingProvider = new JenaSerializerProvider();
    serializingProvider.serialize(out, mGraph, SupportedFormat.RDF_XML);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    Model jenaModel = ModelFactory.createDefaultModel();
    jenaModel.read(in, null);
    return jenaModel;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) Model(com.hp.hpl.jena.rdf.model.Model) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider)

Example 2 with SerializingProvider

use of org.apache.clerezza.rdf.core.serializedform.SerializingProvider in project stanbol by apache.

the class OWLAPIToClerezzaConverter method clerezzaGraphToOWLOntology.

public static OWLOntology clerezzaGraphToOWLOntology(org.apache.clerezza.commons.rdf.Graph graph, OWLOntologyManager ontologyManager) {
    /*
         * The root graph can be serialized and de-serialized, but before that we should decide what to do
         * with imports. We can proceed as follows:
         * 
         * for each import statement, - check if the ontology manager has (1) an ontology or (2) a mapping. -
         * if (1), just get it and add it to the merge pool - if (2), do nothing. the ontology manager should
         * handle it when loading the root, - if neither, fetch the graph and call the procideure on it
         * 
         * Alternatively, construct the whole reverse imports stack, then traverse it again, get the
         * OWLOntology version for each (with the phony mapper set) and add it to the merge pool
         * 
         * If it works, just add all the triples to a Graph, but no, we don't want to store that
         * change.
         */
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SerializingProvider serializingProvider = new JenaSerializerProvider();
    serializingProvider.serialize(out, graph, SupportedFormat.RDF_XML);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    OWLOntology ontology = null;
    try {
        ontology = ontologyManager.loadOntologyFromOntologyDocument(in);
    } catch (OWLOntologyAlreadyExistsException e) {
        ontology = ontologyManager.getOntology(e.getOntologyID());
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to serialize OWL Ontology " + ontology + "for conversion", e);
    }
    return ontology;
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)

Example 3 with SerializingProvider

use of org.apache.clerezza.rdf.core.serializedform.SerializingProvider in project stanbol by apache.

the class OWLOntologyWriter method writeTo.

@Override
public void writeTo(OWLOntology ontology, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    Logger log = LoggerFactory.getLogger(getClass());
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    log.debug("Rendering ontology " + ontology.getOntologyID() + " to knowledge representation format " + mediaType);
    // Native formats first
    if (RDF_XML_TYPE.equals(mediaType) || OWL_XML_TYPE.equals(mediaType) || MANCHESTER_OWL_TYPE.equals(mediaType) || FUNCTIONAL_OWL_TYPE.equals(mediaType) || TURTLE_TYPE.equals(mediaType) || X_TURTLE_TYPE.equals(mediaType)) {
        OWLOntologyFormat format = null;
        if (RDF_XML_TYPE.equals(mediaType))
            format = new RDFXMLOntologyFormat();
        else if (OWL_XML_TYPE.equals(mediaType))
            format = new OWLXMLOntologyFormat();
        else if (MANCHESTER_OWL_TYPE.equals(mediaType))
            format = new ManchesterOWLSyntaxOntologyFormat();
        else if (FUNCTIONAL_OWL_TYPE.equals(mediaType))
            format = new OWLFunctionalSyntaxOntologyFormat();
        else if (TURTLE_TYPE.equals(mediaType) || X_TURTLE_TYPE.equals(mediaType))
            format = new TurtleOntologyFormat();
        if (format != null)
            try {
                manager.saveOntology(ontology, format, out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
                throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
            }
        else
            throw new IOException();
    } else {
        // Non-native formats that require a conversion to Clerezza
        if (RDF_JSON_TYPE.equals(mediaType) || N3_TYPE.equals(mediaType) || TEXT_PLAIN.equals(mediaType.toString()) || N_TRIPLE_TYPE.equals(mediaType)) {
            Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
            SerializingProvider serializer = null;
            if (RDF_JSON_TYPE.equals(mediaType))
                serializer = new RdfJsonSerializingProvider();
            else if (N3_TYPE.equals(mediaType) || N_TRIPLE_TYPE.equals(mediaType) || TEXT_PLAIN.equals(mediaType.toString()))
                serializer = new JenaSerializerProvider();
            // text/plain is interpreted as N3.
            if (serializer != null)
                serializer.serialize(out, mGraph, TEXT_PLAIN.equals(mediaType.toString()) ? N3 : mediaType.toString());
        }
    }
    // JSON_LD not supported until both parser and serializer are stable.
    out.flush();
}
Also used : ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) WebApplicationException(javax.ws.rs.WebApplicationException) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) JenaSerializerProvider(org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) IOException(java.io.IOException) Logger(org.slf4j.Logger) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) SerializingProvider(org.apache.clerezza.rdf.core.serializedform.SerializingProvider) Graph(org.apache.clerezza.commons.rdf.Graph) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) OWLXMLOntologyFormat(org.semanticweb.owlapi.io.OWLXMLOntologyFormat) OWLOntologyFormat(org.semanticweb.owlapi.model.OWLOntologyFormat) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

SerializingProvider (org.apache.clerezza.rdf.core.serializedform.SerializingProvider)3 JenaSerializerProvider (org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Model (com.hp.hpl.jena.rdf.model.Model)1 IOException (java.io.IOException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 RdfJsonSerializingProvider (org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider)1 ManchesterOWLSyntaxOntologyFormat (org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat)1 TurtleOntologyFormat (org.coode.owlapi.turtle.TurtleOntologyFormat)1 OWLFunctionalSyntaxOntologyFormat (org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat)1 OWLXMLOntologyFormat (org.semanticweb.owlapi.io.OWLXMLOntologyFormat)1 RDFXMLOntologyFormat (org.semanticweb.owlapi.io.RDFXMLOntologyFormat)1 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)1 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)1 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)1 OWLOntologyFormat (org.semanticweb.owlapi.model.OWLOntologyFormat)1 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)1 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)1