Search in sources :

Example 1 with NoSuchRuleInRecipeException

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;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Literal(org.apache.clerezza.commons.rdf.Literal) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) Rule(org.apache.stanbol.rules.base.api.Rule) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Example 2 with NoSuchRuleInRecipeException

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();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) Rule(org.apache.stanbol.rules.base.api.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with NoSuchRuleInRecipeException

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();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) Rule(org.apache.stanbol.rules.base.api.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 4 with NoSuchRuleInRecipeException

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);
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe)

Example 5 with NoSuchRuleInRecipeException

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();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) Rule(org.apache.stanbol.rules.base.api.Rule) URISyntaxException(java.net.URISyntaxException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)11 IRI (org.apache.clerezza.commons.rdf.IRI)9 Recipe (org.apache.stanbol.rules.base.api.Recipe)9 Rule (org.apache.stanbol.rules.base.api.Rule)9 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)5 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 Path (javax.ws.rs.Path)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 Graph (org.apache.clerezza.commons.rdf.Graph)2 Literal (org.apache.clerezza.commons.rdf.Literal)2 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)2 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)2 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)2 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)2 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)2