Search in sources :

Example 1 with RuleList

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;
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) SPARQLObject(org.apache.stanbol.rules.base.api.SPARQLObject) Rule(org.apache.stanbol.rules.base.api.Rule)

Example 2 with RuleList

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;
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) Rule(org.apache.stanbol.rules.base.api.Rule) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) SimpleConstructQuery(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery)

Example 3 with RuleList

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;
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Rule(org.apache.stanbol.rules.base.api.Rule) SWRLRule(org.semanticweb.owlapi.model.SWRLRule)

Example 4 with RuleList

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;
}
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 5 with RuleList

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

Aggregations

RuleList (org.apache.stanbol.rules.base.api.util.RuleList)15 Rule (org.apache.stanbol.rules.base.api.Rule)13 IRI (org.apache.clerezza.commons.rdf.IRI)7 Graph (org.apache.clerezza.commons.rdf.Graph)5 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 Recipe (org.apache.stanbol.rules.base.api.Recipe)5 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)5 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)4 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)4 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)3 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)3 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)3 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)3 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)3 RecipeImpl (org.apache.stanbol.rules.manager.RecipeImpl)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2