Search in sources :

Example 16 with OntModel

use of com.hp.hpl.jena.ontology.OntModel in project stanbol by apache.

the class JenaToOwlConvert method ObjPropJenaToOwl.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts a single ObjectProperty of Jena to an OWLObjectProperty of OWLAPI
     * 
     * @param jenadata
     *            {Jena ObjectProperty object}
     * @param format
     *            {RDF/XML}
     * @return {An OWLObjectProperty}
     */
public synchronized OWLObjectProperty ObjPropJenaToOwl(ObjectProperty jenadata, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ClassOwlToJena::: " + e);
        }
    }
    available = false;
    try {
        OntModel model = ModelFactory.createOntologyModel();
        model.createObjectProperty(jenadata.getURI());
        OWLOntology owlmodel = ModelJenaToOwlConvert(model, format);
        available = true;
        notifyAll();
        return owlmodel.getObjectPropertiesInSignature().iterator().next();
    } catch (Exception e) {
        System.err.print("ClassOwlToJena::: ");
        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 17 with OntModel

use of com.hp.hpl.jena.ontology.OntModel in project stanbol by apache.

the class ConversionTester method testDataPropJenaToOwl.

public void testDataPropJenaToOwl() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OntModel model = ModelFactory.createOntologyModel();
    DatatypeProperty jp = model.createDatatypeProperty(DP.toString());
    OWLDataProperty wp = null;
    try {
        wp = j2o.DataPropJenaToOwl(jp, RDFXML);
        if (wp == null) {
            fail("Some problem accours");
        } else {
            assertEquals(wp.getIRI().toURI().toString(), jp.getURI());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(wp);
    }
}
Also used : OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OntModel(com.hp.hpl.jena.ontology.OntModel) DatatypeProperty(com.hp.hpl.jena.ontology.DatatypeProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException)

Example 18 with OntModel

use of com.hp.hpl.jena.ontology.OntModel in project stanbol by apache.

the class UrlInputProvider method getInput.

@Override
public <T> Iterator<T> getInput(Class<T> type) throws IOException {
    if (type.isAssignableFrom(OWLAxiom.class)) {
        // We add additional axioms
        OWLOntology fromUrl;
        try {
            fromUrl = createOWLOntologyManager().loadOntologyFromOntologyDocument(IRI.create(url));
        } catch (OWLOntologyCreationException e) {
            throw new IOException(e);
        }
        Set<OWLOntology> all = fromUrl.getImportsClosure();
        List<OWLAxiom> axiomList = new ArrayList<OWLAxiom>();
        for (OWLOntology o : all) {
            axiomList.addAll(o.getAxioms());
        }
        final Iterator<OWLAxiom> iterator = axiomList.iterator();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else if (type.isAssignableFrom(Statement.class)) {
        final OntModel input = ModelFactory.createOntologyModel();
        synchronized (url) {
            // FIXME: use instead:
            // FileManager.get().loadModel
            input.read(url);
        }
        final StmtIterator iterator = input.listStatements();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else {
        throw new UnsupportedOperationException("This provider does not adapt to the given type");
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Iterator(java.util.Iterator) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 19 with OntModel

use of com.hp.hpl.jena.ontology.OntModel in project stanbol by apache.

the class ByteArrayInputProvider method getInput.

@Override
public <T> Iterator<T> getInput(Class<T> type) throws IOException {
    if (type.isAssignableFrom(OWLAxiom.class)) {
        // We add additional axioms
        OWLOntology fromUrl;
        try {
            fromUrl = createOWLOntologyManager().loadOntologyFromOntologyDocument(new ByteArrayInputStream(bytes));
        } catch (OWLOntologyCreationException e) {
            throw new IOException(e);
        }
        Set<OWLOntology> all = fromUrl.getImportsClosure();
        List<OWLAxiom> axiomList = new ArrayList<OWLAxiom>();
        for (OWLOntology o : all) {
            axiomList.addAll(o.getAxioms());
        }
        final Iterator<OWLAxiom> iterator = axiomList.iterator();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else if (type.isAssignableFrom(Statement.class)) {
        final OntModel input = ModelFactory.createOntologyModel();
        synchronized (bytes) {
            // XXX 
            // Not sure this would always work. What if we have an RDF/XML relying on an implicit base?
            input.read(new ByteArrayInputStream(bytes), "");
        }
        final StmtIterator iterator = input.listStatements();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else {
        throw new UnsupportedOperationException("This provider does not adapt to the given type");
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Iterator(java.util.Iterator) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 20 with OntModel

use of com.hp.hpl.jena.ontology.OntModel in project stanbol by apache.

the class JenaToOwlConvert method ModelOwlToJenaConvert.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts an ontology object from OWLapi to Jena
     * 
     * @param owlmodel
     *            {An OWLOntology object}
     * @param format
     *            {RDF/XML or TURTLE}
     * @return {An OntModel that is a Jena object}
     */
public synchronized OntModel ModelOwlToJenaConvert(OWLOntology owlmodel, String format) {
    while (availablemain == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("ModelOwlToJenaConvert::: " + e);
        }
    }
    availablemain = false;
    OWLOntologyID id;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OWLOntologyManager owlmanager = owlmodel.getOWLOntologyManager();
        format = format.trim();
        if (format.equals("TURTLE") || format.equals("RDF/XML")) {
            if (format.equals("TURTLE"))
                owlmanager.setOntologyFormat(owlmodel, new TurtleOntologyFormat());
            if (format.equals("RDF/XML"))
                owlmanager.setOntologyFormat(owlmodel, new RDFXMLOntologyFormat());
            OWLOntologyFormat owlformat = owlmanager.getOntologyFormat(owlmodel);
            owlmanager.saveOntology(owlmodel, owlformat, out);
            OntModel jenamodel = ModelFactory.createOntologyModel();
            id = owlmodel.getOntologyID();
            jenamodel.read(new ByteArrayInputStream(out.toByteArray()), id.toString().replace("<", "").replace(">", ""), format);
            availablemain = true;
            notifyAll();
            return jenamodel;
        } else {
            System.err.println("The only format supported is RDF/XML or TURTLE. Please check the format!");
            availablemain = true;
            notifyAll();
            return null;
        }
    } catch (OWLOntologyStorageException eos) {
        System.err.print("ModelOwlToJenaConvert::: ");
        eos.printStackTrace();
        return null;
    }
}
Also used : TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) OWLOntologyFormat(org.semanticweb.owlapi.model.OWLOntologyFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntModel(com.hp.hpl.jena.ontology.OntModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

OntModel (com.hp.hpl.jena.ontology.OntModel)21 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)20 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)15 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)9 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)6 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)6 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)5 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)5 DatatypeProperty (com.hp.hpl.jena.ontology.DatatypeProperty)4 ObjectProperty (com.hp.hpl.jena.ontology.ObjectProperty)4 OntClass (com.hp.hpl.jena.ontology.OntClass)4 Statement (com.hp.hpl.jena.rdf.model.Statement)4 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)4 OWLClass (org.semanticweb.owlapi.model.OWLClass)4 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)4 OWLDeclarationAxiom (org.semanticweb.owlapi.model.OWLDeclarationAxiom)4 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)3 AnnotationProperty (com.hp.hpl.jena.ontology.AnnotationProperty)2 Resource (com.hp.hpl.jena.rdf.model.Resource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2