Search in sources :

Example 1 with AlreadyExistingRecipeException

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

the class RefactoringTest method setUp.

@Before
public void setUp() {
    String separator = System.getProperty("line.separator");
    rule = "kres = <http://kres.iks-project.eu/ontology.owl#> . " + separator + "foaf = <http://xmlns.com/foaf/0.1/> . " + separator + "rule1[ is(kres:Person, ?x) . endsWith(str(?x), \"Person\") -> is(foaf:Person, ?x) ]";
    InputStream inputStream = RefactoringTest.class.getResourceAsStream("/META-INF/test/testKReSOnt.owl");
    Model jenaModel = ModelFactory.createDefaultModel();
    jenaModel = jenaModel.read(inputStream, null);
    tripleCollection = JenaToClerezzaConverter.jenaModelToClerezzaGraph(jenaModel);
    Graph mGraph = tcm.createGraph(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/graph"));
    mGraph.addAll(tripleCollection);
    Recipe recipe;
    try {
        recipe = store.createRecipe(new IRI("http://incubator.apache.com/stanbol/rules/refactor/test/recipeA"), "Recipe for testing the Refactor.");
        recipe = store.addRulesToRecipe(recipe, rule, "Test");
    } catch (AlreadyExistingRecipeException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) Recipe(org.apache.stanbol.rules.base.api.Recipe) InputStream(java.io.InputStream) Model(com.hp.hpl.jena.rdf.model.Model) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) Before(org.junit.Before)

Example 2 with AlreadyExistingRecipeException

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

the class RefactorEnhancementEngine method initEngine.

/**
 * Method for adding ontologies to the scope core ontology.
 * <ol>
 * <li>Get all the ontologies from the property.</li>
 * <li>Create a base scope.</li>
 * <li>Retrieve the ontology space from the scope.</li>
 * <li>Add the ontologies to the scope via ontology space.</li>
 * </ol>
 */
private void initEngine(RefactorEnhancementEngineConf engineConfiguration) {
    // IRI dulcifierScopeIRI = org.semanticweb.owlapi.model.IRI.create((String) context.getProperties().get(SCOPE));
    String scopeId = engineConfiguration.getScope();
    // Create or get the scope with the configured ID
    try {
        scope = onManager.createOntologyScope(scopeId);
    // No need to deactivate a newly created scope.
    } catch (DuplicateIDException e) {
        scope = onManager.getScope(scopeId);
        onManager.setScopeActive(scopeId, false);
    }
    // All resolvable ontologies stated in the configuration are loaded into the core space.
    OntologySpace ontologySpace = scope.getCoreSpace();
    ontologySpace.tearDown();
    String[] coreScopeOntologySet = engineConfiguration.getScopeCoreOntologies();
    List<String> success = new ArrayList<String>(), failed = new ArrayList<String>();
    try {
        log.info("Will now load requested ontology into the core space of scope '{}'.", scopeId);
        OWLOntologyManager sharedManager = OWLManager.createOWLOntologyManager();
        org.semanticweb.owlapi.model.IRI physicalIRI = null;
        for (int o = 0; o < coreScopeOntologySet.length; o++) {
            String url = coreScopeOntologySet[o];
            try {
                physicalIRI = org.semanticweb.owlapi.model.IRI.create(url);
            } catch (Exception e) {
                failed.add(url);
            }
            try {
                // TODO replace with a Clerezza equivalent
                ontologySpace.addOntology(new RootOntologySource(physicalIRI, sharedManager));
                success.add(url);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to load ontology from physical location " + physicalIRI + " Continuing with next...", e);
                failed.add(url);
            }
        }
    } catch (UnmodifiableOntologyCollectorException ex) {
        log.error("Ontology space {} was found locked for modification. Cannot populate.", ontologySpace);
    }
    for (String s : success) log.info(" >> {} : SUCCESS", s);
    for (String s : failed) log.info(" >> {} : FAILED", s);
    ontologySpace.setUp();
    // if (!onManager.containsScope(scopeId)) onManager.registerScope(scope);
    onManager.setScopeActive(scopeId, true);
    /*
         * The first thing to do is to create a recipe in the rule store that can be used by the engine to
         * refactor the enhancement graphs.
         */
    String recipeId = engineConfiguration.getRecipeId();
    Recipe recipe = null;
    try {
        recipe = ruleStore.createRecipe(new IRI(recipeId), null);
    } catch (AlreadyExistingRecipeException e1) {
        log.error("A recipe with ID {} already exists in the store.", recipeId);
    }
    if (recipe != null) {
        log.debug("Initialised blank recipe with ID {}", recipeId);
        /*
             * The set of rule to put in the recipe can be provided by the user. A default set of rules is
             * provided in /META-INF/default/seo_rules.sem. Use the property engine.refactor in the felix
             * console to pass to the engine your set of rules.
             */
        String recipeLocation = engineConfiguration.getRecipeLocation();
        InputStream recipeStream = null;
        String recipeString = null;
        if (recipeLocation != null && !recipeLocation.isEmpty()) {
            Dereferencer dereferencer = new DereferencerImpl();
            try {
                recipeStream = dereferencer.resolve(recipeLocation);
                log.debug("Loaded recipe from external source {}", recipeLocation);
            } catch (FileNotFoundException e) {
                log.error("Recipe Stream is null.", e);
            }
        } else {
            // TODO remove this part (or manage it better in the @Activate method).
            String loc = "/META-INF/default/seo_rules.sem";
            recipeStream = getClass().getResourceAsStream(loc);
            log.debug("Loaded default recipe in {}.", loc);
        }
        if (recipeStream != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(recipeStream));
            recipeString = "";
            String line = null;
            try {
                while ((line = reader.readLine()) != null) recipeString += line;
            } catch (IOException e) {
                log.error("Failed to load Refactor Engine recipe from stream. Aborting read. ", e);
                recipeString = null;
            }
        }
        log.debug("Recipe content follows :\n{}", recipeString);
        if (recipeString != null) {
            ruleStore.addRulesToRecipe(recipe, recipeString, null);
            log.debug("Added rules to recipe {}", recipeId);
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) Dereferencer(org.apache.stanbol.enhancer.engines.refactor.dereferencer.Dereferencer) DereferencerImpl(org.apache.stanbol.enhancer.engines.refactor.dereferencer.DereferencerImpl) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) ConfigurationException(org.osgi.service.cm.ConfigurationException) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) FileNotFoundException(java.io.FileNotFoundException) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) SessionLimitException(org.apache.stanbol.ontologymanager.servicesapi.session.SessionLimitException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) DuplicateIDException(org.apache.stanbol.ontologymanager.servicesapi.collector.DuplicateIDException) IOException(java.io.IOException) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) BufferedReader(java.io.BufferedReader)

Example 3 with AlreadyExistingRecipeException

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

the class RulesResource method createRecipe.

/**
 * This method implements a REST service that allows to create a new empty recipe in the store with a
 * given description.<br/>
 * The description parameter is OPTIONAL.
 *
 * @param recipeID
 *            {@link String}
 * @param description
 *            {@link String} - OPTIONAL
 * @param headers
 *            {@link HttpHeaders}
 * @return <ul>
 *         <li>200 - if the recipe is created</li>
 *         <li>409 - if a recipe with the same identifier exists in the store</li>
 *         </ul>
 */
@PUT
@Consumes(MediaType.WILDCARD)
@Path("/recipe/{recipe:.+}")
public Response createRecipe(@PathParam("recipe") String recipeID, @QueryParam("description") String description, @Context HttpHeaders headers) {
    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);
        }
        ruleStore.createRecipe(new IRI(recipeID), description);
        responseBuilder = Response.ok();
    } catch (AlreadyExistingRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.CONFLICT);
    } 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) URISyntaxException(java.net.URISyntaxException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with AlreadyExistingRecipeException

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

the class ClerezzaRuleStore method createRecipe.

/*
     * Moved form AddRecipe class. The AddRecipe should not be used anymore.
     */
@Override
public Recipe createRecipe(IRI recipeID, String recipeDescription) throws AlreadyExistingRecipeException {
    Graph tripleCollection;
    try {
        // create the Graph in the TcManager
        tripleCollection = tcManager.createGraph(recipeID);
    } catch (EntityAlreadyExistsException e) {
        throw new AlreadyExistingRecipeException(e.getMessage());
    }
    Triple recipeTriple = new TripleImpl(recipeID, RDF.type, Symbols.Recipe);
    Graph recipeIndexGraph = tcManager.getGraph(new IRI(recipeIndexLocation));
    recipeIndexGraph.add(recipeTriple);
    if (recipeDescription != null && !recipeDescription.isEmpty()) {
        Triple descriptionTriple = new TripleImpl(recipeID, Symbols.description, new PlainLiteralImpl(recipeDescription));
        tripleCollection.add(descriptionTriple);
        recipeIndexGraph.add(descriptionTriple);
    }
    // add the recpe ID to the list of known recipes
    recipes.add(recipeID);
    return new RecipeImpl(recipeID, recipeDescription, null);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) EntityAlreadyExistsException(org.apache.clerezza.rdf.core.access.EntityAlreadyExistsException) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) AlreadyExistingRecipeException(org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException)

Aggregations

IRI (org.apache.clerezza.commons.rdf.IRI)4 AlreadyExistingRecipeException (org.apache.stanbol.rules.base.api.AlreadyExistingRecipeException)4 InputStream (java.io.InputStream)2 Graph (org.apache.clerezza.commons.rdf.Graph)2 Recipe (org.apache.stanbol.rules.base.api.Recipe)2 Model (com.hp.hpl.jena.rdf.model.Model)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1