use of org.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class SPARQLAdapter method adaptRecipeTo.
@SuppressWarnings("unchecked")
protected <T> T adaptRecipeTo(Recipe recipe, Class<T> type) throws UnsupportedTypeForExportException, UnavailableRuleObjectException {
List<SPARQLObject> sparqlObjects = null;
if (type == SPARQLObject.class) {
RuleList ruleList = recipe.getRuleList();
Iterator<Rule> ruleIterator = ruleList.iterator();
sparqlObjects = new ArrayList<SPARQLObject>();
for (int i = 0; ruleIterator.hasNext(); i++) {
sparqlObjects.add((SPARQLObject) adaptRuleTo(ruleIterator.next(), type));
}
} else {
throw new UnsupportedTypeForExportException("The SPARQL Export Provider does not support the selected serialization : " + type.getCanonicalName());
}
return (T) sparqlObjects;
}
use of org.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class ClerezzaAdapter method adaptRecipeTo.
@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRecipeTo(Recipe recipe, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
List<ConstructQuery> constructQueries = null;
if (type == ConstructQuery.class) {
constructQueries = new ArrayList<ConstructQuery>();
RuleList ruleList = recipe.getRuleList();
Iterator<Rule> ruleIterator = ruleList.iterator();
for (int i = 0; ruleIterator.hasNext(); i++) {
constructQueries.add((ConstructQuery) adaptRuleTo(ruleIterator.next(), type));
}
} else {
throw new UnsupportedTypeForExportException("The adapter for Clerezza does not support the selected serialization : " + type.getCanonicalName());
}
return (T) constructQueries;
}
use of org.apache.stanbol.rules.base.api.util.RuleList in project stanbol by apache.
the class SWRLAdapter method adaptRecipeTo.
@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRecipeTo(Recipe recipe, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
List<SWRLRule> swrlRules = null;
if (type == SWRLRule.class) {
RuleList ruleList = recipe.getRuleList();
swrlRules = new ArrayList<SWRLRule>();
if (ruleList != null && !ruleList.isEmpty()) {
Iterator<Rule> ruleIterator = ruleList.iterator();
while (ruleIterator.hasNext()) {
Rule rule = ruleIterator.next();
swrlRules.add(adaptRuleTo(rule, SWRLRule.class));
}
}
} else {
throw new UnsupportedTypeForExportException("The SPARQL Export Provider does not support the selected serialization : " + type.getCanonicalName());
}
return (T) swrlRules;
}
use of org.apache.stanbol.rules.base.api.util.RuleList 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.util.RuleList 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;
}
Aggregations