Search in sources :

Example 16 with Recipe

use of org.apache.stanbol.rules.base.api.Recipe 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 17 with Recipe

use of org.apache.stanbol.rules.base.api.Recipe 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)

Example 18 with Recipe

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

the class RecipeListWriter method writeTo.

@Override
public void writeTo(RecipeList recipeList, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    Logger log = LoggerFactory.getLogger(getClass());
    log.debug("Rendering the list of recipes.");
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    OWLOntology ontology;
    try {
        ontology = manager.createOntology();
        String recipeClassURI = Symbols.Recipe.toString().replace("<", "").replace(">", "");
        IRI recipeClassIRI = IRI.create(recipeClassURI);
        OWLClass owlRecipeClass = factory.getOWLClass(recipeClassIRI);
        String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
        IRI descriptionIRI = IRI.create(descriptionURI);
        OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
        if (recipeList != null) {
            log.info("Converting the recipe list to OWL.");
            for (Recipe recipe : recipeList) {
                String recipeURI = recipe.getRecipeID().toString().replace("<", "").replace(">", "");
                IRI recipeIRI = IRI.create(recipeURI);
                OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
                OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, recipeIndividual);
                manager.addAxiom(ontology, axiom);
                String description = recipe.getRecipeDescription();
                if (description != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, description);
                    manager.addAxiom(ontology, axiom);
                }
            }
        }
        if (mediaType.toString().equals(KRFormat.RDF_XML)) {
            try {
                manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.OWL_XML)) {
            try {
                manager.saveOntology(ontology, new OWLXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.MANCHESTER_OWL)) {
            try {
                manager.saveOntology(ontology, new ManchesterOWLSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.FUNCTIONAL_OWL)) {
            try {
                manager.saveOntology(ontology, new OWLFunctionalSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.TURTLE)) {
            try {
                manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.RDF_JSON)) {
            Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
            RdfJsonSerializingProvider provider = new RdfJsonSerializingProvider();
            provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
        }
    } catch (OWLOntologyCreationException e1) {
        log.error("An error occurred.", e1);
    }
    out.flush();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) Recipe(org.apache.stanbol.rules.base.api.Recipe) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) Logger(org.slf4j.Logger) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) Graph(org.apache.clerezza.commons.rdf.Graph) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) OWLXMLOntologyFormat(org.semanticweb.owlapi.io.OWLXMLOntologyFormat) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 19 with Recipe

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

the class RefactorResource method doRefactoring.

/**
     * Utility method that groups all calls to the refactorer.
     * 
     * @param input
     * @param recipe
     * @return
     * @throws OWLOntologyCreationException
     * @throws RefactoringException
     */
private OWLOntology doRefactoring(InputStream input, KB kb) throws OWLOntologyCreationException, RefactoringException {
    if (kb == null)
        return null;
    RuleList ruleList = kb.getRuleList();
    if (ruleList == null)
        return null;
    Recipe actualRecipe = new RecipeImpl(null, null, ruleList);
    // Parse the input ontology
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology inputOntology = manager.loadOntologyFromOntologyDocument(input);
    Graph tripleCollection = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(inputOntology), actualRecipe);
    // Refactor
    return OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(tripleCollection);
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Graph(org.apache.clerezza.commons.rdf.Graph) Recipe(org.apache.stanbol.rules.base.api.Recipe) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 20 with Recipe

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

the class RefactorResource method performRefactoring.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(value = { TURTLE, RDF_XML, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, RDF_JSON, X_TURTLE })
public Response performRefactoring(MultiPartBody data, @Context HttpHeaders headers) {
    String recipe = null;
    InputStream input = null;
    if (data.getTextParameterValues("recipe") != null) {
        recipe = data.getTextParameterValues("recipe")[0];
    }
    if (data.getFormFileParameterValues("input") != null) {
        input = new ByteArrayInputStream(data.getFormFileParameterValues("input")[0].getContent());
    }
    if (recipe == null || input == null) {
        throw new WebApplicationException(BAD_REQUEST);
    }
    // Refactorer semionRefactorer = semionManager.getRegisteredRefactorer();
    ResponseBuilder rb;
    Recipe rcp;
    try {
        URI uri = new URI(recipe);
        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);
        }
        IRI recipeID = new IRI(recipe);
        rcp = ruleStore.getRecipe(recipeID);
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology inputOntology = manager.loadOntologyFromOntologyDocument(input);
        Graph tripleCollection = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(inputOntology), rcp);
        OWLOntology outputOntology = OWLAPIToClerezzaConverter.clerezzaGraphToOWLOntology(tripleCollection);
        rb = Response.ok(outputOntology);
        MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
        if (mediaType != null)
            rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    } catch (NoSuchRecipeException e) {
        rb = Response.status(NOT_FOUND);
        log.error(e.getMessage(), e);
    } catch (RecipeConstructionException e) {
        rb = Response.status(NO_CONTENT);
        log.error(e.getMessage(), e);
    } catch (OWLOntologyCreationException e) {
        rb = Response.status(PRECONDITION_FAILED);
        log.error(e.getMessage(), e);
    } catch (RefactoringException e) {
        rb = Response.status(INTERNAL_SERVER_ERROR);
        log.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        rb = Response.status(NOT_ACCEPTABLE);
        log.error(e.getMessage(), e);
    }
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) Recipe(org.apache.stanbol.rules.base.api.Recipe) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) MediaType(javax.ws.rs.core.MediaType) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

Recipe (org.apache.stanbol.rules.base.api.Recipe)35 IRI (org.apache.clerezza.commons.rdf.IRI)29 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)16 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)16 Graph (org.apache.clerezza.commons.rdf.Graph)11 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)9 Rule (org.apache.stanbol.rules.base.api.Rule)9 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)6 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)6 RefactoringException (org.apache.stanbol.rules.refactor.api.RefactoringException)6 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 RecipeImpl (org.apache.stanbol.rules.manager.RecipeImpl)5 InputStream (java.io.InputStream)4 RecipeEliminationException (org.apache.stanbol.rules.base.api.RecipeEliminationException)4 RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)4 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)4 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)4