Search in sources :

Example 41 with OWLDataFactory

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

the class OWLAPIToClerezzaConverterTest method setupClass.

@BeforeClass
public static void setupClass() {
    /*
         * Set-up the OWL ontology for the test. Simply add the axioms: AndreaNuzzolese isA Person -> class
         * assertion axiom EnricoDaga isA Person -> class assertion axiom AndreaNuzzolese knows EnricoDaga ->
         * object property assertion axiom
         */
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = manager.getOWLDataFactory();
    try {
        ontology = manager.createOntology(org.semanticweb.owlapi.model.IRI.create(ns + "testOntology"));
    } catch (OWLOntologyCreationException e) {
        log.error(e.getMessage());
    }
    if (ontology != null) {
        OWLClass personClass = factory.getOWLClass(org.semanticweb.owlapi.model.IRI.create(foaf + "Person"));
        OWLNamedIndividual andreaNuzzoleseOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "AndreaNuzzolese"));
        OWLNamedIndividual enricoDagaOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "EnricoDaga"));
        OWLObjectProperty knowsOWL = factory.getOWLObjectProperty(org.semanticweb.owlapi.model.IRI.create(foaf + "knows"));
        OWLAxiom axiom = factory.getOWLClassAssertionAxiom(personClass, andreaNuzzoleseOWL);
        manager.addAxiom(ontology, axiom);
        axiom = factory.getOWLClassAssertionAxiom(personClass, enricoDagaOWL);
        manager.addAxiom(ontology, axiom);
        axiom = factory.getOWLObjectPropertyAssertionAxiom(knowsOWL, andreaNuzzoleseOWL, enricoDagaOWL);
        manager.addAxiom(ontology, axiom);
    }
    /*
         * Set-up the Clerezza model for the test. As before simply add the triples: AndreaNuzzolese isA
         * Person EnricoDaga isA Person AndreaNuzzolese knows EnricoDaga
         */
    mGraph = new SimpleGraph();
    IRI knowsInClerezza = new IRI(ns + "knows");
    IRI rdfType = new IRI(RDF.getURI() + "type");
    IRI foafPersonInClerezza = new IRI(foaf + "Person");
    BlankNodeOrIRI andreaNuzzoleseInClerezza = new IRI(ns + "AndreaNuzzolese");
    BlankNodeOrIRI enricoDagaInClerezza = new IRI(ns + "EnricoDaga");
    Triple triple = new TripleImpl(andreaNuzzoleseInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(enricoDagaInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(andreaNuzzoleseInClerezza, knowsInClerezza, enricoDagaInClerezza);
    mGraph.add(triple);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) BeforeClass(org.junit.BeforeClass)

Example 42 with OWLDataFactory

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

Example 43 with OWLDataFactory

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

the class OntologyImportUtils method buildImportTree.

/**
     * Non-recursively adds import statements to the root ontology so that it is directly linked to all the
     * ontologies in the subtrees set.
     * 
     * @param parent
     *            the ontology to which import subtrees should be appended. If null, a runtime exception will
     *            be thrown.
     * @param subtrees
     *            the set of target ontologies for import statements. These can in turn be importing other
     *            ontologies, hence the &quot;subtree&quot; notation. A single statement will be added for
     *            each member of this set.
     * @param mgr
     *            the OWL ontology manager to use for constructing the import tree. If null, an internal one
     *            will be used instead, otherwise an existing ontology manager can be used e.g. for extracting
     *            import statements from its IRI mappers or known ontologies. Note that the supplied manager
     *            will <i>never</i> try to load any ontologies, even when they are unknown.
     * @return the same input ontology as defined in <code>root</code>, but with the added import statements.
     */
public static OWLOntology buildImportTree(OWLOntology parent, Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
    if (parent == null)
        throw new NullPointerException("Cannot append import trees to a nonexistent ontology.");
    // If no manager was supplied, use a temporary one.
    if (mgr == null)
        mgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory owlFactory = mgr.getOWLDataFactory();
    List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
    for (OWLOntology o : subtrees) {
        IRI importIri = null;
        try {
            /*
                 * First query the manager, as it could know the physical location of anonymous ontologies, if
                 * previously loaded or IRI-mapped.
                 */
            importIri = mgr.getOntologyDocumentIRI(o);
        } catch (UnknownOWLOntologyException ex) {
            /*
                 * Otherwise, ask the ontology itself (the location of an anonymous ontology may have been
                 * known at creation/loading time, even if another manager built it.)
                 */
            importIri = o.getOntologyID().getDefaultDocumentIRI();
        } catch (Exception ex) {
            logger.error("Exception caught during tree building. Skipping import of ontology " + o.getOntologyID(), ex);
        } finally {
            /*
                 * It is still possible that an imported ontology is anonymous but has no physical document
                 * IRI (for example, because it was only generated in-memory but not stored). In this case it
                 * is necessary (and generally safe) to copy all its axioms and import statements to the
                 * parent ontology, or else it is lost.
                 */
            if (o.isAnonymous() && importIri == null) {
                logger.warn("Anonymous import target " + o.getOntologyID() + " not mapped to physical IRI. Will add extracted axioms to parent ontology.");
                for (OWLImportsDeclaration im : o.getImportsDeclarations()) changes.add(new AddImport(parent, im));
                for (OWLAxiom im : o.getAxioms()) changes.add(new AddAxiom(parent, im));
            } else if (importIri != null) {
                // An anonymous ontology can still be imported if it has a
                // valid document IRI.
                changes.add(new AddImport(parent, owlFactory.getOWLImportsDeclaration(importIri)));
            }
        }
    }
    // apply the changes one by one, just in case.
    for (OWLOntologyChange im : changes) try {
        mgr.applyChange(im);
    } catch (Exception ex) {
        logger.error("KReS :: Exception caught during tree building. Skipping import", ex);
        continue;
    }
    return parent;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) UnknownOWLOntologyException(org.semanticweb.owlapi.model.UnknownOWLOntologyException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 44 with OWLDataFactory

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

the class DatavaluedPropertyAtom method getOWLTypedLiteral.

private OWLLiteral getOWLTypedLiteral(Object argument) {
    OWLDataFactory factory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
    OWLLiteral owlLiteral;
    if (argument instanceof String) {
        owlLiteral = factory.getOWLTypedLiteral((String) argument);
    } else if (argument instanceof Integer) {
        owlLiteral = factory.getOWLTypedLiteral(((Integer) argument).intValue());
    } else if (argument instanceof Double) {
        owlLiteral = factory.getOWLTypedLiteral(((Double) argument).doubleValue());
    } else if (argument instanceof Float) {
        owlLiteral = factory.getOWLTypedLiteral(((Float) argument).floatValue());
    } else if (argument instanceof Boolean) {
        owlLiteral = factory.getOWLTypedLiteral(((Boolean) argument).booleanValue());
    } else {
        owlLiteral = factory.getOWLStringLiteral(argument.toString());
    }
    return owlLiteral;
}
Also used : OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 45 with OWLDataFactory

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

the class GreaterEqualThanAtom method getOWLTypedLiteral.

private OWLLiteral getOWLTypedLiteral(Object argument) {
    OWLDataFactory factory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
    OWLLiteral owlLiteral;
    if (argument instanceof String) {
        owlLiteral = factory.getOWLTypedLiteral((String) argument);
    } else if (argument instanceof Integer) {
        owlLiteral = factory.getOWLTypedLiteral(((Integer) argument).intValue());
    } else if (argument instanceof Double) {
        owlLiteral = factory.getOWLTypedLiteral(((Double) argument).doubleValue());
    } else if (argument instanceof Float) {
        owlLiteral = factory.getOWLTypedLiteral(((Float) argument).floatValue());
    } else if (argument instanceof Boolean) {
        owlLiteral = factory.getOWLTypedLiteral(((Boolean) argument).booleanValue());
    } else {
        owlLiteral = factory.getOWLStringLiteral(argument.toString());
    }
    return owlLiteral;
}
Also used : OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Aggregations

OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)58 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)24 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)23 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)21 ArgumentSWRLAtom (org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom)18 ArrayList (java.util.ArrayList)16 SWRLAtom (org.semanticweb.owlapi.model.SWRLAtom)16 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)15 SWRLDArgument (org.semanticweb.owlapi.model.SWRLDArgument)15 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)15 HigherOrderSWRLAtom (org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom)12 OWLClass (org.semanticweb.owlapi.model.OWLClass)12 SWRLArgument (org.semanticweb.owlapi.model.SWRLArgument)12 IRI (org.semanticweb.owlapi.model.IRI)11 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)11 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)10 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)10 HashSet (java.util.HashSet)9 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)9 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)9