Package: RemoteInterpreter

RemoteInterpreter

nameinstructionbranchcomplexitylinemethod
RemoteInterpreter(Interpreter, DBGPReader)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
create(String, String)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
execute(String)
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
finish()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getClasses()
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getDebugReader()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getEnvironment()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInterpreter()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getModules()
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getSourceFile(File)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSourceFiles()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getValueFactory()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
init()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
processRemoteCalls()
M: 76 C: 0
0%
M: 11 C: 0
0%
M: 7 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
valueExecute(String)
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2009 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: package org.overture.interpreter.debug;
25:
26: import java.io.File;
27: import java.io.IOException;
28: import java.util.List;
29: import java.util.Set;
30: import java.util.Vector;
31: import java.util.concurrent.ArrayBlockingQueue;
32:
33: import org.overture.ast.definitions.SClassDefinition;
34: import org.overture.ast.modules.AModuleModules;
35: import org.overture.interpreter.debug.RemoteInterpreter.Call.CallType;
36: import org.overture.interpreter.runtime.ClassInterpreter;
37: import org.overture.interpreter.runtime.Interpreter;
38: import org.overture.interpreter.runtime.ModuleInterpreter;
39: import org.overture.interpreter.runtime.SourceFile;
40: import org.overture.interpreter.scheduler.BasicSchedulableThread;
41: import org.overture.interpreter.values.Value;
42: import org.overture.interpreter.values.ValueFactory;
43:
44: public class RemoteInterpreter
45: {
46:         private final Interpreter interpreter;
47:         private final DBGPReader dbgp;
48:         private boolean running = false;
49:         private Thread vdmExecuteThread;
50:
51:         public RemoteInterpreter(Interpreter interpreter, DBGPReader dbgp)
52:         {
53:                 this.interpreter = interpreter;
54:                 this.dbgp = dbgp;
55:         }
56:
57:         public Interpreter getInterpreter()
58:         {
59:                 return interpreter;
60:         }
61:
62:         public ValueFactory getValueFactory()
63:         {
64:                 return new ValueFactory();
65:         }
66:
67:         public DBGPReader getDebugReader()
68:         {
69:                 return dbgp;
70:         }
71:
72:         public String execute(String line) throws Exception
73:         {
74:•                if (isFinished)
75:                 {
76:                         throw new Exception("RemoteInterpreter has finished.");
77:                 }
78:
79:                 executionQueueRequest.add(new Call(CallType.Execute, line));
80:                 Object result = executionQueueResult.take();
81:•                if (result instanceof Exception)
82:                 {
83:                         throw (Exception) result;
84:                 } else
85:                 {
86:                         return ((Value) result).toString();
87:                 }
88:         }
89:
90:         public Value valueExecute(String line) throws Exception
91:         {
92:•                if (isFinished)
93:                 {
94:                         throw new Exception("RemoteInterpreter has finished.");
95:                 }
96:
97:                 executionQueueRequest.add(new Call(CallType.Execute, line));
98:                 Object result = executionQueueResult.take();
99:•                if (result instanceof Exception)
100:                 {
101:                         throw (Exception) result;
102:                 } else
103:                 {
104:                         return (Value) result;
105:                 }
106:         }
107:
108:         public void init()
109:         {
110:                 interpreter.init(dbgp);
111:         }
112:
113:         public void create(String var, String exp) throws Exception
114:         {
115:•                if (isFinished)
116:                 {
117:                         throw new Exception("RemoteInterpreter has finished.");
118:                 }
119:
120:•                if (interpreter instanceof ClassInterpreter)
121:                 {
122:                         executionQueueRequest.add(new Call(CallType.Create, var, exp));
123:                         Object result = executionQueueResult.take();
124:•                        if (result instanceof Exception)
125:                         {
126:                                 throw (Exception) result;
127:                         }
128:                 } else
129:                 {
130:                         throw new Exception("Only available for VDM++ and VDM-RT");
131:                 }
132:         }
133:
134:         public String getEnvironment()
135:         {
136:                 return interpreter.getInitialContext();
137:         }
138:
139:         public Set<File> getSourceFiles()
140:         {
141:                 return interpreter.getSourceFiles();
142:         }
143:
144:         public SourceFile getSourceFile(File file) throws IOException
145:         {
146:                 return interpreter.getSourceFile(file);
147:         }
148:
149:         public List<String> getModules() throws Exception
150:         {
151:                 List<String> names = new Vector<String>();
152:
153:•                if (interpreter instanceof ClassInterpreter)
154:                 {
155:                         throw new Exception("Only available for VDM-SL");
156:                 } else
157:                 {
158:•                        for (AModuleModules m : ((ModuleInterpreter) interpreter).getModules())
159:                         {
160:                                 names.add(m.getName().getName());
161:                         }
162:                 }
163:
164:                 return names;
165:         }
166:
167:         public List<String> getClasses() throws Exception
168:         {
169:                 List<String> names = new Vector<String>();
170:
171:•                if (interpreter instanceof ClassInterpreter)
172:                 {
173:•                        for (SClassDefinition def : ((ClassInterpreter) interpreter).getClasses())
174:                         {
175:                                 names.add(def.getName().getName());
176:                         }
177:                 } else
178:                 {
179:                         throw new Exception("Only available for VDM++ and VDM-RT");
180:                 }
181:
182:                 return names;
183:         }
184:
185:         public void finish()
186:         {
187:                 isFinished = true;
188:•                if (vdmExecuteThread != null)
189:                 {
190:                         vdmExecuteThread.interrupt();
191:                 }
192:         }
193:
194:         ArrayBlockingQueue<Call> executionQueueRequest = new ArrayBlockingQueue<Call>(1);
195:         ArrayBlockingQueue<Object> executionQueueResult = new ArrayBlockingQueue<Object>(1);
196:         private boolean isFinished;
197:
198:         public static class Call
199:         {
200:                 public enum CallType
201:                 {
202:                         Execute, Create
203:                 };
204:
205:                 public final CallType type;
206:                 public final String exp;
207:                 public final String var;
208:
209:                 public Call(CallType type, String var, String exp)
210:                 {
211:                         this.type = type;
212:                         this.var = var;
213:                         this.exp = exp;
214:                 }
215:
216:                 public Call(CallType type, String exp)
217:                 {
218:                         this.type = type;
219:                         this.var = null;
220:                         this.exp = exp;
221:                 }
222:
223:                 @Override
224:                 public String toString()
225:                 {
226:                         return this.type + " " + this.var + " " + this.exp;
227:                 }
228:         }
229:
230:         public void processRemoteCalls() throws Exception
231:         {
232:•                if (running)
233:                 {
234:                         return;
235:                 }
236:
237:•                if (BasicSchedulableThread.getThread(Thread.currentThread()) == null)
238:                 {
239:                         throw new Exception("Process Remote Calls can only be called from a valid VDM thread");
240:                 }
241:
242:                 running = true;
243:                 vdmExecuteThread = Thread.currentThread();
244:•                while (!isFinished)
245:                 {
246:                         try
247:                         {
248:                                 Call call = executionQueueRequest.take();
249:                                 try
250:                                 {
251:•                                        switch (call.type)
252:                                         {
253:                                                 case Create:
254:•                                                        if (interpreter instanceof ClassInterpreter)
255:                                                         {
256:                                                                 ClassInterpreter ci = (ClassInterpreter) interpreter;
257:                                                                 ci.create(call.var, call.exp);
258:                                                                 executionQueueResult.add(new Object());
259:                                                         }
260:                                                         break;
261:                                                 case Execute:
262:                                                         executionQueueResult.add(interpreter.execute(call.exp, dbgp));
263:                                                         break;
264:
265:                                         }
266:                                 } catch (Exception e)
267:                                 {
268:                                         executionQueueResult.add(e);
269:                                 }
270:                         } catch (InterruptedException e)
271:                         {
272:                                 return;
273:                         }
274:                 }
275:         }
276:
277: }