for(SentenceForm nextForm : model.getSentenceForms()) {
if(nextForm.getName().equals(NEXT)) {
//See if there is exactly one update rule, and it is the persistence rule
Set<GdlRule> rules = model.getRules(nextForm);
if(rules.size() == 1) {
GdlRule rule = rules.iterator().next();
//Persistence rule: Exactly one literal, the "true" form of the sentence
if(rule.arity() == 1) {
GdlLiteral literal = rule.get(0);
if(literal instanceof GdlRelation) {
//Check that it really is the true form
SentenceForm trueForm = nextForm.withName(GdlPool.getConstant("true"));
if(trueForm.matches((GdlRelation) literal)) {
GdlSentence head = rule.getHead();
GdlSentence body = (GdlSentence) literal;
//Check that the tuples are the same, and that they
//consist of distinct variables
List<GdlTerm> headTuple = GdlUtils.getTupleFromSentence(head);
List<GdlTerm> bodyTuple = GdlUtils.getTupleFromSentence(body);
if(headTuple.equals(bodyTuple)) {
//Distinct variables?
Set<GdlTerm> vars = new HashSet<GdlTerm>(headTuple);
if(vars.size() == headTuple.size()) {
nextFormsToReplace.add(nextForm);
}
}
}
}
}
}
}
}
List<Gdl> newDescription = new ArrayList<Gdl>(description);
//Now, replace the next forms
for(SentenceForm nextForm : nextFormsToReplace) {
SentenceForm initForm = nextForm.withName(GdlPool.getConstant("init"));
SentenceForm trueForm = nextForm.withName(GdlPool.getConstant("true"));
//Go through the rules and relations, making replacements as needed
for(int i = 0; i < newDescription.size(); i++) {
Gdl gdl = newDescription.get(i);
if(gdl instanceof GdlRelation) {
//Replace initForm
GdlRelation relation = (GdlRelation) gdl;
if(initForm.matches(relation)) {
GdlSentence newSentence = relation.get(0).toSentence();
newDescription.set(i, newSentence);
}
} else if(gdl instanceof GdlRule) {
GdlRule rule = (GdlRule) gdl;
//Remove persistence rule (i.e. rule with next form as head)
GdlSentence head = rule.getHead();
if(nextForm.matches(head)) {
newDescription.remove(i);
i--;
} else {
//Replace true in bodies of rules with relation-only form
List<GdlLiteral> body = rule.getBody();
List<GdlLiteral> newBody = replaceRelationInBody(body, trueForm);
if(!body.equals(newBody)) {
GdlRule newRule = GdlPool.getRule(head, newBody);
newDescription.set(i, newRule);
}
}
}
}