package stoppers; import java.lang.*; import java.io.*; import main.BetterTokenizer; import main.Cell; import main.GeneralInput; import main.Model; import main.Node; public class CellThresholdStop extends SimpleStop { String [] nodes = new String[5]; int [] nodeNums = new int[5]; int numNodes = 0; float [] thresholds = new float[5]; float epsilon, interval; int xPos, yPos; boolean passed; public final static int HIGH = 1, LOW = 2; int mode = HIGH; // No arg constructor for instantiating by name public CellThresholdStop() { this(1); } public CellThresholdStop(float stop_time) { stopTime = stop_time; } public void init(Model model) { super.init(model); } public boolean stop(float time) { Cell cell; int node; float val, deriv; // trivial reasons to return if(time < lastTime + interval) return false; if(time > stopTime) return true; passed = true; score = 0; cell = model.cells[yPos * Cell.arrayWidth + xPos]; for(node = 0; node < numNodes; node++) { if(mode == HIGH && cell.getValue(nodeNums[node]) < thresholds[node] - epsilon) { passed = false; score += thresholds[node] - cell.getValue(nodeNums[node]); } else if(mode == LOW && cell.getValue(nodeNums[node]) > thresholds[node] + epsilon) { passed = false; score += cell.getValue(nodeNums[node]) - thresholds[node]; } } lastTime = time; return false; // Only stop when reached stop time } public void reset() { super.reset(); } public boolean didPass() { return passed; } public boolean didPass(float s) { return passed; } public void clearNodes() { numNodes = 0; } public void addNode(String node_name, float threshold) throws Exception { nodes[numNodes] = node_name; nodeNums[numNodes] = model.cells[0].getNodeNum(node_name); thresholds[numNodes] = threshold; numNodes++; } public void setMode(int mode) { this.mode = mode; } public void setEpsilon(float epsilon) { this.epsilon = epsilon; } public void setInterval(float interval) { this.interval = interval; } protected void loadParameter(String info, BetterTokenizer tokenizer) throws Exception { if(info.equals("Node")) { GeneralInput.nextToken(tokenizer); String node_name = tokenizer.sval; GeneralInput.nextToken(tokenizer); float thresh = (float)tokenizer.nval; addNode(node_name, thresh); } else if(info.equals("Epsilon")) { GeneralInput.nextToken(tokenizer); epsilon = ((float)tokenizer.nval); } else if(info.equals("Mode")) { GeneralInput.nextToken(tokenizer); if(tokenizer.sval.toUpperCase().equals("HIGH")) mode = HIGH; else mode = LOW; } else if(info.equals("Interval")) { GeneralInput.nextToken(tokenizer); interval = ((float)tokenizer.nval); } else if(info.equals("XPos")) { GeneralInput.nextToken(tokenizer); xPos = ((int)tokenizer.nval); } else if(info.equals("YPos")) { GeneralInput.nextToken(tokenizer); yPos = ((int)tokenizer.nval); } else super.loadParameter(info,tokenizer); } }