Search in sources :

Example 11 with RecipeConstructionException

use of org.apache.stanbol.rules.base.api.RecipeConstructionException 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 12 with RecipeConstructionException

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();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) Recipe(org.apache.stanbol.rules.base.api.Recipe) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) 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) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 13 with RecipeConstructionException

use of org.apache.stanbol.rules.base.api.RecipeConstructionException in project stanbol by apache.

the class RefactoringTest method refactoringWithARecipeWithNotSupportedAtoms.

@Test
public void refactoringWithARecipeWithNotSupportedAtoms() {
    String separator = System.getProperty("line.separator");
    // the localname atom is not supported by the clerezza adapter and should throw an exception.
    String rule = "kres = <http://kres.iks-project.eu/ontology.owl#> . " + separator + "foaf = <http://xmlns.com/foaf/0.1/> . " + separator + "rule2[ is(kres:Person, ?x) . same(localname(?y), \"text\") -> is(foaf:Person, ?x) ]";
    try {
        Recipe recipe = store.getRecipe(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/recipeA"));
        recipe = store.addRulesToRecipe(recipe, rule, "Test");
        refactorer.graphRefactoring(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/refactoredGraph"), new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/graph"), new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/recipeA"));
    } catch (NoSuchRecipeException e) {
        Assert.fail();
    } catch (RecipeConstructionException e) {
        Assert.fail();
    } catch (RefactoringException e) {
        Assert.assertTrue(e.getMessage(), true);
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Test(org.junit.Test)

Example 14 with RecipeConstructionException

use of org.apache.stanbol.rules.base.api.RecipeConstructionException in project stanbol by apache.

the class ClerezzaRuleStore method findRulesByDescription.

@Override
public RuleList findRulesByDescription(String term) {
    String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + 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 15 with RecipeConstructionException

use of org.apache.stanbol.rules.base.api.RecipeConstructionException in project stanbol by apache.

the class ClerezzaRuleStore method getRecipe.

@Override
public Recipe getRecipe(IRI recipeID) throws NoSuchRecipeException, RecipeConstructionException {
    log.info("Called get recipe for id: " + recipeID);
    Graph recipeGraph = null;
    /**
         * Throw a NoSuchRecipeException in case of the TcManager throws a NoSuchEntityException with respect
         * to IRI representing the recipe.
         */
    try {
        recipeGraph = tcManager.getGraph(recipeID);
    } catch (NoSuchEntityException e) {
        throw new NoSuchRecipeException(recipeID.toString());
    }
    Iterator<Triple> descriptions = recipeGraph.filter(null, Symbols.description, null);
    String recipeDescription = null;
    if (descriptions != null && descriptions.hasNext()) {
        recipeDescription = descriptions.next().getObject().toString();
    }
    String query = "SELECT ?rule ?ruleName ?ruleBody ?ruleHead " + "WHERE { " + "	" + recipeID.toString() + " " + Symbols.hasRule.toString() + " ?rule . " + "	?rule " + Symbols.ruleName.toString() + " ?ruleName . " + "	?rule " + Symbols.ruleBody.toString() + " ?ruleBody . " + "	?rule " + Symbols.ruleHead.toString() + " ?ruleHead . " + "}";
    Query sparql;
    try {
        sparql = QueryParser.getInstance().parse(query);
        ResultSet resultSet = tcManager.executeSparqlQuery((SelectQuery) sparql, recipeGraph);
        StringBuilder stanbolRulesBuilder = new StringBuilder();
        boolean firstIteration = true;
        while (resultSet.hasNext()) {
            SolutionMapping solutionMapping = resultSet.next();
            RDFTerm nameResource = solutionMapping.get("ruleName");
            RDFTerm bodyResource = solutionMapping.get("ruleBody");
            RDFTerm headResource = solutionMapping.get("ruleHead");
            StringBuilder stanbolRuleBuilder = new StringBuilder();
            stanbolRuleBuilder.append(((Literal) nameResource).getLexicalForm());
            stanbolRuleBuilder.append("[");
            stanbolRuleBuilder.append(((Literal) bodyResource).getLexicalForm());
            stanbolRuleBuilder.append(" -> ");
            stanbolRuleBuilder.append(((Literal) headResource).getLexicalForm());
            stanbolRuleBuilder.append("]");
            if (!firstIteration) {
                stanbolRulesBuilder.append(" . ");
            } else {
                firstIteration = false;
            }
            String stanbolSyntax = stanbolRuleBuilder.toString();
            log.info("Rule content {}", stanbolSyntax);
            stanbolRulesBuilder.append(stanbolSyntax);
        }
        String stanbolSyntax = stanbolRulesBuilder.toString();
        RuleList ruleList = null;
        if (!stanbolSyntax.isEmpty()) {
            String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
            ruleList = RuleParserImpl.parse(namespace, stanbolSyntax).getRuleList();
        }
        return new RecipeImpl(recipeID, recipeDescription, ruleList);
    } catch (ParseException e) {
        throw new RecipeConstructionException(e);
    }
}
Also used : SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Triple(org.apache.clerezza.commons.rdf.Triple) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Aggregations

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