Search in sources :

Example 6 with OWLOntology

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

the class AbstractOntologyLoader method getStringLiteralAnnotationValues.

protected Set<String> getStringLiteralAnnotationValues(OWLOntology ontology, OWLClass ontologyClass, OWLAnnotationProperty annotationProperty) {
    Set<String> vals = new HashSet<>();
    Collection<OWLAnnotation> annotations = ontologyClass.getAnnotations(ontology, annotationProperty);
    annotations.stream().filter(annotation -> annotation.getValue() instanceof OWLLiteral).forEach(annotation -> {
        OWLLiteral val = (OWLLiteral) annotation.getValue();
        vals.add(val.getLiteral());
    });
    return vals;
}
Also used : OWLRDFVocabulary(org.semanticweb.owlapi.vocab.OWLRDFVocabulary) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) OWLManager(org.semanticweb.owlapi.apibinding.OWLManager) Initializable(uk.ac.ebi.spot.goci.ontology.Initializable) HashSet(java.util.HashSet) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) IRI(org.semanticweb.owlapi.model.IRI) Map(java.util.Map) URI(java.net.URI) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Resource(org.springframework.core.io.Resource) SimpleIRIMapper(org.semanticweb.owlapi.util.SimpleIRIMapper) Iterator(java.util.Iterator) Collection(java.util.Collection) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) Set(java.util.Set) Collectors(java.util.stream.Collectors) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) Optional(java.util.Optional) Collections(java.util.Collections) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) HashSet(java.util.HashSet)

Example 7 with OWLOntology

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

the class AbstractOntologyLoader method loadOntology.

/**
     * Extracts and loads into memory all the class labels and corresponding IRIs.  This class makes the assumption that
     * one primary label per class exists. If any classes contain multiple rdfs:labels, these classes are ignored.
     * <p>
     * Once loaded, this method must set the IRI of the ontology, and should add class labels, class types (however you
     * chose to implement the concept of a "type") and synonyms, where they exist.
     * <p>
     * Implementations do not need to concern themselves with resolving imports or physical/logical mappings as this is
     * done in initialisation at the abstract level.  Subclasses can simply do <code>OWLOntology ontology =
     * getManager().loadOntology(IRI.create(getOntologyURI()));</code> as a basic implementation before populating the
     * various required caches
     */
protected OWLOntology loadOntology() throws OWLOntologyCreationException {
    try {
        getLog().debug("Loading ontology...");
        OWLOntology ontology = getManager().loadOntology(IRI.create(getOntologyURI()));
        IRI ontologyIRI = ontology.getOntologyID().getOntologyIRI();
        if (ontologyIRI == null) {
            throw new OWLOntologyCreationException("Failed to load ontology from " + getOntologyURI() + ": " + "no IRI present for this ontology");
        } else {
            setOntologyIRI(ontologyIRI);
            if (getOntologyName() == null) {
                URI ontologyURI = ontologyIRI.toURI();
                String name = ontologyURI.getFragment() != null ? ontologyURI.getFragment() : ontologyURI.getPath();
                if (name == null) {
                    getLog().warn("Can't shorten the name for " + ontologyIRI.toString());
                    name = ontologyURI.toString();
                }
                setOntologyName(name);
            }
            getLog().debug("Successfully loaded ontology " + ontologyIRI);
            getLog().debug("Computing indexes...");
            return indexOntology(ontology);
        }
    } finally {
        getLog().debug("Done loading/indexing");
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) URI(java.net.URI)

Example 8 with OWLOntology

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

the class HermitReasoningServiceTest method testInstantiation.

@Test
public void testInstantiation() {
    log.info("Testing the innstantiation of an HermitReasoningService");
    OWLOntology foaf = TestData.manager.getOntology(IRI.create(TestData.FOAF_NS));
    // Here we want to be sure that the getReasoner() method creates an
    // instance correctly
    HermitReasoningService akindofhermit = new HermitReasoningService() {

        protected OWLReasoner getReasoner(OWLOntology ontology) {
            try {
                OWLReasoner reasoner = super.getReasoner(ontology);
                assertTrue(reasoner != null);
                return reasoner;
            } catch (Throwable t) {
                log.error("Some prolem occurred while instantiating the HermitReasoningService. Message was: {}", t.getLocalizedMessage());
                // We force tests to stop here
                assertTrue(false);
                return null;
            }
        }
    };
    long startHere = System.currentTimeMillis();
    // This is possible only because the subclass is in this scope...
    akindofhermit.getReasoner(foaf);
    long endHere = System.currentTimeMillis();
    log.info("Instantiating an Hermit reasoner lasts {} milliseconds", (endHere - startHere));
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) Test(org.junit.Test)

Example 9 with OWLOntology

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

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

the class OntologyManagerInputProvider method getFromOntoMgr.

private OWLOntology getFromOntoMgr() throws IOException {
    try {
        Scope scope = null;
        synchronized (onManager) {
            scope = onManager.getScope(this.scopeId);
        }
        if (scope == null) {
            log.error("Scope {} cannot be retrieved", this.scopeId);
            throw new IOException("Scope " + this.scopeId + " cannot be retrieved");
        }
        Session session = null;
        if (sessionManager != null)
            synchronized (sessionManager) {
                session = sessionManager.getSession(sessionId);
            }
        if (session == null)
            log.warn("Session {} cannot be retrieved. Ignoring.", this.sessionId);
        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        set.add(scope.export(OWLOntology.class, true));
        if (session != null)
            set.add(session.export(OWLOntology.class, true));
        if (set.size() == 1)
            return set.iterator().next();
        OWLOntologyMerger merger = new OWLOntologyMerger(new OWLOntologySetProvider() {

            @Override
            public Set<OWLOntology> getOntologies() {
                return set;
            }
        });
        return merger.createMergedOntology(createOWLOntologyManager(), IRI.create("reasoners:input-" + System.currentTimeMillis()));
    } catch (OWLOntologyCreationException e) {
        String message = "The network for scope/session cannot be retrieved";
        log.error(message + ":", e);
        throw new IllegalArgumentException(message);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) IOException(java.io.IOException) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Session(org.apache.stanbol.ontologymanager.servicesapi.session.Session) HashSet(java.util.HashSet)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)116 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)58 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)49 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)31 IRI (org.semanticweb.owlapi.model.IRI)30 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