Search in sources :

Example 56 with OWLOntology

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

the class GOCIDataPublisherDriver method publishAndSave.

public void publishAndSave(File assertedOntologyFile, File inferredOntologyFile) throws RuntimeException {
    try {
        // publishAndSave the data
        System.out.println("Attempting to convert and publish GWAS data as OWL...");
        OWLOntology ontology = getGwasOwlPublisher().publishGWASData();
        // and save the result
        System.out.print("Ontology converted, saving asserted results...");
        getGwasOwlPublisher().saveGWASData(ontology, assertedOntologyFile);
        System.out.println("..done!");
        if (inferredOntologyFile != null) {
            // now get the inferred view
            System.out.println("Evaluating inferred view...");
            OWLReasoner reasoner = getGwasOwlPublisher().publishGWASDataInferredView(ontology);
            // now save inferred view
            System.out.print("Ontology fully classified, saving inferred results...");
            getGwasOwlPublisher().saveGWASDataInferredView(reasoner, inferredOntologyFile);
            System.out.println("..done!");
        }
    } catch (OWLConversionException e) {
        System.err.println("Failed to publish data to OWL: " + e.getMessage());
        getLog().error("Failed to publish data to OWL: ", e);
        throw new RuntimeException(e);
    } catch (Exception e) {
        System.err.println("Failed to publish data to OWL (an unexpected exception occurred): " + e.getMessage());
        getLog().error("Failed to publish data to OWL (an unexpected exception occurred): ", e);
        throw new RuntimeException(e);
    }
}
Also used : OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) ParseException(org.apache.commons.cli.ParseException)

Example 57 with OWLOntology

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

the class ReasonedOntologyLoader method indexOntology.

protected OWLOntology indexOntology(OWLOntology ontology) throws OWLOntologyCreationException {
    getLog().debug("Trying to create a reasoner over ontology '" + getOntologyURI() + "'");
    OWLReasonerFactory factory = new Reasoner.ReasonerFactory();
    ReasonerProgressMonitor progressMonitor = new LoggingReasonerProgressMonitor(getLog());
    OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor);
    this.reasoner = factory.createReasoner(ontology, config);
    getLog().debug("Precomputing inferences...");
    reasoner.precomputeInferences();
    getLog().debug("Checking ontology consistency...");
    reasoner.isConsistent();
    getLog().debug("Checking for unsatisfiable classes...");
    if (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() > 0) {
        throw new OWLOntologyCreationException("Once classified, unsatisfiable classes were detected in '" + getOntologyIRI() + "'");
    } else {
        getLog().debug("Reasoning complete! ");
    }
    Set<OWLClass> allClasses = ontology.getClassesInSignature();
    removeExcludedClasses(ontology, allClasses, superclass -> reasoner.getSubClasses(superclass, false).getFlattened());
    int labelCount = 0;
    int labelledClassCount = 0;
    int synonymCount = 0;
    int synonymedClassCount = 0;
    getLog().debug("Loading " + allClasses.size() + " classes...");
    for (OWLClass ontologyClass : allClasses) {
        IRI clsIri = ontologyClass.getIRI();
        // get IRI fragment/path
        Optional<String> accession = evaluateAccessionValue(ontology, ontologyClass);
        if (accession.isPresent()) {
            addClassAccession(clsIri, accession.get());
        }
        // get label annotations
        Optional<String> label = evaluateLabelAnnotationValue(ontology, ontologyClass);
        if (label.isPresent()) {
            addClassLabel(clsIri, label.get());
            labelledClassCount++;
            labelCount++;
        }
        // get all synonym annotations
        getLog().trace("Loading synonyms of " + clsIri.toString() + "...");
        Set<String> synonyms = evaluateSynonymAnnotationValues(ontology, ontologyClass);
        if (!synonyms.isEmpty()) {
            addSynonyms(clsIri, synonyms);
            synonymCount += synonyms.size();
            synonymedClassCount++;
        }
        // get parent labels
        getLog().trace("Loading parents of " + clsIri.toString() + "...");
        Set<OWLClass> parents = reasoner.getSuperClasses(ontologyClass, false).getFlattened();
        // only add type if the parent isn't excluded
        Set<String> parentLabelSet = parents.stream().filter(allClasses::contains).peek(parent -> getLog().trace("Next parent of " + label + ": " + parent)).map(parent -> evaluateLabelAnnotationValue(ontology, parent)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
        // always also add current class to the parents
        label.ifPresent(parentLabelSet::add);
        addClassParentLabels(clsIri, parentLabelSet);
        // get child labels
        getLog().trace("Loading children of " + clsIri.toString() + "...");
        Set<OWLClass> children = reasoner.getSubClasses(ontologyClass, false).getFlattened();
        // only add type if the child isn't excluded
        Set<String> childLabelSet = children.stream().filter(allClasses::contains).peek(child -> getLog().trace("Next child of " + label + ": " + child)).map(child -> evaluateLabelAnnotationValue(ontology, child)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
        // always also add current class to the parents
        label.ifPresent(childLabelSet::add);
        addClassChildLabels(clsIri, childLabelSet);
    }
    getLog().debug("Successfully indexed " + labelCount + " labels on " + labelledClassCount + " classes and " + synonymCount + " synonyms on " + synonymedClassCount + " classes!");
    return ontology;
}
Also used : Logger(org.slf4j.Logger) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) Reasoner(org.semanticweb.HermiT.Reasoner) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) Set(java.util.Set) Collectors(java.util.stream.Collectors) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) ReasonerProgressMonitor(org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Component(org.springframework.stereotype.Component) IRI(org.semanticweb.owlapi.model.IRI) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) Optional(java.util.Optional) OWLClass(org.semanticweb.owlapi.model.OWLClass) IRI(org.semanticweb.owlapi.model.IRI) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory) Optional(java.util.Optional) ReasonerProgressMonitor(org.semanticweb.owlapi.reasoner.ReasonerProgressMonitor) OWLReasonerConfiguration(org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) SimpleConfiguration(org.semanticweb.owlapi.reasoner.SimpleConfiguration) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLReasonerFactory(org.semanticweb.owlapi.reasoner.OWLReasonerFactory)

Example 58 with OWLOntology

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

the class OntologyService method populateClassMaps.

private void populateClassMaps() {
    OWLOntology ontology = ontologyLoader.getOntology();
    for (OWLClass owlClass : ontology.getClassesInSignature()) {
        // check this isn't an obsolete class
        if (!isObsolete(ontology, obsoleteClass, owlClass)) {
            // get class names, and enter them in the maps
            List<String> classNames = getClassNames(owlClass);
            classToLabelMap.put(owlClass, classNames);
            for (String name : getClassNames(owlClass)) {
                if (name != null) {
                    name = normalizeSearchString(name);
                    if (!labelToClassMap.containsKey(name)) {
                        labelToClassMap.put(name, new HashSet<OWLClass>());
                    }
                    labelToClassMap.get(name).add(owlClass);
                }
            }
            // get IRI, and enter it into the map
            iriToClassMap.put(owlClass.getIRI(), owlClass);
        }
    }
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLClass(org.semanticweb.owlapi.model.OWLClass)

Example 59 with OWLOntology

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

the class ScopeSetRenderer method getScopes.

public static OWLOntology getScopes(Set<Scope> scopes) {
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        LoggerFactory.getLogger(ScopeSetRenderer.class).error("KReS :: could not create empty ontology for rendering scopes.", e);
        return null;
    }
    List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
    // The ODP metadata vocabulary is always imported.
    // TODO : also import the ONM meta when it goes online.
    additions.add(new AddImport(ont, __factory.getOWLImportsDeclaration(IRI.create("http://www.ontologydesignpatterns.org/schemas/meta.owl"))));
    for (Scope scope : scopes) {
        OWLNamedIndividual iScope = __factory.getOWLNamedIndividual(IRI.create(scope.getDefaultNamespace() + scope.getID()));
        OWLAxiom ax = __factory.getOWLClassAssertionAxiom(cScope, iScope);
        additions.add(new AddAxiom(ont, ax));
    }
    mgr.applyChanges(additions);
    return ont;
}
Also used : AddAxiom(org.semanticweb.owlapi.model.AddAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList)

Example 60 with OWLOntology

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

the class CustomSpaceImpl method getOntologyAsOWLOntology.

@Override
protected OWLOntology getOntologyAsOWLOntology(OWLOntologyID ontologyId, boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    OWLOntology o = super.getOntologyAsOWLOntology(ontologyId, merge, universalPrefix);
    switch(getConnectivityPolicy()) {
        case LOOSE:
            break;
        case TIGHT:
            String s = getID();
            // strip "custom"
            s = s.substring(0, s.indexOf(SUFFIX));
            // concatenate "core"
            s += SpaceType.CORE.getIRISuffix();
            org.semanticweb.owlapi.model.IRI target = org.semanticweb.owlapi.model.IRI.create(universalPrefix + s);
            o.getOWLOntologyManager().applyChange(new AddImport(o, OWLManager.getOWLDataFactory().getOWLImportsDeclaration(target)));
            break;
        default:
            break;
    }
    return o;
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) AddImport(org.semanticweb.owlapi.model.AddImport)

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