📄 shell.java
字号:
long free = Runtime.getRuntime().freeMemory(); NumberFormat nf = NumberFormat.getInstance(); Log.println("(mem: " + NumberFormat.getPercentInstance().format( (double) free / (double) total) + " = " + nf.format(free) + " bytes free, " + nf.format(total) + " bytes total)"); } /** * Prints information about the current system state. */ private void cmdInfoState() throws NoSystemException { MSystem system = system(); MModel model = system.model(); MSystemState state = system.state(); NumberFormat nf = NumberFormat.getInstance(); System.out.println("State: " + state.name()); // generate report for objects Report report = new Report(3, "$l : $r $r"); // header report.addRow(); report.addCell("class"); report.addCell("#objects"); report.addCell("+ #objects in subclasses"); report.addRuler('-'); // data Iterator it = model.classes().iterator(); long total = 0; int n; while (it.hasNext()) { MClass cls = (MClass) it.next(); report.addRow(); String clsname = cls.name(); if (cls.isAbstract()) clsname = '(' + clsname + ')'; report.addCell(clsname); n = state.objectsOfClass(cls).size(); total += n; report.addCell(nf.format(n)); n = state.objectsOfClassAndSubClasses(cls).size(); report.addCell(nf.format(n)); } // footer report.addRuler('-'); report.addRow(); report.addCell("total"); report.addCell(nf.format(total)); report.addCell(""); // print report report.printOn(System.out); System.out.println(); // generate report for links report = new Report(2, "$l : $r"); // header report.addRow(); report.addCell("association"); report.addCell("#links"); report.addRuler('-'); // data it = model.associations().iterator(); total = 0; while (it.hasNext()) { MAssociation assoc = (MAssociation) it.next(); report.addRow(); report.addCell(assoc.name()); n = state.linksOfAssociation(assoc).size(); report.addCell(nf.format(n)); total += n; } // footer report.addRuler('-'); report.addRow(); report.addCell("total"); report.addCell(nf.format(total)); // print report report.printOn(System.out); } /** * Prints information about global variables. */ private void cmdInfoVars() throws NoSystemException { MSystem system = system(); Iterator it = system.topLevelBindings().iterator(); while (it.hasNext()) { VarBindings.Entry entry = (VarBindings.Entry) it.next(); System.out.println(entry); } } /** * Sets input to multi-line mode. */ private void cmdMultiLine() { fMultiLineMode = true; } /** * Reads commands from a socket. */ private void cmdNet() { int port = 1777; try { Log.verbose("waiting for connection on port " + port + "..."); ServerSocket socket = new ServerSocket(port); Socket client = socket.accept(); InetAddress clientAddr = client.getInetAddress(); Log.verbose("connected to " + clientAddr.getHostName() + "/" + client.getPort()); Readline readline = new SocketReadline(client, true, "net>"); fReadlineStack.push(readline); } catch (IOException ex) { Log.error("Can't bind or listen on port " + port + "."); } } /** * Checks which file type is to be opened and calls the specific open * command (<code>cmdOpenUseFile</code>,<code>cmdRead</code>, * <code>cmdLoad</code>). * * @param line * Path and filename to be opened. */ private void cmdOpen(String line) { boolean doEcho = true; StringTokenizer st = new StringTokenizer(line); // if there is no filename and option if (!st.hasMoreTokens()) { Log.error("Unknown command `open " + line + "'. " + "Try `help'."); return; } String token = st.nextToken(); // option quiet if (token.equals("-q")) { doEcho = false; // if there is no filename if (!st.hasMoreTokens()) { Log.error("Unknown command `open " + line + "'. " + "Try `help'."); return; } token = st.nextToken(); } // to find out what command will be needed try { String firstWord = getFirstWordOfFile(token); // if getFirstWordOfFile returned with error code, than // end this method. if (firstWord != null && firstWord.equals("ERROR: -1")) { return; } if (firstWord == null) { Log.println("Nothing to do, because file `" + line + "' " + "contains no data!"); // Necessary if USE is started with a cmd-file and option -q or // -qv. This call provides the readline stack with the one // readline object and no EmptyStackException will be thrown. if (Options.cmdFilename != null) { cmdRead(Options.cmdFilename, false); } return; } if (firstWord.startsWith("model")) { cmdOpenUseFile(token); } else if (firstWord.startsWith("context")) { cmdGenLoadInvariants(token, system(), doEcho); } else { cmdRead(token, doEcho); } } catch (NoSystemException e) { Log.error("No System available. Please load a model before " + "executing this command."); } } /** * Reads a specification file. */ private void cmdOpenUseFile(String filename) { MModel model = null; Reader r = null; try { Log.verbose("compiling specification..."); r = new BufferedReader(new FileReader(filename)); model = USECompiler.compileSpecification(r, filename, new PrintWriter(System.err), new ModelFactory()); } catch (FileNotFoundException e) { Log.error("File `" + filename + "' not found."); } finally { if (r != null) try { r.close(); } catch (IOException ex) { //TODO: Should this be silently ignored? [throw new // Error(ex)?] } } // compile ok? if (model != null) { // print some info about model Log.verbose(model.getStats()); // create system fSession.setSystem(new MSystem(model)); } } /** * Performs a query. */ private void cmdQuery(String line, boolean verboseEval) throws NoSystemException { Log.trace(this, line); if (line.length() == 0) { Log.error("Expression expected after `?'. Try `help'."); return; } // compile query MSystem system = system(); Expression expr = USECompiler.compileExpression(system.model(), new StringReader(line), "<input>", new PrintWriter(System.err), system.topLevelBindings()); // compile errors? if (expr == null) return; // evaluate it with current system state PrintWriter output = null; Evaluator evaluator = new Evaluator(); if (verboseEval) { Log.println("Detailed results of subexpressions:"); output = new PrintWriter(Log.out()); evaluator.enableEvalTree(); } try { Value val = evaluator.eval(expr, system.state(), system .topLevelBindings(), output); // print result System.out.println("-> " + val.toStringWithType()); // System.out.println("-> " + val.toString() + ":" + expr.type()); if (verboseEval && Options.doGUI) ExprEvalBrowser.create(evaluator.getEvalNodeRoot()); } catch (MultiplicityViolationException e) { System.out.println("-> " + "Could not evaluate. " + e.getMessage()); } } /** * Derives the static type of an expression. */ private void cmdDeriveStaticType(String line) throws NoSystemException { Log.trace(this, line); if (line.length() == 0) { Log.error("Expression expected after `?'. Try `help'."); return; } // compile query MSystem system = system(); Expression expr = USECompiler.compileExpression(system.model(), new StringReader(line), "<input>", new PrintWriter(System.err), system.topLevelBindings()); // compile errors? if (expr == null) return; // // evaluate it with current system state // PrintWriter output = null; // Evaluator evaluator = new Evaluator(); // if ( verboseEval ) { // Log.println("Detailed results of subexpressions:"); // output = new PrintWriter(Log.out()); // evaluator.enableEvalTree(); // } // try { // Value val = evaluator.eval(expr, system.state(), // system.topLevelBindings(), // output); // // print result // System.out.println("-> " + val.toStringWithType()); // // System.out.println("-> " + val.toString() + ":" + expr.type()); // if ( verboseEval && Options.doGUI ) // ExprEvalBrowser.create(evaluator.getEvalNodeRoot()); // } catch (MultiplicityViolationException e) { // System.out.println("-> " + "Could not evaluate. " + e.getMessage()); // } System.out.println("-> " + expr.type()); } /** * Reads a file with commands and processes them. */ private void cmdRead(String filename, boolean doEcho) { try { BufferedReader reader = new BufferedReader(new FileReader(filename)); // read from file, echo each line as it is read Readline fReadline; if (Options.quiet || !doEcho) fReadline = LineInput.getStreamReadline(reader, false, false, ""); else fReadline = LineInput.getStreamReadline(reader, true, true, filename + "> "); fReadlineStack.push(fReadline); } catch (FileNotFoundException e) { Log.error("File `" + filename + "' not found."); } } /** * Resets system to empty state. */ private void cmdReset() throws NoSystemException { fSession.reset(); } /** * Activates step mode. */ private void cmdStepOn() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -