Search in sources :

Example 6 with StmtIterator

use of com.hp.hpl.jena.rdf.model.StmtIterator 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 7 with StmtIterator

use of com.hp.hpl.jena.rdf.model.StmtIterator 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 8 with StmtIterator

use of com.hp.hpl.jena.rdf.model.StmtIterator in project stanbol by apache.

the class JenaToOwlConvert method EntityOwlToJenaResource.

// //////////////////////////////////////////////////////////////////////////////
/**
     * This function converts any thingths relatives to an OWL entity in an iterator over Jena statement
     * 
     * @param entity
     *            {It could be a class, an object property or a data property}
     * @param owlmodel
     *            {OWLOntology model where to retrieve information about the entity}
     * @param format
     *            {RDF/XML or TURTLE}
     * @return {An iterator over jena statement}
     */
public synchronized StmtIterator EntityOwlToJenaResource(OWLEntity entity, OWLOntology owlmodel, String format) {
    while (available == false) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.err.println("EntityOwlToJenaResource::: " + e);
        }
    }
    available = false;
    try {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology(IRI.create("http://www.semanticweb.org/owlapi/ontologies/ontology"));
        // If the entity is a class
        if (entity.isOWLClass()) {
            OWLClass owldata = entity.asOWLClass();
            Iterator<OWLClassAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        // If the entity is a data property
        if (entity.isOWLDataProperty()) {
            OWLDataProperty owldata = entity.asOWLDataProperty();
            Iterator<OWLDataPropertyAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        // If the entity is an object property
        if (entity.isOWLObjectProperty()) {
            OWLObjectProperty owldata = entity.asOWLObjectProperty();
            Iterator<OWLObjectPropertyAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        // If the entity is a data type
        if (entity.isOWLDatatype()) {
            OWLDatatype owldata = entity.asOWLDatatype();
            Iterator<OWLDatatypeDefinitionAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        // If the entity is an individual
        if (entity.isOWLNamedIndividual()) {
            OWLNamedIndividual owldata = entity.asOWLNamedIndividual();
            Iterator<OWLIndividualAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        // If the entity is an annotations property
        if (entity.isOWLAnnotationProperty()) {
            OWLAnnotationProperty owldata = entity.asOWLAnnotationProperty();
            Iterator<OWLAnnotationAxiom> entityaxiom = owlmodel.getAxioms(owldata).iterator();
            while (entityaxiom.hasNext()) manager.addAxiom(ontology, entityaxiom.next());
            Iterator<OWLAnnotationAssertionAxiom> annotations = entity.getAnnotationAssertionAxioms(owlmodel).iterator();
            while (annotations.hasNext()) manager.addAxiom(ontology, annotations.next());
        }
        OntModel ontmodel = ModelOwlToJenaConvert(ontology, format);
        StmtIterator statement = ontmodel.listStatements();
        available = true;
        notifyAll();
        return statement;
    } catch (OWLOntologyCreationException eoc) {
        System.err.print("EntityOwlToJenaResource::: ");
        eoc.printStackTrace();
        return null;
    }
}
Also used : OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) OWLClassAxiom(org.semanticweb.owlapi.model.OWLClassAxiom) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OWLIndividualAxiom(org.semanticweb.owlapi.model.OWLIndividualAxiom) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLObjectPropertyAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAxiom) OWLDatatypeDefinitionAxiom(org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom) OWLDataPropertyAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAxiom) OWLAnnotationAxiom(org.semanticweb.owlapi.model.OWLAnnotationAxiom) OWLClass(org.semanticweb.owlapi.model.OWLClass)

Example 9 with StmtIterator

use of com.hp.hpl.jena.rdf.model.StmtIterator in project stanbol by apache.

the class ConversionTester method testEntityOwlToJenaResource.

public void testEntityOwlToJenaResource() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    StmtIterator resource = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
        fail("Could not load ontology");
    }
    OWLDataFactory factory = mgr.getOWLDataFactory();
    OWLClass cls = factory.getOWLClass(IRI.create(CLAZZ));
    OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(DP));
    OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(OP));
    OWLAnnotationProperty oa = factory.getOWLAnnotationProperty(IRI.create(label));
    OWLAnnotation oav = factory.getOWLAnnotation(oa, factory.getOWLStringLiteral(clazzlabel, "en"));
    OWLDatatype dt = factory.getOWLDatatype(IRI.create(DATATYPE));
    OWLNamedIndividual sub = factory.getOWLNamedIndividual(IRI.create(SUBJECT));
    OWLNamedIndividual obj = factory.getOWLNamedIndividual(IRI.create(OBJECT));
    OWLLiteral literal1 = factory.getOWLTypedLiteral(VALUE, dt);
    // Classe
    OWLDeclarationAxiom daxiomcls = factory.getOWLDeclarationAxiom(cls);
    // obj prop
    OWLDeclarationAxiom daxiomop = factory.getOWLDeclarationAxiom(op);
    // data prop
    OWLDeclarationAxiom daxiomdp = factory.getOWLDeclarationAxiom(dp);
    // subject
    OWLDeclarationAxiom daxiomsub = factory.getOWLDeclarationAxiom(sub);
    // object
    OWLDeclarationAxiom daxiomobj = factory.getOWLDeclarationAxiom(obj);
    // Istanza
    OWLClassAssertionAxiom axiomsub = factory.getOWLClassAssertionAxiom(cls, sub);
    // Istanza
    OWLClassAssertionAxiom axiomobj = factory.getOWLClassAssertionAxiom(cls, obj);
    // Obj
    OWLObjectPropertyAssertionAxiom axiomop = factory.getOWLObjectPropertyAssertionAxiom(op, sub, obj);
    // prop
    // tra
    // individui
    OWLDataPropertyAssertionAxiom axiomvalue = factory.getOWLDataPropertyAssertionAxiom(dp, sub, // Dataprop all'istanza;
    literal1);
    // Annotazione
    OWLAnnotationAssertionAxiom axioman = factory.getOWLAnnotationAssertionAxiom(cls.getIRI(), oav);
    mgr.addAxiom(ont, daxiomcls);
    mgr.addAxiom(ont, daxiomop);
    mgr.addAxiom(ont, daxiomdp);
    mgr.addAxiom(ont, daxiomsub);
    mgr.addAxiom(ont, daxiomobj);
    mgr.addAxiom(ont, axiomsub);
    mgr.addAxiom(ont, axiomobj);
    mgr.addAxiom(ont, axiomop);
    mgr.addAxiom(ont, axiomvalue);
    mgr.addAxiom(ont, axioman);
    Set<OWLIndividualAxiom> ind = ont.getAxioms(sub);
    try {
        resource = j2o.EntityOwlToJenaResource(daxiomsub.getEntity(), ont, RDFXML);
        if (resource == null) {
            fail("Some errors accour");
        } else {
            int cont = 0;
            while (resource.hasNext()) {
                Statement stm = resource.nextStatement();
                IRI subres = IRI.create(stm.getSubject().getURI());
                if (("<" + subres + ">").equals(daxiomsub.getEntity().toString()))
                    cont++;
            }
            assertEquals(ind.size(), (cont - 1));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(resource);
    }
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLDeclarationAxiom(org.semanticweb.owlapi.model.OWLDeclarationAxiom) OWLDatatype(org.semanticweb.owlapi.model.OWLDatatype) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLDataPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAnnotation(org.semanticweb.owlapi.model.OWLAnnotation) Statement(com.hp.hpl.jena.rdf.model.Statement) OWLIndividualAxiom(org.semanticweb.owlapi.model.OWLIndividualAxiom) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 10 with StmtIterator

use of com.hp.hpl.jena.rdf.model.StmtIterator in project stanbol by apache.

the class ConversionTester method testResourceJenaToOwlAxiom.

public void testResourceJenaToOwlAxiom() {
    JenaToOwlConvert j2o = new JenaToOwlConvert();
    OntModel model = ModelFactory.createOntologyModel();
    OntClass jenaclass = model.createClass(CLAZZ.toString());
    ObjectProperty jenaobprop = model.createObjectProperty(OP.toString());
    DatatypeProperty jenadataprop = model.createDatatypeProperty(DP.toString());
    Individual jenasub = model.createIndividual(SUBJECT.toString(), jenaclass);
    Individual jenaobj = model.createIndividual(OBJECT.toString(), jenaclass);
    AnnotationProperty jenaanno = model.createAnnotationProperty(label.toString());
    Literal value = model.createTypedLiteral(VALUE, DATATYPE.toString());
    model.add(jenasub, jenaobprop, jenaobj);
    model.add(jenasub, jenadataprop, value);
    model.add(jenasub, jenaanno, "Lucy", "en");
    Set<OWLAxiom> owlaxiom = null;
    try {
        owlaxiom = j2o.ResourceJenaToOwlAxiom(jenasub, RDFXML);
        if (owlaxiom == null) {
            fail("Some errors occur");
        } else {
            StmtIterator str = model.listStatements();
            int count = 0;
            while (str.hasNext()) {
                Statement stm = str.next();
                Resource subject = stm.getSubject();
                if (SUBJECT.toString().equals(subject.getURI()))
                    count++;
            }
            if (count == owlaxiom.size()) {
                assertEquals(count, owlaxiom.size());
            } else {
                fail("The number of axioms don't match the number of statement");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception caugth");
    } finally {
        assertNotNull(owlaxiom);
    }
}
Also used : OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) ObjectProperty(com.hp.hpl.jena.ontology.ObjectProperty) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) AnnotationProperty(com.hp.hpl.jena.ontology.AnnotationProperty) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) Individual(com.hp.hpl.jena.ontology.Individual) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) Literal(com.hp.hpl.jena.rdf.model.Literal) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OntClass(com.hp.hpl.jena.ontology.OntClass) DatatypeProperty(com.hp.hpl.jena.ontology.DatatypeProperty)

Aggregations

StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)11 Statement (com.hp.hpl.jena.rdf.model.Statement)8 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)7 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)6 OntModel (com.hp.hpl.jena.ontology.OntModel)5 Resource (com.hp.hpl.jena.rdf.model.Resource)5 OWLAnnotationProperty (org.semanticweb.owlapi.model.OWLAnnotationProperty)4 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)4 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)4 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)4 OWLAnnotationAssertionAxiom (org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom)3 OWLClass (org.semanticweb.owlapi.model.OWLClass)3 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)3 OWLDatatype (org.semanticweb.owlapi.model.OWLDatatype)3 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)3 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)3 Literal (com.hp.hpl.jena.rdf.model.Literal)2 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2