📄 inputreader.java
字号:
/** * simulator/InputInterpreter.java */package simulator;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Iterator;import util.CommandStatus;import util.Verbose;/** * */public class InputReader extends EventSender{ private String streamName; private String parentPath; private BufferedReader myReader = null; private InputLine nextLine = null; private ArrayList<String> nextNames = null; private ArrayList<String> inputLines = null; public InputReader(Network net, ArrayList<String> inputArr, String name) { super(net); streamName = name; parentPath = null; inputLines = new ArrayList<String>(inputArr); } public InputReader(Network net, InputStream is, String name) { super(net); myReader = new BufferedReader(new InputStreamReader(is)); streamName = name; parentPath = null; assert myReader != null : "Buffered Reader of Input Interpreter == null!"; } public InputReader(Network net, String fileName) { super(net); openFile(fileName); } private void openFile(String fileName) { streamName = fileName; try { myReader = new BufferedReader(new FileReader(fileName)); if (network.vShouldLog(Verbose.INFO)) network.vprint("Interpreting file '" + fileName + "'..."); } catch (FileNotFoundException e) { throw new IllegalArgumentException("File not found: " + fileName); } File f = new File(fileName); parentPath = f.getParent(); } public String getStreamName() { return streamName; } public InputLine peekNextLine() { if (nextLine == null) { if (inputLines != null) { if (inputLines.size() < 1) return null; nextLine = new InputLine(network, parentPath, inputLines.remove(0)); return nextLine; } assert myReader != null : "Buffered Reader of Input Interpreter == null!"; try { String line = null; String nl = null; do { nl = myReader.readLine(); if (nl != null) { if (line == null) { line = nl.trim(); } else { line = line.substring(0, line.lastIndexOf('\\')) + " " + nl.trim(); } } } while (nl != null && line != null && line.endsWith("\\")); // EOF if (line == null) return null; nextLine = new InputLine(network, parentPath, line); } catch (IOException e) { throw new RuntimeException("Error reading stream: '" + streamName + "': " + e.getMessage()); } } return nextLine; } public void clearNextLine() { nextLine = null; } public InputLine pollNextLine() { InputLine ret = peekNextLine(); clearNextLine(); return ret; } public void runInput(double curTime) { if (curTime > network.getCurrentTime()) { ExternalEvent ee = new ExternalEvent(curTime, this); scheduleEvent(ee); return; } while (true) { InputLine line = peekNextLine(); if (line == null) { if (nextNames != null && nextNames.size() > 0) { // Get the path of the next file, relative to this file File currentPath = new File(streamName); File nextPath = new File(currentPath.getParentFile(), nextNames.get(0)); openFile(nextPath.getPath()); nextNames.remove(0); continue; } return; } if (!line.isComment() && !line.isEmpty()) { if (line.isCorrect()) { if (line.hasTime()) { double time = line.time(); if (time < 0) { throw new RuntimeException("Error reading stream '" + streamName + "': " + "Next Time in Input Stream < 0!\nLine: " + line); } // Next line in input has time < current time - error! else if (time < curTime) { throw new RuntimeException("Error reading stream '" + streamName + "': " + "Next Time in Input Stream < Current Event Time!\nCurrent Time: " + curTime + " Line: " + line); } // Next line in input has time > current time - create // new event // and schedule it to be fired else if (time > curTime) { ExternalEvent ee = new ExternalEvent(time, this); scheduleEvent(ee); return; } assert time == curTime : "Line Time != Current Time!"; } // Next line does not have time specified, or has time = // currentTime - we can execute it immediately CommandStatus cs = null; cs = checkLocalCmds(line, curTime); if (cs == null) { cs = line.execCommand(); } if (line.hasCommand() && !line.isEmpty() && cs == null) { cs = new CommandStatus("Unrecognized command!"); } if (cs != null) { cs.printStatus(line.toString(), streamName); } } else { System.err.println("Strange input line in stream '" + streamName + "': '" + line + "'"); System.exit(-1); } } clearNextLine(); } } private CommandStatus checkLocalCmds(InputLine line, double curTime) { assert curTime < 0 || curTime == network.getCurrentTime(); if (line.hasCommand() && (line.command().equals("interpret") || line.command().equals("interpreter"))) { Iterator<ArrayList<String>> it = line.commandArgs().iterator(); while (it.hasNext()) { ArrayList<String> l = it.next(); if (l.size() != 2) return new CommandStatus( "Interpret option takes list of file names. Each parameter schould look like: action=name!"); if (l.get(0).equals("schedule_file")) { if (nextNames == null) nextNames = new ArrayList<String>(); nextNames.add(l.get(1)); if (network.vShouldLog(Verbose.INFO)) network.vprint("Interpreter: Scheduling file '" + l.get(1) + "'."); } else if (l.get(0).equals("run_file")) { File path = new File(parentPath, l.get(1)); if (network.vShouldLog(Verbose.INFO)) network.vprint("Interpreter: Running file '" + path + "' now."); (new InputReader(network, path.getPath())).runInput(curTime); } else { return new CommandStatus( "Unknown type of interpreter action. Valid names are: schedule_file, run_file.\n"); } } return new CommandStatus(CommandStatus.COMMAND_OK); } return null; } protected void receiveLocalEvent(Event event) { runInput(event.getTime()); } private class ExternalEvent extends Event { private InputReader input = null; public ExternalEvent(double timeToFire, InputReader ir) { super(timeToFire, ir); input = ir; assert input != null : "Input Interpreter of External Event == null!"; } public String toString() { return "EXT_EVENT '" + input.getStreamName() + "': Time: " + getTime(); } // Events from input readers are always before other events. public long getSeq() { return -seq; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -