Search in sources :

Example 1 with Recipe

use of org.apache.stanbol.rules.base.api.Recipe 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;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException)

Example 2 with Recipe

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

Example 3 with Recipe

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

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

the class RecipeList method removeAll.

public boolean removeAll(Collection<?> c) {
    if (contains(c)) {
        for (Object o : c) {
            boolean removed = false;
            for (int i = 0; i < recipes.length && !removed; i++) {
                Recipe recipe = recipes[i];
                if (recipe.equals(o)) {
                    Recipe[] recipesCopy = new Recipe[recipes.length - 1];
                    System.arraycopy(recipes, 0, recipesCopy, 0, i);
                    System.arraycopy(recipes, i + 1, recipesCopy, 0, recipesCopy.length - i);
                    recipes = recipesCopy;
                    removed = true;
                }
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : Recipe(org.apache.stanbol.rules.base.api.Recipe)

Example 5 with Recipe

use of org.apache.stanbol.rules.base.api.Recipe 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)

Aggregations

Recipe (org.apache.stanbol.rules.base.api.Recipe)35 IRI (org.apache.clerezza.commons.rdf.IRI)29 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)16 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)16 Graph (org.apache.clerezza.commons.rdf.Graph)11 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)9 Rule (org.apache.stanbol.rules.base.api.Rule)9 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)6 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)6 RefactoringException (org.apache.stanbol.rules.refactor.api.RefactoringException)6 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 RecipeImpl (org.apache.stanbol.rules.manager.RecipeImpl)5 InputStream (java.io.InputStream)4 RecipeEliminationException (org.apache.stanbol.rules.base.api.RecipeEliminationException)4 RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)4 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)4 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)4