package jsetool.gui; import java.util.ArrayList; import java.util.Iterator; import jsetool.data.ExplorationState; /** * @author Mike Lawson * * @since Oct 23, 2004 * * The ExplorationStateLeveler class breaks the results of a * StateExplorer.exploreStates into a series of levels that will be rendered on * the GUI. This serves as the model for the result display. * * */ public class ExplorationStateLeveler { /** The levels as generated from the root node */ protected ArrayList levels = new ArrayList(); /** This holds the size of the biggest level found. */ protected int maxStatesPerLevel = 0; private ArrayList knownStates = new ArrayList(); /** * Constructs an ExplorationStateLeveler for the given root state * * @param rootExplorationState * the results of a call to StateExplorer.exploreStates * @see jsetool.engine.StateExplorer#exploreStates() */ public ExplorationStateLeveler(ExplorationState rootExplorationState) { levelize(rootExplorationState); } /** * Gets the generated levels * * @return ArrayList */ public ArrayList getLevels() { return levels; } /** * Returns the maximum states in any given level. * * @return int */ public int getMaxStatesPerLevel() { return maxStatesPerLevel; } /** * Breaks the given ExplorationState up into a series of levels fit for * rendering graphically. * * @param root */ protected void levelize(ExplorationState root) { // Build the 0th level out of the root state. ArrayList level0 = new ArrayList(); knownStates.add(root); level0.add(root); levels.add(level0); ArrayList nextLevel = level0; // Search breadth-first down the tree of states // adding the previous and creating the next // level until no more levels are created. for (;;) { nextLevel = createNextLevel(nextLevel); if (nextLevel.size() == 0) { break; } else { levels.add(nextLevel); if (nextLevel.size() > maxStatesPerLevel) { maxStatesPerLevel = nextLevel.size(); } } } } /** * Places the next level of exploration states into a collection. * * @param level * @return ArrayList */ protected ArrayList createNextLevel(ArrayList level) { ArrayList nextLevel = new ArrayList(); for (Iterator stateIter = level.iterator(); stateIter.hasNext();) { ExplorationState es = (ExplorationState) stateIter.next(); followTransitions(es, nextLevel); } return nextLevel; } /** * Adds all the states connected to the given exploration state to the next * level. * * @param es * @param nextLevel */ protected void followTransitions(ExplorationState es, ArrayList nextLevel) { for (Iterator nextStateIterator = es.nextStateIterator(); nextStateIterator.hasNext();) { ExplorationState.NextStateContainer nsc = (ExplorationState.NextStateContainer) nextStateIterator.next(); ExplorationState nextState = nsc.getExplorationState(); if (!knownStates.contains(nextState)) { nextLevel.add(nextState); knownStates.add(nextState); } } } }