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;
}
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;
}
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();
}
Aggregations