Search in sources :

Example 1 with RefactoringException

use of org.apache.stanbol.rules.refactor.api.RefactoringException in project stanbol by apache.

the class RefactorResource method applyRefactoringFromRuleFile.

/**
 * The apply mode allows the client to compose a recipe, by mean of string containg the rules, and apply
 * it "on the fly" to the graph in input.
 *
 * @param recipe
 *            String
 * @param input
 *            InputStream
 * @return a Response containing the transformed graph
 */
@POST
@Path("/applyfile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(value = { TURTLE, RDF_XML, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, RDF_JSON, X_TURTLE })
public Response applyRefactoringFromRuleFile(MultiPartBody data, @Context HttpHeaders headers) {
    InputStream recipeStream = null;
    InputStream input = null;
    if (data.getFormFileParameterValues("recipe") != null) {
        recipeStream = new ByteArrayInputStream(data.getFormFileParameterValues("recipe")[0].getContent());
    }
    if (data.getFormFileParameterValues("input") != null) {
        input = new ByteArrayInputStream(data.getFormFileParameterValues("input")[0].getContent());
    }
    if (recipeStream == null || input == null) {
        throw new WebApplicationException(BAD_REQUEST);
    }
    ResponseBuilder rb;
    OWLOntology output = null;
    try {
        output = doRefactoring(input, RuleParserImpl.parse("http://incubator.apache.com/stanbol/rules/refactor/", recipeStream));
    } catch (OWLOntologyCreationException e1) {
        throw new WebApplicationException(e1, INTERNAL_SERVER_ERROR);
    } catch (RefactoringException e1) {
        throw new WebApplicationException(e1, INTERNAL_SERVER_ERROR);
    }
    if (output != null) {
        rb = Response.ok(output);
        MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
        if (mediaType != null)
            rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    } else
        rb = Response.status(NOT_FOUND);
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) MediaType(javax.ws.rs.core.MediaType) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with RefactoringException

use of org.apache.stanbol.rules.refactor.api.RefactoringException in project stanbol by apache.

the class RefactorResource method performRefactoringLazyCreateGraph.

@GET
public Response performRefactoringLazyCreateGraph(@QueryParam("recipe") String recipe, @QueryParam("input-graph") String inputGraph, @QueryParam("output-graph") String outputGraph, @Context HttpHeaders headers) {
    log.info("recipe: {}", recipe);
    log.info("input-graph: {}", inputGraph);
    log.info("output-graph: {}", outputGraph);
    IRI recipeID = new IRI(recipe);
    IRI inputGraphID = new IRI(inputGraph);
    IRI outputGraphID = new IRI(outputGraph);
    // Refactorer semionRefactorer = semionManager.getRegisteredRefactorer();
    ResponseBuilder responseBuilder = null;
    try {
        refactorer.graphRefactoring(outputGraphID, inputGraphID, recipeID);
        responseBuilder = Response.ok();
    } catch (RefactoringException e) {
        // refactoring exceptions are re-thrown
        log.error(e.getMessage(), e);
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    } catch (NoSuchRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(NOT_FOUND);
    }
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        responseBuilder.header(HttpHeaders.CONTENT_TYPE, mediaType);
    // addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) MediaType(javax.ws.rs.core.MediaType) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) GET(javax.ws.rs.GET)

Example 3 with RefactoringException

use of org.apache.stanbol.rules.refactor.api.RefactoringException in project stanbol by apache.

the class RefactoringTest method persistentRefactoringTest.

@Test
public void persistentRefactoringTest() 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/recipeA"));
        Graph tc = tcm.getGraph(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/refactoredGraph"));
        Assert.assertNotNull(tc);
    } catch (RefactoringException e) {
        fail("Error while refactoring.");
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) Test(org.junit.Test)

Example 4 with RefactoringException

use of org.apache.stanbol.rules.refactor.api.RefactoringException 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 5 with RefactoringException

use of org.apache.stanbol.rules.refactor.api.RefactoringException 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

RefactoringException (org.apache.stanbol.rules.refactor.api.RefactoringException)10 Graph (org.apache.clerezza.commons.rdf.Graph)6 IRI (org.apache.clerezza.commons.rdf.IRI)6 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 Recipe (org.apache.stanbol.rules.base.api.Recipe)5 WebApplicationException (javax.ws.rs.WebApplicationException)4 MediaType (javax.ws.rs.core.MediaType)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)4 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Produces (javax.ws.rs.Produces)3 Test (org.junit.Test)3 List (java.util.List)2 Path (javax.ws.rs.Path)2 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)2