Search in sources :

Example 1 with OWLAxiom

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

the class DefaultGWASOWLPublisher method saveGWASDataInferredView.

public void saveGWASDataInferredView(OWLReasoner reasoner, File outputFile) throws OWLConversionException {
    try {
        // create new ontology to hold inferred axioms
        OWLOntology inferredOntology = getConverter().createConversionOntology();
        getLog().info("Saving inferred view...");
        List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
        // we require all inferred stuff except for disjoints...
        gens.add(new InferredClassAssertionAxiomGenerator());
        gens.add(new InferredDataPropertyCharacteristicAxiomGenerator());
        gens.add(new InferredEquivalentClassAxiomGenerator());
        gens.add(new InferredEquivalentDataPropertiesAxiomGenerator());
        gens.add(new InferredEquivalentObjectPropertyAxiomGenerator());
        gens.add(new InferredInverseObjectPropertiesAxiomGenerator());
        gens.add(new InferredObjectPropertyCharacteristicAxiomGenerator());
        gens.add(new InferredPropertyAssertionGenerator());
        gens.add(new InferredSubClassAxiomGenerator());
        gens.add(new InferredSubDataPropertyAxiomGenerator());
        gens.add(new InferredSubObjectPropertyAxiomGenerator());
        // now create the target ontology and save
        OWLOntologyManager inferredManager = inferredOntology.getOWLOntologyManager();
        InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
        iog.fillOntology(inferredManager, inferredOntology);
        inferredManager.saveOntology(inferredOntology, IRI.create(outputFile));
        getLog().info("Inferred view saved ok");
    } catch (OWLOntologyStorageException e) {
        throw new OWLConversionException("Failed to save GWAS data (inferred view)", e);
    }
}
Also used : InferredSubClassAxiomGenerator(org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator) InferredDataPropertyCharacteristicAxiomGenerator(org.semanticweb.owlapi.util.InferredDataPropertyCharacteristicAxiomGenerator) InferredEquivalentDataPropertiesAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentDataPropertiesAxiomGenerator) InferredPropertyAssertionGenerator(org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator) InferredObjectPropertyCharacteristicAxiomGenerator(org.semanticweb.owlapi.util.InferredObjectPropertyCharacteristicAxiomGenerator) InferredInverseObjectPropertiesAxiomGenerator(org.semanticweb.owlapi.util.InferredInverseObjectPropertiesAxiomGenerator) InferredAxiomGenerator(org.semanticweb.owlapi.util.InferredAxiomGenerator) ArrayList(java.util.ArrayList) InferredSubDataPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator) InferredEquivalentClassAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentClassAxiomGenerator) OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) InferredClassAssertionAxiomGenerator(org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) InferredSubObjectPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator) InferredEquivalentObjectPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentObjectPropertyAxiomGenerator) InferredOntologyGenerator(org.semanticweb.owlapi.util.InferredOntologyGenerator) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 2 with OWLAxiom

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

the class HermitReasoningServiceTest method testRun.

private void testRun(String testID, String expectedID) {
    log.info("Testing the run() method");
    OWLOntologyManager manager = TestData.manager;
    // We prepare the input ontology
    try {
        OWLOntology testOntology = manager.createOntology();
        OWLOntologyID testOntologyID = testOntology.getOntologyID();
        log.debug("Created test ontology with ID: {}", testOntologyID);
        AddImport addImport = new AddImport(testOntology, TestData.factory.getOWLImportsDeclaration(IRI.create(testID)));
        manager.applyChange(addImport);
        // We just test class assertions
        List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
        gens.add(new InferredClassAssertionAxiomGenerator());
        // Maybe we want to see what is in before
        if (log.isDebugEnabled())
            TestUtils.debug(manager.getOntology(testOntologyID), log);
        // Now we test the method
        log.debug("Running HermiT");
        Set<OWLAxiom> inferred = this.theinstance.run(manager.getOntology(testOntologyID), gens);
        // Maybe we want to see the inferred axiom list
        if (log.isDebugEnabled()) {
            TestUtils.debug(inferred, log);
        }
        // These are the set of expected axioms
        Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(expectedID)).getLogicalAxioms();
        Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
        for (OWLAxiom expected : expectedAxioms) {
            if (!inferred.contains(expected)) {
                log.error("missing expected axiom: {}", expected);
                missing.add(expected);
            }
        }
        log.info("Are all expected axioms in the result (true)? {}", missing.isEmpty());
        assertTrue(missing.isEmpty());
        // We want to remove the ontology from the manager
        manager.removeOntology(testOntology);
    } catch (OWLOntologyCreationException e) {
        log.error("An {} have been thrown while creating the input ontology for test", e.getClass());
        assertTrue(false);
    } catch (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    }
}
Also used : OWLLogicalAxiom(org.semanticweb.owlapi.model.OWLLogicalAxiom) InferredAxiomGenerator(org.semanticweb.owlapi.util.InferredAxiomGenerator) ArrayList(java.util.ArrayList) AddImport(org.semanticweb.owlapi.model.AddImport) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) InferredClassAssertionAxiomGenerator(org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) HashSet(java.util.HashSet)

Example 3 with OWLAxiom

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

the class TestUtils method checkProperties.

/**
	 * This is for monitoring hermit with datatype properties.
	 * 
	 * @param ont
	 */
public static void checkProperties(OWLOntology ont, Logger log) {
    // When throw inconsistent exception = false and ignoreUnsupportedDatatypes=true
    //- Datatypes which are not builtIn break the reasoner
    //- Looks like rdf:PlainLiteral is not supported by Hermit, even if it is marked as BuiltIn datatype by OWLApi
    // This incoherence generates an unexpected error!
    //
    Map<OWLDataProperty, Set<OWLDatatype>> properties = new HashMap<OWLDataProperty, Set<OWLDatatype>>();
    Set<OWLAxiom> remove = new HashSet<OWLAxiom>();
    for (OWLAxiom a : ont.getLogicalAxioms()) {
        if (a instanceof OWLDataPropertyAssertionAxiom) {
            OWLDataPropertyAssertionAxiom aa = (OWLDataPropertyAssertionAxiom) a;
            for (OWLDataProperty p : aa.getDataPropertiesInSignature()) {
                if (!properties.keySet().contains(p)) {
                    properties.put(p, new HashSet<OWLDatatype>());
                }
                for (OWLDatatype dt : aa.getDatatypesInSignature()) {
                    properties.get(p).add(dt);
                }
            }
        }
    }
    log.info("Data properties : ");
    for (Entry<OWLDataProperty, Set<OWLDatatype>> p : properties.entrySet()) {
        log.info(" - {} ", p.getKey());
        for (OWLDatatype d : p.getValue()) {
            log.info(" ---> {} [{}]", d, d.isBuiltIn());
        }
    }
    log.info("Data property axioms removed:");
    for (OWLAxiom d : remove) {
        log.info(" removed ---> {} ", d.getDataPropertiesInSignature());
    }
}
Also used : OWLDataPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) HashSet(java.util.HashSet)

Example 4 with OWLAxiom

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

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

the class ReasoningServiceExecutor method execute.

/**
     * General method for execution, delegates to specific implementations.
     * 
     * @param task
     * @param service
     * @param targetGraphID
     * @param parameters
     * @return
     * @throws ReasoningServiceException
     * @throws UnsupportedTaskException
     * @throws InconsistentInputException
     */
private ReasoningServiceResult<?> execute(String task, ReasoningService<?, ?, ?> service, String targetGraphID, Map<String, List<String>> parameters) throws ReasoningServiceException, UnsupportedTaskException, InconsistentInputException {
    long start = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.debug("[start] Execution: {}", service.getClass().getCanonicalName());
        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.debug("-----------------------------------------------------");
        log.debug("execute()");
        log.debug(" > task: {}", task);
        log.debug(" > service: {}", service.getClass().getCanonicalName());
        log.debug(" > target: {}", targetGraphID);
        log.debug(" > parameters:");
        for (Entry<String, List<String>> e : parameters.entrySet()) {
            log.debug(" >> {}: {}", e.getKey());
            for (String v : e.getValue()) {
                log.debug(" >>> value: {}", v);
            }
        }
        log.debug(" > input providers:");
        for (ReasoningServiceInputProvider p : inmgr.getProviders()) {
            log.debug(" >> {}", p.getClass().getCanonicalName());
        }
        log.debug("-----------------------------------------------------");
    }
    ReasoningServiceResult<?> result = null;
    /**
         * TODO Switch this into the ReasoningService implementation
         */
    if (service instanceof JenaReasoningService) {
        Model input = ModelFactory.createDefaultModel();
        synchronized (inmgr) {
            Iterator<Statement> statements = inmgr.getInputData(Statement.class);
            while (statements.hasNext()) {
                input.add(statements.next());
            }
        }
        List<Rule> rules = null;
        synchronized (inmgr) {
            Iterator<Rule> rulesI = inmgr.getInputData(Rule.class);
            while (rulesI.hasNext()) {
                Rule o = rulesI.next();
                log.debug("Rule: {}", o);
                if (rules == null) {
                    rules = new ArrayList<Rule>();
                }
                rules.add(o);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Input size is {} statements", input.listStatements().toSet().size());
        }
        result = executeJenaReasoningService(task, (JenaReasoningService) service, input, rules, targetGraphID, true, parameters);
    } else if (service instanceof OWLApiReasoningService) {
        OWLOntology input;
        try {
            input = OWLManager.createOWLOntologyManager().createOntology();
        } catch (OWLOntologyCreationException e) {
            throw new ReasoningServiceException(e);
        }
        synchronized (inmgr) {
            Iterator<OWLAxiom> statements = inmgr.getInputData(OWLAxiom.class);
            while (statements.hasNext()) {
                input.getOWLOntologyManager().addAxiom(input, statements.next());
            }
        }
        // FIXME Please check if this is really necessary!!!
        input = input.getOWLOntologyManager().getOntology(input.getOntologyID());
        List<SWRLRule> rules = null;
        synchronized (inmgr) {
            Iterator<SWRLRule> rulesI = inmgr.getInputData(SWRLRule.class);
            while (rulesI.hasNext()) {
                if (rules == null) {
                    rules = new ArrayList<SWRLRule>();
                }
                rules.add(rulesI.next());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Input size is {} statements", input.getAxiomCount());
        }
        result = executeOWLApiReasoningService(task, (OWLApiReasoningService) service, input, rules, targetGraphID, true, parameters);
    } else
        throw new UnsupportedOperationException("Service implementation not supported!");
    if (log.isDebugEnabled()) {
        log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        long end = System.currentTimeMillis();
        log.debug("[end] In time: {}ms", (end - start));
        log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }
    return result;
}
Also used : JenaReasoningService(org.apache.stanbol.reasoners.jena.JenaReasoningService) Statement(com.hp.hpl.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) OWLApiReasoningService(org.apache.stanbol.reasoners.owlapi.OWLApiReasoningService) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) ReasoningServiceInputProvider(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Model(com.hp.hpl.jena.rdf.model.Model) Iterator(java.util.Iterator) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) ArrayList(java.util.ArrayList) List(java.util.List) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Aggregations

OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)30 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)24 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)20 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)17 HashSet (java.util.HashSet)9 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)9 ArrayList (java.util.ArrayList)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 AddImport (org.semanticweb.owlapi.model.AddImport)7 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)7 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)6 Statement (com.hp.hpl.jena.rdf.model.Statement)5 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)5 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)5 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)5 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)5 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)5 IRI (org.semanticweb.owlapi.model.IRI)4 OWLClass (org.semanticweb.owlapi.model.OWLClass)4 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)4