Package: TraceInterpreter$StopWatch

TraceInterpreter$StopWatch

nameinstructionbranchcomplexitylinemethod
set()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
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%
stop(String)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * Combinatorial Testing Runtime
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ct.ctruntime;
23:
24: import java.io.File;
25: import java.io.IOException;
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.overture.ast.analysis.AnalysisException;
30: import org.overture.ast.definitions.ANamedTraceDefinition;
31: import org.overture.ast.definitions.PDefinition;
32: import org.overture.ast.definitions.SClassDefinition;
33: import org.overture.ast.modules.AModuleModules;
34: import org.overture.ast.node.INode;
35: import org.overture.ast.util.modules.CombinedDefaultModule;
36: import org.overture.config.Settings;
37: import org.overture.ct.utils.TraceXmlWrapper;
38: import org.overture.interpreter.runtime.ClassInterpreter;
39: import org.overture.interpreter.runtime.Context;
40: import org.overture.interpreter.runtime.ContextException;
41: import org.overture.interpreter.runtime.Interpreter;
42: import org.overture.interpreter.runtime.ModuleInterpreter;
43: import org.overture.interpreter.runtime.ValueException;
44: import org.overture.interpreter.traces.CallSequence;
45: import org.overture.interpreter.traces.TestSequence;
46: import org.overture.interpreter.traces.TraceReductionType;
47: import org.overture.interpreter.traces.Verdict;
48: import org.overture.typechecker.Environment;
49: import org.overture.typechecker.ModuleEnvironment;
50: import org.overture.typechecker.PrivateClassEnvironment;
51: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
52:
53: public class TraceInterpreter
54: {
55:         private final static boolean DEBUG = false;
56:
57:         private static class StopWatch
58:         {
59:                 static long before = 0;
60:
61:                 public static void set()
62:                 {
63:                         before = System.currentTimeMillis();
64:                 }
65:
66:                 public static void stop(String msg)
67:                 {
68:                         if (DEBUG)
69:                         {
70:                                 long after = System.currentTimeMillis();
71:                                 long duration = after - before;
72:                                 System.out.println("Completed '" + msg + "' in "
73:                                                 + (double) duration / 1000 + " secs. ");
74:                         }
75:                 }
76:
77:         }
78:
79:         protected long beginClass = 0;
80:         protected long beginTrace = 0;
81:         protected String activeClass = "";
82:         protected String activeTrace;
83:
84:         Interpreter interpreter;
85:         protected File coverage;
86:         boolean reduce = false;
87:         protected float subset;
88:         protected long seed = 999;
89:         protected TraceReductionType traceReductionType = TraceReductionType.NONE;
90:         IProgressMonitor monitor = null;
91:
92:         private Integer currentPct = 0;
93:
94:         public final ITypeCheckerAssistantFactory assistantFactory;
95:
96:         public TraceInterpreter(IProgressMonitor monitor,
97:                         ITypeCheckerAssistantFactory af)
98:         {
99:                 this.monitor = monitor;
100:                 this.assistantFactory = af;
101:         }
102:
103:         public TraceInterpreter(IProgressMonitor monitor, float subset,
104:                         TraceReductionType traceReductionType, long seed,
105:                         ITypeCheckerAssistantFactory af)
106:         {
107:                 this(monitor, af);
108:                 this.reduce = true;
109:                 this.seed = seed;
110:                 this.traceReductionType = traceReductionType;
111:                 this.subset = subset;
112:         }
113:
114:         public void run(String moduleName, String traceName,
115:                         Interpreter interpreter, TraceXmlWrapper store) throws Exception
116:         {
117:                 this.interpreter = interpreter;
118:
119:                 List<PDefinition> definitions = null;
120:
121:                 if (interpreter instanceof ModuleInterpreter)
122:                 {
123:                         for (AModuleModules module : ((ModuleInterpreter) interpreter).modules)
124:                         {
125:                                 if (module.getName().getName().equals(moduleName))
126:                                 {
127:                                         definitions = module.getDefs();
128:                                 }
129:                         }
130:                 } else
131:                 {
132:                         for (SClassDefinition classDefinition : ((ClassInterpreter) interpreter).getClasses())
133:                         {
134:                                 if (classDefinition.getName().getName().equals(moduleName))
135:                                 {
136:                                         definitions = assistantFactory.createPDefinitionAssistant().getDefinitions(classDefinition);
137:
138:                                 }
139:                         }
140:                 }
141:
142:                 processTraces(definitions, moduleName, traceName, store);
143:         }
144:
145:         private void processTraces(List<PDefinition> definitions, String className,
146:                         String traceName, TraceXmlWrapper storage) throws Exception
147:         {
148:                 try
149:                 {
150:                         Settings.prechecks = true;
151:                         Settings.postchecks = true;
152:                         Settings.dynamictypechecks = true;
153:
154:                         if (storage != null)
155:                         {
156:                                 storage.StartClass(className);
157:                         }
158:
159:                         Integer numberOfTraces = 0;
160:
161:                         for (Object string : definitions)
162:                         {
163:                                 if (string instanceof ANamedTraceDefinition)
164:                                 {
165:                                         numberOfTraces++;
166:                                 }
167:
168:                         }
169:                         infoProcessingClass(className, numberOfTraces);
170:
171:                         List<ANamedTraceDefinition> traceDefs = getAllTraceDefinitions(definitions, traceName);
172:
173:                         for (ANamedTraceDefinition def : traceDefs)
174:                         {
175:                                 evaluateTraceDefinition(className, storage, def);
176:                         }
177:
178:                         infoCompleted();
179:
180:                         if (DEBUG)
181:                         {
182:                                 System.out.println("Completed");
183:                         }
184:                 } catch (ContextException e)
185:                 {
186:                         error(e.getMessage());
187:                         throw e;
188:                 } catch (ValueException e)
189:                 {
190:                         error(e.getMessage());
191:                         throw e;
192:                 } catch (Exception e)
193:                 {
194:                         error(e.getMessage());
195:                         throw e;
196:                 } finally
197:                 {
198:                         if (storage != null)
199:                         {
200:                                 storage.Stop();
201:                         }
202:                 }
203:         }
204:
205:         /**
206:          * obtain all trace definitions, or just the one named
207:          *
208:          * @param definitions
209:          * @param traceName
210:          * null or a name of a trace
211:          * @return a list of named trace definitions
212:          */
213:         private List<ANamedTraceDefinition> getAllTraceDefinitions(
214:                         List<PDefinition> definitions, String traceName)
215:         {
216:                 List<ANamedTraceDefinition> traceDefs = new Vector<ANamedTraceDefinition>();
217:
218:                 for (Object definition : definitions)
219:                 {
220:                         if (definition instanceof ANamedTraceDefinition)
221:                         {
222:                                 if (traceName == null
223:                                                 || ((ANamedTraceDefinition) definition).getName().getName().equals(traceName))
224:                                 {
225:                                         traceDefs.add((ANamedTraceDefinition) definition);
226:                                 }
227:                         }
228:                 }
229:                 return traceDefs;
230:         }
231:
232:         protected void evaluateTraceDefinition(String className,
233:                         TraceXmlWrapper storage, Object definition) throws ValueException,
234:                         AnalysisException, Exception
235:         {
236:                 interpreter.init(null);
237:                 Context ctxt = interpreter.getInitialTraceContext((ANamedTraceDefinition) definition, false);
238:
239:                 evaluateTests(className, storage, definition, ctxt);
240:         }
241:
242:         private void evaluateTests(String className, TraceXmlWrapper storage,
243:                         Object traceDefinition, Context ctxt) throws Exception
244:         {
245:                 ANamedTraceDefinition mtd = (ANamedTraceDefinition) traceDefinition;
246:                 TestSequence tests = null;
247:                 if (!reduce)
248:                 {
249:                         subset = 1.0F;
250:                         traceReductionType = TraceReductionType.NONE;
251:                         seed = 999;
252:                 }
253:
254:                 tests = ctxt.assistantFactory.createANamedTraceDefinitionAssistant().getTests(mtd, ctxt, subset, traceReductionType, seed);
255:
256:                 int size = tests.size();
257:
258:                 infoProcessingTrace(className, mtd.getName().getName(), size);
259:                 if (storage != null)
260:                 {
261:                         storage.StartTrace(mtd.getName().getName(), mtd.getLocation().getFile().getName(), mtd.getLocation().getStartLine(), mtd.getLocation().getStartPos(), size, new Float(subset), TraceReductionType.valueOf(traceReductionType.toString()), new Long(seed));
262:                 }
263:
264:                 INode traceContainer = null;
265:                 Environment rootEnv = null;
266:                 if (interpreter instanceof ClassInterpreter)
267:                 {
268:                         traceContainer = mtd.getClassDefinition();
269:                         rootEnv = new PrivateClassEnvironment(interpreter.getAssistantFactory(), mtd.getClassDefinition(), interpreter.getGlobalEnvironment());
270:                         
271:                 } else
272:                 {
273:                         traceContainer = mtd.parent();
274:                         if(((AModuleModules)traceContainer).getIsFlat())
275:                         {
276:                                 //search for the combined module
277:                                 for(AModuleModules m : ((ModuleInterpreter)interpreter).modules)
278:                                 {
279:                                         if(m instanceof CombinedDefaultModule)
280:                                         {
281:                                                 traceContainer = m;
282:                                                 break;
283:                                         }
284:                                 }
285:                         }
286:                         rootEnv = new ModuleEnvironment(interpreter.getAssistantFactory(), (AModuleModules) traceContainer);
287:                 }
288:
289:                 int n = 1;
290:
291:                 int faildCount = 0;
292:                 int inconclusiveCount = 0;
293:                 int skippedCount = 0;
294:
295:                 StopWatch.set();
296:
297:                 for (CallSequence test : tests)
298:                 {
299:                         StopWatch.stop("Getting test");
300:
301:                         infoProcessingTest(className, mtd.getName().getName(), n, size);
302:
303:                         List<Object> result = null;
304:                         Verdict verdict = null;
305:
306:                         try
307:                         {
308:                                 if (interpreter instanceof ClassInterpreter)
309:                                 {
310:                                         Interpreter.typeCheck(traceContainer, interpreter, test, rootEnv);
311:                                 } else
312:                                 {
313:                                         Interpreter.typeCheck(traceContainer, interpreter, test, rootEnv);
314:                                 }
315:                         } catch (Exception e)
316:                         {
317:                                 result = new Vector<Object>();
318:                                 result.add(e);
319:                                 verdict = Verdict.FAILED;
320:                                 result.add(verdict);
321:                         }
322:
323:                         StopWatch.set();
324:                         result = evaluateCallSequence(mtd, test);
325:                         StopWatch.stop("Executing ");
326:                         StopWatch.set();
327:
328:                         verdict = (Verdict) result.get(result.size() - 1);
329:
330:                         if (verdict == Verdict.ERROR)
331:                         {
332:                         } else
333:                         {
334:                                 tests.filter(result, test, n);
335:                         }
336:
337:                         switch (verdict)
338:                         {
339:                                 case FAILED:
340:                                         faildCount++;
341:                                         break;
342:                                 case INCONCLUSIVE:
343:                                         inconclusiveCount++;
344:                                         break;
345:                                 default:
346:                                         break;
347:                         }
348:
349:                         if (storage != null)
350:                         {/*
351:                          * Bodge until we figure out how to not have explicit op names.
352:                          */
353:                                 String clean = test.toString().replaceAll("\\.\\w+`", ".");
354:                                 storage.StartTest(new Integer(n).toString(), clean);
355:                                 storage.StopElement();
356:                         }
357:
358:                         if (test.getFilter() > 0)
359:                         {
360:                                 skippedCount++;
361:                                 infoTestFiltered(n, test.getFilter(), test);
362:                                 if (storage != null)
363:                                 {
364:                                         storage.AddSkippedResult(new Integer(n).toString());
365:                                 }
366:                         } else
367:                         {
368:
369:                                 if (verdict == Verdict.ERROR)
370:                                 {
371:                                         if (storage != null)
372:                                         {
373:                                                 storage.AddResults(new Integer(n).toString(), result);
374:                                                 storage.AddTraceStatus(Verdict.valueOf(Verdict.FAILED.toString()), size, skippedCount, faildCount, inconclusiveCount);
375:                                                 storage.StopElement();
376:                                         }
377:
378:                                         Exception e = (Exception) result.get(result.size() - 2);
379:                                         result.remove(result.size() - 2);
380:
381:                                         throw e;
382:                                 }
383:
384:                                 if (storage != null)
385:                                 {
386:                                         storage.AddResults(new Integer(n).toString(), result);
387:                                 }
388:                         }
389:
390:                         n++;
391:                         StopWatch.stop("store&filter");
392:                         StopWatch.set();
393:                 }
394:
395:                 if (storage != null)
396:                 {
397:                         Verdict worstVerdict = Verdict.PASSED;
398:                         if (faildCount > 0)
399:                         {
400:                                 worstVerdict = Verdict.FAILED;
401:                         } else if (inconclusiveCount > 0)
402:                         {
403:                                 worstVerdict = Verdict.INCONCLUSIVE;
404:                         }
405:
406:                         storage.AddTraceStatus(Verdict.valueOf(worstVerdict.toString()), size, skippedCount, faildCount, inconclusiveCount);
407:                         storage.StopElement();
408:                 }
409:
410:                 infoProcessingTraceFinished(className, mtd.getName().getName(), size, faildCount, inconclusiveCount, skippedCount);
411:         }
412:
413:         /**
414:          * interpret a test
415:          *
416:          * @param mtd
417:          * @param test
418:          * @return list of results or list of errors
419:          */
420:         protected List<Object> evaluateCallSequence(ANamedTraceDefinition mtd,
421:                         CallSequence test)
422:         {
423:                 List<Object> result;
424:                 try
425:                 {
426:                         interpreter.init(null); /* Initialize completely between every run... */
427:                         result = interpreter.runOneTrace(mtd, test, false);
428:                 } catch (Exception e)
429:                 {
430:                         result = new Vector<Object>();
431:                         result.add(e.getMessage());
432:                         result.add(e);
433:                         result.add(Verdict.ERROR);
434:
435:                 }
436:                 return result;
437:         }
438:
439:         protected void infoProcessingTraceFinished(String className, String name,
440:                         int size, int faildCount, int inconclusiveCount, int skippedCount)
441:         {
442:         }
443:
444:         private void infoProcessingClass(String className, Integer traceCount)
445:         {
446:                 preProcessingClass(className, traceCount);
447:         }
448:
449:         protected void preProcessingClass(String className, Integer traceCount)
450:         {
451:
452:         }
453:
454:         protected void infoProcessingTrace(String className, String traceName,
455:                         Integer testCount) throws IOException
456:         {
457:                 if (monitor != null)
458:                 {
459:                         monitor.progressStartTrace(className + "`" + traceName);
460:                         currentPct = 0;
461:                 }
462:
463:                 preProcessingTrace(className, traceName, testCount);
464:         }
465:
466:         protected void preProcessingTrace(String className, String traceName,
467:                         Integer testCount)
468:         {
469:
470:         }
471:
472:         protected void infoProcessingTest(String className, String traceName,
473:                         Integer testNumber, Integer total) throws IOException
474:         {
475:                 if (monitor != null)
476:                 {
477:                         Integer pct = new Double((double) testNumber / (double) total * 100).intValue();
478:                         if (currentPct + 10 <= pct)
479:                         {
480:                                 monitor.progress(pct);
481:                                 currentPct = pct;
482:                         }
483:
484:                 }
485:
486:                 if (DEBUG)
487:                 {
488:                         System.out.println(testNumber);
489:                 }
490:         }
491:
492:         protected void infoCompleted() throws IOException
493:         {
494:                 if (monitor != null)
495:                 {
496:                         monitor.progressCompleted();
497:                 }
498:                 infoPrintTraceStatus();
499:
500:                 infoPreCompleted();
501:         }
502:
503:         protected void infoPreCompleted()
504:         {
505:
506:         }
507:
508:         protected void infoTestFiltered(Integer number, Integer filteredBy,
509:                         CallSequence test)
510:         {
511:         }
512:
513:         protected void error(String message) throws IOException
514:         {
515:                 System.err.println(message);
516:                 if (this.monitor != null)
517:                 {
518:                         this.monitor.progressError(message);
519:                 }
520:         }
521:
522:         protected void typeError(String message)
523:         {
524:                 System.err.println(message);
525:         }
526:
527:         protected void parseError(String message)
528:         {
529:                 System.err.println(message);
530:         }
531:
532:         protected void typeCheckStarted()
533:         {
534:
535:         }
536:
537:         private void infoPrintTraceStatus()
538:         {
539:                 infoPrePrintTraceStatus();
540:         }
541:
542:         protected void infoPrePrintTraceStatus()
543:         {
544:
545:         }
546:
547:         public void setCoverageDir(File coverageDir)
548:         {
549:                 this.coverage = coverageDir;
550:         }
551:
552: }