use of org.semanticweb.owlapi.model.OWLOntologyCreationException in project stanbol by apache.
the class RefactorEnhancementEngine method initEngine.
/**
* Method for adding ontologies to the scope core ontology.
* <ol>
* <li>Get all the ontologies from the property.</li>
* <li>Create a base scope.</li>
* <li>Retrieve the ontology space from the scope.</li>
* <li>Add the ontologies to the scope via ontology space.</li>
* </ol>
*/
private void initEngine(RefactorEnhancementEngineConf engineConfiguration) {
// IRI dulcifierScopeIRI = org.semanticweb.owlapi.model.IRI.create((String) context.getProperties().get(SCOPE));
String scopeId = engineConfiguration.getScope();
// Create or get the scope with the configured ID
try {
scope = onManager.createOntologyScope(scopeId);
// No need to deactivate a newly created scope.
} catch (DuplicateIDException e) {
scope = onManager.getScope(scopeId);
onManager.setScopeActive(scopeId, false);
}
// All resolvable ontologies stated in the configuration are loaded into the core space.
OntologySpace ontologySpace = scope.getCoreSpace();
ontologySpace.tearDown();
String[] coreScopeOntologySet = engineConfiguration.getScopeCoreOntologies();
List<String> success = new ArrayList<String>(), failed = new ArrayList<String>();
try {
log.info("Will now load requested ontology into the core space of scope '{}'.", scopeId);
OWLOntologyManager sharedManager = OWLManager.createOWLOntologyManager();
org.semanticweb.owlapi.model.IRI physicalIRI = null;
for (int o = 0; o < coreScopeOntologySet.length; o++) {
String url = coreScopeOntologySet[o];
try {
physicalIRI = org.semanticweb.owlapi.model.IRI.create(url);
} catch (Exception e) {
failed.add(url);
}
try {
// TODO replace with a Clerezza equivalent
ontologySpace.addOntology(new RootOntologySource(physicalIRI, sharedManager));
success.add(url);
} catch (OWLOntologyCreationException e) {
log.error("Failed to load ontology from physical location " + physicalIRI + " Continuing with next...", e);
failed.add(url);
}
}
} catch (UnmodifiableOntologyCollectorException ex) {
log.error("Ontology space {} was found locked for modification. Cannot populate.", ontologySpace);
}
for (String s : success) log.info(" >> {} : SUCCESS", s);
for (String s : failed) log.info(" >> {} : FAILED", s);
ontologySpace.setUp();
// if (!onManager.containsScope(scopeId)) onManager.registerScope(scope);
onManager.setScopeActive(scopeId, true);
/*
* The first thing to do is to create a recipe in the rule store that can be used by the engine to
* refactor the enhancement graphs.
*/
String recipeId = engineConfiguration.getRecipeId();
Recipe recipe = null;
try {
recipe = ruleStore.createRecipe(new IRI(recipeId), null);
} catch (AlreadyExistingRecipeException e1) {
log.error("A recipe with ID {} already exists in the store.", recipeId);
}
if (recipe != null) {
log.debug("Initialised blank recipe with ID {}", recipeId);
/*
* The set of rule to put in the recipe can be provided by the user. A default set of rules is
* provided in /META-INF/default/seo_rules.sem. Use the property engine.refactor in the felix
* console to pass to the engine your set of rules.
*/
String recipeLocation = engineConfiguration.getRecipeLocation();
InputStream recipeStream = null;
String recipeString = null;
if (recipeLocation != null && !recipeLocation.isEmpty()) {
Dereferencer dereferencer = new DereferencerImpl();
try {
recipeStream = dereferencer.resolve(recipeLocation);
log.debug("Loaded recipe from external source {}", recipeLocation);
} catch (FileNotFoundException e) {
log.error("Recipe Stream is null.", e);
}
} else {
// TODO remove this part (or manage it better in the @Activate method).
String loc = "/META-INF/default/seo_rules.sem";
recipeStream = getClass().getResourceAsStream(loc);
log.debug("Loaded default recipe in {}.", loc);
}
if (recipeStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(recipeStream));
recipeString = "";
String line = null;
try {
while ((line = reader.readLine()) != null) recipeString += line;
} catch (IOException e) {
log.error("Failed to load Refactor Engine recipe from stream. Aborting read. ", e);
recipeString = null;
}
}
log.debug("Recipe content follows :\n{}", recipeString);
if (recipeString != null) {
ruleStore.addRulesToRecipe(recipe, recipeString, null);
log.debug("Added rules to recipe {}", recipeId);
}
}
}
use of org.semanticweb.owlapi.model.OWLOntologyCreationException 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.OWLOntologyCreationException 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.OWLOntologyCreationException 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.OWLOntologyCreationException 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;
}
}
Aggregations