Search in sources :

Example 21 with Rule

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

the class ClerezzaRuleStore method addRulesToRecipe.

/**
     * 
     * @param recipeIRI
     *            the IRI of the recipe
     * @param stanbolRule
     *            the rule in Rule syntax
     */
@Override
public Recipe addRulesToRecipe(Recipe recipe, String stanbolRule, String description) {
    IRI recipeID = recipe.getRecipeID();
    String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
    RuleList ruleList = RuleParserImpl.parse(namespace, stanbolRule).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 22 with Rule

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

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

the class ClerezzaRuleStore method main.

public static void main(String[] args) {
    InputStream inputStream;
    try {
        inputStream = new FileInputStream(new File("/Users/mac/Desktop/domain.rule"));
        RuleList ruleList = RuleParserImpl.parse("http://www.prova.it/", inputStream).getRuleList();
        for (Rule rule : ruleList) {
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Rule(org.apache.stanbol.rules.base.api.Rule) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with Rule

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

the class RecipeImpl method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    String separator = System.getProperty("line.separator");
    boolean firstLoop = true;
    for (Rule rule : ruleList) {
        if (!firstLoop) {
            sb.append(" . ");
            sb.append(separator);
        } else {
            firstLoop = false;
        }
        sb.append(rule.toString());
    }
    return sb.toString();
}
Also used : Rule(org.apache.stanbol.rules.base.api.Rule)

Example 25 with Rule

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

the class RecipeImpl method getRule.

@Override
public Rule getRule(IRI ruleID) throws NoSuchRuleInRecipeException {
    for (Rule rule : ruleList) {
        if (rule.getRuleID().toString().equals(ruleID.toString())) {
            return rule;
        }
    }
    StringBuilder message = new StringBuilder();
    message.append("No rule with ID ");
    message.append(ruleID.toString());
    message.append(" exists in recipe ");
    message.append(this.getRecipeID());
    throw new NoSuchRuleInRecipeException(message.toString());
}
Also used : NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) 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