/** * returns true iff the theory is well-founded, i.e., there is no infinite sequence A1,A2,... of arguments with Ai attacking Ai+1 * @return true iff the theory is well-founded */ public boolean isWellFounded(){ List<Argument> arguments = new ArrayList<Argument>(); for(Formula f: this) arguments.add((Argument) f); boolean[] dfn = new boolean[arguments.size()]; boolean[] inProgress = new boolean[arguments.size()]; for(int i = 0; i < arguments.size(); i++){ dfn[i] = false; inProgress[i] = false; } for(int i = 0; i < arguments.size(); i++) if(!dfn[i]) if(dfs(i,arguments,dfn,inProgress)) return false; return true; }
/** * Depth-First-Search to find a cycle in the theory. Auxiliary method to determine if the theory is well-founded * @param i current node * @param arguments list of all nodes (arguments) * @param dfn array which keeps track whether a node has been visited * @param inProgress array which keeps track which nodes are currently being processed * @return true iff the theory contains a cycle */ private boolean dfs(int i, List<Argument> arguments, boolean[] dfn, boolean[] inProgress){ dfn[i] = true; inProgress[i] = true; Set<Argument> attackers = getAttackers(arguments.get(i)); Iterator<Argument> it = attackers.iterator(); while(it.hasNext()){ Argument argument = (Argument) it.next(); if(inProgress[arguments.indexOf(argument)]) return true; else if(!dfn[arguments.indexOf(argument)]) if(dfs(arguments.indexOf(argument),arguments,dfn,inProgress)) return true; } inProgress[i] = false; return false; }