use of org.semanticweb.owlapi.model.OWLAxiom 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");
}
}
use of org.semanticweb.owlapi.model.OWLAxiom 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.OWLAxiom in project stanbol by apache.
the class AbstractOWLApiReasoningService method run.
/**
* Merges the SWRL rules in the input ontology, then calls run(OWLOntology,List<InferredAxiomGenerator<?
* extends OWLAxiom>>)
*
* @param ontology
* @param rules
* @param generators
* @return
*/
@Override
public Set<OWLAxiom> run(OWLOntology ontology, List<SWRLRule> rules, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
log.debug("Called method run(OWLOntology,List<SWRLRule>,List)");
OWLOntologyManager manager = ontology.getOWLOntologyManager();
log.debug("Adding SWRL rules to the input ontology.");
Set<SWRLRule> ruleSet = new HashSet<SWRLRule>();
ruleSet.addAll(rules);
manager.addAxioms(ontology, ruleSet);
if (log.isDebugEnabled())
for (OWLAxiom a : ontology.getAxioms()) {
log.debug("Axiom {}", a);
}
log.debug("Calling the run method.");
return run(ontology, generators);
}
use of org.semanticweb.owlapi.model.OWLAxiom 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.OWLAxiom 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);
}
}
Aggregations