Search in sources :

Example 81 with OWLOntologyManager

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

the class AbstractOWLApiReasoningService method createOWLOntologyManager.

/**
 * An OWLOntologyManager to be used to place a target ontology. We don't want to use the manager bound to
 * the input ontologies for that, we don't want to interfere with it.
 *
 * Other {@see OWLApiReasoningService}s may want to change this.
 *
 * @return
 */
protected OWLOntologyManager createOWLOntologyManager() {
    log.info("createOWLOntologyManager()");
    // We want a single instance here
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    log.info("manager: {}", manager);
    // 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) LoadingFinishedEvent(org.semanticweb.owlapi.model.OWLOntologyLoaderListener.LoadingFinishedEvent) LoadingStartedEvent(org.semanticweb.owlapi.model.OWLOntologyLoaderListener.LoadingStartedEvent) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 82 with OWLOntologyManager

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

the class AbstractOWLApiReasoningService method run.

/**
 * Generic method for running the reasoner
 *
 * @param input
 * @param generators
 * @return
 */
@Override
public Set<OWLAxiom> run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
    log.debug("run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators)");
    try {
        // Get the manager
        OWLOntologyManager manager = createOWLOntologyManager();
        // Get the reasoner
        OWLReasoner reasoner = getReasoner(input);
        log.info("Running {} reasoner on {} ", reasoner.getClass(), input.getOntologyID());
        // To generate inferred axioms
        InferredOntologyGenerator inferred = new InferredOntologyGenerator(reasoner, generators);
        // We fill an anonymous ontology with the result, the return the
        // axiom set
        Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
        try {
            OWLOntology output = manager.createOntology();
            log.debug("Created output ontology: {}", output);
            try {
                inferred.fillOntology(manager, output);
            } catch (InconsistentOntologyException i) {
                throw i;
            } catch (Throwable t) {
                log.error("Some problem occurred:\n {}", t.getStackTrace());
                throw new ReasoningServiceException();
            }
            log.debug("Filled ontology: {}", output);
            log.debug("Temporary ID is {}", output.getOntologyID());
            axioms = manager.getOntology(output.getOntologyID()).getAxioms();
            // IMPORTANT We remove the ontology from the manager
            manager.removeOntology(output);
        } catch (OWLOntologyCreationException e) {
            log.error("An exception have been thrown when instantiating the ontology");
            throw new ReasoningServiceException();
        }
        return axioms;
    } catch (InconsistentOntologyException inconsistent) {
        /**
         * TODO Add report. Why it is inconsistent?
         */
        throw new InconsistentInputException();
    } catch (Exception exception) {
        log.error("An exception have been thrown while executing method run()", exception);
        throw new ReasoningServiceException();
    }
}
Also used : OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) InferredOntologyGenerator(org.semanticweb.owlapi.util.InferredOntologyGenerator) HashSet(java.util.HashSet)

Example 83 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project goci by EBISPOT.

the class IRITreeBuilder method buildIRITree.

// OntologyLoader ontologyLoader;
// 
// @Autowired
// public IRITreeBuilder(OntologyLoader ontologyLoader){
// this.ontologyLoader = ontologyLoader;
// }
public IRITree buildIRITree(URL efoLocation) throws URISyntaxException, OWLOntologyCreationException {
    // load efo
    getLog().info("Loading efo...");
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology efo = manager.loadOntology(IRI.create(efoLocation));
    owlNothingIRI = manager.getOWLDataFactory().getOWLNothing().getIRI().toString();
    // create a reasoner over efo
    getLog().info("Reasoning over efo...");
    OWLReasonerFactory factory = new Reasoner.ReasonerFactory();
    ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor();
    OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
    OWLReasoner reasoner = factory.createReasoner(efo, config);
    getLog().info("Precomputing inferences...");
    reasoner.precomputeInferences();
    getLog().info("Checking ontology consistency...");
    reasoner.isConsistent();
    // get 'top' class
    OWLClass topClass = reasoner.getTopClassNode().getRepresentativeElement();
    getLog().info("Reasoner 'top class' element is " + topClass.getIRI());
    IRITree tree = new IRITree();
    // do one level deep manually - should only be experimental factor
    IRINode rootNode = null;
    OWLClass efClass = null;
    NodeSet<OWLClass> subclasses = reasoner.getSubClasses(topClass, true);
    for (Node<OWLClass> node : subclasses) {
        OWLClass cls = node.getRepresentativeElement();
        getLog().debug("Next child of " + topClass + " is " + cls);
        if (cls.getIRI().toString().equals(OntologyConstants.EXPERIMENTAL_FACTOR_CLASS_IRI)) {
            efClass = cls;
            rootNode = new IRINode(cls.getIRI(), getClassLabel(efo, cls));
        }
    }
    if (rootNode != null) {
        getLog().info("Building tree... walking ontology from " + rootNode.getLabel() + " down...");
        tree.setRootNode(rootNode);
        recurse(reasoner, efo, efClass, rootNode);
        getLog().info("...Tree build complete!");
    } else {
        throw new RuntimeException("Could not find Experimental Factor as a child of OWL:Thing");
    }
    return tree;
}
Also used : OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) ConsoleProgressMonitor(org.semanticweb.owlapi.reasoner.ConsoleProgressMonitor) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory)

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