public GraphListener buildGraphBreadthFirst(int maxDepth, boolean clear)
{
System.out.println("Called new buildgraph");
Random old = rand_;
rand_ = new Random(FIXEDSEED);
GraphListener graph = (GraphListener) model_.addListener("graph");
boolean wasTesting = model_.setTesting(false);
// Build graph logic
Set<String> seenActions = new HashSet<String>();
Set<Object> seenStates = new HashSet<Object>();
Set<List<Integer>> visitedPaths = new HashSet<List<Integer>>();
Queue<List<Integer>> highPriority = new LinkedList<List<Integer>>();
Queue<List<Integer>> lowPriority = new LinkedList<List<Integer>>();
BitSet enabled = model_.enabledGuards();
//seenStates.put(model_.getState());
// Initially populate the queues and then proceed
for(int i = 0; i<enabled.cardinality(); i++) {
List<Integer> path = new LinkedList<Integer>();
if(enabled.get(i))
path.add(i);
highPriority.offer(path);
System.out.println("Populated buildgraph");
}
while(true) {
System.out.println("Called new buildgraph main loop");
List<Integer> path = highPriority.poll();
if(path == null) path = lowPriority.poll();
if(path == null) break; // No work left to do - exit
model_.doReset("Buildgraph");
// Exit condition
if(path.size() > maxDepth) break;
for(Integer p : path) {
String actionName = model_.getActionName(p);
model_.doAction(p);
seenActions.add(actionName);
// seenStates.add(model_.getState());
BitSet nenabled = model_.enabledGuards();
for(int i = 0; i<nenabled.cardinality(); i++) {
if(nenabled.get(i)) {
if(!seenActions.contains(model_.getActionName(i))) {
// High priority - not seen before
List<Integer> newPath = (List<Integer>) ((LinkedList<Integer>) path).clone();
newPath.add(i);
highPriority.offer(newPath);
} else {
// Low priority
List<Integer> newPath = (List<Integer>) ((LinkedList<Integer>) path).clone();
newPath.add(i);
//TODO: Need to make this a proper equality test - will fail at the moment
// because it is checking references. Should be equivalent to checking
// if we have taken the current path + the action under consideration before.
if(!visitedPaths.contains(newPath)) {
lowPriority.offer(newPath);
}
}
}
}
}
visitedPaths.add(path);
}
model_.setTesting(wasTesting);
model_.doReset("Buildgraph");
if (clear) {
graph.clearDoneTodo();
}
// restore the original random number generator.
rand_ = old;
return graph;