Search in sources :

Example 41 with OWLOntologyManager

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

the class HermitReasoningServiceTest method testEnrichWithRules.

private void testEnrichWithRules(String testID, String rulesID, String testExpectedID) {
    log.info("Testing the task ENRICH 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.ENRICH, 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 all kind of axioms
            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) 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) HashSet(java.util.HashSet)

Example 42 with OWLOntologyManager

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

the class RegistryManagerImpl method activate.

protected void activate(Dictionary<String, Object> configuration) {
    // Parse configuration.
    try {
        lazyLoading = (Boolean) (configuration.get(RegistryManager.LAZY_LOADING));
    } catch (Exception ex) {
        lazyLoading = _LAZY_LOADING_DEFAULT;
    }
    try {
        retainIncomplete = (Boolean) (configuration.get(RegistryManager.RETAIN_INCOMPLETE));
    } catch (Exception ex) {
        retainIncomplete = _RETAIN_INCOMPLETE_DEFAULT;
    }
    Object obj = configuration.get(RegistryManager.REGISTRY_LOCATIONS);
    if (obj instanceof String[])
        locations = (String[]) obj;
    else if (obj instanceof String)
        locations = new String[] { (String) obj };
    if (locations == null)
        locations = new String[] { "stanbol_network.owl" };
    Object cachingPolicy = configuration.get(RegistryManager.CACHING_POLICY);
    if (cachingPolicy == null) {
        this.cachingPolicyString = _CACHING_POLICY_DEFAULT.name();
    } else {
        this.cachingPolicyString = cachingPolicy.toString();
    }
    final IRI[] offlineResources;
    if (this.offline != null) {
        List<IRI> paths = offline.getOntologySourceLocations();
        if (paths != null)
            offlineResources = paths.toArray(new IRI[0]);
        else
            // There are no offline paths.
            offlineResources = new IRI[0];
    } else
        // There's no offline configuration at all.
        offlineResources = new IRI[0];
    // Used only for creating the registry model, do not use for caching.
    OWLOntologyManager mgr = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
    OWLOntologyLoaderConfiguration conf = new OWLOntologyLoaderConfiguration();
    // If we are retaining incomplete registries, do not throw exceptions if imports fail.
    conf.setSilentMissingImportsHandling(retainIncomplete);
    // Load registries
    Set<OWLOntology> regOnts = new HashSet<OWLOntology>();
    for (String loc : locations) {
        try {
            IRI iri = IRI.create(loc);
            OWLOntologyDocumentSource src = null;
            OWLOntology o = null;
            if (iri.isAbsolute())
                src = new IRIDocumentSource(iri);
            else {
                // Relative IRI : use data file provider
                log.debug("Found relative IRI {} . Will try to retrieve from data file providers.", iri);
                Map<String, String> info = new HashMap<String, String>();
                if (dataFileProvider != null && dataFileProvider.isAvailable(null, loc, info))
                    src = new StreamDocumentSource(dataFileProvider.getInputStream(null, loc, info));
            }
            if (src != null)
                o = mgr.loadOntologyFromOntologyDocument(src, conf);
            if (o != null)
                regOnts.add(o);
            else
                log.warn("Failed to obtain OWL ontology from resource {}", loc);
        } catch (OWLOntologyAlreadyExistsException e) {
            log.info("Skipping cached ontology {}.", e.getOntologyID());
            continue;
        } catch (OWLOntologyCreationException e) {
            log.warn("Failed to load ontology " + loc + " - Skipping...", e);
            continue;
        } catch (Exception e) {
            log.warn("Invalid registry configuration " + loc + " - Skipping...", e);
            continue;
        }
    }
    // Create and set the cache.
    if (cachingPolicyString.equals(CachingPolicy.CENTRALISED.name())) {
        // this.cache = OWLOntologyManagerFactory.createOWLOntologyManager(offlineResources);
        if (cache == null) {
            log.warn("Caching policy is set as Centralised, but no ontology provider is supplied. Will use new in-memory tcProvider.");
            cache = new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance());
        }
    // else sta bene cosi'
    } else if (cachingPolicyString.equals(CachingPolicy.DISTRIBUTED.name())) {
        this.cache = null;
    }
    riFactory = new RegistryItemFactoryImpl(cache);
    // Build the model.
    createModel(regOnts);
    // Set the cache on libraries.
    Set<RegistryItem> visited = new HashSet<RegistryItem>();
    for (Registry reg : getRegistries()) for (RegistryItem child : reg.getChildren()) if (!visited.contains(child)) {
        if (child instanceof Library) {
            if (this.cache != null)
                ((Library) child).setCache(this.cache);
            else
                ((Library) child).setCache(new ClerezzaOntologyProvider(TcManager.getInstance(), offline, Parser.getInstance()));
        }
        visited.add(child);
    }
    if (isLazyLoading()) {
    // Nothing to do about it at the moment.
    } else {
        loadEager();
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) HashMap(java.util.HashMap) OWLOntologyLoaderConfiguration(org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) IRIDocumentSource(org.semanticweb.owlapi.io.IRIDocumentSource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) StreamDocumentSource(org.semanticweb.owlapi.io.StreamDocumentSource) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLObject(org.semanticweb.owlapi.model.OWLObject) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLOntologyDocumentSource(org.semanticweb.owlapi.io.OWLOntologyDocumentSource) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet)

Example 43 with OWLOntologyManager

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

the class SessionImpl method attachScopeImportsOwlApi.

private void attachScopeImportsOwlApi(OWLOntology target, org.semanticweb.owlapi.model.IRI prefix) {
    if (!attachedScopes.isEmpty()) {
        String scopePrefix = prefix.toString();
        scopePrefix = scopePrefix.substring(0, scopePrefix.lastIndexOf("/" + shortName + "/")) + "/ontology/";
        List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
        OWLOntologyManager ontologyManager = target.getOWLOntologyManager();
        OWLDataFactory df = ontologyManager.getOWLDataFactory();
        // Add import declarations for attached scopes.
        for (String scopeID : attachedScopes) {
            org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(scopePrefix + scopeID);
            changes.add(new AddImport(target, df.getOWLImportsDeclaration(physIRI)));
        }
        // Commit
        ontologyManager.applyChanges(changes);
    }
}
Also used : OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList)

Example 44 with OWLOntologyManager

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

the class TestOntologyCollectors method spacePreservesImports.

@Test
public void spacePreservesImports() throws Exception {
    InputStream content = getClass().getResourceAsStream("/ontologies/characters_all.owl");
    URL url = getClass().getResource("/ontologies/characters_all.owl");
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    mgr.addOntologyLoaderListener(new OWLOntologyLoaderListener() {

        @Override
        public void startedLoadingOntology(LoadingStartedEvent arg0) {
        }

        @Override
        public void finishedLoadingOntology(LoadingFinishedEvent arg0) {
            log.info((arg0.isSuccessful() ? "Loaded" : "Failed") + (arg0.isImported() ? " imported " : " ") + "ontology " + arg0.getDocumentIRI());
        }
    });
    mgr.addIRIMapper(new PhonyIRIMapper(null));
    File f = new File(url.toURI());
    OntologyInputSource<OWLOntology> src = new ParentPathInputSource(f, mgr);
    // OntologyInputSource<OWLOntology> src = new RootOntologyIRISource(IRI.create(f), mgr);
    // OntologyInputSource<OWLOntology> src = new OntologyContentInputSource(content,mgr);
    OWLOntology original = src.getRootOntology();
    Assert.assertNotNull(original);
    OntologySpace spc = new CustomSpaceImpl("Test", scopeNs, ontologyProvider);
    spc.addOntology(src);
}
Also used : InputStream(java.io.InputStream) URL(java.net.URL) CustomSpaceImpl(org.apache.stanbol.ontologymanager.multiplexer.clerezza.impl.CustomSpaceImpl) OWLOntologyLoaderListener(org.semanticweb.owlapi.model.OWLOntologyLoaderListener) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) PhonyIRIMapper(org.apache.stanbol.commons.owl.PhonyIRIMapper) ParentPathInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) File(java.io.File) Test(org.junit.Test)

Example 45 with OWLOntologyManager

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

the class TestOntologyReconciliation method anonymousFromURL.

/*
     * Anonymous ontologies loaded from a URL must reconcile with a public key that matches the resource URL
     * in its ontology IRI.
     */
@Test
public void anonymousFromURL() throws Exception {
    URL in = getClass().getResource(location_nameless);
    OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
    OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(IRI.create(in));
    assertTrue(o1.isAnonymous());
    // in = getClass().getResourceAsStream(location); // use if stream cannot be reset
    OWLOntologyID key = ontologyProvider.loadInStore(IRI.create(in), RDF_XML, false);
    assertNotNull(key);
    assertFalse(key.isAnonymous());
    log.info("Anonymous ontology loaded with non-anonymous public key {}", key);
    OWLOntology o2 = ontologyProvider.getStoredOntology(key, OWLOntology.class, false);
    assertTrue(o2.isAnonymous());
    // Cannot equal OWLOntology objects
    assertEquals(o1.getAxioms(), o2.getAxioms());
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) URL(java.net.URL) Test(org.junit.Test)

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