use of org.semanticweb.owlapi.model.OWLOntologyStorageException 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.OWLOntologyStorageException 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;
}
}
use of org.semanticweb.owlapi.model.OWLOntologyStorageException in project stanbol by apache.
the class ODPRegistryCacheManager method getOntologyInputSource.
public static synchronized OWLOntologyDocumentSource getOntologyInputSource(URI uri) throws ODPRegistryCacheException, URIUnresolvableException {
if (getUnresolvedURIs().contains(uri))
throw new URIUnresolvableException();
if (uris.containsKey(uri)) {
File f = uris.get(uri);
FileDocumentSource fds = new FileDocumentSource(f);
return fds;
} else {
try {
retrieveRemoteResource(uri);
return getOntologyInputSource(uri);
} catch (UnknownOWLOntologyException e) {
throw new ODPRegistryCacheException(e);
} catch (OWLOntologyCreationException e) {
throw new ODPRegistryCacheException(e);
} catch (OWLOntologyStorageException e) {
throw new ODPRegistryCacheException(e);
}
}
}
use of org.semanticweb.owlapi.model.OWLOntologyStorageException in project stanbol by apache.
the class RuleListWriter method writeTo.
@Override
public void writeTo(RuleList ruleList, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
Logger log = LoggerFactory.getLogger(getClass());
log.debug("Rendering the list of recipes.");
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = OWLManager.getOWLDataFactory();
OWLOntology ontology;
try {
ontology = manager.createOntology();
String recipeClassURI = Symbols.Recipe.toString().replace("<", "").replace(">", "");
IRI recipeClassIRI = IRI.create(recipeClassURI);
OWLClass owlRecipeClass = factory.getOWLClass(recipeClassIRI);
String ruleClassURI = Symbols.Rule.toString().replace("<", "").replace(">", "");
IRI ruleClassIRI = IRI.create(ruleClassURI);
OWLClass owlRuleClass = factory.getOWLClass(ruleClassIRI);
String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
IRI descriptionIRI = IRI.create(descriptionURI);
OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
IRI hasRuleIRI = IRI.create(hasRuleURI);
OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
IRI ruleBodyIRI = IRI.create(ruleBodyURI);
OWLDataProperty ruleBody = factory.getOWLDataProperty(ruleBodyIRI);
String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
IRI ruleHeadIRI = IRI.create(ruleHeadURI);
OWLDataProperty ruleHead = factory.getOWLDataProperty(ruleHeadIRI);
if (ruleList != null) {
for (Rule rule : ruleList) {
String recipeId = rule.getRecipe().getRecipeID().toString().replace("<", "").replace(">", "");
IRI reicpeIRI = IRI.create(recipeId);
OWLIndividual owlRecipe = factory.getOWLNamedIndividual(reicpeIRI);
String ruleId = rule.getRuleID().toString().replace("<", "").replace(">", "");
IRI ruleIRI = IRI.create(ruleId);
OWLIndividual owlRule = factory.getOWLNamedIndividual(ruleIRI);
OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, owlRecipe);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLClassAssertionAxiom(owlRuleClass, owlRule);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, owlRecipe, owlRule);
manager.addAxiom(ontology, axiom);
String recipeDescription = rule.getRecipe().getRecipeDescription();
String ruleDescription = rule.getDescription();
if (recipeDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRecipe, recipeDescription);
manager.addAxiom(ontology, axiom);
}
if (ruleDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRule, ruleDescription);
manager.addAxiom(ontology, axiom);
}
String ruleContent = rule.toString();
String[] parts = ruleContent.split("\\->");
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBody, owlRule, parts[0]);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHead, owlRule, parts[1]);
manager.addAxiom(ontology, axiom);
}
}
if (mediaType.toString().equals(KRFormat.RDF_XML)) {
try {
manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.OWL_XML)) {
try {
manager.saveOntology(ontology, new OWLXMLOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.MANCHESTER_OWL)) {
try {
manager.saveOntology(ontology, new ManchesterOWLSyntaxOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.FUNCTIONAL_OWL)) {
try {
manager.saveOntology(ontology, new OWLFunctionalSyntaxOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.TURTLE)) {
try {
manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.RDF_JSON)) {
Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
RdfJsonSerializingProvider provider = new RdfJsonSerializingProvider();
provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
}
} catch (OWLOntologyCreationException e1) {
log.error("An error occurred.", e1);
}
out.flush();
}
Aggregations