Search in sources :

Example 1 with RecipeList

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

the class ClerezzaRuleStore method findRecipesByDescription.

@Override
public RecipeList findRecipesByDescription(String term) {
    String sparql = "SELECT ?recipe " + "WHERE { ?recipe a " + Symbols.Recipe.toString() + " . " + "?recipe " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + term + "\", \"i\"))" + "}";
    Graph tripleCollection = tcManager.getGraph(new IRI(recipeIndexLocation));
    RecipeList matchingRecipes = new RecipeList();
    try {
        SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
        ResultSet resultSet = tcManager.executeSparqlQuery(query, tripleCollection);
        while (resultSet.hasNext()) {
            SolutionMapping solutionMapping = resultSet.next();
            IRI recipeID = (IRI) solutionMapping.get("recipe");
            try {
                Recipe recipe = getRecipe(recipeID);
                log.info("Found recipe {}.", recipeID.toString());
                matchingRecipes.add(recipe);
                log.info("Found {} matching recipes.", matchingRecipes.size());
            } 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 (ParseException e) {
        log.error("The sparql query contains errors: ", e);
    }
    return matchingRecipes;
}
Also used : SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) IRI(org.apache.clerezza.commons.rdf.IRI) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException)

Example 2 with RecipeList

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

the class ClerezzaRuleStore method listRecipes.

@Override
public RecipeList listRecipes() throws NoSuchRecipeException, RecipeConstructionException {
    RecipeList recipeList = new RecipeList();
    for (IRI recipeID : recipes) {
        Recipe recipe;
        try {
            recipe = getRecipe(recipeID);
        } catch (NoSuchRecipeException e) {
            throw e;
        } catch (RecipeConstructionException e) {
            throw e;
        }
        recipeList.add(recipe);
    }
    log.info("The Clerezza rule store contains {} recipes", recipeList.size());
    return recipeList;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException)

Example 3 with RecipeList

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

the class RulesResource method listRecipes.

@GET
@Path("/recipe")
@Produces(value = { KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML, KRFormat.RDF_JSON, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL })
public Response listRecipes(@Context HttpHeaders headers) {
    ResponseBuilder responseBuilder = null;
    try {
        RecipeList recipeList = getListRecipes();
        responseBuilder = Response.ok(recipeList);
    } 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.INTERNAL_SERVER_ERROR);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with RecipeList

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

the class RuleStoreTest method removeRecipeTest.

private void removeRecipeTest() throws Exception {
    RecipeList recipeListInitial = store.listRecipes();
    Recipe[] initialRecipes = new Recipe[recipeListInitial.size()];
    initialRecipes = recipeListInitial.toArray(initialRecipes);
    Recipe recipe = store.getRecipe(new IRI("http://incubator.apache.com/stanbol/rules/test/recipeA"));
    store.removeRecipe(recipe);
    RecipeList recipeListFinal = store.listRecipes();
    Recipe[] finalRecipes = new Recipe[recipeListInitial.size()];
    finalRecipes = recipeListFinal.toArray(finalRecipes);
    Assert.assertNotSame(initialRecipes, finalRecipes);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList)

Example 5 with RecipeList

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

the class RulesResource method findRecipes.

/**
     * It returns the list of recipes whose descriptions match the string passed in the description parameter.<br>
     * If some recipe matches the description passed a 200 code with the list of recipes is returned.
     * Otherwise a 404 status code is returned.
     * 
     * @param description
     *            {@link String}
     * @return <ul>
     *         <li>200: The list of recipe matching the description provided is returned</li>
     *         <li>404: No recipe matches the description provided</li>
     *         </ul>
     */
@GET
@Produces(value = { MediaType.APPLICATION_JSON, KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML, KRFormat.RDF_JSON, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL, MediaType.TEXT_PLAIN })
@Path("/find/recipes")
public Response findRecipes(@QueryParam("description") String description) {
    log.info("Searching for recipes with description like to {}.", description);
    RecipeList recipes = ruleStore.findRecipesByDescription(description);
    log.info("The recipe list is emplty? {} ", recipes.isEmpty());
    if (recipes.isEmpty()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    return Response.ok(recipes).build();
}
Also used : RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RecipeList (org.apache.stanbol.rules.base.api.util.RecipeList)5 IRI (org.apache.clerezza.commons.rdf.IRI)3 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)3 Recipe (org.apache.stanbol.rules.base.api.Recipe)3 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)1 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)1 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)1 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)1 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)1