use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RuleMLWriter method write.
@Override
public RuleMLWriter write(Rule rule) throws IOException {
Set<Variable> existVar = rule.getExistentials();
Set<Variable> universalVar = rule.getVariables();
universalVar.removeAll(existVar);
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
if (it.hasNext()) {
it.next();
}
boolean isAtomicHead = !it.hasNext();
this.openBalise("Assert");
this.writeLabel(rule.getLabel());
this.openBalise("Forall");
for (Term t : universalVar) {
this.writeTerm(t);
}
this.openBalise("Implies");
this.openBalise("if");
this.openBalise("And");
this.writeAtomSet(rule.getBody().iterator());
this.closeBaliseWithReturnLine("And");
this.closeBaliseWithReturnLine("if");
this.openBalise("then");
if (!existVar.isEmpty()) {
this.openBalise("Exists");
for (Term t : existVar) {
this.writeTerm(t);
}
}
if (!isAtomicHead) {
this.openBalise("And");
}
this.writeAtomSet(rule.getHead().iterator());
if (!isAtomicHead) {
this.closeBaliseWithReturnLine("And");
}
if (!existVar.isEmpty()) {
this.closeBaliseWithReturnLine("Exists");
}
this.closeBaliseWithReturnLine("then");
this.closeBaliseWithReturnLine("Implies");
this.closeBaliseWithReturnLine("Forall");
this.closeBaliseWithReturnLine("Assert");
return this;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class AbstractAffectedPositionSet method step1.
/**
* for each rule and for each existentially quantified variable occuring at
* position p[i] in its head, p[i] is affected;
*/
protected void step1() {
int i;
Set<Variable> existentials;
PredicatePosition predicatePosition;
for (Rule rule : ruleSet) {
existentials = rule.getExistentials();
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
i = -1;
for (Term t : atom) {
++i;
if (existentials.contains(t)) {
predicatePosition = new PredicatePosition(atom.getPredicate(), i);
this.affectedPosition.add(predicatePosition);
}
}
}
}
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class FrontierGuardedProperty method check.
@Override
public int check(Rule rule) {
Set<Variable> frontier = rule.getFrontier();
boolean isGuarded = true;
CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
while (it.hasNext()) {
Atom a = it.next();
isGuarded = true;
for (Term v : frontier) {
if (!a.getTerms().contains(v)) {
isGuarded = false;
break;
}
}
if (isGuarded) {
break;
}
}
if (isGuarded)
return 1;
return -1;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class GraphPositionDependencies method init.
private void init() {
int bodyTermIndex, headTermIndex;
Set<Variable> existentials;
for (Rule r : this.rules) {
existentials = r.getExistentials();
CloseableIteratorWithoutException<Atom> bodyIt = r.getBody().iterator();
while (bodyIt.hasNext()) {
Atom bodyAtom = bodyIt.next();
bodyTermIndex = -1;
for (Term bodyTerm : bodyAtom) {
++bodyTermIndex;
if (r.getHead().getTerms().contains(bodyTerm)) {
CloseableIteratorWithoutException<Atom> headIt = r.getHead().iterator();
while (headIt.hasNext()) {
Atom headAtom = headIt.next();
headTermIndex = -1;
for (Term headTerm : headAtom) {
++headTermIndex;
if (bodyTerm.equals(headTerm)) {
this.addEdge(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex), new PredicatePosition(headAtom.getPredicate(), headTermIndex));
} else if (existentials.contains(headTerm)) {
this.addSpecialEdge(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex), new PredicatePosition(headAtom.getPredicate(), headTermIndex));
}
}
}
} else {
if (!this.graph.containsVertex(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex))) {
this.graph.addVertex(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex));
}
}
}
}
}
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class IDConditionImpl method generateBody.
@Override
public Pair<List<Term>, Substitution> generateBody(List<Term> head) {
Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
Set<Variable> toRemoveFromPartition = new TreeSet<Variable>();
for (int i = 0; i < condHead.length; i++) {
Variable v = DefaultTermFactory.instance().createVariable(condHead[i]);
toRemoveFromPartition.add(v);
if (!s.aggregate(v, head.get(i))) {
return null;
}
}
List<Term> body = new ArrayList<Term>(condBody.length);
for (int i = 0; i < condBody.length; i++) {
Variable v = DefaultTermFactory.instance().createVariable(condBody[i]);
toRemoveFromPartition.add(v);
body.add(s.createImageOf(v));
}
for (Variable v : toRemoveFromPartition) {
s.remove(v);
}
return new ImmutablePair<List<Term>, Substitution>(body, s);
}
Aggregations