use of org.apache.stanbol.rules.base.api.RecipeConstructionException in project stanbol by apache.
the class ClerezzaRuleStore method listRecipes.
@Override
public RecipeList listRecipes() throws NoSuchRecipeException, RecipeConstructionException {
RecipeList recipeList = new RecipeList();
for (IRI recipeID : recipes) {
Recipe recipe;
try {
recipe = getRecipe(recipeID);
} catch (NoSuchRecipeException e) {
throw e;
} catch (RecipeConstructionException e) {
throw e;
}
recipeList.add(recipe);
}
log.info("The Clerezza rule store contains {} recipes", recipeList.size());
return recipeList;
}
use of org.apache.stanbol.rules.base.api.RecipeConstructionException in project stanbol by apache.
the class ClerezzaRuleStore method findRecipesByDescription.
@Override
public RecipeList findRecipesByDescription(String term) {
String sparql = "SELECT ?recipe " + "WHERE { ?recipe a " + Symbols.Recipe.toString() + " . " + "?recipe " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + term + "\", \"i\"))" + "}";
Graph tripleCollection = tcManager.getGraph(new IRI(recipeIndexLocation));
RecipeList matchingRecipes = new RecipeList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, tripleCollection);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
try {
Recipe recipe = getRecipe(recipeID);
log.info("Found recipe {}.", recipeID.toString());
matchingRecipes.add(recipe);
log.info("Found {} matching recipes.", matchingRecipes.size());
} 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 (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRecipes;
}
use of org.apache.stanbol.rules.base.api.RecipeConstructionException 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.RecipeConstructionException in project stanbol by apache.
the class RulesResource method addRulesToRecipe.
/**
* Add rules to a recipe. An optional description can be provided to the rules.
*
* @param recipe
* {A string contains the IRI of the recipe to be added}
* @param rules
* {A string contains the rules in Stanbol syntax}
* @param description
* {A string contains a description of the rule}
* @param headers
* {The {@link HttpHeaders}
* @return Return: <br/>
* 200 The recipe has been added<br/>
* 409 The recipe has not been added<br/>
* 500 Some error occurred
*/
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(value = { KRFormat.TEXT_PLAIN, KRFormat.RDF_JSON })
@Path("/recipe/{recipe:.+}")
public Response addRulesToRecipe(@PathParam(value = "recipe") String recipe, MultiPartBody data, @Context HttpHeaders headers) {
String description = null;
InputStream rules = null;
if (data.getTextParameterValues("description") != null) {
description = data.getTextParameterValues("description")[0];
}
if (data.getFormFileParameterValues("rules") != null) {
rules = new ByteArrayInputStream(data.getFormFileParameterValues("rules")[0].getContent());
}
if (recipe == null || rules == null || description == null) {
throw new WebApplicationException(BAD_REQUEST);
}
ResponseBuilder responseBuilder;
Recipe rcp;
try {
URI uri = new URI(recipe);
if (uri.getScheme() == null) {
recipe = "urn:" + recipe;
log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
}
rcp = ruleStore.getRecipe(new IRI(recipe));
ruleStore.addRulesToRecipe(rcp, rules, description);
responseBuilder = Response.ok();
} 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.INTERNAL_SERVER_ERROR);
} 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.RecipeConstructionException in project stanbol by apache.
the class RulesResource method adaptTo.
/**
* Return the String representation of a recipe adapted to another format, e.g., Jena rules, SPARQL
* CONSTRUCTs, Clerezza, SWRL, etc.
*
* @param recipe
* {The ID of the recipe}
* @param format
* {The canonical name of the class we want have back, e.g.,
* org.apache.stanbol.rules.base.api.Rule for Jena Rules}
* @param headers
* {@link HttpHeaders}
* @return <ul>
* <li>200: it works properly and the string representation of the recipe according to the format
* provided is returned</li>
* <li>204: the recipe does not exist in the store</li>
* <li>403: a class exists for the format provided but there is no adapter for that</li>
* <li>404: no class exists in the context for the format provided</li>
* <li>406: some error occurred while converting a rule of the recipe</li>
* <li>409: some atom of a rule in the recipe cannot be converted to the format provided</li>
* </ul>
*/
@GET
@Produces(value = { KRFormat.RDF_JSON })
@Path("/adapters/{recipe:.+}")
public Response adaptTo(@PathParam("recipe") String recipe, @QueryParam("format") String format, @Context HttpHeaders headers) {
ResponseBuilder responseBuilder = null;
Class<?> classToLoad;
try {
// ClassLoader loader = Thread.currentThread().getContextClassLoader();
// classToLoad = loader.loadClass(format);
classToLoad = Class.forName(format);
URI uri = new URI(recipe);
if (uri.getScheme() == null) {
recipe = "urn:" + recipe;
log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
}
Recipe rcp = ruleStore.getRecipe(new IRI(recipe));
RuleAdapter adapter = adapterManager.getAdapter(rcp, classToLoad);
Object adaptedRecipe = adapter.adaptTo(rcp, classToLoad);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("recipe", rcp.getRecipeID().toString());
jsonObject.put("adaptedTo", format);
jsonObject.put("result", adaptedRecipe.toString());
} catch (JSONException e) {
log.error(e.getMessage(), e);
}
responseBuilder = Response.ok(jsonObject.toString());
} catch (ClassNotFoundException e) {
responseBuilder = Response.status(Status.NOT_FOUND);
log.error(e.getMessage(), e);
} catch (NoSuchRecipeException e) {
responseBuilder = Response.status(Status.NO_CONTENT);
log.error(e.getMessage(), e);
} catch (RecipeConstructionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnavailableRuleObjectException e) {
responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
log.error(e.getMessage(), e);
} catch (RuleAtomCallExeption e) {
responseBuilder = Response.status(Status.CONFLICT);
log.error(e.getMessage(), e);
} catch (UnsupportedTypeForExportException e) {
responseBuilder = Response.status(Status.FORBIDDEN);
log.error(e.getMessage(), e);
} catch (URISyntaxException e) {
responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
log.error(e.getMessage(), e);
}
// addCORSOrigin(servletContext, responseBuilder, headers);
return responseBuilder.build();
}
Aggregations