use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class SWRLAdapter method adaptRuleTo.
@SuppressWarnings("unchecked")
@Override
protected <T> T adaptRuleTo(Rule rule, Class<T> type) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
if (type == SWRLRule.class) {
OWLDataFactory factory = OWLManager.getOWLDataFactory();
Set<SWRLAtom> bodyAtoms = new HashSet<SWRLAtom>();
Set<SWRLAtom> headAtoms = new HashSet<SWRLAtom>();
for (RuleAtom atom : rule.getBody()) {
bodyAtoms.add((SWRLAtom) adaptRuleAtomTo(atom, SWRLRule.class));
}
for (RuleAtom atom : rule.getHead()) {
headAtoms.add((SWRLAtom) adaptRuleAtomTo(atom, SWRLRule.class));
}
return (T) factory.getSWRLRule(bodyAtoms, headAtoms);
} 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 DatavaluedPropertyAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnsupportedTypeForExportException, UnavailableRuleObjectException {
org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom tmp = (org.apache.stanbol.rules.manager.atoms.DatavaluedPropertyAtom) ruleAtom;
IObjectAtom argument1 = tmp.getArgument1();
IObjectAtom datatypeProperty = tmp.getDatatypeProperty();
RuleAtom argument2 = tmp.getArgument2();
SWRLAtom arg1Atom = (SWRLAtom) adapter.adaptTo(argument1, SWRLRule.class);
SWRLAtom predicateAtom = (SWRLAtom) adapter.adaptTo(datatypeProperty, SWRLRule.class);
SWRLAtom arg2Atom = (SWRLAtom) adapter.adaptTo(argument2, SWRLRule.class);
OWLDataFactory factory = OWLManager.getOWLDataFactory();
OWLDataProperty owlDataProperty;
SWRLIArgument swrliArgument;
SWRLDArgument swrldArgument;
if (predicateAtom instanceof ArgumentSWRLAtom) {
owlDataProperty = factory.getOWLDataProperty(IRI.create(((ArgumentSWRLAtom) predicateAtom).getId()));
} else {
throw new RuleAtomCallExeption(getClass());
}
if (arg1Atom instanceof ArgumentSWRLAtom) {
swrliArgument = (SWRLIArgument) ((ArgumentSWRLAtom) arg1Atom).getSwrlArgument();
} else {
throw new RuleAtomCallExeption(getClass());
}
if (arg2Atom instanceof ArgumentSWRLAtom) {
swrldArgument = (SWRLDArgument) ((ArgumentSWRLAtom) arg2Atom).getSwrlArgument();
} else {
throw new RuleAtomCallExeption(getClass());
}
return (T) factory.getSWRLDataPropertyAtom(owlDataProperty, swrliArgument, swrldArgument);
}
use of org.apache.stanbol.rules.base.api.RuleAtom 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);
}
use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class RuleImpl method toString.
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(ruleName);
stringBuilder.append("[");
boolean firstLoop = true;
// add the rule body
for (RuleAtom atom : body) {
if (!firstLoop) {
stringBuilder.append(" . ");
} else {
firstLoop = false;
}
stringBuilder.append(atom.toString());
}
// add the rule head
if (head != null) {
stringBuilder.append(" -> ");
firstLoop = true;
for (RuleAtom atom : head) {
if (!firstLoop) {
stringBuilder.append(" . ");
} else {
firstLoop = false;
}
stringBuilder.append(atom.toString());
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
use of org.apache.stanbol.rules.base.api.RuleAtom in project stanbol by apache.
the class AtomIterator method next.
public RuleAtom next() {
RuleAtom atom = kReSRuleAtoms[currentIndex];
currentIndex++;
return atom;
}
Aggregations