Method: run(RemoteInterpreter)

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2011 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: import java.io.BufferedReader;
25: import java.io.File;
26: import java.io.InputStreamReader;
27: import java.io.PrintWriter;
28: import java.util.regex.Matcher;
29: import java.util.regex.Pattern;
30:
31: import org.overture.interpreter.debug.RemoteControl;
32: import org.overture.interpreter.debug.RemoteInterpreter;
33: import org.overture.interpreter.runtime.SourceFile;
34:
35: public class RemoteSession implements RemoteControl
36: {
37:         private final static String COVERAGE = "./generated/word/";
38:
39:         public void run(RemoteInterpreter interpreter)
40:         {
41:                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
42:                 boolean carryOn = true;
43:                 System.out.println("Session started in "
44:                                 + new File(".").getAbsolutePath());
45:
46:•                while (carryOn)
47:                 {
48:                         try
49:                         {
50:                                 System.out.print(">> ");
51:                                 String line = in.readLine();
52:
53:•                                if (line == null)
54:                                 {
55:                                         return; // EOF
56:•                                } else if (line.length() == 0)
57:                                 {
58:                                         // Fine...
59:•                                } else if (line.equals("quit"))
60:                                 {
61:                                         in.close();
62:                                         carryOn = false;
63:•                                } else if (line.equals("?") || line.equals("help"))
64:                                 {
65:                                         System.out.println("? or help - show this message");
66:                                         System.out.println("init - reinitialize the specification");
67:                                         System.out.println("create - create a local value");
68:                                         System.out.println("env - show the local environment and values");
69:                                         System.out.println("files - list the source files of the specification");
70:                                         System.out.println("coverage - write Word HTML coverage for all files");
71:                                         System.out.println("modules - list the modules loaded");
72:                                         System.out.println("classes - list the classes loaded");
73:                                         System.out.println("<expression> - evaluate the expression");
74:                                         System.out.println("quit - leave the session");
75:•                                } else if (line.equals("init"))
76:                                 {
77:                                         long before = System.currentTimeMillis();
78:                                         interpreter.init();
79:                                         long after = System.currentTimeMillis();
80:                                         System.out.println("Initialized in "
81:                                                         + (double) (after - before) / 1000 + " secs.");
82:•                                } else if (line.startsWith("create "))
83:                                 {
84:                                         Pattern p = Pattern.compile("^create (\\w+)\\s*?:=\\s*(.+)$");
85:                                         Matcher m = p.matcher(line);
86:
87:•                                        if (m.matches())
88:                                         {
89:                                                 String var = m.group(1);
90:                                                 String exp = m.group(2);
91:
92:                                                 interpreter.create(var, exp);
93:                                                 System.out.println(var + " = "
94:                                                                 + interpreter.execute(var));
95:                                         } else
96:                                         {
97:                                                 System.out.println("Usage: create <id> := <value>");
98:                                         }
99:•                                } else if (line.equals("env"))
100:                                 {
101:                                         System.out.print(interpreter.getEnvironment());
102:•                                } else if (line.equals("files"))
103:                                 {
104:•                                        for (File file : interpreter.getSourceFiles())
105:                                         {
106:                                                 System.out.println(file.getAbsolutePath());
107:                                         }
108:•                                } else if (line.equals("coverage"))
109:                                 {
110:•                                        for (File file : interpreter.getSourceFiles())
111:                                         {
112:                                                 doCoverage(interpreter.getSourceFile(file));
113:                                         }
114:•                                } else if (line.equals("modules"))
115:                                 {
116:•                                        for (String name : interpreter.getModules())
117:                                         {
118:                                                 System.out.println(name);
119:                                         }
120:•                                } else if (line.equals("classes"))
121:                                 {
122:•                                        for (String name : interpreter.getClasses())
123:                                         {
124:                                                 System.out.println(name);
125:                                         }
126:                                 } else
127:                                 {
128:                                         long before = System.currentTimeMillis();
129:                                         String output = interpreter.execute(line);
130:                                         long after = System.currentTimeMillis();
131:                                         System.out.println(line + " = " + output);
132:                                         System.out.println("Executed in "
133:                                                         + (double) (after - before) / 1000 + " secs.");
134:                                 }
135:                         } catch (Exception e)
136:                         {
137:                                 System.out.println(e.getMessage());
138:                         }
139:                 }
140:         }
141:
142:         private void doCoverage(SourceFile source)
143:         {
144:                 try
145:                 {
146:                         File dir = new File(COVERAGE);
147:                         dir.mkdirs();
148:                         File html = new File(COVERAGE + source.filename.getName() + ".doc");
149:                         PrintWriter pw = new PrintWriter(html, "UTF-8");
150:                         source.printWordCoverage(pw);
151:                         pw.close();
152:                         System.out.println("Word HTML coverage written to " + html);
153:                 } catch (Exception e)
154:                 {
155:                         System.out.println("coverage: " + e.getMessage());
156:                 }
157:         }
158: }