use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class AbstractOntologyCollectorImpl method exportToOWLOntology.
/**
* This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
* afterwards.
*
* FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
*
* @param merge
* @return
*/
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
long before = System.currentTimeMillis();
// Create a new ontology
OWLOntology root;
OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
try {
root = ontologyManager.createOntology(iri);
} catch (OWLOntologyAlreadyExistsException e) {
// It should be impossible, but just in case.
ontologyManager.removeOntology(ontologyManager.getOntology(iri));
try {
root = ontologyManager.createOntology(iri);
} catch (OWLOntologyAlreadyExistsException e1) {
root = ontologyManager.getOntology(iri);
} catch (OWLOntologyCreationException e1) {
log.error("Failed to assemble root ontology for scope " + iri, e);
root = null;
}
} catch (OWLOntologyCreationException e) {
log.error("Failed to assemble root ontology for scope " + _id, e);
root = null;
}
// Add the import declarations for directly managed ontologies.
if (root != null) {
if (merge) {
final Set<OWLOntology> set = new HashSet<OWLOntology>();
log.debug("Merging {} with its imports.", root);
set.add(root);
for (OWLOntologyID ontologyId : managedOntologies) {
log.debug("Merging {} with {}.", ontologyId, root);
set.add(getOntology(ontologyId, OWLOntology.class, true));
}
OWLOntologySetProvider provider = new OWLOntologySetProvider() {
@Override
public Set<OWLOntology> getOntologies() {
return set;
}
};
OWLOntologyMerger merger = new OWLOntologyMerger(provider);
try {
root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
} catch (OWLOntologyCreationException e) {
log.error("Failed to merge imports for ontology " + iri, e);
root = null;
}
} else {
// Add the import declarations for directly managed ontologies.
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
OWLDataFactory df = ontologyManager.getOWLDataFactory();
String base = prefix + getID();
for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
base += "/";
// The key set of managedOntologies contains the ontology IRIs, not their storage keys.
for (OWLOntologyID ontologyId : managedOntologies) {
// XXX some day the versionIRI will be the only physical reference for the ontology
org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
}
ontologyManager.applyChanges(changes);
}
}
log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
return root;
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class ScopeImpl method exportToOWLOntology.
/**
* Get an OWL API {@link OWLOntology} representation of the scope.
*
* @param merge
* if true the core and custom spaces will be recursively merged with the scope ontology,
* otherwise owl:imports statements will be added.
* @return the OWL representation of the scope.
*/
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
// if (merge) throw new UnsupportedOperationException(
// "Ontology merging only implemented for managed ontologies, not for collectors. "
// + "Please set merge parameter to false.");
// Create an ontology manager on the fly. We don't really need a permanent one.
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
OWLDataFactory df = mgr.getOWLDataFactory();
OWLOntology ont = null;
try {
if (merge) {
final Set<OWLOntology> set = new HashSet<OWLOntology>();
log.debug("Merging custom space of {}.", getID());
set.add(this.getCustomSpace().export(OWLOntology.class, merge));
log.debug("Merging core space of {}.", getID());
set.add(this.getCoreSpace().export(OWLOntology.class, merge));
OWLOntologySetProvider provider = new OWLOntologySetProvider() {
@Override
public Set<OWLOntology> getOntologies() {
return set;
}
};
OWLOntologyMerger merger = new OWLOntologyMerger(provider);
try {
ont = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), org.semanticweb.owlapi.model.IRI.create(getDefaultNamespace() + getID()));
} catch (OWLOntologyCreationException e) {
log.error("Failed to merge imports for ontology.", e);
ont = null;
}
} else {
// The root ontology ID is in the form [namespace][scopeId]
ont = mgr.createOntology(org.semanticweb.owlapi.model.IRI.create(universalPrefix + getID()));
List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
// Add the import statement for the custom space, if existing and not empty
OntologySpace spc = getCustomSpace();
if (spc != null && spc.listManagedOntologies().size() > 0) {
org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
}
// Add the import statement for the core space, if existing and not empty
spc = getCoreSpace();
if (spc != null && spc.listManagedOntologies().size() > 0) {
org.semanticweb.owlapi.model.IRI spaceIri = org.semanticweb.owlapi.model.IRI.create(universalPrefix + spc.getID());
additions.add(new AddImport(ont, df.getOWLImportsDeclaration(spaceIri)));
}
mgr.applyChanges(additions);
}
} catch (OWLOntologyCreationException e) {
log.error("Failed to generate an OWL form of scope " + getID(), e);
ont = null;
}
return ont;
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class ScopeManagerImpl method activate.
/**
* Called within both OSGi and non-OSGi environments.
*
* @param configuration
* @throws IOException
*/
protected void activate(Dictionary<String, Object> configuration) throws IOException {
long before = System.currentTimeMillis();
// Assign singleton instance
me = this;
// Parse configuration
if (offline != null)
ontonetNS = offline.getDefaultOntologyNetworkNamespace();
scopeRegistryId = (String) configuration.get(ScopeManager.ID_SCOPE_REGISTRY);
if (scopeRegistryId == null)
scopeRegistryId = _ID_SCOPE_REGISTRY_DEFAULT;
configPath = (String) configuration.get(ScopeManager.CONFIG_ONTOLOGY_PATH);
if (configPath == null)
configPath = _CONFIG_ONTOLOGY_PATH_DEFAULT;
// Bind components, starting with the local directories.
List<String> dirs = new ArrayList<String>();
try {
for (IRI iri : offline.getOntologySourceLocations()) dirs.add(iri.toString());
} catch (NullPointerException ex) {
// Ok, go empty
}
bindResources();
// String tfile = (String) configuration.get(CONFIG_FILE_PATH);
// if (tfile != null) this.configPath = tfile;
// String tns = (String) configuration.get(KRES_NAMESPACE);
// if (tns != null) this.kresNs = tns;
// configPath = (String) configuration.get(CONFIG_FILE_PATH);
// If there is no configuration file, just start with an empty scope set
Object connectivityPolicy = configuration.get(ScopeManager.CONNECTIVITY_POLICY);
if (connectivityPolicy == null) {
this.connectivityPolicyString = _CONNECTIVITY_POLICY_DEFAULT;
} else {
this.connectivityPolicyString = connectivityPolicy.toString();
}
String configPath = getOntologyNetworkConfigurationPath();
if (configPath != null && !configPath.trim().isEmpty()) {
OWLOntology oConf = null;
OWLOntologyManager tempMgr = OWLOntologyManagerFactory.createOWLOntologyManager(offline.getOntologySourceLocations().toArray(new IRI[0]));
OWLOntologyDocumentSource oConfSrc = null;
try {
log.debug("Try to load the configuration ontology from a local bundle relative path");
InputStream is = this.getClass().getResourceAsStream(configPath);
oConfSrc = new StreamDocumentSource(is);
} catch (Exception e1) {
try {
log.debug("Cannot load from a local bundle relative path", e1);
log.debug("Try to load the configuration ontology resolving the given IRI");
IRI iri = IRI.create(configPath);
if (!iri.isAbsolute())
throw new Exception("IRI seems to be not absolute! value was: " + iri.toQuotedString());
oConfSrc = new IRIDocumentSource(iri);
} catch (Exception e) {
try {
log.debug("Cannot load from the web", e1);
log.debug("Try to load the configuration ontology as full local file path");
oConfSrc = new FileDocumentSource(new File(configPath));
} catch (Exception e2) {
log.error("Cannot load the configuration ontology from parameter value: " + configPath, e2);
}
}
}
if (oConfSrc == null) {
log.warn("No ONM configuration file found at path " + configPath + ". Starting with blank scope set.");
} else {
try {
oConf = tempMgr.loadOntologyFromOntologyDocument(oConfSrc);
} catch (OWLOntologyCreationException e) {
log.error("Cannot create the configuration ontology", e);
}
}
// Create and populate the scopes from the config ontology.
bootstrapOntologyNetwork(oConf);
} else {
// No ontology supplied. Access the local graph
rebuildScopes();
}
log.debug(ScopeManager.class + " activated. Time : {} ms.", System.currentTimeMillis() - before);
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class RecipeListWriter method writeTo.
@Override
public void writeTo(RecipeList recipeList, 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 descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
IRI descriptionIRI = IRI.create(descriptionURI);
OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
if (recipeList != null) {
log.info("Converting the recipe list to OWL.");
for (Recipe recipe : recipeList) {
String recipeURI = recipe.getRecipeID().toString().replace("<", "").replace(">", "");
IRI recipeIRI = IRI.create(recipeURI);
OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, recipeIndividual);
manager.addAxiom(ontology, axiom);
String description = recipe.getRecipeDescription();
if (description != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, description);
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();
}
use of org.semanticweb.owlapi.model.OWLOntology in project stanbol by apache.
the class RecipeWriter method writeTo.
@Override
public void writeTo(Recipe recipe, 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.");
if (mediaType.toString().equals(MediaType.TEXT_PLAIN)) {
String recipeString = recipe.toString();
out.write(recipeString.getBytes());
} else if (mediaType.toString().equals(MediaType.TEXT_HTML)) {
String recipeString = recipe.toString();
out.write(recipeString.getBytes());
} else {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = OWLManager.getOWLDataFactory();
OWLOntology ontology;
try {
ontology = manager.createOntology();
RuleList rules = recipe.getRuleList();
IRI recipeID = recipe.getRecipeID();
String recipeURI = recipeID.toString().replace("<", "").replace(">", "");
org.semanticweb.owlapi.model.IRI recipeIRI = org.semanticweb.owlapi.model.IRI.create(recipeURI);
OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
org.semanticweb.owlapi.model.IRI descriptionIRI = org.semanticweb.owlapi.model.IRI.create(descriptionURI);
OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
OWLAxiom axiom;
String recipeDescription = recipe.getRecipeDescription();
if (recipeDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, recipeDescription);
manager.addAxiom(ontology, axiom);
}
if (rules != null) {
for (Rule rule : rules) {
IRI ruleID = rule.getRuleID();
String ruleName = rule.getRuleName();
String ruleDescription = rule.getDescription();
String ruleURI = ruleID.toString().replace("<", "").replace(">", "");
String ruleNameURI = Symbols.ruleName.toString().replace("<", "").replace(">", "");
String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
String ruleContent = rule.toString();
String[] ruleParts = ruleContent.split("\\->");
org.semanticweb.owlapi.model.IRI ruleIRI = org.semanticweb.owlapi.model.IRI.create(ruleURI);
org.semanticweb.owlapi.model.IRI ruleNameIRI = org.semanticweb.owlapi.model.IRI.create(ruleNameURI);
org.semanticweb.owlapi.model.IRI ruleBodyIRI = org.semanticweb.owlapi.model.IRI.create(ruleBodyURI);
org.semanticweb.owlapi.model.IRI ruleHeadIRI = org.semanticweb.owlapi.model.IRI.create(ruleHeadURI);
org.semanticweb.owlapi.model.IRI hasRuleIRI = org.semanticweb.owlapi.model.IRI.create(hasRuleURI);
OWLIndividual ruleIndividual = factory.getOWLNamedIndividual(ruleIRI);
OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
OWLDataProperty nameProperty = factory.getOWLDataProperty(ruleNameIRI);
OWLDataProperty ruleBodyProperty = factory.getOWLDataProperty(ruleBodyIRI);
OWLDataProperty ruleHeadProperty = factory.getOWLDataProperty(ruleHeadIRI);
// add the name to the rule individual
axiom = factory.getOWLDataPropertyAssertionAxiom(nameProperty, ruleIndividual, ruleName);
manager.addAxiom(ontology, axiom);
// add the description to the rule individual
if (ruleDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, ruleIndividual, ruleDescription);
manager.addAxiom(ontology, axiom);
}
// add the rule body to the rule individual
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBodyProperty, ruleIndividual, ruleParts[0]);
manager.addAxiom(ontology, axiom);
// add the rule head to the rule individual
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHeadProperty, ruleIndividual, ruleParts[1]);
manager.addAxiom(ontology, axiom);
// bind the rule to the recipe
axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, recipeIndividual, ruleIndividual);
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