use of org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException in project stanbol by apache.
the class ClerezzaRuleStore method findRulesByName.
@Override
public RuleList findRulesByName(String term) {
String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.ruleName + " ?name . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?name, \"" + term + "\", \"i\"))" + "}";
List<IRI> recipeIDs = listRecipeIDs();
Graph[] tripleCollections = new Graph[recipeIDs.size()];
for (int i = 0; i < tripleCollections.length; i++) {
tripleCollections[i] = tcManager.getGraph(recipeIDs.get(i));
}
UnionGraph unionGraph = new UnionGraph(tripleCollections);
RuleList matchingRules = new RuleList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
IRI ruleID = (IRI) solutionMapping.get("rule");
Literal description = (Literal) solutionMapping.get("description");
try {
Recipe recipe = getRecipe(recipeID);
Rule rule = new RecipeRule(recipe, getRule(recipe, ruleID));
if (description != null) {
rule.setDescription(description.getLexicalForm());
}
matchingRules.add(rule);
} catch (NoSuchRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (RecipeConstructionException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (NoSuchRuleInRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
}
}
} catch (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRules;
}
use of org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException 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.NoSuchRuleInRecipeException in project stanbol by apache.
the class RulesResource method removeRecipe.
/**
* This method allows to delete a recipe or a rule from the store.<br/>
* If the optional rule identifier id provided as second parameter that the rule is deleted. Otherwise it
* is the whole recipe to be deleted.
*
* @param recipe
* {@link String}
* @param rule
* {@link String} - OPTIONAL
* @param headers
* {@link HttpHeaders}
* @return <ul>
* <li>200 - if either the recipe or the rule is deleted</li>
* <li>204 - it is not possible to delete the rule because the internal construction of the recipe
* failed</li>
* <li>404 - the rule does not exist</li>
* <li>412 - the recipe to which the rule belongs does not exist</li>
* <li>500 - if a {@link RecipeEliminationException} exception is thrown</li>
* </ul>
*/
@DELETE
@Path("/recipe/{recipe:.+}")
public Response removeRecipe(@PathParam("recipe") String recipe, @QueryParam("rule") String rule, @Context HttpHeaders headers) {
ResponseBuilder responseBuilder;
boolean stop = false;
URI uri = null;
try {
uri = new URI(recipe);
} catch (URISyntaxException e1) {
log.error(e1.getMessage(), e1);
responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
stop = true;
}
if (!stop) {
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);
}
log.info("The recipe ID is : " + recipe);
if (rule != null && !rule.isEmpty()) {
Recipe rcp;
try {
rcp = ruleStore.getRecipe(new IRI(recipe));
Rule rl = ruleStore.getRule(rcp, new IRI(rule));
ruleStore.removeRule(rcp, rl);
} catch (NoSuchRecipeException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.PRECONDITION_FAILED);
} 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);
}
} else {
try {
ruleStore.removeRecipe(new IRI(recipe));
} catch (RecipeEliminationException e) {
log.error(e.getMessage(), e);
responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR);
}
}
}
responseBuilder = Response.ok();
// addCORSOrigin(servletContext, responseBuilder, headers);
return responseBuilder.build();
}
use of org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException in project stanbol by apache.
the class RuleStoreTest method getNotExistingRuleByNameInRecipeTest.
private void getNotExistingRuleByNameInRecipeTest() throws Exception {
Recipe recipe = store.getRecipe(new IRI("http://incubator.apache.com/stanbol/rules/test/recipeA"));
try {
recipe.getRule("ruleX");
Assert.fail();
} catch (NoSuchRuleInRecipeException e) {
Assert.assertTrue(true);
}
}
use of org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException in project stanbol by apache.
the class RulesResource method getRule.
/**
* Get a recipe from the rule base (that is the ontology that contains the rules and the recipe). <br/>
* If the second parameter is not null then the method returns the rule in the recipe identified by that
* parameter. <br/>
*
* curl -v -X GET http://localhost:8080/kres/rule/http
* ://kres.iks-project.eu/ontology/meta/rmi.owl#ProvaParentRule
*
* @param uri
* {A string contains the IRI full name of the rule.}
* @return Return: <br/>
* 200 The rule is retrieved (import declarations point to KReS Services) <br/>
* 404 The rule does not exists in the manager <br/>
* 500 Some error occurred
*/
@GET
@Path("/recipe/{recipe:.+}")
@Produces(value = { KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML, KRFormat.RDF_JSON, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL, MediaType.TEXT_PLAIN })
public Response getRule(@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(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();
}
Aggregations