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;
}
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;
}
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();
}
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();
}
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();
}
Aggregations