use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class AtomList method removeAll.
public boolean removeAll(Collection<?> c) {
if (contains(c)) {
for (Object o : c) {
boolean removed = false;
for (int i = 0; i < kReSRuleAtoms.length && !removed; i++) {
RuleAtom semionRule = kReSRuleAtoms[i];
if (semionRule.equals(o)) {
RuleAtom[] semionRulesCopy = new RuleAtom[kReSRuleAtoms.length - 1];
System.arraycopy(kReSRuleAtoms, 0, semionRulesCopy, 0, i);
System.arraycopy(kReSRuleAtoms, i + 1, semionRulesCopy, 0, semionRulesCopy.length - i);
kReSRuleAtoms = semionRulesCopy;
removed = true;
}
}
}
return true;
} else {
return false;
}
}
use of org.apache.stanbol.rules.base.api.RuleAtom 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.RuleAtom in project stanbol by apache.
the class JenaAdapter method adaptRuleTo.
@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
if (type == com.hp.hpl.jena.reasoner.rulesys.Rule.class) {
AtomList bodyAtomList = rule.getBody();
AtomList headAtomList = rule.getHead();
List<ClauseEntry> headClauseEntries = new ArrayList<ClauseEntry>();
List<ClauseEntry> bodyClauseEntries = new ArrayList<ClauseEntry>();
variableMap = new HashMap<String, Integer>();
Iterator<RuleAtom> it = headAtomList.iterator();
while (it.hasNext()) {
RuleAtom atom = it.next();
ClauseEntry clauseEntry = adaptRuleAtomTo(atom, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
if (clauseEntry instanceof HigherOrderClauseEntry) {
List<ClauseEntry> clauseEntries = ((HigherOrderClauseEntry) clauseEntry).getClauseEntries();
for (ClauseEntry ce : clauseEntries) {
headClauseEntries.add(ce);
}
} else {
headClauseEntries.add(clauseEntry);
}
}
it = bodyAtomList.iterator();
while (it.hasNext()) {
RuleAtom atom = it.next();
ClauseEntry clauseEntry = adaptRuleAtomTo(atom, com.hp.hpl.jena.reasoner.rulesys.Rule.class);
if (clauseEntry instanceof HigherOrderClauseEntry) {
List<ClauseEntry> clauseEntries = ((HigherOrderClauseEntry) clauseEntry).getClauseEntries();
for (ClauseEntry ce : clauseEntries) {
bodyClauseEntries.add(ce);
}
} else {
bodyClauseEntries.add(clauseEntry);
}
}
return (T) new com.hp.hpl.jena.reasoner.rulesys.Rule(rule.getRuleName(), headClauseEntries, bodyClauseEntries);
} else {
throw new UnsupportedTypeForExportException("The adapter " + getClass() + " does not support type : " + type.getCanonicalName());
}
}
use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class ClerezzaAdapter method adaptRuleTo.
@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
Set<TriplePattern> triplePatterns = new HashSet<TriplePattern>();
List<Expression> expressions = new ArrayList<Expression>();
Iterator<RuleAtom> it = rule.getBody().iterator();
while (it.hasNext()) {
RuleAtom ruleAtom = it.next();
ClerezzaSparqlObject clerezzaSparqlObject = null;
log.debug("Type to adapt {}", type);
clerezzaSparqlObject = (ClerezzaSparqlObject) adaptRuleAtomTo(ruleAtom, type);
Object clerezzaObj = clerezzaSparqlObject.getClerezzaObject();
if (clerezzaObj instanceof TriplePattern) {
triplePatterns.add((TriplePattern) clerezzaObj);
} else if (clerezzaObj instanceof Expression) {
expressions.add((Expression) clerezzaObj);
}
}
SimpleGroupGraphPattern groupGraphPattern = new SimpleGroupGraphPattern();
groupGraphPattern.addTriplePatterns(triplePatterns);
for (Expression expression : expressions) {
groupGraphPattern.addConstraint(expression);
}
triplePatterns = new HashSet<TriplePattern>();
it = rule.getHead().iterator();
while (it.hasNext()) {
RuleAtom ruleAtom = it.next();
ClerezzaSparqlObject clerezzaSparqlObject = (ClerezzaSparqlObject) adaptRuleAtomTo(ruleAtom, type);
triplePatterns.add((TriplePattern) clerezzaSparqlObject.getClerezzaObject());
}
SimpleConstructQuery constructQuery = new SimpleConstructQuery(triplePatterns);
constructQuery.setQueryPattern(groupGraphPattern);
return (T) constructQuery;
}
use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class RuleParserImpl method atom.
public final RuleAtom atom() throws ParseException {
RuleAtom ruleAtom;
switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case IS:
ruleAtom = classAtom();
{
if (true)
return ruleAtom;
}
break;
case HAS:
ruleAtom = individualPropertyAtom();
{
if (true)
return ruleAtom;
}
break;
case VALUES:
ruleAtom = datavaluedPropertyAtom();
{
if (true)
return ruleAtom;
}
break;
case LET:
ruleAtom = letAtom();
{
if (true)
return ruleAtom;
}
break;
case NEW_IRI:
ruleAtom = newIRIAtom();
{
if (true)
return ruleAtom;
}
break;
case NEW_LITERAL:
ruleAtom = newLiteralAtom();
{
if (true)
return ruleAtom;
}
break;
case SAME:
case DIFFERENT:
case LESSTHAN:
case GREATERTHAN:
case STARTS_WITH:
case ENDS_WITH:
case NOT:
case IS_BLANK:
ruleAtom = comparisonAtom();
{
if (true)
return ruleAtom;
}
break;
case UNION:
ruleAtom = unionAtom();
{
if (true)
return ruleAtom;
}
break;
default:
jj_la1[4] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
throw new Error("Missing return statement in function");
}
Aggregations