Search in sources :

Example 11 with Rule

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

the class RuleParserImpl method expression.

public final void expression() throws ParseException {
    Rule rule;
    prefix();
    expressionCont();
}
Also used : Rule(org.apache.stanbol.rules.base.api.Rule)

Example 12 with Rule

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

the class RuleStoreTest method removeRuleInRecipeTest.

private void removeRuleInRecipeTest() throws Exception {
    Recipe recipe = store.getRecipe(new IRI("http://incubator.apache.com/stanbol/rules/test/recipeA"));
    String tmp = recipe.toString();
    Rule rule = recipe.getRule(recipe.listRuleNames().get(0));
    store.removeRule(recipe, rule);
    Recipe recipe2 = store.getRecipe(new IRI("http://incubator.apache.com/stanbol/rules/test/recipeA"));
    String tmp2 = recipe2.toString();
    Assert.assertNotSame(tmp, tmp2);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) Rule(org.apache.stanbol.rules.base.api.Rule)

Example 13 with Rule

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

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

the class RecipeWriter method writeTo.

@Override
public void writeTo(Recipe recipe, 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.");
    if (mediaType.toString().equals(MediaType.TEXT_PLAIN)) {
        String recipeString = recipe.toString();
        out.write(recipeString.getBytes());
    } else if (mediaType.toString().equals(MediaType.TEXT_HTML)) {
        String recipeString = recipe.toString();
        out.write(recipeString.getBytes());
    } else {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory factory = OWLManager.getOWLDataFactory();
        OWLOntology ontology;
        try {
            ontology = manager.createOntology();
            RuleList rules = recipe.getRuleList();
            IRI recipeID = recipe.getRecipeID();
            String recipeURI = recipeID.toString().replace("<", "").replace(">", "");
            org.semanticweb.owlapi.model.IRI recipeIRI = org.semanticweb.owlapi.model.IRI.create(recipeURI);
            OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
            String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
            org.semanticweb.owlapi.model.IRI descriptionIRI = org.semanticweb.owlapi.model.IRI.create(descriptionURI);
            OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
            OWLAxiom axiom;
            String recipeDescription = recipe.getRecipeDescription();
            if (recipeDescription != null) {
                axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, recipeDescription);
                manager.addAxiom(ontology, axiom);
            }
            if (rules != null) {
                for (Rule rule : rules) {
                    IRI ruleID = rule.getRuleID();
                    String ruleName = rule.getRuleName();
                    String ruleDescription = rule.getDescription();
                    String ruleURI = ruleID.toString().replace("<", "").replace(">", "");
                    String ruleNameURI = Symbols.ruleName.toString().replace("<", "").replace(">", "");
                    String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
                    String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
                    String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
                    String ruleContent = rule.toString();
                    String[] ruleParts = ruleContent.split("\\->");
                    org.semanticweb.owlapi.model.IRI ruleIRI = org.semanticweb.owlapi.model.IRI.create(ruleURI);
                    org.semanticweb.owlapi.model.IRI ruleNameIRI = org.semanticweb.owlapi.model.IRI.create(ruleNameURI);
                    org.semanticweb.owlapi.model.IRI ruleBodyIRI = org.semanticweb.owlapi.model.IRI.create(ruleBodyURI);
                    org.semanticweb.owlapi.model.IRI ruleHeadIRI = org.semanticweb.owlapi.model.IRI.create(ruleHeadURI);
                    org.semanticweb.owlapi.model.IRI hasRuleIRI = org.semanticweb.owlapi.model.IRI.create(hasRuleURI);
                    OWLIndividual ruleIndividual = factory.getOWLNamedIndividual(ruleIRI);
                    OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
                    OWLDataProperty nameProperty = factory.getOWLDataProperty(ruleNameIRI);
                    OWLDataProperty ruleBodyProperty = factory.getOWLDataProperty(ruleBodyIRI);
                    OWLDataProperty ruleHeadProperty = factory.getOWLDataProperty(ruleHeadIRI);
                    // add the name to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(nameProperty, ruleIndividual, ruleName);
                    manager.addAxiom(ontology, axiom);
                    // add the description to the rule individual
                    if (ruleDescription != null) {
                        axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, ruleIndividual, ruleDescription);
                        manager.addAxiom(ontology, axiom);
                    }
                    // add the rule body to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBodyProperty, ruleIndividual, ruleParts[0]);
                    manager.addAxiom(ontology, axiom);
                    // add the rule head to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHeadProperty, ruleIndividual, ruleParts[1]);
                    manager.addAxiom(ontology, axiom);
                    // bind the rule to the recipe
                    axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, recipeIndividual, ruleIndividual);
                    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.apache.clerezza.commons.rdf.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) Logger(org.slf4j.Logger) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) 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) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) Rule(org.apache.stanbol.rules.base.api.Rule) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 15 with Rule

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

the class RulesResource method showRecipe.

@GET
@Path("/recipe/{recipe:.+}")
@Produces(value = { MediaType.TEXT_HTML })
public Response showRecipe(@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(new Viewable("rules", new RulesPrettyPrintResource(uriInfo, 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) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) Rule(org.apache.stanbol.rules.base.api.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Rule (org.apache.stanbol.rules.base.api.Rule)29 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)13 IRI (org.apache.clerezza.commons.rdf.IRI)12 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)9 Recipe (org.apache.stanbol.rules.base.api.Recipe)8 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)5 Graph (org.apache.clerezza.commons.rdf.Graph)4 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 Path (javax.ws.rs.Path)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 Literal (org.apache.clerezza.commons.rdf.Literal)2 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)2 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)2 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)2 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)2