fr.lirmm.graphik.graal.elder.reasoning.SGRuleApplicationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graal-elder Show documentation
Show all versions of graal-elder Show documentation
A defeasible reasoning tool for existential rules,
it supports defeasible reasoning with amibguity blocking/propagating with or without team defeat
package fr.lirmm.graphik.graal.elder.reasoning;
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
// so we create our own image of those facts
CloseableIteratorWithoutException itNewFacts = substitution.createImageOf(rule.getHead()).iterator();
CloseableIteratorWithoutException itBody = substitution.createImageOf(rule.getBody()).iterator();
AtomSet body = new LinkedListAtomSet();
while(itBody.hasNext()) {
try {
body.add(new FlexibleAtom(itBody.next()));
} catch (AtomSetException e) {
e.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.addStatementForRuleApplication(body, newAtom, rule);
} catch (IteratorException e) {
e.printStackTrace();
} catch (AtomSetException 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;
}
}