fr.lirmm.graphik.graal.elder.reasoning.SGRuleApplicationHandler Maven / Gradle / Ivy
package fr.lirmm.graphik.graal.elder.reasoning;
import java.util.HashSet;
import fr.lirmm.graphik.graal.api.core.Atom;
import fr.lirmm.graphik.graal.api.core.AtomSet;
import fr.lirmm.graphik.graal.api.core.AtomSetException;
import fr.lirmm.graphik.graal.api.core.Rule;
import fr.lirmm.graphik.graal.api.core.Substitution;
import fr.lirmm.graphik.graal.api.forward_chaining.ChaseHaltingCondition;
import fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationHandler;
import fr.lirmm.graphik.graal.api.forward_chaining.RuleApplier;
import fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet;
import fr.lirmm.graphik.graal.defeasible.core.atoms.FlexibleAtom;
import fr.lirmm.graphik.graal.defeasible.core.preferences.AlternativePreference;
import fr.lirmm.graphik.graal.defeasible.core.rules.PreferenceRule;
import fr.lirmm.graphik.graal.elder.core.StatementGraph;
import fr.lirmm.graphik.graal.forward_chaining.halting_condition.FrontierRestrictedChaseHaltingCondition;
import fr.lirmm.graphik.graal.forward_chaining.halting_condition.HaltingConditionWithHandler;
import fr.lirmm.graphik.graal.forward_chaining.rule_applier.ExhaustiveRuleApplier;
import fr.lirmm.graphik.graal.homomorphism.SmartHomomorphism;
import fr.lirmm.graphik.util.stream.CloseableIterator;
import fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException;
import fr.lirmm.graphik.util.stream.IteratorException;
public class SGRuleApplicationHandler implements RuleApplicationHandler{
private StatementGraph sg;
public SGRuleApplicationHandler(StatementGraph sg) {
super();
this.sg = sg;
}
// No pre rule application code is needed
public boolean preRuleApplication(Rule rule, Substitution substitution,
AtomSet data) {
return true;
}
// After rule application code
public CloseableIterator postRuleApplication(Rule rule,
Substitution substitution, AtomSet data, CloseableIterator atomsToAdd) {
// atomsToAdd is not reliable as atoms reference could be deleted if they are considered redundant
// data does not represent the used atoms for this application but the whole knowledge that has been given as input.
// so we create our own image of those facts
CloseableIteratorWithoutException itNewFacts = substitution.createImageOf(rule.getHead()).iterator();
CloseableIterator itBody = substitution.createImageOf(rule.getBody()).iterator();
AtomSet body = new LinkedListAtomSet();
try {
while(itBody.hasNext()) {
try {
body.add(new FlexibleAtom(itBody.next()));
} catch (AtomSetException e) {
e.printStackTrace();
}
}
} catch (IteratorException e1) {
e1.printStackTrace();
}
// Add a rule application (statement) for each generated Atom
while(itNewFacts.hasNext()) {
try {
FlexibleAtom newAtom = null;
// If the generated atom is a prefernce then instanticate a preference.
if(rule instanceof PreferenceRule) {
newAtom = new AlternativePreference(itNewFacts.next());
} else {
newAtom = new FlexibleAtom(itNewFacts.next());
}
sg.getOrCreateStatement(body, newAtom, rule);
} catch (IteratorException e) {
e.printStackTrace();
}
}
return atomsToAdd;
}
public RuleApplier getRuleApplier() {
return getRuleApplier(new FrontierRestrictedChaseHaltingCondition());
}
public RuleApplier getRuleApplier(ChaseHaltingCondition chaseCondition) {
HaltingConditionWithHandler chaseConditionHandler = new HaltingConditionWithHandler(chaseCondition, this);
RuleApplier ruleApplier = new ExhaustiveRuleApplier(SmartHomomorphism.instance(), chaseConditionHandler);
return ruleApplier;
}
}