Search in sources :

Example 1 with RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException 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 RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException in project stanbol by apache.

the class RegistryManagerImpl method populateOntology.

protected RegistryOntology populateOntology(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
    IRI ontId = ind.getIRI();
    RegistryItem ront = null;
    if (population.containsKey(ontId)) {
        // We are not allowing multityping either.
        ront = population.get(ontId);
        if (!(ront instanceof RegistryOntology))
            throw new RegistryContentException("Inconsistent multityping: for item " + ontId + " : {" + RegistryOntology.class + ", " + ront.getClass() + "}");
    } else {
        ront = riFactory.createRegistryOntology(ind);
        try {
            population.put(ront.getIRI(), ront);
        } catch (Exception e) {
            log.error("Invalid identifier for library item " + ront, e);
            return null;
        }
    }
    // EXIT nodes.
    Set<OWLNamedObject> libs = new HashSet<OWLNamedObject>();
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (OWLOntology o : registries) {
        if (ind instanceof OWLIndividual) {
            // Get usages of isOntologyOf as an object property
            for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(isOntologyOf, o)) if (value.isNamed())
                libs.add(value.asOWLNamedIndividual());
            // Get usages of isOntologyOf as an annotation property
            for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (isOntologyOfAnn.equals(ann.getProperty())) {
                OWLAnnotationValue value = ann.getValue();
                if (value instanceof OWLNamedObject)
                    libs.add((OWLNamedObject) value);
                else if (value instanceof IRI)
                    libs.add(df.getOWLNamedIndividual((IRI) value));
            }
        }
    }
    for (OWLNamedObject ilib : libs) {
        IRI parentId = ilib.getIRI();
        // If some populate*() method has created it, it will be there.
        RegistryItem rlib = population.get(parentId);
        // Otherwise populating it will also put it in population.
        if (rlib == null)
            rlib = populateLibrary(ilib, registries);
        ront.addParent(rlib);
        if (ontologyIndex.get(ontId) == null)
            ontologyIndex.put(ontId, new HashSet<IRI>());
        ontologyIndex.get(ontId).add(parentId);
    }
    return (RegistryOntology) ront;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) RegistryOntology(org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLAnnotationValue(org.semanticweb.owlapi.model.OWLAnnotationValue) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 3 with RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException in project stanbol by apache.

the class RegistryManagerImpl method populateRegistry.

protected Registry populateRegistry(OWLOntology registry) throws RegistryContentException {
    log.debug("Populating registry content from ontology {}", registry);
    Registry reg = riFactory.createRegistry(registry);
    Set<OWLOntology> closure = registry.getOWLOntologyManager().getImportsClosure(registry);
    // Just scan all individuals. Recurse in case the registry imports more registries.
    for (OWLIndividual ind : registry.getIndividualsInSignature(true)) {
        // We do not allow anonymous registry items.
        if (ind.isAnonymous())
            continue;
        RegistryItem item = null;
        // IRI id = ind.asOWLNamedIndividual().getIRI();
        Type t = RegistryUtils.getType(ind, closure);
        if (t == null) {
            log.warn("Undetermined type for registry ontology individual {}", ind);
            continue;
        }
        switch(t) {
            case LIBRARY:
                log.debug("Found library for individual {}", ind);
                // Create the library and attach to parent and children
                item = populateLibrary(ind.asOWLNamedIndividual(), closure);
                reg.addChild(item);
                item.addRegistryContentListener(this);
                break;
            case ONTOLOGY:
                log.debug("Found ontology for individual {}", ind);
                // Create the ontology and attach to parent
                item = populateOntology(ind.asOWLNamedIndividual(), closure);
                item.addRegistryContentListener(this);
                // We don't know where to attach it within this method.
                break;
            default:
                break;
        }
    }
    try {
        reg.addRegistryContentListener(this);
        log.info("Registry {} added.", reg.getIRI());
        population.put(reg.getIRI(), reg);
    } catch (Exception e) {
        log.error("Invalid identifier for library item " + reg, e);
        return null;
    }
    return reg;
}
Also used : Type(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem.Type) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 4 with RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException in project stanbol by apache.

the class RegistryManagerImpl method populateLibrary.

protected Library populateLibrary(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
    IRI libId = ind.getIRI();
    RegistryItem lib = null;
    if (population.containsKey(libId)) {
        // We are not allowing multityping either.
        lib = population.get(libId);
        if (!(lib instanceof Library))
            throw new RegistryContentException("Inconsistent multityping: for item " + libId + " : {" + Library.class + ", " + lib.getClass() + "}");
    } else {
        lib = riFactory.createLibrary(ind);
        try {
            population.put(lib.getIRI(), lib);
        } catch (Exception e) {
            log.error("Invalid identifier for library item " + lib, e);
            return null;
        }
    }
    // EXIT nodes.
    Set<OWLNamedObject> ironts = new HashSet<OWLNamedObject>();
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (OWLOntology o : registries) {
        if (ind instanceof OWLIndividual) {
            // Get usages of hasOntology as an object property
            for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(hasOntology, o)) if (value.isNamed())
                ironts.add(value.asOWLNamedIndividual());
            // Get usages of hasOntology as an annotation property
            for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (hasOntologyAnn.equals(ann.getProperty())) {
                OWLAnnotationValue value = ann.getValue();
                if (value instanceof OWLNamedObject)
                    ironts.add((OWLNamedObject) value);
                else if (value instanceof IRI)
                    ironts.add(df.getOWLNamedIndividual((IRI) value));
            }
        }
    }
    for (OWLNamedObject iront : ironts) {
        IRI childId = iront.getIRI();
        // If some populate*() method has created it, it will be there.
        RegistryItem ront = population.get(childId);
        // Otherwise populating it will also put it in population.
        if (ront == null)
            ront = populateOntology(iront, registries);
        lib.addChild(ront);
        if (ontologyIndex.get(childId) == null)
            ontologyIndex.put(childId, new HashSet<IRI>());
        ontologyIndex.get(childId).add(libId);
    }
    return (Library) lib;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLAnnotationValue(org.semanticweb.owlapi.model.OWLAnnotationValue) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 5 with RegistryContentException

use of org.apache.stanbol.ontologymanager.registry.api.RegistryContentException in project stanbol by apache.

the class RootResource method getOWLOntology.

private OWLOntology getOWLOntology(String ontologyId, boolean merge, URI requestUri) {
    long before = System.currentTimeMillis();
    IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
    log.debug("Will try to retrieve ontology {} from provider.", iri);
    // TODO be selective: if the ontology is small enough, use OWLOntology otherwise export to ImmutableGraph.
    OWLOntology o = null;
    try {
        // XXX Guarantee that there MUST always be an entry for any decoded ontology ID submitted.
        OWLOntologyID id = OntologyUtils.decode(ontologyId);
        o = ontologyProvider.getStoredOntology(id, OWLOntology.class, merge);
    } catch (Exception ex) {
        log.warn("Retrieval of ontology with ID " + iri + " failed.", ex);
    }
    if (o == null) {
        log.debug("Ontology {} missing from provider. Trying libraries...", iri);
        // See if we can touch a library. TODO: replace with event model on the ontology provider.
        int minSize = -1;
        IRI smallest = null;
        for (Library lib : registryManager.getLibraries(iri)) {
            int size = lib.getChildren().length;
            if (minSize < 1 || size < minSize) {
                smallest = lib.getIRI();
                minSize = size;
            }
        }
        if (smallest != null) {
            log.debug("Selected library for ontology {} is {} .", iri, smallest);
            try {
                o = registryManager.getLibrary(smallest).getOntology(iri, OWLOntology.class);
            } catch (RegistryContentException e) {
                log.warn("The content of library " + smallest + " could not be accessed.", e);
            }
        }
    }
    if (o == null) {
        log.debug("Ontology {} not found in any ontology provider or library.", iri);
        return null;
    }
    log.debug("Retrieved ontology {} .", iri);
    // Rewrite import statements - no ontology collector to do it for us here.
    URI base = URI.create(getPublicBaseUri() + "ontonet/");
    List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
    OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
    // TODO manage import rewrites better once the container ID is fully configurable.
    for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
        changes.add(new RemoveImport(o, oldImp));
        String s = oldImp.getIRI().toString();
        if (s.contains("::")) {
            s = s.substring(s.indexOf("::") + 2, s.length());
        }
        IRI target = IRI.create(base + s);
        changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
    }
    // Versioning.
    OWLOntologyID id = o.getOntologyID();
    if (!id.isAnonymous() && id.getVersionIRI() == null) {
        IRI viri = IRI.create(requestUri);
        log.debug("Setting version IRI for export : {}", viri);
        changes.add(new SetOntologyID(o, new OWLOntologyID(id.getOntologyIRI(), viri)));
    }
    o.getOWLOntologyManager().applyChanges(changes);
    log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
    return o;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ArrayList(java.util.ArrayList) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) URI(java.net.URI) AddImport(org.semanticweb.owlapi.model.AddImport) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) SetOntologyID(org.semanticweb.owlapi.model.SetOntologyID) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Aggregations

RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)6 IRI (org.semanticweb.owlapi.model.IRI)5 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)5 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)5 HashSet (java.util.HashSet)4 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)4 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)4 Library (org.apache.stanbol.ontologymanager.registry.api.model.Library)3 RegistryItem (org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem)3 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)3 OWLOntologyAlreadyExistsException (org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 UnsupportedFormatException (org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException)2 Registry (org.apache.stanbol.ontologymanager.registry.api.model.Registry)2 OntologyHandleException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException)2 OntologyLoadingException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException)2 OrphanOntologyKeyException (org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException)2