Search in sources :

Example 1 with NoSuchRecipeException

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

the class RecipeInputProvider method getInput.

@SuppressWarnings("unchecked")
@Override
public <T> Iterator<T> getInput(Class<T> type) throws IOException {
    ReasoningProvider reasoningProvider = null;
    if (type.isAssignableFrom(SWRLRule.class)) {
        reasoningProvider = ReasoningProvider.OWL2;
    } else if (type.isAssignableFrom(Rule.class)) {
        reasoningProvider = ReasoningProvider.Jena;
    } else {
        log.error("Cannot adapt to this type {}", type.getCanonicalName());
        throw new UnsupportedOperationException("Cannot adapt to " + type.getCanonicalName());
    }
    switch(reasoningProvider) {
        case OWL2:
            List<SWRLRule> rules = null;
            if (recipeId != null) {
                long start = System.currentTimeMillis();
                log.info("[start] Prepare rules for OWLApi ");
                // If recipe exists, return it as a list of SWRL rules
                rules = new ArrayList<SWRLRule>();
                try {
                    Recipe recipe = null;
                    synchronized (store) {
                        try {
                            recipe = store.getRecipe(new IRI(recipeId));
                        } catch (RecipeConstructionException e) {
                            log.error("An error occurred while generating the recipe.", e);
                        }
                    }
                    log.debug("Recipe is: {}", recipe);
                    /*
	                 * We ask to the adapter manager to get the right adapter in order to transform
	                 * recipes into SWRLRule objects.
	                 */
                    RuleAdapter adapter;
                    try {
                        adapter = adapterManager.getAdapter(recipe, SWRLRule.class);
                        rules = (List<SWRLRule>) adapter.adaptTo(recipe, SWRLRule.class);
                    } catch (UnavailableRuleObjectException e) {
                        log.error(e.getMessage(), e);
                    } catch (RuleAtomCallExeption e) {
                        log.error(e.getMessage(), e);
                    } catch (UnsupportedTypeForExportException e) {
                        log.error(e.getMessage(), e);
                    }
                /*
	                RuleList ruleList = recipe.getRuleList();
	                log.debug("RuleList is: {}",ruleList);
	                for(org.apache.stanbol.rules.base.api.Rule r : ruleList ){
	                    SWRLRule swrl = r.toSWRL(OWLManager.getOWLDataFactory());
	                    log.debug("Prepared rule: {}",swrl);
	                    rules.add(swrl);
	                }*/
                } catch (NoSuchRecipeException e) {
                    log.error("Recipe {} does not exists", recipeId);
                    throw new IOException(e);
                }
                long end = System.currentTimeMillis();
                log.info("[end] Prepared {} rules for OWLApi in {} ms.", rules.size(), (end - start));
            }
            if (rules == null) {
                log.error("No rules have been loaded");
                throw new IOException("No rules loaded");
            }
            final Iterator<SWRLRule> iterator = Collections.unmodifiableList(rules).iterator();
            return new Iterator<T>() {

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public T next() {
                    return (T) iterator.next();
                }

                @Override
                public void remove() {
                    log.error("Cannot remove items from this iterator. This may be cused by an error in the program");
                    throw new UnsupportedOperationException("Cannot remove items from this iterator");
                }
            };
        case Jena:
            List<Rule> jenaRules = null;
            if (recipeId != null) {
                long start = System.currentTimeMillis();
                log.info("[start] Prepare rules for Jena ");
                try {
                    Recipe recipe = null;
                    synchronized (store) {
                        try {
                            recipe = store.getRecipe(new IRI(recipeId));
                        } catch (RecipeConstructionException e) {
                            log.error("An error occurred while generating the recipe.", e);
                        }
                    }
                    if (recipe != null) {
                        log.debug("Recipe is: {}", recipe);
                        /*
		                 * We ask to the adapter manager to get the right adapter in order to transform
		                 * recipes into Jena Rule objects.
		                 */
                        RuleAdapter adapter;
                        try {
                            adapter = adapterManager.getAdapter(recipe, Rule.class);
                            jenaRules = (List<Rule>) adapter.adaptTo(recipe, Rule.class);
                        } catch (UnavailableRuleObjectException e) {
                            log.error(e.getMessage(), e);
                        } catch (RuleAtomCallExeption e) {
                            log.error(e.getMessage(), e);
                        } catch (UnsupportedTypeForExportException e) {
                            log.error(e.getMessage(), e);
                        }
                    }
                //jenaRules = recipe.toJenaRules();
                } catch (NoSuchRecipeException e) {
                    log.error("Recipe {} does not exists", recipeId);
                    throw new IOException(e);
                }
                long end = System.currentTimeMillis();
                log.info("[end] Prepared {} rules for Jena in {} ms.", jenaRules.size(), (end - start));
            }
            if (jenaRules == null) {
                log.error("No rules have been loaded");
                throw new IOException("No rules loaded");
            }
            final Iterator<Rule> jRiterator = Collections.unmodifiableList(jenaRules).iterator();
            return new Iterator<T>() {

                @Override
                public boolean hasNext() {
                    return jRiterator.hasNext();
                }

                @Override
                public T next() {
                    return (T) jRiterator.next();
                }

                @Override
                public void remove() {
                    log.error("Cannot remove items from this iterator. This may be cused by an error in the program");
                    throw new UnsupportedOperationException("Cannot remove items from this iterator");
                }
            };
        default:
            return null;
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) IOException(java.io.IOException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) Iterator(java.util.Iterator) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter)

Example 2 with NoSuchRecipeException

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

the class RefactorerImpl method graphRefactoring.

@SuppressWarnings("unchecked")
@Override
public void graphRefactoring(IRI refactoredOntologyID, IRI datasetID, IRI recipeID) throws RefactoringException, NoSuchRecipeException {
    Recipe recipe;
    try {
        try {
            recipe = ruleStore.getRecipe(recipeID);
            RuleAdapter ruleAdapter = ruleAdapterManager.getAdapter(recipe, ConstructQuery.class);
            List<ConstructQuery> constructQueries = (List<ConstructQuery>) ruleAdapter.adaptTo(recipe, ConstructQuery.class);
            Graph mGraph = tcManager.createGraph(refactoredOntologyID);
            for (ConstructQuery constructQuery : constructQueries) {
                mGraph.addAll(this.sparqlConstruct(constructQuery, datasetID));
            }
        } catch (RecipeConstructionException e) {
            throw new RefactoringException("The cause of the refactoring excpetion is: " + e.getMessage(), e);
        } catch (UnavailableRuleObjectException e) {
            throw new RefactoringException("The cause of the refactoring excpetion is: " + e.getMessage(), e);
        } catch (UnsupportedTypeForExportException e) {
            throw new RefactoringException("The cause of the refactoring excpetion is: " + e.getMessage(), e);
        } catch (RuleAtomCallExeption e) {
            throw new RefactoringException("The cause of the refactoring excpetion is: " + e.getMessage(), e);
        }
    } catch (NoSuchRecipeException e1) {
        log.error("No Such recipe in the Rule Store", e1);
        throw e1;
    }
}
Also used : ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) List(java.util.List) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery)

Example 3 with NoSuchRecipeException

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

the class RefactoringTest method refactoringWithNonExistentRecipeTest.

@Test
public void refactoringWithNonExistentRecipeTest() throws Exception {
    try {
        refactorer.graphRefactoring(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/refactoredGraph"), new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/graph"), new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/recipeB"));
        Assert.fail();
    } catch (NoSuchRecipeException e) {
        Assert.assertTrue(e.getMessage(), true);
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) Test(org.junit.Test)

Example 4 with NoSuchRecipeException

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

the class RulesResource method adaptTo.

/**
     * Return the String representation of a recipe adapted to another format, e.g., Jena rules, SPARQL
     * CONSTRUCTs, Clerezza, SWRL, etc.
     * 
     * @param recipe
     *            {The ID of the recipe}
     * @param format
     *            {The canonical name of the class we want have back, e.g.,
     *            org.apache.stanbol.rules.base.api.Rule for Jena Rules}
     * @param headers
     *            {@link HttpHeaders}
     * @return <ul>
     *         <li>200: it works properly and the string representation of the recipe according to the format
     *         provided is returned</li>
     *         <li>204: the recipe does not exist in the store</li>
     *         <li>403: a class exists for the format provided but there is no adapter for that</li>
     *         <li>404: no class exists in the context for the format provided</li>
     *         <li>406: some error occurred while converting a rule of the recipe</li>
     *         <li>409: some atom of a rule in the recipe cannot be converted to the format provided</li>
     *         </ul>
     */
@GET
@Produces(value = { KRFormat.RDF_JSON })
@Path("/adapters/{recipe:.+}")
public Response adaptTo(@PathParam("recipe") String recipe, @QueryParam("format") String format, @Context HttpHeaders headers) {
    ResponseBuilder responseBuilder = null;
    Class<?> classToLoad;
    try {
        // ClassLoader loader = Thread.currentThread().getContextClassLoader();
        // classToLoad = loader.loadClass(format);
        classToLoad = Class.forName(format);
        URI uri = new URI(recipe);
        if (uri.getScheme() == null) {
            recipe = "urn:" + recipe;
            log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
        }
        Recipe rcp = ruleStore.getRecipe(new IRI(recipe));
        RuleAdapter adapter = adapterManager.getAdapter(rcp, classToLoad);
        Object adaptedRecipe = adapter.adaptTo(rcp, classToLoad);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("recipe", rcp.getRecipeID().toString());
            jsonObject.put("adaptedTo", format);
            jsonObject.put("result", adaptedRecipe.toString());
        } catch (JSONException e) {
            log.error(e.getMessage(), e);
        }
        responseBuilder = Response.ok(jsonObject.toString());
    } catch (ClassNotFoundException e) {
        responseBuilder = Response.status(Status.NOT_FOUND);
        log.error(e.getMessage(), e);
    } catch (NoSuchRecipeException e) {
        responseBuilder = Response.status(Status.NO_CONTENT);
        log.error(e.getMessage(), e);
    } catch (RecipeConstructionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnavailableRuleObjectException e) {
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        log.error(e.getMessage(), e);
    } catch (RuleAtomCallExeption e) {
        responseBuilder = Response.status(Status.CONFLICT);
        log.error(e.getMessage(), e);
    } catch (UnsupportedTypeForExportException e) {
        responseBuilder = Response.status(Status.FORBIDDEN);
        log.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        log.error(e.getMessage(), e);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) JSONException(org.codehaus.jettison.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) JSONObject(org.codehaus.jettison.json.JSONObject) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) JSONObject(org.codehaus.jettison.json.JSONObject) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with NoSuchRecipeException

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

the class RulesResource method removeRecipe.

/**
     * This method allows to delete a recipe or a rule from the store.<br/>
     * If the optional rule identifier id provided as second parameter that the rule is deleted. Otherwise it
     * is the whole recipe to be deleted.
     * 
     * @param recipe
     *            {@link String}
     * @param rule
     *            {@link String} - OPTIONAL
     * @param headers
     *            {@link HttpHeaders}
     * @return <ul>
     *         <li>200 - if either the recipe or the rule is deleted</li>
     *         <li>204 - it is not possible to delete the rule because the internal construction of the recipe
     *         failed</li>
     *         <li>404 - the rule does not exist</li>
     *         <li>412 - the recipe to which the rule belongs does not exist</li>
     *         <li>500 - if a {@link RecipeEliminationException} exception is thrown</li>
     *         </ul>
     */
@DELETE
@Path("/recipe/{recipe:.+}")
public Response removeRecipe(@PathParam("recipe") String recipe, @QueryParam("rule") String rule, @Context HttpHeaders headers) {
    ResponseBuilder responseBuilder;
    boolean stop = false;
    URI uri = null;
    try {
        uri = new URI(recipe);
    } catch (URISyntaxException e1) {
        log.error(e1.getMessage(), e1);
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        stop = true;
    }
    if (!stop) {
        if (uri != null && uri.getScheme() == null) {
            recipe = "urn:" + recipe;
            log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
        }
        log.info("The recipe ID is : " + recipe);
        if (rule != null && !rule.isEmpty()) {
            Recipe rcp;
            try {
                rcp = ruleStore.getRecipe(new IRI(recipe));
                Rule rl = ruleStore.getRule(rcp, new IRI(rule));
                ruleStore.removeRule(rcp, rl);
            } catch (NoSuchRecipeException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.PRECONDITION_FAILED);
            } 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);
            }
        } else {
            try {
                ruleStore.removeRecipe(new IRI(recipe));
            } catch (RecipeEliminationException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR);
            }
        }
    }
    responseBuilder = Response.ok();
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) Rule(org.apache.stanbol.rules.base.api.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)19 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)17 IRI (org.apache.clerezza.commons.rdf.IRI)16 Recipe (org.apache.stanbol.rules.base.api.Recipe)15 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 Graph (org.apache.clerezza.commons.rdf.Graph)7 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 GET (javax.ws.rs.GET)5 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)5 Rule (org.apache.stanbol.rules.base.api.Rule)5 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)5 RefactoringException (org.apache.stanbol.rules.refactor.api.RefactoringException)5 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)4 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)4 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)4 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)4 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)4