Search in sources :

Example 11 with OWLOntologyCreationException

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

the class AbstractOWLApiReasoningService method isConsistent.

/**
     * Only check consistency.
     * 
     * Subclasses may want to change how.
     * 
     * @param ontology
     * @param rules
     * @return
     * @throws ReasoningServiceException
     */
@Override
public boolean isConsistent(OWLOntology ontology, List<SWRLRule> rules) throws ReasoningServiceException {
    log.debug("Create a input ontology to merge rules in.");
    OWLOntology input;
    try {
        OWLOntologyManager manager = createOWLOntologyManager();
        input = manager.createOntology();
        Set<SWRLRule> ruleSet = new HashSet<SWRLRule>();
        ruleSet.addAll(rules);
        manager.addAxioms(input, ruleSet);
        input = manager.getOntology(input.getOntologyID());
        log.debug("Created ontology: {}", input);
        return getReasoner(ontology).isConsistent();
    } catch (OWLOntologyCreationException e) {
        log.error("An error have been thrown while attempting to create ontology. Message was: {}", e.getLocalizedMessage());
        // TODO Add explanation of this exception
        throw new ReasoningServiceException();
    }
}
Also used : ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) HashSet(java.util.HashSet)

Example 12 with OWLOntologyCreationException

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

the class AbstractOntologyCollectorImpl method getOntologyAsOWLOntology.

protected OWLOntology getOntologyAsOWLOntology(OWLOntologyID ontologyId, boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // if (merge) throw new UnsupportedOperationException("Merge not implemented yet for OWLOntology.");
    // Remove the check below. It might be an unmanaged dependency (TODO remove from collector and
    // reintroduce check?).
    // if (!hasOntology(ontologyIri)) return null;
    OWLOntology o;
    o = ontologyProvider.getStoredOntology(ontologyId, OWLOntology.class, merge);
    if (merge) {
        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        log.debug("Merging {} with its imports, if any.", o);
        set.add(o);
        // Actually, if the provider already performed the merge, this won't happen
        for (OWLOntology impo : o.getImportsClosure()) {
            log.debug("Imported ontology {} will be merged with {}.", impo, o);
            set.add(impo);
        }
        OWLOntologySetProvider provider = new OWLOntologySetProvider() {

            @Override
            public Set<OWLOntology> getOntologies() {
                return set;
            }
        };
        OWLOntologyMerger merger = new OWLOntologyMerger(provider);
        try {
            o = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), ontologyId.getOntologyIRI());
        } catch (OWLOntologyCreationException e) {
            log.error("Failed to merge imports for ontology " + ontologyId, e);
        // do not reassign the root ontology
        }
    } else {
        // Rewrite import statements
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        OWLDataFactory df = OWLManager.getOWLDataFactory();
        /*
             * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
             * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering
             * imported ontologies as *not* managed.
             */
        for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
            changes.add(new RemoveImport(o, oldImp));
            String s = oldImp.getIRI().toString();
            // FIXME Ugly way to check, but we'll get through with it
            if (s.contains("::"))
                s = s.substring(s.indexOf("::") + 2, s.length());
            boolean managed = managedOntologies.contains(oldImp.getIRI());
            // For space, always go up at least one
            String tid = getID();
            if (backwardPathLength > 0)
                tid = tid.split("/")[0];
            org.semanticweb.owlapi.model.IRI target = org.semanticweb.owlapi.model.IRI.create((managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s);
            changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
        }
        o.getOWLOntologyManager().applyChanges(changes);
    }
    return o;
}
Also used : ArrayList(java.util.ArrayList) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) AddImport(org.semanticweb.owlapi.model.AddImport) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 13 with OWLOntologyCreationException

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

the class AbstractOntologyCollectorImpl method exportToOWLOntology.

/**
     * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
     * afterwards.
     * 
     * FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
     * 
     * @param merge
     * @return
     */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
    long before = System.currentTimeMillis();
    // Create a new ontology
    OWLOntology root;
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
    try {
        root = ontologyManager.createOntology(iri);
    } catch (OWLOntologyAlreadyExistsException e) {
        // It should be impossible, but just in case.
        ontologyManager.removeOntology(ontologyManager.getOntology(iri));
        try {
            root = ontologyManager.createOntology(iri);
        } catch (OWLOntologyAlreadyExistsException e1) {
            root = ontologyManager.getOntology(iri);
        } catch (OWLOntologyCreationException e1) {
            log.error("Failed to assemble root ontology for scope " + iri, e);
            root = null;
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to assemble root ontology for scope " + _id, e);
        root = null;
    }
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging {} with its imports.", root);
            set.add(root);
            for (OWLOntologyID ontologyId : managedOntologies) {
                log.debug("Merging {} with {}.", ontologyId, root);
                set.add(getOntology(ontologyId, OWLOntology.class, true));
            }
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology " + iri, e);
                root = null;
            }
        } else {
            // Add the import declarations for directly managed ontologies.
            List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
            OWLDataFactory df = ontologyManager.getOWLDataFactory();
            String base = prefix + getID();
            for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
            base += "/";
            // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
            for (OWLOntologyID ontologyId : managedOntologies) {
                // XXX some day the versionIRI will be the only physical reference for the ontology
                org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
                changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
            }
            ontologyManager.applyChanges(changes);
        }
    }
    log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
    return root;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 14 with OWLOntologyCreationException

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

the class ScopeImpl method exportToOWLOntology.

/**
     * Get an OWL API {@link OWLOntology} representation of the scope.
     * 
     * @param merge
     *            if true the core and custom spaces will be recursively merged with the scope ontology,
     *            otherwise owl:imports statements will be added.
     * @return the OWL representation of the scope.
     */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // if (merge) throw new UnsupportedOperationException(
    // "Ontology merging only implemented for managed ontologies, not for collectors. "
    // + "Please set merge parameter to false.");
    // Create an ontology manager on the fly. We don't really need a permanent one.
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = mgr.getOWLDataFactory();
    OWLOntology ont = null;
    try {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging custom space of {}.", getID());
            set.add(this.getCustomSpace().export(OWLOntology.class, merge));
            log.debug("Merging core space of {}.", getID());
            set.add(this.getCoreSpace().export(OWLOntology.class, merge));
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                ont = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), org.semanticweb.owlapi.model.IRI.create(getDefaultNamespace() + getID()));
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology.", e);
                ont = null;
            }
        } else {
            // The root ontology ID is in the form [namespace][scopeId]
            ont = mgr.createOntology(org.semanticweb.owlapi.model.IRI.create(universalPrefix + getID()));
            List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
            // Add the import statement for the custom space, if existing and not empty
            OntologySpace spc = getCustomSpace();
            if (spc != null && spc.listManagedOntologies().size() > 0) {
                org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
                additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
            }
            // Add the import statement for the core space, if existing and not empty
            spc = getCoreSpace();
            if (spc != null && spc.listManagedOntologies().size() > 0) {
                org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
                additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
            }
            mgr.applyChanges(additions);
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to generate an OWL form of scope " + getID(), e);
        ont = null;
    }
    return ont;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 15 with OWLOntologyCreationException

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

the class ScopeManagerImpl method activate.

/**
     * Called within both OSGi and non-OSGi environments.
     * 
     * @param configuration
     * @throws IOException
     */
protected void activate(Dictionary<String, Object> configuration) throws IOException {
    long before = System.currentTimeMillis();
    // Assign singleton instance
    me = this;
    // Parse configuration
    if (offline != null)
        ontonetNS = offline.getDefaultOntologyNetworkNamespace();
    scopeRegistryId = (String) configuration.get(ScopeManager.ID_SCOPE_REGISTRY);
    if (scopeRegistryId == null)
        scopeRegistryId = _ID_SCOPE_REGISTRY_DEFAULT;
    configPath = (String) configuration.get(ScopeManager.CONFIG_ONTOLOGY_PATH);
    if (configPath == null)
        configPath = _CONFIG_ONTOLOGY_PATH_DEFAULT;
    // Bind components, starting with the local directories.
    List<String> dirs = new ArrayList<String>();
    try {
        for (IRI iri : offline.getOntologySourceLocations()) dirs.add(iri.toString());
    } catch (NullPointerException ex) {
    // Ok, go empty
    }
    bindResources();
    // String tfile = (String) configuration.get(CONFIG_FILE_PATH);
    // if (tfile != null) this.configPath = tfile;
    // String tns = (String) configuration.get(KRES_NAMESPACE);
    // if (tns != null) this.kresNs = tns;
    // configPath = (String) configuration.get(CONFIG_FILE_PATH);
    // If there is no configuration file, just start with an empty scope set
    Object connectivityPolicy = configuration.get(ScopeManager.CONNECTIVITY_POLICY);
    if (connectivityPolicy == null) {
        this.connectivityPolicyString = _CONNECTIVITY_POLICY_DEFAULT;
    } else {
        this.connectivityPolicyString = connectivityPolicy.toString();
    }
    String configPath = getOntologyNetworkConfigurationPath();
    if (configPath != null && !configPath.trim().isEmpty()) {
        OWLOntology oConf = null;
        OWLOntologyManager tempMgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations().toArray(new IRI[0]));
        OWLOntologyDocumentSource oConfSrc = null;
        try {
            log.debug("Try to load the configuration ontology from a local bundle relative path");
            InputStream is = this.getClass().getResourceAsStream(configPath);
            oConfSrc = new StreamDocumentSource(is);
        } catch (Exception e1) {
            try {
                log.debug("Cannot load from a local bundle relative path", e1);
                log.debug("Try to load the configuration ontology resolving the given IRI");
                IRI iri = IRI.create(configPath);
                if (!iri.isAbsolute())
                    throw new Exception("IRI seems to be not absolute! value was: " + iri.toQuotedString());
                oConfSrc = new IRIDocumentSource(iri);
            } catch (Exception e) {
                try {
                    log.debug("Cannot load from the web", e1);
                    log.debug("Try to load the configuration ontology as full local file path");
                    oConfSrc = new FileDocumentSource(new File(configPath));
                } catch (Exception e2) {
                    log.error("Cannot load the configuration ontology from parameter value: " + configPath, e2);
                }
            }
        }
        if (oConfSrc == null) {
            log.warn("No ONM configuration file found at path " + configPath + ". Starting with blank scope set.");
        } else {
            try {
                oConf = tempMgr.loadOntologyFromOntologyDocument(oConfSrc);
            } catch (OWLOntologyCreationException e) {
                log.error("Cannot create the configuration ontology", e);
            }
        }
        // Create and populate the scopes from the config ontology.
        bootstrapOntologyNetwork(oConf);
    } else {
        // No ontology supplied. Access the local graph
        rebuildScopes();
    }
    log.debug(ScopeManager.class + " activated. Time : {} ms.", System.currentTimeMillis() - before);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IRIDocumentSource(org.semanticweb.owlapi.io.IRIDocumentSource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) NoSuchScopeException(org.apache.stanbol.ontologymanager.servicesapi.scope.NoSuchScopeException) MissingOntologyException(org.apache.stanbol.ontologymanager.servicesapi.collector.MissingOntologyException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException) StreamDocumentSource(org.semanticweb.owlapi.io.StreamDocumentSource) ScopeManager(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeManager) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) FileDocumentSource(org.semanticweb.owlapi.io.FileDocumentSource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLOntologyDocumentSource(org.semanticweb.owlapi.io.OWLOntologyDocumentSource) File(java.io.File)

Aggregations

OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)54 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)45 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)33 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)18 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)15 HashSet (java.util.HashSet)13 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)13 AddImport (org.semanticweb.owlapi.model.AddImport)12 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)11 ArrayList (java.util.ArrayList)10 OntModel (com.hp.hpl.jena.ontology.OntModel)9 WebApplicationException (javax.ws.rs.WebApplicationException)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)9 IRI (org.semanticweb.owlapi.model.IRI)9 OWLClass (org.semanticweb.owlapi.model.OWLClass)9 IOException (java.io.IOException)8 Consumes (javax.ws.rs.Consumes)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 POST (javax.ws.rs.POST)7