Search in sources :

Example 66 with OWLOntology

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

the class ODPRegistryCacheManager method retrieveLocalResource.

private static synchronized OWLOntology retrieveLocalResource(URI uri) throws OWLOntologyCreationException, ODPRegistryCacheException, URIUnresolvableException {
    File file = uris.get(uri);
    if (!file.exists()) {
        uris.remove(uri);
        return getOntology(uri);
    }
    manager.setSilentMissingImportsHandling(true);
    manager.addMissingImportListener(new MissingImportListener() {

        public void importMissing(MissingImportEvent arg0) {
            if (!getUnresolvedURIs().contains(arg0.getImportedOntologyURI()))
                getUnresolvedURIs().add(arg0.getImportedOntologyURI().toURI());
        }
    });
    IRI oi = oiri.get(uri);
    OWLOntology ontology = null;
    ontology = manager.getOntology(oi);
    if (ontology == null)
        try {
            ontology = manager.loadOntologyFromOntologyDocument(IRI.create(file));
        } catch (OWLOntologyAlreadyExistsException e) {
            ontology = manager.getOntology(e.getOntologyID());
        }
    return ontology;
}
Also used : MissingImportListener(org.semanticweb.owlapi.model.MissingImportListener) IRI(org.semanticweb.owlapi.model.IRI) MissingImportEvent(org.semanticweb.owlapi.model.MissingImportEvent) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) File(java.io.File) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)

Example 67 with OWLOntology

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

the class TestOntologyNetworkPersistence method canRetrieveOntologySingleton.

@Test
public void canRetrieveOntologySingleton() throws Exception {
    OWLOntologyID foaf = new OWLOntologyID(org.semanticweb.owlapi.model.IRI.create("http://xmlns.com/foaf/0.1/"));
    OWLOntology o1;
    // Get the fake FOAF and load it into the ontology provider
    InputStream data = getClass().getResourceAsStream("/ontologies/mockfoaf.rdf");
    // Keep track of its storage key
    OWLOntologyID key = ontologyProvider.loadInStore(data, SupportedFormat.RDF_XML, false);
    assertNotNull(key);
    assertFalse(key.isAnonymous());
    // Retrieve the stored fake FOAF
    assertEquals(1, ontologyProvider.listPrimaryKeys().size());
    o1 = ontologyProvider.getStoredOntology(key, OWLOntology.class, false);
    OWLOntologyID id = o1.getOntologyID();
    assertNotNull(id);
    assertEquals(foaf, id);
    // Check there is a storage key for the (real) ID of the FOAF ontology
    // key = ontologyProvider.getKey(foaf);
    // assertNotNull(key);
    // assertFalse(key.isAnonymous());
    assertTrue(ontologyProvider.hasOntology(foaf));
    log.info("Stanbol going down...");
    // but keep the TcProvider
    resetOntologyProvider();
    assertEquals(1, ontologyProvider.listPrimaryKeys().size());
    // Check again for the FOAF key
    // key = ontologyProvider.getKey(foaf);
    // assertNotNull(key);
    // assertFalse(key.isAnonymous());
    assertTrue(ontologyProvider.hasOntology(foaf));
    // The OWL API implements OWLOntology#equals()
    assertEquals(o1, ontologyProvider.getStoredOntology(key, OWLOntology.class, false));
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Test(org.junit.Test)

Example 68 with OWLOntology

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

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

the class TestOntologyNetworkPersistence method canRetrieveOntologyImported.

@Test
public void canRetrieveOntologyImported() throws Exception {
    String pcomics = "http://stanbol.apache.org/ontologies/pcomics/";
    OWLOntologyID foaf = new OWLOntologyID(org.semanticweb.owlapi.model.IRI.create("http://xmlns.com/foaf/0.1/")), all = new OWLOntologyID(org.semanticweb.owlapi.model.IRI.create(pcomics + "characters_all.owl")), main = new OWLOntologyID(org.semanticweb.owlapi.model.IRI.create(pcomics + "maincharacters.owl")), minor = new OWLOntologyID(org.semanticweb.owlapi.model.IRI.create(pcomics + "minorcharacters.owl"));
    OWLOntology oAll, oMain, oMinor, oFoaf;
    final int total = 4;
    // Load the Peanuts characters_all ontology (has 2 import levels)
    InputStream data = getClass().getResourceAsStream("/ontologies/characters_all.owl");
    OWLOntologyID key = ontologyProvider.loadInStore(data, SupportedFormat.RDF_XML, false);
    assertNotNull(key);
    assertFalse(key.isAnonymous());
    /*
         * characters_all, main, minor + mockfoaf (note: imports are available only because the xml:base is
         * set to be the same as the import target)
         */
    assertEquals(total, ontologyProvider.listPrimaryKeys().size());
    // Check that each imported ontology is stored
    oAll = ontologyProvider.getStoredOntology(all, OWLOntology.class, false);
    OWLOntologyID id = oAll.getOntologyID();
    assertNotNull(id);
    assertEquals(all, id);
    oMain = ontologyProvider.getStoredOntology(main, OWLOntology.class, false);
    id = oMain.getOntologyID();
    assertNotNull(id);
    assertEquals(main, id);
    oMinor = ontologyProvider.getStoredOntology(minor, OWLOntology.class, false);
    id = oMinor.getOntologyID();
    assertNotNull(id);
    assertEquals(minor, id);
    oFoaf = ontologyProvider.getStoredOntology(foaf, OWLOntology.class, false);
    id = oFoaf.getOntologyID();
    assertNotNull(id);
    assertEquals(foaf, id);
    log.info("Stanbol going down...");
    // but keep the TcProvider
    resetOntologyProvider();
    assertEquals(total, ontologyProvider.listPrimaryKeys().size());
    // The OWL API implements OWLOntology#equals()
    assertEquals(oAll, ontologyProvider.getStoredOntology(all, OWLOntology.class, false));
    assertEquals(oMain, ontologyProvider.getStoredOntology(main, OWLOntology.class, false));
    assertEquals(oMinor, ontologyProvider.getStoredOntology(minor, OWLOntology.class, false));
    assertEquals(oFoaf, ontologyProvider.getStoredOntology(foaf, OWLOntology.class, false));
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Test(org.junit.Test)

Example 70 with OWLOntology

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

the class TestOntologyReconciliation method versionedOnlyFromURL.

/*
     * If an ontology has no ontology IRI but does have a version IRI, it should still be possible to load it,
     * but the version IRI must be erased. Plus, the public key should be created after the resource URL.
     */
@Test
public void versionedOnlyFromURL() throws Exception {
    String location = "/ontologies/naming/versionedonly.owl";
    IRI url = IRI.create(getClass().getResource(location));
    OWLOntologyID expected = new OWLOntologyID(url);
    OWLOntologyManager onMgr = OWLManager.createOWLOntologyManager();
    OWLOntology o1 = onMgr.loadOntologyFromOntologyDocument(url);
    // Ensure that the OWL API erases the version IRI.
    assertTrue(o1.isAnonymous());
    assertNull(o1.getOntologyID().getVersionIRI());
    // The public key must be non-anonymous nonetheless.
    OWLOntologyID key = ontologyProvider.loadInStore(url, RDF_XML, false);
    assertNotNull(key);
    assertFalse(key.isAnonymous());
    log.info("Wrongly versioned ontology loaded with public key {}", key);
    assertFalse(o1.equals(key));
    assertEquals(expected, key);
    log.info(" -- (matches resource URL).");
    OWLOntology o1_1 = ontologyProvider.getStoredOntology(key, OWLOntology.class, false);
    assertNotNull(o1_1);
    assertTrue(o1_1.isAnonymous());
    assertNull(o1_1.getOntologyID().getVersionIRI());
    // Cannot equal two OWLOntology objects, especially if anonymous.
    // Check that they match axiom-wise.
    log.warn("Plain OWL API seems to be failing to preserve owl:versionInfo. Will test non-annotation axioms only.");
    assertEquals(o1.getTBoxAxioms(false), o1_1.getTBoxAxioms(false));
    log.info(" -- TBox axiom check successful.");
    assertEquals(o1.getABoxAxioms(false), o1_1.getABoxAxioms(false));
    log.info(" -- ABox axiom check successful.");
    // No aliases should have been created.
    assertSame(0, ontologyProvider.listAliases(key).size());
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Test(org.junit.Test)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)118 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)59 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)50 IRI (org.semanticweb.owlapi.model.IRI)31 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)31 Test (org.junit.Test)25 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)24 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)22 HashSet (java.util.HashSet)21 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)18 Produces (javax.ws.rs.Produces)17 AddImport (org.semanticweb.owlapi.model.AddImport)16 OntModel (com.hp.hpl.jena.ontology.OntModel)15 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)15 InputStream (java.io.InputStream)14 GET (javax.ws.rs.GET)14 OWLClass (org.semanticweb.owlapi.model.OWLClass)14 ArrayList (java.util.ArrayList)13 Graph (org.apache.clerezza.commons.rdf.Graph)12 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)11