Set<GdlRelation> cyclicRelations = new HashSet<GdlRelation>();
Set<GdlRelation> acyclicRelations = new HashSet<GdlRelation>();
for(GdlLiteral literal : rule.getBody()) {
//Is it a relation?
if(literal instanceof GdlRelation) {
GdlRelation relation = (GdlRelation) literal;
//Is it in a cycle with the head?
if(ancestorsGraph.get(relation.getName()).contains(head)) {
cyclicRelations.add(relation);
} else {
acyclicRelations.add(relation);
}
} else if(literal instanceof GdlOr) {
//We'll look one layer deep for the cyclic kind
GdlOr or = (GdlOr) literal;
for(int i = 0; i < or.arity(); i++) {
GdlLiteral internal = or.get(i);
if(internal instanceof GdlRelation) {
GdlRelation relation = (GdlRelation) internal;
if(ancestorsGraph.get(relation.getName()).contains(head)) {
cyclicRelations.add(relation);
} //Don't add acyclic relations, as we can't count on them
}
}
}
}
for(GdlRelation relation : cyclicRelations) {
for(GdlTerm term : relation.getBody()) {
//There are three ways to be okay
boolean safe = false;
//One: Is it ground?
if(term.isGround())
safe = true;