use of org.apache.stanbol.rules.base.api.UnavailableRuleObjectException 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;
}
}
use of org.apache.stanbol.rules.base.api.UnavailableRuleObjectException 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();
}
use of org.apache.stanbol.rules.base.api.UnavailableRuleObjectException in project stanbol by apache.
the class SWRLAdpterTest method test.
@SuppressWarnings("unchecked")
@Test
public void test() {
try {
List<SWRLRule> rules = (List<SWRLRule>) ruleAdapter.adaptTo(recipeGood, SWRLRule.class);
StringBuilder sb = new StringBuilder();
for (SWRLRule rule : rules) {
sb.append(rule.toString());
}
Assert.assertNotSame(sb.toString(), "");
} catch (UnavailableRuleObjectException e) {
Assert.fail(e.getMessage());
} catch (UnsupportedTypeForExportException e) {
Assert.fail(e.getMessage());
} catch (RuleAtomCallExeption e) {
Assert.fail(e.getMessage());
}
}
use of org.apache.stanbol.rules.base.api.UnavailableRuleObjectException in project stanbol by apache.
the class SumAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
org.apache.stanbol.rules.manager.atoms.SumAtom tmp = (org.apache.stanbol.rules.manager.atoms.SumAtom) ruleAtom;
NumericFunctionAtom argument1 = tmp.getNumericFunctionAtom1();
NumericFunctionAtom argument2 = tmp.getNumericFunctionAtom2();
try {
SPARQLObject sparqlArgument1 = adapter.adaptTo(argument1, SPARQLObject.class);
SPARQLObject sparqlArgument2 = adapter.adaptTo(argument2, SPARQLObject.class);
String sparqlFunction1 = sparqlArgument1.getObject();
String sparqlFunction2 = sparqlArgument2.getObject();
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(sparqlFunction1);
sb.append(" + ");
sb.append(sparqlFunction2);
sb.append(")");
return (T) new SPARQLComparison(sb.toString());
} catch (UnsupportedTypeForExportException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
} catch (UnavailableRuleObjectException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
}
}
use of org.apache.stanbol.rules.base.api.UnavailableRuleObjectException in project stanbol by apache.
the class UnionAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
org.apache.stanbol.rules.manager.atoms.UnionAtom tmp = (org.apache.stanbol.rules.manager.atoms.UnionAtom) ruleAtom;
AtomList atomList1 = tmp.getAtomList1();
AtomList atomList2 = tmp.getAtomList2();
String scope1 = "";
for (RuleAtom inGroupRuleAtom : atomList1) {
if (!scope1.isEmpty()) {
scope1 += " . ";
}
try {
SPARQLObject inGroupSparqlAtom = adapter.adaptTo(inGroupRuleAtom, SPARQLObject.class);
scope1 += inGroupSparqlAtom.getObject();
} catch (UnsupportedTypeForExportException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
} catch (UnavailableRuleObjectException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
}
}
String scope2 = "";
for (RuleAtom inGroupRuleAtom : atomList2) {
if (!scope2.isEmpty()) {
scope2 += " . ";
}
try {
SPARQLObject inGroupSparqlAtom = adapter.adaptTo(inGroupRuleAtom, SPARQLObject.class);
scope2 += inGroupSparqlAtom.getObject();
} catch (UnsupportedTypeForExportException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
} catch (UnavailableRuleObjectException e) {
throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
}
}
String sparqlUnion = " { " + scope1 + " } UNION { " + scope2 + " } ";
return (T) new SPARQLFunction(sparqlUnion);
}
Aggregations