Search in sources :

Example 21 with OWLClass

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

the class SparqlPussycatSession method getRelatedTraits.

@Override
public Set<URI> getRelatedTraits(String traitName) {
    // get OWLClasses by name
    Collection<OWLClass> traitClasses = getOntologyService().getOWLClassesByLabel(traitName);
    Set<URI> results = new HashSet<URI>();
    // check reasoner
    OWLReasoner reasoner = getOntologyService().getOntologyLoader().getOWLReasoner();
    for (OWLClass traitClass : traitClasses) {
        results.add(traitClass.getIRI().toURI());
        Set<OWLClass> subclasses = reasoner.getSubClasses(traitClass, false).getFlattened();
        for (OWLClass subclass : subclasses) {
            results.add(subclass.getIRI().toURI());
        }
    }
    return results;
}
Also used : OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) URI(java.net.URI) HashSet(java.util.HashSet)

Example 22 with OWLClass

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

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

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

the class AbstractOntologyLoader method removeExcludedClasses.

protected Set<OWLClass> removeExcludedClasses(OWLOntology ontology, Set<OWLClass> allClasses, SubclassCollector subclassCollector) {
    // remove excluded classes from allClasses by subclass
    if (getExclusionClassURI() != null) {
        OWLClass excludeClass = getFactory().getOWLClass(IRI.create(getExclusionClassURI()));
        subclassCollector.collect(excludeClass).forEach(allClasses::remove);
    }
    // remove excluded classes from allClasses by annotation property
    if (getExclusionAnnotationURI() != null) {
        OWLAnnotationProperty excludeAnnotation = getFactory().getOWLAnnotationProperty(IRI.create(getExclusionAnnotationURI()));
        Iterator<OWLClass> allClassesIt = allClasses.iterator();
        while (allClassesIt.hasNext()) {
            OWLClass owlClass = allClassesIt.next();
            Collection<OWLAnnotation> annotations = owlClass.getAnnotations(ontology, excludeAnnotation);
            if (!annotations.isEmpty()) {
                allClassesIt.remove();
            }
        }
    }
    // and return
    return allClasses;
}
Also used : OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) OWLClass(org.semanticweb.owlapi.model.OWLClass)

Example 25 with OWLClass

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

the class ConversionTester method testEntityOwlToJenaResource.

public void testEntityOwlToJenaResource() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    StmtIterator resource = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
        fail("Could not load ontology");
    }
    OWLDataFactory factory = mgr.getOWLDataFactory();
    OWLClass cls = factory.getOWLClass(IRI.create(CLAZZ));
    OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(DP));
    OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(OP));
    OWLAnnotationProperty oa = factory.getOWLAnnotationProperty(IRI.create(label));
    OWLAnnotation oav = factory.getOWLAnnotation(oa, factory.getOWLStringLiteral(clazzlabel, "en"));
    OWLDatatype dt = factory.getOWLDatatype(IRI.create(DATATYPE));
    OWLNamedIndividual sub = factory.getOWLNamedIndividual(IRI.create(SUBJECT));
    OWLNamedIndividual obj = factory.getOWLNamedIndividual(IRI.create(OBJECT));
    OWLLiteral literal1 = factory.getOWLTypedLiteral(VALUE, dt);
    // Classe
    OWLDeclarationAxiom daxiomcls = factory.getOWLDeclarationAxiom(cls);
    // obj prop
    OWLDeclarationAxiom daxiomop = factory.getOWLDeclarationAxiom(op);
    // data prop
    OWLDeclarationAxiom daxiomdp = factory.getOWLDeclarationAxiom(dp);
    // subject
    OWLDeclarationAxiom daxiomsub = factory.getOWLDeclarationAxiom(sub);
    // object
    OWLDeclarationAxiom daxiomobj = factory.getOWLDeclarationAxiom(obj);
    // Istanza
    OWLClassAssertionAxiom axiomsub = factory.getOWLClassAssertionAxiom(cls, sub);
    // Istanza
    OWLClassAssertionAxiom axiomobj = factory.getOWLClassAssertionAxiom(cls, obj);
    // Obj
    OWLObjectPropertyAssertionAxiom axiomop = factory.getOWLObjectPropertyAssertionAxiom(op, sub, obj);
    // prop
    // tra
    // individui
    OWLDataPropertyAssertionAxiom axiomvalue = factory.getOWLDataPropertyAssertionAxiom(dp, sub, // Dataprop all'istanza;
    literal1);
    // Annotazione
    OWLAnnotationAssertionAxiom axioman = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), oav);
    mgr.addAxiom(ont, daxiomcls);
    mgr.addAxiom(ont, daxiomop);
    mgr.addAxiom(ont, daxiomdp);
    mgr.addAxiom(ont, daxiomsub);
    mgr.addAxiom(ont, daxiomobj);
    mgr.addAxiom(ont, axiomsub);
    mgr.addAxiom(ont, axiomobj);
    mgr.addAxiom(ont, axiomop);
    mgr.addAxiom(ont, axiomvalue);
    mgr.addAxiom(ont, axioman);
    Set<OWLIndividualAxiom> ind = ont.getAxioms(sub);
    try {
        resource = j2o.EntityOwlToJenaResource(daxiomsub.getEntity(), ont, RDFXML);
        if (resource == null) {
            fail("Some errors accour");
        } else {
            int cont = 0;
            while (resource.hasNext()) {
                Statement stm = resource.nextStatement();
                IRI subres = IRI.create(stm.getSubject().getURI());
                if (("<" + subres + ">").equals(daxiomsub.getEntity().toString()))
                    cont++;
            }
            assertEquals(ind.size(), (cont - 1));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(resource);
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLDataPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) Statement(com.hp.hpl.jena.rdf.model.Statement) OWLIndividualAxiom(org.semanticweb.owlapi.model.OWLIndividualAxiom) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Aggregations

OWLClass (org.semanticweb.owlapi.model.OWLClass)30 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)14 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)13 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)13 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)13 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)11 IRI (org.semanticweb.owlapi.model.IRI)10 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)10 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)9 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)8 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)6 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)6 AddAxiom (org.semanticweb.owlapi.model.AddAxiom)5 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)5 OWLDataPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom)5 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)5 OWLObjectPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom)5 OntClass (com.hp.hpl.jena.ontology.OntClass)4 OntModel (com.hp.hpl.jena.ontology.OntModel)4 OWLAnnotation (org.semanticweb.owlapi.model.OWLAnnotation)4