use of org.apache.stanbol.rules.base.api.SPARQLObject 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.SPARQLObject in project stanbol by apache.
the class SPARQLAdapter method adaptRuleTo.
/*
* public <T> T exportRecipe(String recipeID, Class<T> type) throws UnsupportedTypeForExportException,
* UnavailableRuleObjectException, NoSuchRecipeException {
*
*
* Recipe recipe; try { recipe = ruleStore.getRecipe(IRI.create(recipeID));
*
* return exportRecipe(recipe, type); } catch (NoSuchRecipeException e) { throw e; }
*
*
* }
*/
@SuppressWarnings("unchecked")
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws UnsupportedTypeForExportException, UnavailableRuleObjectException {
String sparql = "CONSTRUCT {";
boolean firstIte = true;
for (RuleAtom ruleAtom : rule.getHead()) {
if (!firstIte) {
sparql += " . ";
}
firstIte = false;
sparql += ((SPARQLObject) adaptRuleAtomTo(ruleAtom, type)).getObject();
}
sparql += "} ";
sparql += "WHERE {";
firstIte = true;
ArrayList<SPARQLObject> sparqlObjects = new ArrayList<SPARQLObject>();
for (RuleAtom ruleAtom : rule.getBody()) {
SPARQLObject tmp = ((SPARQLObject) adaptRuleAtomTo(ruleAtom, type));
if (tmp instanceof SPARQLNot) {
sparqlObjects.add(tmp);
} else if (tmp instanceof SPARQLComparison) {
sparqlObjects.add(tmp);
} else {
if (!firstIte) {
sparql += " . ";
} else {
firstIte = false;
}
sparql += tmp.getObject();
}
}
firstIte = true;
String optional = "";
String filter = "";
for (SPARQLObject sparqlObj : sparqlObjects) {
if (sparqlObj instanceof SPARQLNot) {
SPARQLNot sparqlNot = (SPARQLNot) sparqlObj;
if (!firstIte) {
optional += " . ";
} else {
firstIte = false;
}
optional += sparqlNot.getObject();
String[] filters = sparqlNot.getFilters();
for (String theFilter : filters) {
if (!filter.isEmpty()) {
filter += " && ";
}
filter += theFilter;
}
} else if (sparqlObj instanceof SPARQLComparison) {
SPARQLComparison sparqlDifferent = (SPARQLComparison) sparqlObj;
String theFilter = sparqlDifferent.getObject();
if (!filter.isEmpty()) {
filter += " && ";
}
filter += theFilter;
}
}
if (!optional.isEmpty()) {
sparql += " . OPTIONAL { " + optional + " } ";
}
if (!filter.isEmpty()) {
sparql += " . FILTER ( " + filter + " ) ";
}
sparql += "}";
return (T) new SPARQLQuery(sparql);
}
use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.
the class PropStringAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption {
org.apache.stanbol.rules.manager.atoms.PropStringAtom tmp = (org.apache.stanbol.rules.manager.atoms.PropStringAtom) ruleAtom;
StringFunctionAtom namespaceArg = tmp.getNamespaceArg();
StringFunctionAtom labelArg = tmp.getLabelArg();
try {
SPARQLObject namespaceSparqlAtom = adapter.adaptTo(namespaceArg, SPARQLObject.class);
SPARQLObject labelSparqlAtom = adapter.adaptTo(labelArg, SPARQLObject.class);
String ns = namespaceSparqlAtom.getObject();
String label = labelSparqlAtom.getObject();
String sparql = "<http://www.stlab.istc.cnr.it/semion/function#propString>(" + ns + ", " + label + ")";
return (T) new SPARQLFunction(sparql);
} 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.SPARQLObject in project stanbol by apache.
the class StrAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
org.apache.stanbol.rules.manager.atoms.StrAtom tmp = (org.apache.stanbol.rules.manager.atoms.StrAtom) ruleAtom;
IObjectAtom argumentAtom = tmp.getUriResource();
SPARQLObject sparqlObject = adapter.adaptTo(argumentAtom, SPARQLObject.class);
String sparql = "str(" + sparqlObject.getObject() + ")";
return (T) new SPARQLFunction(sparql);
}
use of org.apache.stanbol.rules.base.api.SPARQLObject in project stanbol by apache.
the class TypedLiteralAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom tmp = (org.apache.stanbol.rules.manager.atoms.TypedLiteralAtom) ruleAtom;
ExpressionAtom valueExpression = tmp.getValue();
URIResource xsdTypeResource = tmp.getXsdType();
SPARQLObject sparqlObject = null;
sparqlObject = adapter.adaptTo(valueExpression, SPARQLObject.class);
String value = sparqlObject.getObject();
if (!value.startsWith("\"")) {
value = "\"" + value;
}
if (!value.endsWith("\"")) {
value += "\"";
}
String xsdType = "<" + xsdTypeResource.getURI().toString() + ">";
return (T) new SPARQLFunction(value + "^^" + xsdType);
}
Aggregations