Search in sources :

Example 11 with OWLOntologyStorageException

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

the class ResponseTaskBuilder method stream.

/**
     * This supports OWLOntology and jena Model objects.
     * In the case of Jena the reuslt is printed as Turtle, 
     * in case of OWLApi the result is in Manchester syntax (more readable).
     * 
     * FIXME: Both should return the same format
     * 
     * @param object
     * @return
     */
private OutputStream stream(Object object) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    if (object instanceof OWLOntology) {
        OWLOntology o = (OWLOntology) object;
        ManchesterOWLSyntaxOntologyStorer mosos = new ManchesterOWLSyntaxOntologyStorer();
        try {
            mosos.storeOntology(o.getOWLOntologyManager(), o, new StreamDocumentTarget(out), new ManchesterOWLSyntaxOntologyFormat());
        } catch (OWLOntologyStorageException e) {
            log.error("Cannot stream the ontology", e);
            throw new RuntimeException(e);
        }
    } else if (object instanceof Model) {
        Model m = (Model) object;
        // FIXME Both should return the same format
        m.write(out, "TURTLE");
    }
    return out;
}
Also used : ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Model(com.hp.hpl.jena.rdf.model.Model) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamDocumentTarget(org.semanticweb.owlapi.io.StreamDocumentTarget) ManchesterOWLSyntaxOntologyStorer(uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxOntologyStorer) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 12 with OWLOntologyStorageException

use of org.semanticweb.owlapi.model.OWLOntologyStorageException 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 13 with OWLOntologyStorageException

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

the class ODPRegistryCacheManager method getOntologyInputSource.

public static synchronized OWLOntologyDocumentSource getOntologyInputSource(URI uri) throws ODPRegistryCacheException, URIUnresolvableException {
    if (getUnresolvedURIs().contains(uri))
        throw new URIUnresolvableException();
    if (uris.containsKey(uri)) {
        File f = uris.get(uri);
        FileDocumentSource fds = new FileDocumentSource(f);
        return fds;
    } else {
        try {
            retrieveRemoteResource(uri);
            return getOntologyInputSource(uri);
        } catch (UnknownOWLOntologyException e) {
            throw new ODPRegistryCacheException(e);
        } catch (OWLOntologyCreationException e) {
            throw new ODPRegistryCacheException(e);
        } catch (OWLOntologyStorageException e) {
            throw new ODPRegistryCacheException(e);
        }
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) FileDocumentSource(org.semanticweb.owlapi.io.FileDocumentSource) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) File(java.io.File) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 14 with OWLOntologyStorageException

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

the class RuleListWriter method writeTo.

@Override
public void writeTo(RuleList ruleList, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    Logger log = LoggerFactory.getLogger(getClass());
    log.debug("Rendering the list of recipes.");
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    OWLOntology ontology;
    try {
        ontology = manager.createOntology();
        String recipeClassURI = Symbols.Recipe.toString().replace("<", "").replace(">", "");
        IRI recipeClassIRI = IRI.create(recipeClassURI);
        OWLClass owlRecipeClass = factory.getOWLClass(recipeClassIRI);
        String ruleClassURI = Symbols.Rule.toString().replace("<", "").replace(">", "");
        IRI ruleClassIRI = IRI.create(ruleClassURI);
        OWLClass owlRuleClass = factory.getOWLClass(ruleClassIRI);
        String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
        IRI descriptionIRI = IRI.create(descriptionURI);
        OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
        String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
        IRI hasRuleIRI = IRI.create(hasRuleURI);
        OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
        String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
        IRI ruleBodyIRI = IRI.create(ruleBodyURI);
        OWLDataProperty ruleBody = factory.getOWLDataProperty(ruleBodyIRI);
        String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
        IRI ruleHeadIRI = IRI.create(ruleHeadURI);
        OWLDataProperty ruleHead = factory.getOWLDataProperty(ruleHeadIRI);
        if (ruleList != null) {
            for (Rule rule : ruleList) {
                String recipeId = rule.getRecipe().getRecipeID().toString().replace("<", "").replace(">", "");
                IRI reicpeIRI = IRI.create(recipeId);
                OWLIndividual owlRecipe = factory.getOWLNamedIndividual(reicpeIRI);
                String ruleId = rule.getRuleID().toString().replace("<", "").replace(">", "");
                IRI ruleIRI = IRI.create(ruleId);
                OWLIndividual owlRule = factory.getOWLNamedIndividual(ruleIRI);
                OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, owlRecipe);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLClassAssertionAxiom(owlRuleClass, owlRule);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, owlRecipe, owlRule);
                manager.addAxiom(ontology, axiom);
                String recipeDescription = rule.getRecipe().getRecipeDescription();
                String ruleDescription = rule.getDescription();
                if (recipeDescription != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRecipe, recipeDescription);
                    manager.addAxiom(ontology, axiom);
                }
                if (ruleDescription != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRule, ruleDescription);
                    manager.addAxiom(ontology, axiom);
                }
                String ruleContent = rule.toString();
                String[] parts = ruleContent.split("\\->");
                axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBody, owlRule, parts[0]);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHead, owlRule, parts[1]);
                manager.addAxiom(ontology, axiom);
            }
        }
        if (mediaType.toString().equals(KRFormat.RDF_XML)) {
            try {
                manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.OWL_XML)) {
            try {
                manager.saveOntology(ontology, new OWLXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.MANCHESTER_OWL)) {
            try {
                manager.saveOntology(ontology, new ManchesterOWLSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.FUNCTIONAL_OWL)) {
            try {
                manager.saveOntology(ontology, new OWLFunctionalSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.TURTLE)) {
            try {
                manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.RDF_JSON)) {
            Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
            RdfJsonSerializingProvider provider = new RdfJsonSerializingProvider();
            provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
        }
    } catch (OWLOntologyCreationException e1) {
        log.error("An error occurred.", e1);
    }
    out.flush();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) Logger(org.slf4j.Logger) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) Graph(org.apache.clerezza.commons.rdf.Graph) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) OWLXMLOntologyFormat(org.semanticweb.owlapi.io.OWLXMLOntologyFormat) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Rule(org.apache.stanbol.rules.base.api.Rule) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)14 RDFXMLOntologyFormat (org.semanticweb.owlapi.io.RDFXMLOntologyFormat)8 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)8 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)8 ManchesterOWLSyntaxOntologyFormat (org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 TurtleOntologyFormat (org.coode.owlapi.turtle.TurtleOntologyFormat)5 Graph (org.apache.clerezza.commons.rdf.Graph)4 RdfJsonSerializingProvider (org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider)4 OWLFunctionalSyntaxOntologyFormat (org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat)4 OWLXMLOntologyFormat (org.semanticweb.owlapi.io.OWLXMLOntologyFormat)4 IRI (org.semanticweb.owlapi.model.IRI)4 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 Logger (org.slf4j.Logger)4 WebApplicationException (javax.ws.rs.WebApplicationException)3 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)3 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)3 OWLOntologyFormat (org.semanticweb.owlapi.model.OWLOntologyFormat)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2