use of org.apache.stanbol.rules.base.api.util.RuleList 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.apache.stanbol.rules.base.api.util.RuleList 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.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class RulesResource method showRecipe.
@GET
@Path("/recipe/{recipe:.+}")
@Produces(value = { MediaType.TEXT_HTML })
public Response showRecipe(@PathParam("recipe") String recipeID, @QueryParam("rule") String ruleID, @Context HttpHeaders headers) {
Recipe recipe;
Rule rule;
ResponseBuilder responseBuilder;
try {
URI uri = new URI(recipeID);
if (uri.getScheme() == null) {
recipeID = "urn:" + recipeID;
log.info("The recipe ID is a URI without scheme. The ID is set to " + recipeID);
}
recipe = ruleStore.getRecipe(new IRI(recipeID));
if (ruleID != null && !ruleID.isEmpty()) {
rule = ruleStore.getRule(recipe, new IRI(ruleID));
RuleList ruleList = new RuleList();
ruleList.add(rule);
recipe = new RecipeImpl(recipe.getRecipeID(), recipe.getRecipeDescription(), ruleList);
}
responseBuilder = Response.ok(new Viewable("rules", new RulesPrettyPrintResource(uriInfo, recipe)));
} catch (NoSuchRecipeException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.NOT_FOUND);
} catch (RecipeConstructionException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.NO_CONTENT);
} catch (NoSuchRuleInRecipeException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.NOT_FOUND);
} catch (URISyntaxException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
}
// addCORSOrigin(servletContext, responseBuilder, headers);
return responseBuilder.build();
}
use of org.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class RuleParserTest method testParser.
@Test
public void testParser() {
try {
KB kReSKB = RuleParserImpl.parse("http://incubator.apache.org/stanbol/rules/test/", kReSRule);
if (kReSKB != null) {
RuleList kReSRuleList = kReSKB.getRuleList();
if (kReSRuleList != null) {
for (Rule kReSRule : kReSRuleList) {
log.debug("RULE : " + kReSRule.toString());
}
}
log.debug("RULE LIST IS NULL");
} else {
log.debug("KB IS NULL");
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class ClerezzaRuleStore method addRulesToRecipe.
/**
*
* @param recipeIRI
* the IRI of the recipe
* @param stanbolRule
* the rule in Rule syntax
*/
@Override
public Recipe addRulesToRecipe(Recipe recipe, String stanbolRule, String description) {
IRI recipeID = recipe.getRecipeID();
String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
RuleList ruleList = RuleParserImpl.parse(namespace, stanbolRule).getRuleList();
for (Rule rule : ruleList) {
recipe = addRuleToRecipe(recipe, rule, description);
}
return recipe;
}
Aggregations