Package: ClassInterpreter

ClassInterpreter

nameinstructionbranchcomplexitylinemethod
ClassInterpreter(ClassList)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ClassInterpreter(IInterpreterAssistantFactory, ClassList)
M: 4 C: 30
88%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
create(String, String)
M: 60 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
evaluate(String, Context)
M: 43 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
execute(PExp, DBGPReader)
M: 2 C: 53
96%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 13
93%
M: 0 C: 1
100%
execute(String, DBGPReader)
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
findClass(String)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
findExpression(File, int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findGlobal(LexNameToken)
M: 104 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
findStatement(File, int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
findTraceDefinition(LexNameToken)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
findType(String)
M: 37 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getClasses()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDefaultFile()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getDefaultName()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getGlobalEnvironment()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getGlobalEnvironment(String)
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%
getInitialContext()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getInitialTraceContext(ANamedTraceDefinition, boolean)
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getProofObligations()
M: 6 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%
init(DBGPReader)
M: 0 C: 73
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
logSwapIn()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
logSwapOut()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
parseExpression(String, String)
M: 17 C: 27
61%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
runOneTrace(ANamedTraceDefinition, CallSequence, boolean)
M: 10 C: 39
80%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 5 C: 13
72%
M: 0 C: 1
100%
setDefaultName(String)
M: 26 C: 23
47%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 5 C: 5
50%
M: 0 C: 1
100%
traceInit(DBGPReader)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 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.runtime;
25:
26: import java.io.File;
27: import java.util.List;
28: import java.util.Set;
29: import java.util.Vector;
30:
31: import org.overture.ast.analysis.AnalysisException;
32: import org.overture.ast.annotations.Annotation;
33: import org.overture.ast.definitions.ANamedTraceDefinition;
34: import org.overture.ast.definitions.ATypeDefinition;
35: import org.overture.ast.definitions.PDefinition;
36: import org.overture.ast.definitions.SClassDefinition;
37: import org.overture.ast.expressions.PExp;
38: import org.overture.ast.factory.AstFactory;
39: import org.overture.ast.intf.lex.ILexLocation;
40: import org.overture.ast.lex.Dialect;
41: import org.overture.ast.lex.LexLocation;
42: import org.overture.ast.lex.LexNameToken;
43: import org.overture.ast.lex.LexToken;
44: import org.overture.ast.lex.VDMToken;
45: import org.overture.ast.statements.PStm;
46: import org.overture.ast.typechecker.NameScope;
47: import org.overture.ast.types.PType;
48: import org.overture.ast.util.Utils;
49: import org.overture.ast.util.definitions.ClassList;
50: import org.overture.config.Settings;
51: import org.overture.interpreter.annotations.INAnnotation;
52: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
53: import org.overture.interpreter.assistant.InterpreterAssistantFactory;
54: import org.overture.interpreter.debug.DBGPReader;
55: import org.overture.interpreter.messages.Console;
56: import org.overture.interpreter.messages.rtlog.RTLogger;
57: import org.overture.interpreter.messages.rtlog.RTThreadCreateMessage;
58: import org.overture.interpreter.messages.rtlog.RTThreadKillMessage;
59: import org.overture.interpreter.messages.rtlog.RTThreadSwapMessage;
60: import org.overture.interpreter.messages.rtlog.RTThreadSwapMessage.SwapType;
61: import org.overture.interpreter.scheduler.BasicSchedulableThread;
62: import org.overture.interpreter.scheduler.CTMainThread;
63: import org.overture.interpreter.scheduler.ISchedulableThread;
64: import org.overture.interpreter.scheduler.InitThread;
65: import org.overture.interpreter.scheduler.MainThread;
66: import org.overture.interpreter.scheduler.RunState;
67: import org.overture.interpreter.scheduler.Signal;
68: import org.overture.interpreter.scheduler.SystemClock;
69: import org.overture.interpreter.traces.CallSequence;
70: import org.overture.interpreter.util.ClassListInterpreter;
71: import org.overture.interpreter.values.BUSValue;
72: import org.overture.interpreter.values.CPUValue;
73: import org.overture.interpreter.values.NameValuePair;
74: import org.overture.interpreter.values.NameValuePairList;
75: import org.overture.interpreter.values.NameValuePairMap;
76: import org.overture.interpreter.values.ObjectValue;
77: import org.overture.interpreter.values.Value;
78: import org.overture.parser.lex.LexTokenReader;
79: import org.overture.parser.messages.VDMErrorsException;
80: import org.overture.parser.syntax.ExpressionReader;
81: import org.overture.parser.syntax.ParserException;
82: import org.overture.pog.obligation.ProofObligationList;
83: import org.overture.typechecker.Environment;
84: import org.overture.typechecker.FlatCheckedEnvironment;
85: import org.overture.typechecker.PrivateClassEnvironment;
86: import org.overture.typechecker.PublicClassEnvironment;
87: import org.overture.typechecker.assistant.definition.PDefinitionSet;
88:
89: /**
90: * The VDM++ interpreter.
91: */
92:
93: public class ClassInterpreter extends Interpreter
94: {
95:         private final ClassListInterpreter classes;
96:         private SClassDefinition defaultClass;
97:         private NameValuePairMap createdValues;
98:         private PDefinitionSet createdDefinitions;
99:
100:         public ClassInterpreter(ClassList classes) throws Exception
101:         {
102:                 this(new InterpreterAssistantFactory(), classes);
103:         }
104:
105:         public ClassInterpreter(IInterpreterAssistantFactory assistantFactory,
106:                         ClassList classes) throws Exception
107:         {
108:                 super(assistantFactory);
109:                 this.classes = new ClassListInterpreter(classes);
110:                 this.createdValues = new NameValuePairMap();
111:                 this.createdDefinitions = assistantFactory.createPDefinitionSet();
112:
113:•                if (classes.isEmpty())
114:                 {
115:                         setDefaultName(null);
116:                 } else
117:                 {
118:                         setDefaultName(classes.get(0).getName().getName());
119:                 }
120:         }
121:
122:         @Override
123:         public void setDefaultName(String cname) throws Exception
124:         {
125:•                if (cname == null)
126:                 {
127:                         defaultClass = AstFactory.newAClassClassDefinition();
128:                         classes.add(defaultClass);
129:                 } else
130:                 {
131:•                        for (SClassDefinition c : classes)
132:                         {
133:•                                if (c.getName().getName().equals(cname))
134:                                 {
135:                                         defaultClass = c;
136:                                         return;
137:                                 }
138:                         }
139:
140:                         throw new Exception("Class " + cname + " not loaded");
141:                 }
142:         }
143:
144:         @Override
145:         public String getDefaultName()
146:         {
147:                 return defaultClass.getName().getName();
148:         }
149:
150:         @Override
151:         public File getDefaultFile()
152:         {
153:                 return defaultClass.getName().getLocation().getFile();
154:         }
155:
156:         @Override
157:         public Set<File> getSourceFiles()
158:         {
159:                 return classes.getSourceFiles();
160:         }
161:
162:         public ClassListInterpreter getClasses()
163:         {
164:                 return classes;
165:         }
166:
167:         @Override
168:         public String getInitialContext()
169:         {
170:                 return initialContext.toString()
171:•                                + (createdValues.isEmpty() ? ""
172:                                                 : Utils.listToString("", createdValues.asList(), "\n", "\n"));
173:         }
174:
175:         @Override
176:         public Environment getGlobalEnvironment()
177:         {
178:                 return new PublicClassEnvironment(assistantFactory, classes, null);
179:         }
180:         
181:         @Override
182:         public Environment getGlobalEnvironment(String module)
183:         {
184:                 return getGlobalEnvironment();
185:         }
186:
187:         @Override
188:         public void init(DBGPReader dbgp)
189:         {
190:                 BasicSchedulableThread.terminateAll();
191:                 VdmRuntime.initialize();
192:
193:                 RuntimeValidator.init(this);
194:                 InitThread iniThread = new InitThread(Thread.currentThread());
195:                 BasicSchedulableThread.setInitialThread(iniThread);
196:
197:                 scheduler.init();
198:                 SystemClock.init();
199:                 CPUValue.init(scheduler, assistantFactory);
200:                 BUSValue.init();
201:                 ObjectValue.init();
202:
203:                 logSwapIn();
204:                 initialContext = classes.createInitialContext(assistantFactory);
205:                 classes.initialize(initialContext, assistantFactory, dbgp);
206:                 classes.systemInit(scheduler, dbgp, initialContext);
207:                 Annotation.init(INAnnotation.class, initialContext);
208:                 logSwapOut();
209:
210:                 createdValues = new NameValuePairMap();
211:                 createdDefinitions = assistantFactory.createPDefinitionSet();
212:
213:                 scheduler.reset(); // Required before a run, as well as init above
214:                 BUSValue.start(); // Start any BUS threads first...
215:         }
216:
217:         @Override
218:         public void traceInit(DBGPReader dbgp)
219:         {
220:                 BasicSchedulableThread.terminateAll();
221:                 scheduler.reset();
222:
223:                 SystemClock.init();
224:                 initialContext = classes.createInitialContext(assistantFactory);
225:                 classes.initialize(initialContext, assistantFactory, dbgp);
226:                 createdValues = new NameValuePairMap();
227:                 createdDefinitions = assistantFactory.createPDefinitionSet();
228:         }
229:
230:         @Override
231:         protected PExp parseExpression(String line, String module) throws Exception
232:         {
233:                 LexTokenReader ltr = new LexTokenReader(line, Settings.dialect, Console.charset);
234:                 ExpressionReader reader = new ExpressionReader(ltr);
235:                 reader.setCurrentModule(module);
236:                 PExp ast = reader.readExpression();
237:                 LexToken end = ltr.getLast();
238:                 
239:•                if (!end.is(VDMToken.EOF))
240:                 {
241:                         throw new ParserException(2330, "Tokens found after expression at " + end, new LexLocation(), 0);
242:                 }
243:                 
244:                 return ast;
245:         }
246:
247:         private Value execute(PExp expr, DBGPReader dbgp) throws Exception
248:         {
249:                 Context mainContext = new StateContext(assistantFactory, defaultClass.getName().getLocation(), "global static scope");
250:
251:                 mainContext.putAll(initialContext);
252:                 mainContext.putAll(createdValues);
253:                 mainContext.setThreadState(dbgp, CPUValue.vCPU);
254:                 clearBreakpointHits();
255:
256:                 // scheduler.reset();
257:
258:                 InitThread iniThread = new InitThread(Thread.currentThread());
259:                 BasicSchedulableThread.setInitialThread(iniThread);
260:
261:                 MainThread main = new MainThread(expr, mainContext);
262:                 main.start();
263:                 scheduler.start(main);
264:
265:•                if (Settings.dialect == Dialect.VDM_RT && RTLogger.getLogSize() > 0)
266:                 {
267:                         RTLogger.dump(true);
268:                 }
269:
270:                 RuntimeValidator.stop();
271:
272:                 return main.getResult(); // Can throw ContextException
273:         }
274:
275:         /**
276:          * Parse the line passed, type check it and evaluate it as an expression in the initial context.
277:          *
278:          * @param line
279:          * A VDM expression.
280:          * @return The value of the expression.
281:          * @throws Exception
282:          * Parser, type checking or runtime errors.
283:          */
284:
285:         @Override
286:         public Value execute(String line, DBGPReader dbgp) throws Exception
287:         {
288:                 PExp expr = parseExpression(line, getDefaultName());
289:                 Environment env = getGlobalEnvironment();
290:                 Environment created = new FlatCheckedEnvironment(assistantFactory, createdDefinitions.asList(), env, NameScope.NAMESANDSTATE);
291:
292:                 typeCheck(expr, created);
293:                 return execute(expr, dbgp);
294:         }
295:
296:         /**
297:          * Parse the line passed, and evaluate it as an expression in the context passed.
298:          *
299:          * @param line
300:          * A VDM expression.
301:          * @param ctxt
302:          * The context in which to evaluate the expression.
303:          * @return The value of the expression.
304:          * @throws Exception
305:          * Parser or runtime errors.
306:          */
307:
308:         @Override
309:         public Value evaluate(String line, Context ctxt) throws Exception
310:         {
311:                 PExp expr = parseExpression(line, getDefaultName());
312:                 PublicClassEnvironment globals = new PublicClassEnvironment(assistantFactory, classes, null);
313:                 Environment env = new PrivateClassEnvironment(assistantFactory, defaultClass, globals);
314:
315:                 try
316:                 {
317:                         typeCheck(expr, env);
318:                 } catch (VDMErrorsException e)
319:                 {
320:                         // We don't care... we just needed to type check it.
321:                 }
322:
323:                 ctxt.threadState.init();
324:                 try
325:                 {
326:                         return expr.apply(VdmRuntime.getExpressionEvaluator(), ctxt);
327:                 } catch (Exception e)
328:                 {
329:                         throw e;
330:                 }
331:         }
332:
333:         @Override
334:         public SClassDefinition findClass(String classname)
335:         {
336:                 LexNameToken name = new LexNameToken("CLASS", classname, null);
337:                 return (SClassDefinition) assistantFactory.createSClassDefinitionAssistant().findType(classes, name);
338:         }
339:
340:         @Override
341:         protected ANamedTraceDefinition findTraceDefinition(LexNameToken name)
342:         {
343:                 PDefinition d = assistantFactory.createSClassDefinitionAssistant().findName(classes, name, NameScope.NAMESANDSTATE);
344:
345:•                if (d == null || !(d instanceof ANamedTraceDefinition))
346:                 {
347:                         return null;
348:                 }
349:
350:                 return (ANamedTraceDefinition) d;
351:         }
352:
353:         @Override
354:         public Value findGlobal(LexNameToken name)
355:         {
356:                 // The name will not be type-qualified, so we can't use the usual
357:                 // findName methods
358:
359:•                for (SClassDefinition c : classes)
360:                 {
361:•                        for (PDefinition d : c.getDefinitions())
362:                         {
363:•                                if (assistantFactory.createPDefinitionAssistant().isFunctionOrOperation(d))
364:                                 {
365:                                         NameValuePairList nvpl = assistantFactory.createPDefinitionAssistant().getNamedValues(d, initialContext);
366:
367:•                                        for (NameValuePair n : nvpl)
368:                                         {
369:•                                                if (n.name.matches(name))
370:                                                 {
371:                                                         return n.value;
372:                                                 }
373:                                         }
374:                                 }
375:                         }
376:
377:•                        for (PDefinition d : c.getAllInheritedDefinitions())
378:                         {
379:•                                if (assistantFactory.createPDefinitionAssistant().isFunctionOrOperation(d))
380:                                 {
381:                                         NameValuePairList nvpl = assistantFactory.createPDefinitionAssistant().getNamedValues(d, initialContext);
382:
383:•                                        for (NameValuePair n : nvpl)
384:                                         {
385:•                                                if (n.name.matches(name))
386:                                                 {
387:                                                         return n.value;
388:                                                 }
389:                                         }
390:                                 }
391:                         }
392:                 }
393:
394:                 return null;
395:         }
396:
397:         @Override
398:         public PStm findStatement(File file, int lineno)
399:         {
400:                 return assistantFactory.createSClassDefinitionAssistant().findStatement(classes, file, lineno);
401:         }
402:
403:         @Override
404:         public PExp findExpression(File file, int lineno)
405:         {
406:                 return assistantFactory.createSClassDefinitionAssistant().findExpression(classes, file, lineno);
407:         }
408:
409:         public void create(String var, String exp) throws Exception
410:         {
411:                 PExp expr = parseExpression(exp, getDefaultName());
412:                 Environment env = getGlobalEnvironment();
413:                 Environment created = new FlatCheckedEnvironment(assistantFactory, createdDefinitions.asList(), env, NameScope.NAMESANDSTATE);
414:
415:                 PType type = typeCheck(expr, created);
416:                 Value v = execute(exp, null);
417:
418:                 ILexLocation location = defaultClass.getLocation();
419:                 LexNameToken n = new LexNameToken(defaultClass.getName().getName(), var, location);
420:
421:                 createdValues.put(n, v);
422:                 createdDefinitions.add(AstFactory.newALocalDefinition(location, n, NameScope.LOCAL, type));
423:         }
424:
425:         @Override
426:         public ProofObligationList getProofObligations() throws AnalysisException
427:         {
428:                 return classes.getProofObligations(assistantFactory);
429:         }
430:
431:         private void logSwapIn()
432:         {
433:                 // Show the "system constructor" thread creation
434:
435:                 ISchedulableThread thread = BasicSchedulableThread.getThread(Thread.currentThread());
436:
437:                 RTLogger.log(new RTThreadCreateMessage(thread, CPUValue.vCPU.resource));
438:
439:                 RTLogger.log(new RTThreadSwapMessage(SwapType.In, thread, CPUValue.vCPU.resource, 0, 0));
440:         }
441:
442:         private void logSwapOut()
443:         {
444:                 ISchedulableThread thread = BasicSchedulableThread.getThread(Thread.currentThread());
445:
446:                 RTLogger.log(new RTThreadSwapMessage(SwapType.Out, thread, CPUValue.vCPU.resource, 0, 0));
447:
448:                 RTLogger.log(new RTThreadKillMessage(thread, CPUValue.vCPU.resource));
449:         }
450:
451:         @Override
452:         public Context getInitialTraceContext(ANamedTraceDefinition tracedef,
453:                         boolean debug) throws AnalysisException
454:         {
455:                 ObjectValue object = null;
456:
457:                 SClassDefinition classdef = tracedef.getClassDefinition();
458:
459:                 // Create a new test object
460:                 object = assistantFactory.createSClassDefinitionAssistant().newInstance(classdef, null, null, initialContext);
461:
462:                 Context ctxt = new ObjectContext(assistantFactory, classdef.getName().getLocation(), classdef.getName().getName()
463:                                 + "()", initialContext, object);
464:
465:                 ctxt.put(classdef.getName().getSelfName(), object);
466:
467:                 return ctxt;
468:         }
469:
470:         @Override
471:         public List<Object> runOneTrace(ANamedTraceDefinition tracedef,
472:                         CallSequence test, boolean debug) throws AnalysisException
473:         {
474:                 List<Object> list = new Vector<Object>();
475:                 Context ctxt = null;
476:
477:                 try
478:                 {
479:                         ctxt = getInitialTraceContext(tracedef, debug);
480:                 } catch (ValueException e)
481:                 {
482:                         list.add(e.getMessage());
483:                         return list;
484:                 }
485:
486:                 clearBreakpointHits();
487:
488:                 // scheduler.reset();
489:                 CTMainThread main = new CTMainThread(test, ctxt, debug);
490:                 main.start();
491:                 scheduler.start(main);
492:
493:                 // Ensures all threads are terminated for next trace run
494:                 BasicSchedulableThread.signalAll(Signal.TERMINATE);
495:
496:•                while (main.getRunState() != RunState.COMPLETE)
497:                 {
498:                         try
499:                         {
500:                                 Thread.sleep(10);
501:                         } catch (InterruptedException e)
502:                         {
503:                                 break;
504:                         }
505:                 }
506:
507:                 return main.getList();
508:         }
509:
510:         @Override
511:         public PType findType(String typename)
512:         {
513:•                for (SClassDefinition cDef : classes)
514:                 {
515:•                        for (PDefinition def : cDef.getDefinitions())
516:                         {
517:•                                if (def instanceof ATypeDefinition)
518:                                 {
519:•                                        if (def.getName().equals(typename))
520:                                         {
521:                                                 return def.getType();
522:                                         }
523:                                 }
524:                         }
525:                 }
526:                 return null;
527:         }
528: }