Search in sources :

Example 86 with OWLOntology

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

the class ConversionTester method testModelOwlToJenaConvert.

public void testModelOwlToJenaConvert() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntologyManager mgrf = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = mgrf.getOWLDataFactory();
    String dul = "http://www.loa-cnr.it/ontologies/DUL.owl";
    OWLOntology owl = null;
    OntModel jena = null;
    try {
        owl = mgr.loadOntologyFromOntologyDocument(IRI.create(dul));
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
        fail("Could not load ontology");
    }
    try {
        jena = j2o.ModelOwlToJenaConvert(owl, "RDF/XML");
        if (jena == null) {
            fail("Some errors occur");
        } else {
            ExtendedIterator<OntClass> jenaclass = jena.listNamedClasses();
            int jenaclassset = jenaclass.toSet().size();
            jenaclass = jena.listNamedClasses();
            Set<OWLClass> owlclass = owl.getClassesInSignature();
            int countclass = 0;
            while (jenaclass.hasNext()) if (owlclass.contains(factory.getOWLClass(IRI.create(jenaclass.next().getURI()))))
                countclass++;
            if (countclass == jenaclassset)
                assertEquals(countclass, jenaclassset);
            else
                fail("Error in number of classes");
            ExtendedIterator<ObjectProperty> jenaprop = jena.listObjectProperties();
            int jenapropset = jenaprop.toSet().size();
            jenaprop = jena.listObjectProperties();
            Set<OWLObjectProperty> owlprop = owl.getObjectPropertiesInSignature();
            int countprop = 0;
            while (jenaprop.hasNext()) if (owlprop.contains(factory.getOWLObjectProperty(IRI.create(jenaprop.next().getURI()))))
                countprop++;
            if (countprop == jenapropset)
                assertEquals(countprop, jenapropset);
            else
                fail("Error in number of object properties");
            ExtendedIterator<DatatypeProperty> jenadata = jena.listDatatypeProperties();
            int jenadataset = jenadata.toSet().size();
            jenadata = jena.listDatatypeProperties();
            Set<OWLDataProperty> owldata = owl.getDataPropertiesInSignature();
            int countdata = 0;
            while (jenadata.hasNext()) if (owldata.contains(factory.getOWLDataProperty(IRI.create(jenadata.next().getURI()))))
                countdata++;
            if (countdata == jenadataset)
                assertEquals(countdata, jenadataset);
            else
                fail("Error in number of data properties");
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(jena);
    }
}
Also used : OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) ObjectProperty(com.hp.hpl.jena.ontology.ObjectProperty) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OntClass(com.hp.hpl.jena.ontology.OntClass) DatatypeProperty(com.hp.hpl.jena.ontology.DatatypeProperty)

Example 87 with OWLOntology

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

the class JenaToOwlConvert method DataPropOwlToJena.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts a single OWLDataProperty of OWL to DatatypeProperty of Jena
     * 
     * @param data
     *            {An OWLDataProperty object}
     * @param format
     *            {RDF/XML or TURTLE}
     * @return {A DatatypeProperty object}
     */
public synchronized DatatypeProperty DataPropOwlToJena(OWLDataProperty data, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("DataPropOwlToJena::: " + e);
        }
    }
    available = false;
    try {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology(IRI.create("http://www.semanticweb.org/owlapi/ontologies/ontology"));
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(data);
        manager.addAxiom(ontology, declarationAxiom);
        OntModel jenamodel = ModelOwlToJenaConvert(ontology, format);
        available = true;
        notifyAll();
        return jenamodel.getDatatypeProperty(data.getIRI().toString());
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("DataPropOwlToJena::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 88 with OWLOntology

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

the class JenaToOwlConvert method ModelJenaToOwlConvert.

/**
     * This function converts an ontology object from Jena to OWLapi
     * 
     * @param jenamodel
     *            {An OntModel object}
     * @param format
     *            {only in "RDF/XML"}
     * @return {An OWLOntology that is an owl object}
     */
public synchronized OWLOntology ModelJenaToOwlConvert(IRI ontologyIRI, Model jenamodel, String format) {
    while (availablemain == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ModelJenaToOwlConvert::: " + e);
        }
    }
    availablemain = false;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (!format.equals("RDF/XML")) {
            System.err.println("The only format supported is RDF/XML. Please check the format!");
            availablemain = true;
            notifyAll();
            return null;
        } else {
            jenamodel.write(out, format);
            OWLOntologyManager owlmanager = OWLManager.createOWLOntologyManager();
            owlmanager.loadOntologyFromOntologyDocument(new ByteArrayInputStream(out.toByteArray()));
            OWLOntologyMerger merger = new OWLOntologyMerger(owlmanager);
            OWLOntology ontology = merger.createMergedOntology(owlmanager, ontologyIRI);
            availablemain = true;
            notifyAll();
            return ontology;
        }
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("ModelJenaToOwlConvert::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 89 with OWLOntology

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

the class JenaToOwlConvert method AnnotationPropJenaToOwl.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts a single AnnotationProperty of Jena to an OWLAnnotationProperty of OWL
     * 
     * @param jenadata
     *            {Jena DatatypeProperty object}
     * @param format
     *            {RDF/XML}
     * @return {An OWLAnnotationProperty object}
     */
public synchronized OWLAnnotationProperty AnnotationPropJenaToOwl(AnnotationProperty jenadata, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("AnnotationPropJenaToOwl::: " + e);
        }
    }
    available = false;
    try {
        OntModel model = ModelFactory.createOntologyModel();
        model.createAnnotationProperty(jenadata.getURI());
        OWLOntology owlmodel = ModelJenaToOwlConvert(model, format);
        available = true;
        notifyAll();
        return owlmodel.getAnnotationPropertiesInSignature().iterator().next();
    } catch (Exception e) {
        System.err.print("AnnotationPropJenaToOwl::: ");
        e.printStackTrace();
        return null;
    }
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 90 with OWLOntology

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

the class JenaToOwlConvert method ClassOwlToJena.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts a single OWLClass of OWLAPI to an OntClass of Jena
     * 
     * @param data
     *            {An OWLClass}
     * @param format
     *            {RDF/XML or TURTLE}
     * @return {An OntClass}
     */
public synchronized OntClass ClassOwlToJena(OWLClass data, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ClassOwlToJena::: " + e);
        }
    }
    available = false;
    try {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology(IRI.create("http://www.semanticweb.org/owlapi/ontologies/ontology"));
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLDeclarationAxiom declarationAxiom = factory.getOWLDeclarationAxiom(data);
        manager.addAxiom(ontology, declarationAxiom);
        OntModel jenamodel = ModelOwlToJenaConvert(ontology, format);
        available = true;
        notifyAll();
        return jenamodel.getOntClass(data.getIRI().toString());
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("ClassOwlToJena::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Aggregations

OWLOntology (org.semanticweb.owlapi.model.OWLOntology)118 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)59 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)50 IRI (org.semanticweb.owlapi.model.IRI)31 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)31 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