use of org.semanticweb.owlapi.model.OWLOntologyManager 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();
}
use of org.semanticweb.owlapi.model.OWLOntologyManager 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.OWLOntologyManager in project stanbol by apache.
the class RefactorResource method doRefactoring.
/**
* Utility method that groups all calls to the refactorer.
*
* @param input
* @param recipe
* @return
* @throws OWLOntologyCreationException
* @throws RefactoringException
*/
private OWLOntology doRefactoring(InputStream input, KB kb) throws OWLOntologyCreationException, RefactoringException {
if (kb == null)
return null;
RuleList ruleList = kb.getRuleList();
if (ruleList == null)
return null;
Recipe actualRecipe = new RecipeImpl(null, null, ruleList);
// Parse the input ontology
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology inputOntology = manager.loadOntologyFromOntologyDocument(input);
Graph tripleCollection = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(inputOntology), actualRecipe);
// Refactor
return OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(tripleCollection);
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class RefactorResource method performRefactoring.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(value = { TURTLE, RDF_XML, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, RDF_JSON, X_TURTLE })
public Response performRefactoring(MultiPartBody data, @Context HttpHeaders headers) {
String recipe = null;
InputStream input = null;
if (data.getTextParameterValues("recipe") != null) {
recipe = data.getTextParameterValues("recipe")[0];
}
if (data.getFormFileParameterValues("input") != null) {
input = new ByteArrayInputStream(data.getFormFileParameterValues("input")[0].getContent());
}
if (recipe == null || input == null) {
throw new WebApplicationException(BAD_REQUEST);
}
// Refactorer semionRefactorer = semionManager.getRegisteredRefactorer();
ResponseBuilder rb;
Recipe rcp;
try {
URI uri = new URI(recipe);
if (uri != null && uri.getScheme() == null) {
recipe = "urn:" + recipe;
log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
}
IRI recipeID = new IRI(recipe);
rcp = ruleStore.getRecipe(recipeID);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology inputOntology = manager.loadOntologyFromOntologyDocument(input);
Graph tripleCollection = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(inputOntology), rcp);
OWLOntology outputOntology = OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(tripleCollection);
rb = Response.ok(outputOntology);
MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
if (mediaType != null)
rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
} catch (NoSuchRecipeException e) {
rb = Response.status(NOT_FOUND);
log.error(e.getMessage(), e);
} catch (RecipeConstructionException e) {
rb = Response.status(NO_CONTENT);
log.error(e.getMessage(), e);
} catch (OWLOntologyCreationException e) {
rb = Response.status(PRECONDITION_FAILED);
log.error(e.getMessage(), e);
} catch (RefactoringException e) {
rb = Response.status(INTERNAL_SERVER_ERROR);
log.error(e.getMessage(), e);
} catch (URISyntaxException e) {
rb = Response.status(NOT_ACCEPTABLE);
log.error(e.getMessage(), e);
}
// addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
use of org.semanticweb.owlapi.model.OWLOntologyManager in project stanbol by apache.
the class HermitReasoningServiceTest method testRun.
private void testRun(String testID, String expectedID) {
log.info("Testing the run() method");
OWLOntologyManager manager = TestData.manager;
// We prepare the input ontology
try {
OWLOntology testOntology = manager.createOntology();
OWLOntologyID testOntologyID = testOntology.getOntologyID();
log.debug("Created test ontology with ID: {}", testOntologyID);
AddImport addImport = new AddImport(testOntology, TestData.factory.getOWLImportsDeclaration(IRI.create(testID)));
manager.applyChange(addImport);
// We just test class assertions
List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
gens.add(new InferredClassAssertionAxiomGenerator());
// Maybe we want to see what is in before
if (log.isDebugEnabled())
TestUtils.debug(manager.getOntology(testOntologyID), log);
// Now we test the method
log.debug("Running HermiT");
Set<OWLAxiom> inferred = this.theinstance.run(manager.getOntology(testOntologyID), gens);
// Maybe we want to see the inferred axiom list
if (log.isDebugEnabled()) {
TestUtils.debug(inferred, log);
}
// These are the set of expected axioms
Set<OWLLogicalAxiom> expectedAxioms = manager.getOntology(IRI.create(expectedID)).getLogicalAxioms();
Set<OWLAxiom> missing = new HashSet<OWLAxiom>();
for (OWLAxiom expected : expectedAxioms) {
if (!inferred.contains(expected)) {
log.error("missing expected axiom: {}", expected);
missing.add(expected);
}
}
log.info("Are all expected axioms in the result (true)? {}", missing.isEmpty());
assertTrue(missing.isEmpty());
// We want to remove the ontology from the manager
manager.removeOntology(testOntology);
} catch (OWLOntologyCreationException e) {
log.error("An {} have been thrown while creating the input ontology for test", e.getClass());
assertTrue(false);
} catch (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException e) {
log.error("An {} have been thrown while executing the reasoning", e.getClass());
assertTrue(false);
} catch (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException e) {
log.error("An {} have been thrown while executing the reasoning", e.getClass());
assertTrue(false);
}
}
Aggregations