use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.
the class ChaseTest method restrictedChaseTestWithGrd.
@Theory
public void restrictedChaseTestWithGrd(InMemoryAtomSet atomSet) throws IOException, ChaseException, ParseException, AtomSetException, IteratorException {
atomSet.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
LinkedList<Rule> ruleSet = new LinkedList<>();
ruleSet.add(DlgpParser.parseRule("<Q>(X,Z) :- <P>(X,X)."));
ruleSet.add(DlgpParser.parseRule("<R>(X,Z) :- <Q>(X,Y)."));
ruleSet.add(DlgpParser.parseRule("<Q>(X,Z) :- <R>(X,Y)."));
ruleSet.add(DlgpParser.parseRule("<S>(X,X) :- <Q>(Y,X)."));
GraphOfRuleDependencies grd = new DefaultGraphOfRuleDependencies(ruleSet);
Chase chase = new ChaseWithGRDAndUnfiers<AtomSet>(grd, atomSet);
chase.execute();
int size = 0;
for (CloseableIterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
++size;
}
Assert.assertEquals(4, size);
}
use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.
the class IDCompilation method appendTo.
public void appendTo(StringBuilder sb) {
for (Rule r : this.getSaturation()) {
r.appendTo(sb);
sb.append('\n');
}
}
use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.
the class Rules method computeAtomicHead.
/**
* Generate a set of atomic head rules equivalent of the specified rule.
*
* @param rule
* @return a Collection of Rule which is a decomposition of the specified rule to atomic head rules.
*/
public static Collection<Rule> computeAtomicHead(Rule rule) {
String label = rule.getLabel();
Collection<Rule> atomicHead = new LinkedList<Rule>();
if (rule.getHead().isEmpty() || hasAtomicHead(rule)) {
return Collections.<Rule>singleton(rule);
} else {
Predicate predicate = new Predicate("aux_" + ++auxIndex, rule.getTerms().size());
Atom aux = DefaultAtomFactory.instance().create(predicate, rule.getTerms().toArray(new Term[rule.getTerms().size()]));
if (label.isEmpty()) {
atomicHead.add(DefaultRuleFactory.instance().create(rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(aux, atom));
}
} else {
int i = -1;
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, aux, atom));
}
}
}
return atomicHead;
}
use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.
the class AbstractRulesCompilation method extractCompilable.
/**
* Remove compilable rule from ruleSet and return a List of compilable
* rules.
*
* @param ruleSet
* @return a List containing the compilable rules.
*/
protected final LinkedList<Rule> extractCompilable(Iterator<Rule> ruleSet) {
LinkedList<Rule> compilable = new LinkedList<Rule>();
Rule r;
while (ruleSet.hasNext()) {
r = ruleSet.next();
if (this.isCompilable(r)) {
compilable.add(r);
ruleSet.remove();
}
}
if (this.getProfiler() != null) {
this.getProfiler().put("Compiled rules", compilable.size());
}
return compilable;
}
use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.
the class HierarchicalCompilation method computeIndex.
private void computeIndex(Iterable<Rule> ruleset) {
for (Rule rule : ruleset) {
// count the number of new pred in r
CloseableIteratorWithoutException<Predicate> it = rule.getBody().predicatesIterator();
while (it.hasNext()) {
this.addPredicate(it.next());
}
it = rule.getHead().predicatesIterator();
while (it.hasNext()) {
this.addPredicate(it.next());
}
}
int nbPred = this.indexPredicate.size();
Atom father, son;
this.order = new byte[nbPred][nbPred];
for (Rule ru : ruleset) {
father = ru.getHead().iterator().next();
son = ru.getBody().iterator().next();
this.addRule(father, son);
}
// reflexivity
for (int i = 0; i < this.order.length; ++i) {
this.order[i][i] = 1;
}
}
Aggregations