Search in sources :

Example 6 with Rule

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

the class ClerezzaRuleStore method addRulesToRecipe.

/**
     * 
     * Parse the set of rules provided by the rulesStream parameter as Stanbol syntax rules and add them to
     * the Recipe in the store.<br/>
     * The recipe is a {@link Graph} managed by the {@link TcManager}.
     * 
     * 
     * @param recipe
     *            {@link Recipe} the recipe
     * @param rulesStream
     *            {@link InputStream} the rule in Stanbol syntax
     * 
     * @return the recipe with the new rule.
     */
@Override
public Recipe addRulesToRecipe(Recipe recipe, InputStream rulesStream, String description) {
    log.debug("Adding rule to recipe " + recipe);
    IRI recipeID = recipe.getRecipeID();
    String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
    log.info("Rule Namespace is " + namespace);
    RuleList ruleList = RuleParserImpl.parse(namespace, rulesStream).getRuleList();
    for (Rule rule : ruleList) {
        recipe = addRuleToRecipe(recipe, rule, description);
    }
    return recipe;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Rule(org.apache.stanbol.rules.base.api.Rule)

Example 7 with Rule

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

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

the class KB method write.

public void write(OutputStream outputStream) throws IOException {
    boolean firstIt = true;
    for (Rule ruleS : ruleList) {
        String rule;
        if (firstIt) {
            rule = ruleS.toString();
            firstIt = false;
        } else {
            rule = " . " + System.getProperty("line.separator") + ruleS.toString();
        }
        outputStream.write(rule.getBytes());
    }
    outputStream.close();
}
Also used : Rule(org.apache.stanbol.rules.base.api.Rule)

Example 9 with Rule

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

the class KB method write.

public void write(FileWriter fileWriter) throws IOException {
    boolean write = true;
    for (Rule rule : ruleList) {
        if (write) {
            fileWriter.write(rule.toString());
            write = false;
        } else {
            fileWriter.write(" . " + System.getProperty("line.separator") + rule.toString());
        }
    }
    fileWriter.close();
}
Also used : Rule(org.apache.stanbol.rules.base.api.Rule)

Example 10 with Rule

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

the class RecipeImpl method prettyPrint.

@Override
public String prettyPrint() {
    String separator = System.getProperty("line.separator");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("The recipe contains the following rules:");
    stringBuilder.append(separator);
    for (Rule rule : ruleList) {
        stringBuilder.append(rule.prettyPrint());
        stringBuilder.append(separator);
    }
    return stringBuilder.toString();
}
Also used : Rule(org.apache.stanbol.rules.base.api.Rule)

Aggregations

Rule (org.apache.stanbol.rules.base.api.Rule)29 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)13 IRI (org.apache.clerezza.commons.rdf.IRI)12 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)9 Recipe (org.apache.stanbol.rules.base.api.Recipe)8 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)5 Graph (org.apache.clerezza.commons.rdf.Graph)4 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)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 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