Search in sources :

Example 16 with OWLOntologyManager

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

the class JenaToOwlConvert method ModelJenaToOwlConvert.

// //////////////////////////////////////////////////////////////////////////////
// ////////////////////////////FUNCTIONS/////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////
/**
 * 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(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();
            OWLOntology owlmodel = owlmanager.loadOntologyFromOntologyDocument(new ByteArrayInputStream(out.toByteArray()));
            availablemain = true;
            notifyAll();
            return owlmodel;
        }
    } 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 17 with OWLOntologyManager

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

the class JenaToOwlConvert method AnnotationPropOwlToJena.

// //////////////////////////////////////////////////////////////////////////////
/**
 * This function converts a single OWLAnnotationProperty of OWL to an AnnotationProperty of Jena
 *
 * @param data
 *            {An OWLAnnotationProperty object}
 * @param format
 *            {RDF/XML or TURTLE}
 * @return {An AnnotationProperty object}
 */
public synchronized AnnotationProperty AnnotationPropOwlToJena(OWLAnnotationProperty data, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("AnnotationPropOwlToJena::: " + e);
        }
    }
    available = false;
    try {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology(IRI.create("http://www.semanticweb.org/owlapi/ontologies/ontology"));
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(data);
        manager.addAxiom(ontology, declarationAxiom);
        OntModel jenamodel = ModelOwlToJenaConvert(ontology, format);
        available = true;
        notifyAll();
        return jenamodel.getAnnotationProperty(data.getIRI().toString());
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("AnnotationPropOwlToJena::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 18 with OWLOntologyManager

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

the class OntologyManagerInputProvider method createOWLOntologyManager.

@SuppressWarnings("deprecation")
private OWLOntologyManager createOWLOntologyManager() {
    // We isolate here the creation of the temporary manager
    // TODO How to behave when resolving owl:imports?
    // We should set the manager to use a service to lookup for ontologies,
    // instead of trying on the web directly
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    // FIXME Which is the other way of doing this?
    // Maybe -> OWLOntologyManagerProperties();
    manager.setSilentMissingImportsHandling(true);
    // Listening for missing imports
    manager.addMissingImportListener(new MissingImportListener() {

        @Override
        public void importMissing(MissingImportEvent arg0) {
            log.warn("Missing import {} ", arg0.getImportedOntologyURI());
        }
    });
    manager.addOntologyLoaderListener(new OWLOntologyLoaderListener() {

        @Override
        public void finishedLoadingOntology(LoadingFinishedEvent arg0) {
            log.info("Finished loading {} (imported: {})", arg0.getOntologyID(), arg0.isImported());
        }

        @Override
        public void startedLoadingOntology(LoadingStartedEvent arg0) {
            log.info("Started loading {} (imported: {}) ...", arg0.getOntologyID(), arg0.isImported());
            log.info(" ... from {}", arg0.getDocumentIRI().toString());
        }
    });
    return manager;
}
Also used : MissingImportListener(org.semanticweb.owlapi.model.MissingImportListener) OWLOntologyLoaderListener(org.semanticweb.owlapi.model.OWLOntologyLoaderListener) MissingImportEvent(org.semanticweb.owlapi.model.MissingImportEvent) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 19 with OWLOntologyManager

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

the class ReasoningServiceExecutor method executeOWLApiReasoningService.

/**
 * Executes the OWLApiReasoingService
 *
 * @param task
 * @param s
 * @param input
 * @param rules
 * @param targetGraphID
 * @param parameters
 * @return
 * @throws InconsistentInputException
 * @throws ReasoningServiceException
 * @throws UnsupportedTaskException
 */
private ReasoningServiceResult<OWLOntology> executeOWLApiReasoningService(String task, OWLApiReasoningService s, OWLOntology input, List<SWRLRule> rules, String targetGraphID, boolean filtered, Map<String, List<String>> parameters) throws InconsistentInputException, ReasoningServiceException, UnsupportedTaskException {
    // Check task: this is managed directly by the endpoint
    if (task.equals(ReasoningServiceExecutor.TASK_CHECK)) {
        log.debug("Task is '{}'", ReasoningServiceExecutor.TASK_CHECK);
        try {
            boolean is = s.isConsistent(input);
            return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, is);
        } catch (ReasoningServiceException e) {
            throw e;
        }
    }
    // We get the manager from the input ontology
    // XXX We must be aware of this.
    OWLOntologyManager manager = input.getOWLOntologyManager();
    try {
        OWLOntology output = manager.createOntology();
        Set<OWLAxiom> axioms = s.runTask(task, input, rules, filtered, parameters);
        log.debug("Prepare output: {} axioms", axioms.size());
        manager.addAxioms(output, axioms);
        if (targetGraphID == null) {
            return new ReasoningServiceResult<OWLOntology>(task, true, manager.getOntology(output.getOntologyID()));
        } else {
            save(output, targetGraphID);
            return new ReasoningServiceResult<OWLOntology>(task, true);
        }
    } catch (InconsistentInputException e) {
        log.warn("The input is not consistent");
        return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, false);
    } catch (ReasoningServiceException e) {
        throw e;
    } catch (OWLOntologyCreationException e) {
        log.error("Error! \n", e);
        throw new ReasoningServiceException(new IOException(e));
    } catch (UnsupportedTaskException e) {
        log.error("Error! \n", e);
        throw e;
    } catch (Throwable t) {
        log.error("Error! \n", t);
        throw new ReasoningServiceException(t);
    }
}
Also used : InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) IOException(java.io.IOException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 20 with OWLOntologyManager

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

the class SessionManagerResource method listSessions.

@GET
@Produces(value = { RDF_XML, OWL_XML, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON, N3, N_TRIPLE, TEXT_PLAIN })
public Response listSessions(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    OWLOntologyManager ontMgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = ontMgr.getOWLDataFactory();
    OWLClass cSession = df.getOWLClass(IRI.create("http://stanbol.apache.org/ontologies/meta/Session"));
    OWLOntology o;
    try {
        o = ontMgr.createOntology(IRI.create(uriInfo.getRequestUri()));
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        for (String id : sessionManager.getRegisteredSessionIDs()) {
            IRI sessionid = IRI.create(sessionManager.getDefaultNamespace() + sessionManager.getID() + "/" + id);
            OWLNamedIndividual ind = df.getOWLNamedIndividual(sessionid);
            changes.add(new AddAxiom(o, df.getOWLClassAssertionAxiom(cSession, ind)));
        }
        ontMgr.applyChanges(changes);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok(o);
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) MediaType(javax.ws.rs.core.MediaType) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)82 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)52 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)41 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)23 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)21 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)17 HashSet (java.util.HashSet)13 IRI (org.semanticweb.owlapi.model.IRI)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