use of org.semanticweb.owlapi.model.OWLOntology 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");
}
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class AbstractOWLApiReasoningService method run.
/**
* Generic method for running the reasoner
*
* @param input
* @param generators
* @return
*/
@Override
public Set<OWLAxiom> run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
log.debug("run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators)");
try {
// Get the manager
OWLOntologyManager manager = createOWLOntologyManager();
// Get the reasoner
OWLReasoner reasoner = getReasoner(input);
log.info("Running {} reasoner on {} ", reasoner.getClass(), input.getOntologyID());
// To generate inferred axioms
InferredOntologyGenerator inferred = new InferredOntologyGenerator(reasoner, generators);
// We fill an anonymous ontology with the result, the return the
// axiom set
Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
try {
OWLOntology output = manager.createOntology();
log.debug("Created output ontology: {}", output);
try {
inferred.fillOntology(manager, output);
} catch (InconsistentOntologyException i) {
throw i;
} catch (Throwable t) {
log.error("Some problem occurred:\n {}", t.getStackTrace());
throw new ReasoningServiceException();
}
log.debug("Filled ontology: {}", output);
log.debug("Temporary ID is {}", output.getOntologyID());
axioms = manager.getOntology(output.getOntologyID()).getAxioms();
// IMPORTANT We remove the ontology from the manager
manager.removeOntology(output);
} catch (OWLOntologyCreationException e) {
log.error("An exception have been thrown when instantiating the ontology");
throw new ReasoningServiceException();
}
return axioms;
} catch (InconsistentOntologyException inconsistent) {
/**
* TODO Add report. Why it is inconsistent?
*/
throw new InconsistentInputException();
} catch (Exception exception) {
log.error("An exception have been thrown while executing method run()", exception);
throw new ReasoningServiceException();
}
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class ResponseTaskBuilder method stream.
/**
* This supports OWLOntology and jena Model objects.
* In the case of Jena the reuslt is printed as Turtle,
* in case of OWLApi the result is in Manchester syntax (more readable).
*
* FIXME: Both should return the same format
*
* @param object
* @return
*/
private OutputStream stream(Object object) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (object instanceof OWLOntology) {
OWLOntology o = (OWLOntology) object;
ManchesterOWLSyntaxOntologyStorer mosos = new ManchesterOWLSyntaxOntologyStorer();
try {
mosos.storeOntology(o.getOWLOntologyManager(), o, new StreamDocumentTarget(out), new ManchesterOWLSyntaxOntologyFormat());
} catch (OWLOntologyStorageException e) {
log.error("Cannot stream the ontology", e);
throw new RuntimeException(e);
}
} else if (object instanceof Model) {
Model m = (Model) object;
// FIXME Both should return the same format
m.write(out, "TURTLE");
}
return out;
}
use of org.semanticweb.owlapi.model.OWLOntology 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;
}
}
use of org.semanticweb.owlapi.model.OWLOntology 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);
}
}
Aggregations