Search in sources :

Example 11 with RuleList

use of org.apache.stanbol.rules.base.api.util.RuleList 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 12 with RuleList

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

the class ClerezzaRuleStore method getRecipe.

@Override
public Recipe getRecipe(IRI recipeID) throws NoSuchRecipeException, RecipeConstructionException {
    log.info("Called get recipe for id: " + recipeID);
    Graph recipeGraph = null;
    /**
         * Throw a NoSuchRecipeException in case of the TcManager throws a NoSuchEntityException with respect
         * to IRI representing the recipe.
         */
    try {
        recipeGraph = tcManager.getGraph(recipeID);
    } catch (NoSuchEntityException e) {
        throw new NoSuchRecipeException(recipeID.toString());
    }
    Iterator<Triple> descriptions = recipeGraph.filter(null, Symbols.description, null);
    String recipeDescription = null;
    if (descriptions != null && descriptions.hasNext()) {
        recipeDescription = descriptions.next().getObject().toString();
    }
    String query = "SELECT ?rule ?ruleName ?ruleBody ?ruleHead " + "WHERE { " + "	" + recipeID.toString() + " " + Symbols.hasRule.toString() + " ?rule . " + "	?rule " + Symbols.ruleName.toString() + " ?ruleName . " + "	?rule " + Symbols.ruleBody.toString() + " ?ruleBody . " + "	?rule " + Symbols.ruleHead.toString() + " ?ruleHead . " + "}";
    Query sparql;
    try {
        sparql = QueryParser.getInstance().parse(query);
        ResultSet resultSet = tcManager.executeSparqlQuery((SelectQuery) sparql, recipeGraph);
        StringBuilder stanbolRulesBuilder = new StringBuilder();
        boolean firstIteration = true;
        while (resultSet.hasNext()) {
            SolutionMapping solutionMapping = resultSet.next();
            RDFTerm nameResource = solutionMapping.get("ruleName");
            RDFTerm bodyResource = solutionMapping.get("ruleBody");
            RDFTerm headResource = solutionMapping.get("ruleHead");
            StringBuilder stanbolRuleBuilder = new StringBuilder();
            stanbolRuleBuilder.append(((Literal) nameResource).getLexicalForm());
            stanbolRuleBuilder.append("[");
            stanbolRuleBuilder.append(((Literal) bodyResource).getLexicalForm());
            stanbolRuleBuilder.append(" -> ");
            stanbolRuleBuilder.append(((Literal) headResource).getLexicalForm());
            stanbolRuleBuilder.append("]");
            if (!firstIteration) {
                stanbolRulesBuilder.append(" . ");
            } else {
                firstIteration = false;
            }
            String stanbolSyntax = stanbolRuleBuilder.toString();
            log.info("Rule content {}", stanbolSyntax);
            stanbolRulesBuilder.append(stanbolSyntax);
        }
        String stanbolSyntax = stanbolRulesBuilder.toString();
        RuleList ruleList = null;
        if (!stanbolSyntax.isEmpty()) {
            String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
            ruleList = RuleParserImpl.parse(namespace, stanbolSyntax).getRuleList();
        }
        return new RecipeImpl(recipeID, recipeDescription, ruleList);
    } catch (ParseException e) {
        throw new RecipeConstructionException(e);
    }
}
Also used : SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Triple(org.apache.clerezza.commons.rdf.Triple) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Example 13 with RuleList

use of org.apache.stanbol.rules.base.api.util.RuleList 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 14 with RuleList

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

the class JenaAdapter method adaptRecipeTo.

@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRecipeTo(Recipe recipe, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
    List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = null;
    if (type == com.hp.hpl.jena.reasoner.rulesys.Rule.class) {
        RuleList ruleList = recipe.getRuleList();
        Iterator<Rule> ruleIterator = ruleList.iterator();
        jenaRules = new ArrayList<com.hp.hpl.jena.reasoner.rulesys.Rule>();
        for (int i = 0; ruleIterator.hasNext(); i++) {
            jenaRules.add((com.hp.hpl.jena.reasoner.rulesys.Rule) adaptRuleTo(ruleIterator.next(), type));
        }
    } else {
        throw new UnsupportedTypeForExportException("The Jena Adapter does not support the selected serialization : " + type.getCanonicalName());
    }
    return (T) jenaRules;
}
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)

Example 15 with RuleList

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

the class RulesResource method getRule.

/**
     * Get a recipe from the rule base (that is the ontology that contains the rules and the recipe). <br/>
     * If the second parameter is not null then the method returns the rule in the recipe identified by that
     * parameter. <br/>
     * 
     * curl -v -X GET http://localhost:8080/kres/rule/http
     * ://kres.iks-project.eu/ontology/meta/rmi.owl#ProvaParentRule
     * 
     * @param uri
     *            {A string contains the IRI full name of the rule.}
     * @return Return: <br/>
     *         200 The rule is retrieved (import declarations point to KReS Services) <br/>
     *         404 The rule does not exists in the manager <br/>
     *         500 Some error occurred
     * 
     */
@GET
@Path("/recipe/{recipe:.+}")
@Produces(value = { KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML, KRFormat.RDF_JSON, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL, MediaType.TEXT_PLAIN })
public Response getRule(@PathParam("recipe") String recipeID, @QueryParam("rule") String ruleID, @Context HttpHeaders headers) {
    Recipe recipe;
    Rule rule;
    ResponseBuilder responseBuilder;
    try {
        URI uri = new URI(recipeID);
        if (uri.getScheme() == null) {
            recipeID = "urn:" + recipeID;
            log.info("The recipe ID is a URI without scheme. The ID is set to " + recipeID);
        }
        recipe = ruleStore.getRecipe(new IRI(recipeID));
        if (ruleID != null && !ruleID.isEmpty()) {
            rule = ruleStore.getRule(recipe, new IRI(ruleID));
            RuleList ruleList = new RuleList();
            ruleList.add(rule);
            recipe = new RecipeImpl(recipe.getRecipeID(), recipe.getRecipeDescription(), ruleList);
        }
        responseBuilder = Response.ok(recipe);
    } catch (NoSuchRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.NOT_FOUND);
    } catch (RecipeConstructionException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.NO_CONTENT);
    } catch (NoSuchRuleInRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.NOT_FOUND);
    } catch (URISyntaxException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) Rule(org.apache.stanbol.rules.base.api.Rule) URISyntaxException(java.net.URISyntaxException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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