Search in sources :

Example 1 with OWLObjectPropertyExpression

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

the class RegistryManagerImpl method createModel.

@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
    Set<Registry> results = new HashSet<Registry>();
    // Reset population
    population.clear();
    // Build the transitive imports closure of the union.
    Set<OWLOntology> closure = new HashSet<OWLOntology>();
    for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
    /*
         * For each value in this map, index 0 is the score of the library class, while 1 is the score of the
         * ontology class.
         */
    final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
    /*
         * Scans class assertions and object property values and tries to determine the type of each
         * individual it finds.
         */
    OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {

        /*
             * For a given identifier, returns the array of integers whose value determine the likelihood if
             * the corresponding entity being a library or an ontology. If no such array exists, it is
             * created.
             */
        private int[] checkScores(IRI key) {
            int[] scores;
            if (candidateTypes.containsKey(key))
                scores = candidateTypes.get(key);
            else {
                scores = new int[] { 0, 0 };
                candidateTypes.put(key, scores);
            }
            return scores;
        }

        @Override
        public void visit(OWLAnnotationAssertionAxiom axiom) {
            /*
                 * Works like object property assertions, in case hasOntology and isOntologyOf are not
                 * detected to be object properties (e.g. due to a failure to load codolight or the registry
                 * metamodel).
                 */
            OWLAnnotationProperty prop = axiom.getProperty();
            if (hasOntologyAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOfAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
            }
        }

        @Override
        public void visit(OWLClassAssertionAxiom axiom) {
            OWLIndividual ind = axiom.getIndividual();
            // Do not accept anonymous registry items.
            if (ind.isAnonymous())
                return;
            IRI iri = ind.asOWLNamedIndividual().getIRI();
            int[] scores = checkScores(iri);
            OWLClassExpression type = axiom.getClassExpression();
            // If the type is stated to be a library, increase its library score.
            if (cRegistryLibrary.equals(type)) {
                scores[0]++;
            } else // If the type is stated to be an ontology, increase its ontology score.
            if (cOntology.equals(type)) {
                scores[1]++;
            }
        }

        @Override
        public void visit(OWLObjectPropertyAssertionAxiom axiom) {
            OWLObjectPropertyExpression prop = axiom.getProperty();
            if (hasOntology.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOf.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
            }
        }
    };
    // First pass to determine the types.
    for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
    // Then populate on the registry
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (IRI iri : candidateTypes.keySet()) {
        int[] scores = candidateTypes.get(iri);
        if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
            if (scores[0] > 0 && scores[1] == 0)
                population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
            else if (scores[0] == 0 && scores[1] > 0)
                population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
        }
    // else log.warn("Unable to determine type for registry item {}", iri);
    }
    for (OWLOntology oReg : registryOntologies) {
        try {
            results.add(populateRegistry(oReg));
        } catch (RegistryContentException e) {
            log.error("An error occurred while populating an ontology registry.", e);
        }
    }
    return results;
}
Also used : OWLObject(org.semanticweb.owlapi.model.OWLObject) IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) HashMap(java.util.HashMap) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 2 with OWLObjectPropertyExpression

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

the class RegistryUtils method getType.

@Deprecated
public static Type getType(final OWLIndividual ind, Set<OWLOntology> ontologies) {
    // 0 is for library, 1 is for ontology (more in the future?)
    final int[] pointsFor = new int[] { 0, 0 };
    final int[] pointsAgainst = new int[] { 0, 0 };
    OWLAxiomVisitor v = new OWLAxiomVisitorAdapter() {

        @Override
        public void visit(OWLClassAssertionAxiom axiom) {
            if (ind.equals(axiom.getIndividual())) {
                OWLClassExpression type = axiom.getClassExpression();
                if (cRegistryLibrary.equals(type)) {
                    pointsFor[0]++;
                    pointsAgainst[1]++;
                } else if (cOntology.equals(type)) {
                    pointsFor[1]++;
                    pointsAgainst[0]++;
                }
            }
        }

        @Override
        public void visit(OWLObjectPropertyAssertionAxiom axiom) {
            OWLObjectPropertyExpression prop = axiom.getProperty();
            if (ind.equals(axiom.getSubject())) {
                if (hasOntology.equals(prop)) {
                    pointsFor[0]++;
                    pointsAgainst[1]++;
                } else if (isOntologyOf.equals(prop)) {
                    pointsFor[1]++;
                    pointsAgainst[0]++;
                }
            } else if (ind.equals(axiom.getObject())) {
                if (isOntologyOf.equals(prop)) {
                    pointsFor[0]++;
                    pointsAgainst[1]++;
                } else if (hasOntology.equals(prop)) {
                    pointsFor[1]++;
                    pointsAgainst[0]++;
                }
            }
        }
    };
    // TODO use this strategy in the single pass algorithm for constructing the model.
    for (OWLOntology o : ontologies) for (OWLAxiom ax : o.getAxioms()) ax.accept(v);
    if (pointsFor[0] > 0 && pointsAgainst[0] == 0)
        return Type.LIBRARY;
    if (pointsFor[1] > 0 && pointsAgainst[1] == 0)
        return Type.ONTOLOGY;
    // Cannot determine registries, since they have no associated individual.
    return null;
}
Also used : OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter)

Aggregations

OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)2 OWLAxiomVisitor (org.semanticweb.owlapi.model.OWLAxiomVisitor)2 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)2 OWLClassExpression (org.semanticweb.owlapi.model.OWLClassExpression)2 OWLObjectPropertyAssertionAxiom (org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom)2 OWLObjectPropertyExpression (org.semanticweb.owlapi.model.OWLObjectPropertyExpression)2 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)2 OWLAxiomVisitorAdapter (org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)1 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)1 IRI (org.semanticweb.owlapi.model.IRI)1 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)1 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)1 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)1 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)1 OWLObject (org.semanticweb.owlapi.model.OWLObject)1