Search in sources :

Example 21 with OWLOntology

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

the class SessionResource method asOntologyOWL.

@GET
@Produces(value = { MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN })
public Response asOntologyOWL(@PathParam(value = "id") String sessionId, @PathParam("scopeid") String scopeid, @DefaultValue("false") @QueryParam("merge") boolean merge, @Context HttpHeaders headers) {
    session = sesMgr.getSession(sessionId);
    if (session == null)
        return Response.status(NOT_FOUND).build();
    IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/session/");
    // Export to OWLOntology, the only to support OWL formats.
    ResponseBuilder rb = Response.ok(session.export(OWLOntology.class, merge, prefix));
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with OWLOntology

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

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

the class TestOWLAPIInputSources method testOfflineImport.

/**
     * Uses a {@link ParentPathInputSource} to load an ontology importing a modified FOAF, both located in the
     * same resource directory.
     * 
     * @throws Exception
     */
@Test
public void testOfflineImport() throws Exception {
    URL url = getClass().getResource("/ontologies/maincharacters.owl");
    assertNotNull(url);
    File f = new File(url.toURI());
    assertNotNull(f);
    OntologyInputSource<OWLOntology> coreSource = new ParentPathInputSource(f);
    // // Check that all the imports closure is made of local files
    // Set<OWLOntology> closure = coreSource.getImports(true);
    // for (OWLOntology o : closure)
    // assertEquals("file", o.getOWLOntologyManager().getOntologyDocumentIRI(o).getScheme());
    assertEquals(coreSource.getRootOntology().getOntologyID().getOntologyIRI(), IRI.create(Constants.PEANUTS_MAIN_BASE));
    // Linus is stated to be a foaf:Person
    OWLNamedIndividual iLinus = df.getOWLNamedIndividual(IRI.create(Constants.PEANUTS_MAIN_BASE + "#Linus"));
    // Lucy is stated to be a foaf:Perzon
    OWLNamedIndividual iLucy = df.getOWLNamedIndividual(IRI.create(Constants.PEANUTS_MAIN_BASE + "#Lucy"));
    OWLClass cPerzon = df.getOWLClass(IRI.create("http://xmlns.com/foaf/0.1/Perzon"));
    Set<OWLIndividual> instances = cPerzon.getIndividuals(coreSource.getRootOntology());
    assertTrue(!instances.contains(iLinus) && instances.contains(iLucy));
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClass(org.semanticweb.owlapi.model.OWLClass) File(java.io.File) URL(java.net.URL) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) Test(org.junit.Test)

Example 24 with OWLOntology

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

the class TestOWLAPIInputSources method testOfflineSingleton.

/**
     * Loads a modified FOAF by resolving a URI from a resource directory.
     * 
     * @throws Exception
     */
@Test
public void testOfflineSingleton() throws Exception {
    URL url = getClass().getResource("/ontologies/mockfoaf.rdf");
    assertNotNull(url);
    OntologyInputSource<OWLOntology> coreSource = new RootOntologySource(IRI.create(url));
    assertNotNull(df);
    /*
         * To check it fetched the correct ontology, we look for a declaration of the bogus class foaf:Perzon
         * (added in the local FOAF)
         */
    OWLClass cPerzon = df.getOWLClass(IRI.create("http://xmlns.com/foaf/0.1/Perzon"));
    assertTrue(coreSource.getRootOntology().getClassesInSignature().contains(cPerzon));
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLClass(org.semanticweb.owlapi.model.OWLClass) URL(java.net.URL) Test(org.junit.Test)

Example 25 with OWLOntology

use of org.semanticweb.owlapi.model.OWLOntology 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)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)116 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)58 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)49 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)31 IRI (org.semanticweb.owlapi.model.IRI)30 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