Search in sources :

Example 71 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.

the class JenaToOwlConvert method ModelJenaToOwlConvert.

/**
 * This function converts an ontology object from Jena to OWLapi
 *
 * @param jenamodel
 *            {An OntModel object}
 * @param format
 *            {only in "RDF/XML"}
 * @return {An OWLOntology that is an owl object}
 */
public synchronized OWLOntology ModelJenaToOwlConvert(IRI ontologyIRI, Model jenamodel, String format) {
    while (availablemain == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ModelJenaToOwlConvert::: " + e);
        }
    }
    availablemain = false;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (!format.equals("RDF/XML")) {
            System.err.println("The only format supported is RDF/XML. Please check the format!");
            availablemain = true;
            notifyAll();
            return null;
        } else {
            jenamodel.write(out, format);
            OWLOntologyManager owlmanager = OWLManager.createOWLOntologyManager();
            owlmanager.loadOntologyFromOntologyDocument(new ByteArrayInputStream(out.toByteArray()));
            OWLOntologyMerger merger = new OWLOntologyMerger(owlmanager);
            OWLOntology ontology = merger.createMergedOntology(owlmanager, ontologyIRI);
            availablemain = true;
            notifyAll();
            return ontology;
        }
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("ModelJenaToOwlConvert::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 72 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.

the class JenaToOwlConvert method ModelOwlToJenaConvert.

// //////////////////////////////////////////////////////////////////////////////
/**
 * This function converts an ontology object from OWLapi to Jena
 *
 * @param owlmodel
 *            {An OWLOntology object}
 * @param format
 *            {RDF/XML or TURTLE}
 * @return {An OntModel that is a Jena object}
 */
public synchronized OntModel ModelOwlToJenaConvert(OWLOntology owlmodel, String format) {
    while (availablemain == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ModelOwlToJenaConvert::: " + e);
        }
    }
    availablemain = false;
    OWLOntologyID id;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OWLOntologyManager owlmanager = owlmodel.getOWLOntologyManager();
        format = format.trim();
        if (format.equals("TURTLE") || format.equals("RDF/XML")) {
            if (format.equals("TURTLE"))
                owlmanager.setOntologyFormat(owlmodel, new TurtleOntologyFormat());
            if (format.equals("RDF/XML"))
                owlmanager.setOntologyFormat(owlmodel, new RDFXMLOntologyFormat());
            OWLOntologyFormat owlformat = owlmanager.getOntologyFormat(owlmodel);
            owlmanager.saveOntology(owlmodel, owlformat, out);
            OntModel jenamodel = ModelFactory.createOntologyModel();
            id = owlmodel.getOntologyID();
            jenamodel.read(new ByteArrayInputStream(out.toByteArray()), id.toString().replace("<", "").replace(">", ""), format);
            availablemain = true;
            notifyAll();
            return jenamodel;
        } else {
            System.err.println("The only format supported is RDF/XML or TURTLE. Please check the format!");
            availablemain = true;
            notifyAll();
            return null;
        }
    } catch (OWLOntologyStorageException eos) {
        System.err.print("ModelOwlToJenaConvert::: ");
        eos.printStackTrace();
        return null;
    }
}
Also used : TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) OWLOntologyFormat(org.semanticweb.owlapi.model.OWLOntologyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntModel(com.hp.hpl.jena.ontology.OntModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 73 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager 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)

Example 74 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.

the class OWLAPIToClerezzaConverter method owlOntologyToClerezzaGraph.

/**
 * Converts a OWL API {@link OWLOntology} to Clerezza {@link Graph}.
 *
 * @param ontology
 *            {@link OWLOntology}
 * @return the equivalent Clerezza {@link Graph}.
 */
public static org.apache.clerezza.commons.rdf.Graph owlOntologyToClerezzaGraph(OWLOntology ontology) {
    org.apache.clerezza.commons.rdf.Graph mGraph = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OWLOntologyManager manager = ontology.getOWLOntologyManager();
    try {
        manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        ParsingProvider parser = new JenaParserProvider();
        mGraph = new SimpleGraph();
        parser.parse(mGraph, in, SupportedFormat.RDF_XML, null);
    } catch (OWLOntologyStorageException e) {
        log.error("Failed to serialize OWL Ontology " + ontology + "for conversion", e);
    }
    return mGraph;
}
Also used : JenaParserProvider(org.apache.clerezza.rdf.jena.parser.JenaParserProvider) ParsingProvider(org.apache.clerezza.rdf.core.serializedform.ParsingProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 75 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.

the class OWLAPIToClerezzaConverter method clerezzaGraphToOWLOntology.

/**
 * Converts a Clerezza {@link Graph} to an OWL API {@link OWLOntology}.
 *
 * @param mGraph
 *            {@link org.apache.clerezza.commons.rdf.Graph}
 * @return the equivalent OWL API {@link OWLOntology}.
 */
public static OWLOntology clerezzaGraphToOWLOntology(org.apache.clerezza.commons.rdf.Graph graph) {
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    // Never try to import
    mgr.addIRIMapper(new PhonyIRIMapper(Collections.<IRI>emptySet()));
    return clerezzaGraphToOWLOntology(graph, mgr);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) PhonyIRIMapper(org.apache.stanbol.commons.owl.PhonyIRIMapper) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Aggregations

OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)83 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)52 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)42 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)23 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)21 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)17 IRI (org.semanticweb.owlapi.model.IRI)14 HashSet (java.util.HashSet)13 AddImport (org.semanticweb.owlapi.model.AddImport)12 OWLClass (org.semanticweb.owlapi.model.OWLClass)12 Test (org.junit.Test)11 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)10 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)10 OntModel (com.hp.hpl.jena.ontology.OntModel)9 InputStream (java.io.InputStream)9 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)9 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)9 ArrayList (java.util.ArrayList)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 OWLOntologyChange (org.semanticweb.owlapi.model.OWLOntologyChange)8