Author: anuzzolese
Date: Wed Mar 28 22:58:52 2012
New Revision: 1306634
URL: http://svn.apache.org/viewvc?rev=1306634&view=rev
Log:
[STANBOL-560] Added the plain text and HTML serializations for recipes.
Modified:
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RulesResource.java
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/writers/RecipeWriter.java
Modified:
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RulesResource.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RulesResource.java?rev=1306634&r1=1306633&r2=1306634&view=diff
==============================================================================
---
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RulesResource.java
(original)
+++
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/resources/RulesResource.java
Wed Mar 28 22:58:52 2012
@@ -197,7 +197,7 @@ public class RulesResource extends BaseS
@GET
@Path("/recipe/{recipe:.+}")
@Produces(value = {KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML,
KRFormat.RDF_JSON,
- KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL})
+ KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL,
MediaType.TEXT_PLAIN})
public Response getRule(@PathParam("recipe") String recipeID,
@QueryParam("rule") String ruleID,
@Context HttpHeaders headers) {
@@ -233,6 +233,45 @@ public class RulesResource extends BaseS
addCORSOrigin(servletContext, responseBuilder, headers);
return responseBuilder.build();
}
+
+ @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 {
+ recipe = ruleStore.getRecipe(new UriRef(recipeID));
+
+ if (ruleID != null && !ruleID.isEmpty()) {
+ rule = ruleStore.getRule(recipe, new UriRef(ruleID));
+ RuleList ruleList = new RuleList();
+ ruleList.add(rule);
+
+ recipe = new RecipeImpl(recipe.getRecipeID(),
recipe.getRecipeDescription(), ruleList);
+ }
+
+ responseBuilder = Response.ok(new Viewable("rules",
recipe.toString()));
+
+ } 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);
+ }
+
+ addCORSOrigin(servletContext, responseBuilder, headers);
+ return responseBuilder.build();
+ }
/**
* This method implements a REST service that allows to create a new empty
recipe in the store with a
Modified:
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/writers/RecipeWriter.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/writers/RecipeWriter.java?rev=1306634&r1=1306633&r2=1306634&view=diff
==============================================================================
---
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/writers/RecipeWriter.java
(original)
+++
incubator/stanbol/trunk/rules/web/src/main/java/org/apache/stanbol/rules/web/writers/RecipeWriter.java
Wed Mar 28 22:58:52 2012
@@ -69,7 +69,7 @@ import org.slf4j.LoggerFactory;
@Provider
@Produces({KRFormat.RDF_XML, KRFormat.OWL_XML, KRFormat.MANCHESTER_OWL,
KRFormat.FUNCTIONAL_OWL,
- KRFormat.TURTLE, KRFormat.RDF_JSON})
+ KRFormat.TURTLE, KRFormat.RDF_JSON, MediaType.TEXT_PLAIN})
public class RecipeWriter implements MessageBodyWriter<Recipe> {
protected Serializer serializer;
@@ -119,139 +119,152 @@ public class RecipeWriter implements Mes
log.debug("Rendering the list of recipes.");
- OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
- OWLDataFactory factory = OWLManager.getOWLDataFactory();
-
- OWLOntology ontology;
- try {
- ontology = manager.createOntology();
-
- RuleList rules = recipe.getRuleList();
-
- UriRef recipeID = recipe.getRecipeID();
-
- String recipeURI = recipeID.toString().replace("<",
"").replace(">", "");
- IRI recipeIRI = IRI.create(recipeURI);
- OWLIndividual recipeIndividual =
factory.getOWLNamedIndividual(recipeIRI);
-
- String descriptionURI =
Symbols.description.toString().replace("<", "").replace(">", "");
- IRI descriptionIRI = IRI.create(descriptionURI);
- OWLDataProperty descriptionProperty =
factory.getOWLDataProperty(descriptionIRI);
-
- OWLAxiom axiom;
+ if(mediaType.toString().equals(MediaType.TEXT_PLAIN)){
+ String recipeString = recipe.toString();
- String recipeDescription = recipe.getRecipeDescription();
- if(recipeDescription != null){
- axiom =
factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual,
- recipeDescription);
- manager.addAxiom(ontology, axiom);
- }
+ out.write(recipeString.getBytes());
+ }
+ else if(mediaType.toString().equals(MediaType.TEXT_HTML)){
+ String recipeString = recipe.toString();
- if(rules != null){
- for (Rule rule : rules) {
- UriRef 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("\\->");
-
- IRI ruleIRI = IRI.create(ruleURI);
-
- IRI ruleNameIRI = IRI.create(ruleNameURI);
- IRI ruleBodyIRI = IRI.create(ruleBodyURI);
- IRI ruleHeadIRI = IRI.create(ruleHeadURI);
- IRI hasRuleIRI = 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);
+ out.write(recipeString.getBytes());
+ }
+
+ else{
+ OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
+ OWLDataFactory factory = OWLManager.getOWLDataFactory();
+
+ OWLOntology ontology;
+ try {
+ ontology = manager.createOntology();
+
+ RuleList rules = recipe.getRuleList();
+
+ UriRef recipeID = recipe.getRecipeID();
+
+ String recipeURI = recipeID.toString().replace("<",
"").replace(">", "");
+ IRI recipeIRI = IRI.create(recipeURI);
+ OWLIndividual recipeIndividual =
factory.getOWLNamedIndividual(recipeIRI);
+
+ String descriptionURI =
Symbols.description.toString().replace("<", "").replace(">", "");
+ IRI descriptionIRI = 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);
-
- // add the description to the rule individual
- if(ruleDescription != null){
- axiom =
factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, ruleIndividual,
- ruleDescription);
+ }
+
+ if(rules != null){
+ for (Rule rule : rules) {
+ UriRef 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("\\->");
+
+ IRI ruleIRI = IRI.create(ruleURI);
+
+ IRI ruleNameIRI = IRI.create(ruleNameURI);
+ IRI ruleBodyIRI = IRI.create(ruleBodyURI);
+ IRI ruleHeadIRI = IRI.create(ruleHeadURI);
+ IRI hasRuleIRI = 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);
+
}
+ }
- // add the rule body to the rule individual
- axiom =
factory.getOWLDataPropertyAssertionAxiom(ruleBodyProperty, ruleIndividual,
- ruleParts[0]);
- manager.addAxiom(ontology, axiom);
+ /*
+ * Write the ontology with the list of recipes
+ */
+
+ 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)) {
- // add the rule head to the rule individual
- axiom =
factory.getOWLDataPropertyAssertionAxiom(ruleHeadProperty, ruleIndividual,
- ruleParts[1]);
- manager.addAxiom(ontology, axiom);
+ TripleCollection mGraph =
OWLAPIToClerezzaConverter.owlOntologyToClerezzaMGraph(ontology);
- // bind the rule to the recipe
- axiom =
factory.getOWLObjectPropertyAssertionAxiom(hasRule, recipeIndividual,
ruleIndividual);
- manager.addAxiom(ontology, axiom);
+ RdfJsonSerializingProvider provider = new
RdfJsonSerializingProvider();
+ provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
}
+ } catch (OWLOntologyCreationException e1) {
+ log.error("An error occurred.", e1);
}
-
- /*
- * Write the ontology with the list of recipes
- */
-
- 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)) {
-
- TripleCollection mGraph =
OWLAPIToClerezzaConverter.owlOntologyToClerezzaMGraph(ontology);
-
- RdfJsonSerializingProvider provider = new
RdfJsonSerializingProvider();
- provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
-
- }
- } catch (OWLOntologyCreationException e1) {
- log.error("An error occurred.", e1);
}
out.flush();