fr.lirmm.graphik.integraal.forward_chaining.ChaseWithGRDAndUnfiers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integraal-graal-ruleset-analysis Show documentation
Show all versions of integraal-graal-ruleset-analysis Show documentation
Rule base analysis for InteGraal. This is imported from Graal
The newest version!
/*
* Copyright (C) Inria Sophia Antipolis - Méditerranée / LIRMM
* (Université de Montpellier & CNRS) (2014 - 2017)
*
* Contributors :
*
* Clément SIPIETER
* Mélanie KÖNIG
* Swan ROCHER
* Jean-François BAGET
* Michel LECLÈRE
* Marie-Laure MUGNIER
*
*
* This file is part of Graal .
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
/**
*
*/
package fr.lirmm.graphik.integraal.forward_chaining;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import fr.lirmm.graphik.integraal.api.core.Atom;
import fr.lirmm.graphik.integraal.api.core.AtomSet;
import fr.lirmm.graphik.integraal.api.core.ConjunctiveQuery;
import fr.lirmm.graphik.integraal.api.core.GraphOfRuleDependencies;
import fr.lirmm.graphik.integraal.api.core.InMemoryAtomSet;
import fr.lirmm.graphik.integraal.api.core.Rule;
import fr.lirmm.graphik.integraal.api.core.Substitution;
import fr.lirmm.graphik.integraal.api.core.Term;
import fr.lirmm.graphik.integraal.api.core.Variable;
import fr.lirmm.graphik.integraal.api.forward_chaining.AbstractChase;
import fr.lirmm.graphik.integraal.api.forward_chaining.ChaseException;
import fr.lirmm.graphik.integraal.api.forward_chaining.ChaseHaltingCondition;
import fr.lirmm.graphik.integraal.api.forward_chaining.RuleApplicationException;
import fr.lirmm.graphik.integraal.api.forward_chaining.RuleApplier;
import fr.lirmm.graphik.integraal.api.homomorphism.HomomorphismException;
import fr.lirmm.graphik.integraal.api.homomorphism.HomomorphismFactoryException;
import fr.lirmm.graphik.integraal.core.DefaultConjunctiveQuery;
import fr.lirmm.graphik.integraal.core.HashMapSubstitution;
import fr.lirmm.graphik.integraal.core.atomset.DefaultInMemoryAtomSet;
import fr.lirmm.graphik.integraal.core.atomset.graph.DefaultInMemoryGraphStore;
import fr.lirmm.graphik.integraal.core.factory.DefaultConjunctiveQueryFactory;
import fr.lirmm.graphik.integraal.core.factory.DefaultSubstitutionFactory;
import fr.lirmm.graphik.integraal.core.grd.DefaultGraphOfRuleDependencies;
import fr.lirmm.graphik.integraal.core.term.DefaultTermFactory;
import fr.lirmm.graphik.integraal.core.unifier.DefaultUnifierAlgorithm;
import fr.lirmm.graphik.integraal.forward_chaining.halting_condition.RestrictedChaseHaltingCondition;
import fr.lirmm.graphik.integraal.forward_chaining.rule_applier.DefaultRuleApplier;
import fr.lirmm.graphik.integraal.homomorphism.SmartHomomorphism;
import fr.lirmm.graphik.util.stream.CloseableIterator;
import fr.lirmm.graphik.util.stream.IteratorException;
import fr.lirmm.graphik.util.stream.Iterators;
/**
* @author Clément Sipieter (INRIA)
*
*/
public class ChaseWithGRDAndUnfiers extends AbstractChase {
private GraphOfRuleDependencies grd;
private T atomSet;
private Queue> queue = new LinkedList>();
private ChaseHaltingCondition hc = new RestrictedChaseHaltingCondition();
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
// /////////////////////////////////////////////////////////////////////////
public ChaseWithGRDAndUnfiers(GraphOfRuleDependencies grd, T atomSet, RuleApplier ruleApplier) {
super(ruleApplier);
this.grd = grd;
this.atomSet = atomSet;
for(Rule r : grd.getRules()) {
this.queue.add(new ImmutableTriple(r, new HashMapSubstitution(), new DefaultInMemoryAtomSet()));
}
}
public ChaseWithGRDAndUnfiers(GraphOfRuleDependencies grd, T atomSet) {
this(grd, atomSet, new DefaultRuleApplier());
}
public ChaseWithGRDAndUnfiers(Iterator rules, T atomSet) {
this(new DefaultGraphOfRuleDependencies(rules, true), atomSet, new DefaultRuleApplier());
}
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
Rule rule, unifiedRule;
Substitution unificator;
Queue> newQueue = new LinkedList>();
InMemoryAtomSet newAtomSet = new DefaultInMemoryGraphStore();
try {
while(!queue.isEmpty()) {
Triple pair = queue.poll();
if(pair != null) {
unificator = pair.getMiddle();
InMemoryAtomSet part = pair.getRight();
rule = pair.getLeft();
unifiedRule = DefaultUnifierAlgorithm.getTargetVariablesSubstitution().createImageOf(rule);
unifiedRule = unificator.createImageOf(unifiedRule);
unifiedRule.getBody().removeAll(part);
unificator = targetToSource(unificator);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(unifiedRule.getBody(),
new LinkedList(unifiedRule.getFrontier()));
// Get projections
List projections = Iterators.toList(SmartHomomorphism.instance().execute(query, atomSet));
try {
for(Substitution proj : projections) {
InMemoryAtomSet newFacts = proj.createImageOf(unifiedRule.getHead());
ConjunctiveQuery q = new DefaultConjunctiveQuery(newFacts);
if (!SmartHomomorphism.instance().execute(q, newAtomSet).hasNext()) {
// Existential variables instantiation added to proj
CloseableIterator it = hc.apply(unifiedRule, proj, atomSet);
if (it.hasNext()) {
DefaultInMemoryAtomSet foundPart = new DefaultInMemoryAtomSet();
foundPart.addAll(it);
newAtomSet.addAll(foundPart);
// Makes the projection compatible with triggered rules unifiers
Substitution compatibleProj = targetToSource(proj);
for (Pair p : this.grd.getTriggeredRulesWithUnifiers(rule)) {
Rule triggeredRule = p.getKey();
Substitution u = p.getValue();
if(u != null) {
Substitution comp = unificator.compose(u);
Substitution aggreg = compatibleProj.aggregate(comp);
aggreg = forgetSource(aggreg);
if(aggreg != null) {
newQueue.add(new ImmutableTriple(triggeredRule, aggreg, foundPart));
}
}
}
}
}
}
} catch (HomomorphismFactoryException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (IteratorException e) {
throw new RuleApplicationException("Error during rule application", e);
}
}
}
queue = newQueue;
atomSet.addAll(newAtomSet);
} catch (Exception e) {
e.printStackTrace();
throw new ChaseException("An error occur pending saturation step.", e);
}
}
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
/**
* @param s
* @return a Substitution which is obtained by replacing all target mark (T::) from the specified substitution by a source mark (S::).
*/
private static Substitution targetToSource(Substitution s) {
Substitution res = DefaultSubstitutionFactory.instance().createSubstitution();
for(Variable v : s.getTerms()) {
res.put(DefaultTermFactory.instance().createVariable(v.getLabel().replaceAll("T::", "S::")), s.createImageOf(v));
}
return res;
}
private static Substitution forgetSource(Substitution s) {
Substitution res = DefaultSubstitutionFactory.instance().createSubstitution();
for(Variable v : s.getTerms()) {
if(v.getLabel().startsWith("T::")) {
res.put(v, s.createImageOf(v));
}
}
return res;
}
}