Search in sources :

Example 1 with RuleAtomCallExeption

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

the class SubtractionAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    String mul_result = "subtraction_result" + System.currentTimeMillis();
    org.apache.stanbol.rules.manager.atoms.SubtractionAtom tmp = (org.apache.stanbol.rules.manager.atoms.SubtractionAtom) ruleAtom;
    ExpressionAtom argument1 = tmp.getNumericFunctionAtom1();
    ExpressionAtom argument2 = tmp.getNumericFunctionAtom2();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    SWRLAtom swrlAtom1 = (SWRLAtom) adapter.adaptTo(argument1, SWRLRule.class);
    SWRLAtom swrlAtom2 = (SWRLAtom) adapter.adaptTo(argument2, SWRLRule.class);
    SWRLDArgument swrldArgument1 = factory.getSWRLVariable(IRI.create(Symbols.variablesPrefix + mul_result));
    SWRLDArgument swrldArgument2;
    SWRLDArgument swrldArgument3;
    List<SWRLAtom> listOfArguments = new ArrayList<SWRLAtom>();
    if (swrlAtom1 instanceof HigherOrderSWRLAtom) {
        swrldArgument2 = ((HigherOrderSWRLAtom) swrlAtom1).getBindableArgument();
        listOfArguments.addAll(((HigherOrderSWRLAtom) swrlAtom1).getAtoms());
    } else if (swrlAtom1 instanceof ArgumentSWRLAtom) {
        SWRLArgument swrlArgument = ((ArgumentSWRLAtom) swrlAtom1).getSwrlArgument();
        swrldArgument2 = (SWRLDArgument) swrlArgument;
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (swrlAtom2 instanceof HigherOrderSWRLAtom) {
        swrldArgument3 = ((HigherOrderSWRLAtom) swrlAtom2).getBindableArgument();
        listOfArguments.addAll(((HigherOrderSWRLAtom) swrlAtom2).getAtoms());
    } else if (swrlAtom2 instanceof ArgumentSWRLAtom) {
        SWRLArgument swrlArgument = ((ArgumentSWRLAtom) swrlAtom2).getSwrlArgument();
        swrldArgument3 = (SWRLDArgument) swrlArgument;
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    List<SWRLDArgument> swrldArguments = new ArrayList<SWRLDArgument>();
    swrldArguments.add(swrldArgument1);
    swrldArguments.add(swrldArgument2);
    swrldArguments.add(swrldArgument3);
    SWRLBuiltInAtom swrlBuiltInAtom = factory.getSWRLBuiltInAtom(SWRLBuiltInsVocabulary.SUBTRACT.getIRI(), swrldArguments);
    listOfArguments.add(swrlBuiltInAtom);
    return (T) new HigherOrderSWRLAtom(swrldArgument1, listOfArguments);
}
Also used : ArrayList(java.util.ArrayList) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) SWRLDArgument(org.semanticweb.owlapi.model.SWRLDArgument) SWRLAtom(org.semanticweb.owlapi.model.SWRLAtom) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) SWRLBuiltInAtom(org.semanticweb.owlapi.model.SWRLBuiltInAtom) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) SWRLArgument(org.semanticweb.owlapi.model.SWRLArgument) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 2 with RuleAtomCallExeption

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

the class UpperCaseAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    String uppercase_result = "uppercase_result" + System.currentTimeMillis();
    org.apache.stanbol.rules.manager.atoms.UpperCaseAtom tmp = (org.apache.stanbol.rules.manager.atoms.UpperCaseAtom) ruleAtom;
    StringFunctionAtom stringFunction = tmp.getStringFunctionAtom();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    SWRLAtom stringFunctionAtom = (SWRLAtom) adapter.adaptTo(stringFunction, SWRLRule.class);
    SWRLDArgument retArgument = factory.getSWRLVariable(IRI.create(Symbols.variablesPrefix + uppercase_result));
    ;
    SWRLDArgument stringArgument;
    List<SWRLAtom> listOfArguments = new ArrayList<SWRLAtom>();
    if (stringFunctionAtom instanceof HigherOrderSWRLAtom) {
        stringArgument = ((HigherOrderSWRLAtom) stringFunctionAtom).getBindableArgument();
        listOfArguments.addAll(((HigherOrderSWRLAtom) stringFunctionAtom).getAtoms());
    } else if (stringFunctionAtom instanceof ArgumentSWRLAtom) {
        SWRLArgument swrlArgument = ((ArgumentSWRLAtom) stringFunctionAtom).getSwrlArgument();
        stringArgument = (SWRLDArgument) swrlArgument;
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    List<SWRLDArgument> swrldArguments = new ArrayList<SWRLDArgument>();
    swrldArguments.add(retArgument);
    swrldArguments.add(stringArgument);
    return (T) factory.getSWRLBuiltInAtom(SWRLBuiltInsVocabulary.UPPER_CASE.getIRI(), swrldArguments);
}
Also used : ArrayList(java.util.ArrayList) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) StringFunctionAtom(org.apache.stanbol.rules.manager.atoms.StringFunctionAtom) SWRLDArgument(org.semanticweb.owlapi.model.SWRLDArgument) SWRLAtom(org.semanticweb.owlapi.model.SWRLAtom) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) SWRLArgument(org.semanticweb.owlapi.model.SWRLArgument) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 3 with RuleAtomCallExeption

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

the class SWRLAdpterTest method wrongAdaptabeClassTest.

@SuppressWarnings("unchecked")
@Test
public void wrongAdaptabeClassTest() {
    try {
        List<ConstructQuery> constructQueries = (List<ConstructQuery>) ruleAdapter.adaptTo(recipeGood, ConstructQuery.class);
        for (ConstructQuery constructQuery : constructQueries) {
            log.debug(constructQuery.toString());
            Assert.fail("The adapter for Jena should not accept " + ConstructQuery.class.getCanonicalName() + " objects.");
        }
    } catch (UnavailableRuleObjectException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedTypeForExportException e) {
        log.debug(e.getMessage());
    } catch (RuleAtomCallExeption e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) List(java.util.List) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) Test(org.junit.Test)

Example 4 with RuleAtomCallExeption

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

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

Aggregations

RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)64 ArrayList (java.util.ArrayList)31 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)21 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)21 Node (com.hp.hpl.jena.graph.Node)20 ClauseEntry (com.hp.hpl.jena.reasoner.rulesys.ClauseEntry)20 NodeClauseEntry (org.apache.stanbol.rules.adapters.jena.NodeClauseEntry)20 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)20 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)18 Functor (com.hp.hpl.jena.reasoner.rulesys.Functor)16 ArgumentSWRLAtom (org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom)16 SWRLAtom (org.semanticweb.owlapi.model.SWRLAtom)16 HigherOrderClauseEntry (org.apache.stanbol.rules.adapters.jena.HigherOrderClauseEntry)15 IObjectAtom (org.apache.stanbol.rules.manager.atoms.IObjectAtom)15 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)15 SWRLDArgument (org.semanticweb.owlapi.model.SWRLDArgument)13 HigherOrderSWRLAtom (org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom)12 SWRLArgument (org.semanticweb.owlapi.model.SWRLArgument)12 List (java.util.List)10 SPARQLObject (org.apache.stanbol.rules.base.api.SPARQLObject)10