Search in sources :

Example 6 with RuleList

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

use of org.apache.stanbol.rules.base.api.util.RuleList 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 8 with RuleList

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

Example 9 with RuleList

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

the class RuleParserTest method testParser.

@Test
public void testParser() {
    try {
        KB kReSKB = RuleParserImpl.parse("http://incubator.apache.org/stanbol/rules/test/", kReSRule);
        if (kReSKB != null) {
            RuleList kReSRuleList = kReSKB.getRuleList();
            if (kReSRuleList != null) {
                for (Rule kReSRule : kReSRuleList) {
                    log.debug("RULE : " + kReSRule.toString());
                }
            }
            log.debug("RULE LIST IS NULL");
        } else {
            log.debug("KB IS NULL");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RuleList(org.apache.stanbol.rules.base.api.util.RuleList) KB(org.apache.stanbol.rules.manager.KB) Rule(org.apache.stanbol.rules.base.api.Rule) Test(org.junit.Test)

Example 10 with RuleList

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

the class ClerezzaRuleStore method addRulesToRecipe.

/**
     * 
     * @param recipeIRI
     *            the IRI of the recipe
     * @param stanbolRule
     *            the rule in Rule syntax
     */
@Override
public Recipe addRulesToRecipe(Recipe recipe, String stanbolRule, String description) {
    IRI recipeID = recipe.getRecipeID();
    String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
    RuleList ruleList = RuleParserImpl.parse(namespace, stanbolRule).getRuleList();
    for (Rule rule : ruleList) {
        recipe = addRuleToRecipe(recipe, rule, description);
    }
    return recipe;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Rule(org.apache.stanbol.rules.base.api.Rule)

Aggregations

RuleList (org.apache.stanbol.rules.base.api.util.RuleList)15 Rule (org.apache.stanbol.rules.base.api.Rule)13 IRI (org.apache.clerezza.commons.rdf.IRI)7 Graph (org.apache.clerezza.commons.rdf.Graph)5 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)5 Recipe (org.apache.stanbol.rules.base.api.Recipe)5 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)5 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)4 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)4 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)3 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)3 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)3 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)3 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)3 RecipeImpl (org.apache.stanbol.rules.manager.RecipeImpl)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2