Search in sources :

Example 1 with OWLImportsDeclaration

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

the class DefaultGWASOWLConverter method createConversionOntology.

public OWLOntology createConversionOntology() throws OWLConversionException {
    try {
        // create a new graph to represent our data dump
        OWLOntology conversion = getManager().createOntology(IRI.create(OntologyConstants.GWAS_ONTOLOGY_BASE_IRI + "/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date())));
        // import the gwas ontology schema and efo
        OWLImportsDeclaration gwasImportDecl = getDataFactory().getOWLImportsDeclaration(IRI.create(OntologyConstants.GWAS_ONTOLOGY_SCHEMA_IRI));
        ImportChange gwasImport = new AddImport(conversion, gwasImportDecl);
        getManager().applyChange(gwasImport);
        OWLImportsDeclaration efoImportDecl = getDataFactory().getOWLImportsDeclaration(IRI.create(OntologyConstants.EFO_ONTOLOGY_SCHEMA_IRI));
        ImportChange efoImport = new AddImport(conversion, efoImportDecl);
        getManager().applyChange(efoImport);
        return conversion;
    } catch (OWLOntologyCreationException e) {
        throw new OWLConversionException("Failed to create new ontology", e);
    }
}
Also used : OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) SimpleDateFormat(java.text.SimpleDateFormat) AddImport(org.semanticweb.owlapi.model.AddImport) Date(java.util.Date) ImportChange(org.semanticweb.owlapi.model.ImportChange)

Example 2 with OWLImportsDeclaration

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

the class AbstractOntologyCollectorImpl method getOntologyAsOWLOntology.

protected OWLOntology getOntologyAsOWLOntology(OWLOntologyID ontologyId, boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // if (merge) throw new UnsupportedOperationException("Merge not implemented yet for OWLOntology.");
    // Remove the check below. It might be an unmanaged dependency (TODO remove from collector and
    // reintroduce check?).
    // if (!hasOntology(ontologyIri)) return null;
    OWLOntology o;
    o = ontologyProvider.getStoredOntology(ontologyId, OWLOntology.class, merge);
    if (merge) {
        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        log.debug("Merging {} with its imports, if any.", o);
        set.add(o);
        // Actually, if the provider already performed the merge, this won't happen
        for (OWLOntology impo : o.getImportsClosure()) {
            log.debug("Imported ontology {} will be merged with {}.", impo, o);
            set.add(impo);
        }
        OWLOntologySetProvider provider = new OWLOntologySetProvider() {

            @Override
            public Set<OWLOntology> getOntologies() {
                return set;
            }
        };
        OWLOntologyMerger merger = new OWLOntologyMerger(provider);
        try {
            o = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), ontologyId.getOntologyIRI());
        } catch (OWLOntologyCreationException e) {
            log.error("Failed to merge imports for ontology " + ontologyId, e);
        // do not reassign the root ontology
        }
    } else {
        // Rewrite import statements
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        OWLDataFactory df = OWLManager.getOWLDataFactory();
        /*
             * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
             * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering
             * imported ontologies as *not* managed.
             */
        for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
            changes.add(new RemoveImport(o, oldImp));
            String s = oldImp.getIRI().toString();
            // FIXME Ugly way to check, but we'll get through with it
            if (s.contains("::"))
                s = s.substring(s.indexOf("::") + 2, s.length());
            boolean managed = managedOntologies.contains(oldImp.getIRI());
            // For space, always go up at least one
            String tid = getID();
            if (backwardPathLength > 0)
                tid = tid.split("/")[0];
            org.semanticweb.owlapi.model.IRI target = org.semanticweb.owlapi.model.IRI.create((managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s);
            changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
        }
        o.getOWLOntologyManager().applyChanges(changes);
    }
    return o;
}
Also used : ArrayList(java.util.ArrayList) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) AddImport(org.semanticweb.owlapi.model.AddImport) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 3 with OWLImportsDeclaration

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

the class HermitReasoningServiceTest method testEnrichResultTypes.

/**
 * @param testID
 *            // The ontology to run
 * @param types
 *            // The type of axioms we expect in the result
 */
private void testEnrichResultTypes(String testID, AxiomType<?>[] types) {
    List<AxiomType<?>> typelist = new ArrayList<AxiomType<?>>();
    typelist.addAll(Arrays.asList(types));
    log.info("Testing the enrich() method (result axioms types)");
    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);
        OWLImportsDeclaration importTest = TestData.factory.getOWLImportsDeclaration(IRI.create(testID));
        manager.applyChange(new AddImport(testOntology, importTest));
        // 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.runTask(ReasoningService.Tasks.ENRICH, manager.getOntology(testOntologyID));
        // Maybe we want to see the inferred axiom list
        if (log.isDebugEnabled()) {
            TestUtils.debug(inferred, log);
        }
        for (OWLAxiom a : inferred) {
            typelist.remove(a.getAxiomType());
        }
        if (!typelist.isEmpty()) {
            for (AxiomType<?> t : typelist) log.error("Missing axiom type: {}", t);
        }
        assertTrue(typelist.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 (ReasoningServiceException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (InconsistentInputException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (UnsupportedTaskException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    }
}
Also used : AxiomType(org.semanticweb.owlapi.model.AxiomType) ArrayList(java.util.ArrayList) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) AddImport(org.semanticweb.owlapi.model.AddImport) 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) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 4 with OWLImportsDeclaration

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

the class HermitReasoningServiceTest method testIsConsistent.

/**
 * Check whether the ontology is consistent
 *
 * @param testID
 *            // The ID of the ontology to test
 * @param expected
 *            // If it is expected to be consistent or not
 */
private void testIsConsistent(String testID, boolean expected) {
    log.info("Testing the isConsistent 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);
        OWLImportsDeclaration importTest = TestData.factory.getOWLImportsDeclaration(IRI.create(testID));
        manager.applyChange(new AddImport(testOntology, importTest));
        // 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");
        try {
            assertTrue(this.theinstance.isConsistent(testOntology) == expected);
        } catch (ReasoningServiceException e) {
            log.error("Error while testing the isConsistent method. Message was: {}", e.getLocalizedMessage());
            assertTrue(false);
        }
        // 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);
    }
}
Also used : 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) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) AddImport(org.semanticweb.owlapi.model.AddImport)

Example 5 with OWLImportsDeclaration

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

the class HermitReasoningServiceTest method testClassifyWithRules.

private void testClassifyWithRules(String testID, String rulesID, String testExpectedID) {
    log.info("Testing the task CLASSIFY with rules");
    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);
        OWLImportsDeclaration importTest = TestData.factory.getOWLImportsDeclaration(IRI.create(testID));
        manager.applyChange(new AddImport(testOntology, importTest));
        Set<SWRLRule> rules = manager.getOntology(IRI.create(rulesID)).getAxioms(AxiomType.SWRL_RULE);
        // Maybe we want to see the list of rules
        if (log.isDebugEnabled()) {
            log.debug("List of {} rules: ", rules.size());
            TestUtils.debug(rules, log);
        }
        log.debug("We add the rules to the ontology");
        manager.addAxioms(manager.getOntology(testOntologyID), rules);
        // Maybe we want to see what is in before
        if (log.isDebugEnabled())
            log.debug("Content of the input is:");
        TestUtils.debug(manager.getOntology(testOntologyID), log);
        // Now we test the method
        log.debug("Running HermiT");
        Set<OWLAxiom> inferred = this.theinstance.runTask(ReasoningService.Tasks.CLASSIFY, manager.getOntology(testOntologyID));
        // Maybe we want to see the inferred axiom list
        if (log.isDebugEnabled()) {
            log.debug("{} inferred axioms:", inferred.size());
            TestUtils.debug(inferred, log);
        }
        Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(testExpectedID)).getLogicalAxioms();
        Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
        for (OWLAxiom expected : expectedAxioms) {
            // We consider here only two kind of axioms
            if (expected instanceof OWLSubClassOfAxiom || expected instanceof OWLClassAssertionAxiom) {
                if (!inferred.contains(expected)) {
                    log.error("missing expected axiom: {}", expected);
                    missing.add(expected);
                }
            }
        }
        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 (ReasoningServiceException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (InconsistentInputException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    } catch (UnsupportedTaskException e) {
        log.error("An {} have been thrown while executing the reasoning", e.getClass());
        assertTrue(false);
    }
}
Also used : OWLLogicalAxiom(org.semanticweb.owlapi.model.OWLLogicalAxiom) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) AddImport(org.semanticweb.owlapi.model.AddImport) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLSubClassOfAxiom(org.semanticweb.owlapi.model.OWLSubClassOfAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) HashSet(java.util.HashSet)

Aggregations

OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)10 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)10 AddImport (org.semanticweb.owlapi.model.AddImport)9 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)9 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)6 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)5 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)5 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)5 HashSet (java.util.HashSet)4 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)4 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)4 ArrayList (java.util.ArrayList)3 IRI (org.semanticweb.owlapi.model.IRI)3 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)3 OWLLogicalAxiom (org.semanticweb.owlapi.model.OWLLogicalAxiom)3 OWLOntologyChange (org.semanticweb.owlapi.model.OWLOntologyChange)3 RemoveImport (org.semanticweb.owlapi.model.RemoveImport)2 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)2 IOException (java.io.IOException)1 URI (java.net.URI)1