Package: ClassListInterpreter

ClassListInterpreter

nameinstructionbranchcomplexitylinemethod
ClassListInterpreter()
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%
ClassListInterpreter(ClassList)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createInitialContext(IInterpreterAssistantFactory)
M: 10 C: 18
64%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
findName(LexNameToken, NameScope)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getProofObligations(IInterpreterAssistantFactory)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
initialize(RootContext, IInterpreterAssistantFactory, DBGPReader)
M: 45 C: 61
58%
M: 11 C: 7
39%
M: 7 C: 3
30%
M: 12 C: 17
59%
M: 0 C: 1
100%
makeVirtualBUS(ValueSet, RootContext)
M: 5 C: 10
67%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 4 C: 1
20%
M: 0 C: 1
100%
notLoaded()
M: 0 C: 20
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 6
100%
M: 0 C: 1
100%
setLoaded()
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
systemInit(ASystemClassDefinition, ResourceScheduler, DBGPReader, RootContext)
M: 47 C: 166
78%
M: 2 C: 16
89%
M: 2 C: 8
80%
M: 10 C: 39
80%
M: 0 C: 1
100%
systemInit(ResourceScheduler, DBGPReader, RootContext)
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.interpreter.util;
2:
3: import java.util.HashMap;
4: import java.util.HashSet;
5: import java.util.List;
6: import java.util.Set;
7: import java.util.Vector;
8:
9: import org.overture.ast.analysis.AnalysisException;
10: import org.overture.ast.definitions.ABusClassDefinition;
11: import org.overture.ast.definitions.ACpuClassDefinition;
12: import org.overture.ast.definitions.ASystemClassDefinition;
13: import org.overture.ast.definitions.PDefinition;
14: import org.overture.ast.definitions.SClassDefinition;
15: import org.overture.ast.factory.AstFactoryTC;
16: import org.overture.ast.intf.lex.ILexNameToken;
17: import org.overture.ast.lex.LexLocation;
18: import org.overture.ast.lex.LexNameToken;
19: import org.overture.ast.typechecker.NameScope;
20: import org.overture.ast.types.AClassType;
21: import org.overture.ast.types.PType;
22: import org.overture.ast.util.definitions.ClassList;
23: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
24: import org.overture.interpreter.debug.DBGPReader;
25: import org.overture.interpreter.messages.Console;
26: import org.overture.interpreter.messages.rtlog.RTOperationMessage;
27: import org.overture.interpreter.runtime.ClassInterpreter;
28: import org.overture.interpreter.runtime.ContextException;
29: import org.overture.interpreter.runtime.RootContext;
30: import org.overture.interpreter.runtime.RuntimeValidator;
31: import org.overture.interpreter.runtime.StateContext;
32: import org.overture.interpreter.runtime.state.ASystemClassDefinitionRuntime;
33: import org.overture.interpreter.scheduler.ResourceScheduler;
34: import org.overture.interpreter.values.BUSValue;
35: import org.overture.interpreter.values.CPUValue;
36: import org.overture.interpreter.values.ObjectValue;
37: import org.overture.interpreter.values.QuoteValue;
38: import org.overture.interpreter.values.RealValue;
39: import org.overture.interpreter.values.TransactionValue;
40: import org.overture.interpreter.values.UpdatableValue;
41: import org.overture.interpreter.values.ValueList;
42: import org.overture.interpreter.values.ValueSet;
43: import org.overture.parser.lex.LexException;
44: import org.overture.parser.syntax.ParserException;
45: import org.overture.pog.obligation.ProofObligationList;
46: import org.overture.pog.pub.ProofObligationGenerator;
47:
48: public class ClassListInterpreter extends ClassList
49: {
50:
51:         /**
52:          *
53:          */
54:         private static final long serialVersionUID = -6070028869925906992L;
55:
56:         public ClassListInterpreter(ClassList classList)
57:         {
58:                 super();
59:                 addAll(classList);
60:         }
61:
62:         public ClassListInterpreter()
63:         {
64:                 super();
65:         }
66:
67:         public void systemInit(ResourceScheduler scheduler, DBGPReader dbgp,
68:                         RootContext initialContext)
69:         {
70:                 ASystemClassDefinition systemClass = null;
71:
72:•                for (SClassDefinition cdef : this)
73:                 {
74:•                        if (cdef instanceof ASystemClassDefinition)
75:                         {
76:                                 systemClass = (ASystemClassDefinition) cdef;
77:                                 systemInit(systemClass, scheduler, dbgp, initialContext);
78:                                 TransactionValue.commitAll();
79:                         }
80:                 }
81:         }
82:
83:         public RootContext createInitialContext(IInterpreterAssistantFactory af)
84:         {
85:                 StateContext globalContext = null;
86:
87:•                if (isEmpty())
88:                 {
89:                         globalContext = new StateContext(af, new LexLocation(), "global environment");
90:                 }
91:                 else
92:                 {
93:                         globalContext = new StateContext(af, this.get(0).getLocation(), "public static environment");
94:                 }
95:
96:                 return globalContext;
97:         }
98:
99:         public void initialize(RootContext ctxt, IInterpreterAssistantFactory af, DBGPReader dbgp)
100:         {
101:                 StateContext globalContext = (StateContext) ctxt;
102:                 globalContext.setThreadState(dbgp, CPUValue.vCPU);
103:
104:                 // Initialize all the functions/operations first because the values
105:                 // "statics" can call them.
106:
107:•                for (SClassDefinition cdef : this)
108:                 {
109:                         af.createSClassDefinitionAssistant().staticInit(cdef, globalContext);
110:                 }
111:
112:                 // Values can forward reference each other, which means that we don't
113:                 // know what order to initialize the classes in. So we have a crude
114:                 // retry mechanism, looking for "forward reference" like exceptions.
115:
116:                 ContextException failed = null;
117:                 int retries = 3; // Potentially not enough.
118:                 Set<ContextException> trouble = new HashSet<ContextException>();
119:
120:                 do
121:                 {
122:                         failed = null;
123:                         trouble.clear();
124:
125:•                        for (SClassDefinition cdef : this)
126:                         {
127:                                 try
128:                                 {
129:                                         af.createSClassDefinitionAssistant().staticValuesInit(cdef, globalContext);
130:                                 } catch (ContextException e)
131:                                 {
132:                                         trouble.add(e);
133:                                         // These two exceptions mean that a member could not be
134:                                         // found, which may be a forward reference, so we retry...
135:
136:•                                        if (e.number == 4034 || e.number == 6)
137:                                         {
138:                                                 failed = e;
139:                                         } else
140:                                         {
141:                                                 throw e;
142:                                         }
143:                                 }
144:                         }
145:•                } while (--retries > 0 && failed != null);
146:
147:•                if (!trouble.isEmpty())
148:                 {
149:                         ContextException toThrow = trouble.iterator().next();
150:
151:•                        for (ContextException e : trouble)
152:                         {
153:                                 Console.err.println(e);
154:
155:•                                if (e.number != 4034) // Not in scope err
156:                                 {
157:                                         toThrow = e;
158:                                 }
159:                         }
160:
161:                         throw toThrow;
162:                 }
163:         }
164:
165:         public ProofObligationList getProofObligations(
166:                         IInterpreterAssistantFactory assistantFactory)
167:                         throws AnalysisException
168:         {
169:                 // TODO: Check this method, where it is used.
170:                 ProofObligationList obligations = new ProofObligationList();
171:
172:•                for (SClassDefinition c : this)
173:                 {
174:
175:                         obligations.addAll(ProofObligationGenerator.generateProofObligations(c));
176:                 }
177:                 obligations.trivialCheck();
178:                 return obligations;
179:         }
180:
181:         public void setLoaded()
182:         {
183:•                for (SClassDefinition d : this)
184:                 {
185:                         d.setTypeChecked(true);
186:                 }
187:         }
188:
189:         public int notLoaded()
190:         {
191:                 int count = 0;
192:
193:•                for (SClassDefinition d : this)
194:                 {
195:•                        if (!d.getTypeChecked())
196:                         {
197:                                 count++;
198:                         }
199:                 }
200:
201:                 return count;
202:         }
203:
204:         public PDefinition findName(LexNameToken name, NameScope scope)
205:         {
206:                 SClassDefinition d = map.get(name.module);
207:
208:•                if (d != null)
209:                 {
210:                         PDefinition def = ClassInterpreter.getInstance().getAssistantFactory().createPDefinitionAssistant().findName(d, name, scope);
211:
212:•                        if (def != null)
213:                         {
214:                                 return def;
215:                         }
216:                 }
217:
218:                 return null;
219:         }
220:         
221:         public void systemInit(ASystemClassDefinition systemClass,
222:                         ResourceScheduler scheduler, DBGPReader dbgp,
223:                         RootContext initialContext)
224:         {
225:                 // systemContext = new StateContext(location, "RT system environment");
226:                 initialContext.setThreadState(dbgp, CPUValue.vCPU);
227:
228:                 try
229:                 {
230:                         // First go through the definitions, looking for CPUs to decl
231:                         // before we can deploy to them in the constructor. We have to
232:                         // predict the CPU numbers at this point.
233:
234:                         List<PDefinition> cpudefs = new Vector<PDefinition>();
235:                         ACpuClassDefinition instance = null;
236:
237:•                        for (PDefinition d : systemClass.getDefinitions())
238:                         {
239:                                 PType t = d.getType();
240:
241:•                                if (t instanceof AClassType)
242:                                 {
243:                                         AClassType ct = (AClassType) t;
244:
245:•                                        if (ct.getClassdef() instanceof ACpuClassDefinition)
246:                                         {
247:                                                 cpudefs.add(d);
248:                                                 instance = (ACpuClassDefinition) ct.getClassdef();
249:                                         }
250:                                 }
251:                         }
252:
253:                         // Run the constructor to do any deploys etc.
254:                         ASystemClassDefinitionRuntime.system = initialContext.assistantFactory.createSClassDefinitionAssistant().makeNewInstance(systemClass, null, new ValueList(), initialContext, new HashMap<ILexNameToken, ObjectValue>(), false);
255:
256:                         // Bind system instances to runtime validator
257:                         RuntimeValidator.bindSystemVariables(systemClass, initialContext.assistantFactory);
258:
259:                         // Do CPUs first so that default BUSses can connect all CPUs.
260:
261:                         ValueSet cpus = new ValueSet();
262:
263:•                        for (PDefinition d : cpudefs)
264:                         {
265:                                 UpdatableValue v = (UpdatableValue) ASystemClassDefinitionRuntime.system.members.get(d.getName());
266:                                 CPUValue cpu = null;
267:
268:•                                if (v.isUndefined())
269:                                 {
270:                                         ValueList args = new ValueList();
271:
272:                                         args.add(new QuoteValue("FCFS")); // Default policy
273:                                         args.add(new RealValue(0)); // Default speed
274:
275:                                         cpu = (CPUValue) initialContext.assistantFactory.createACpuClassDefinitionAssistant().newInstance(instance, null, args, initialContext);
276:                                         v.set(systemClass.getLocation(), cpu, initialContext);
277:                                 } else
278:                                 {
279:                                         cpu = (CPUValue) v.deref();
280:                                 }
281:
282:                                 // RTLogger.log(new RTDeclareCPUMessage(cpu.resource.getNumber(), !v.isUndefined(),
283:                                 // systemClass.getName().getName(), d.getName().getName()));
284:
285:                                 // Set the name and scheduler for the CPU resource, and
286:                                 // associate the resource with the scheduler.
287:
288:                                 cpu.setup(scheduler, d.getName().getName());
289:                                 cpus.add(cpu);
290:                         }
291:
292:                         // We can create vBUS now that all the CPUs have been created
293:                         // This must be first, to ensure it's bus number 0.
294:
295:                         BUSValue.vBUS = makeVirtualBUS(cpus,initialContext);
296:                         BUSValue.vBUS.setup(scheduler, "vBUS");
297:
298:•                        for (PDefinition d : systemClass.getDefinitions())
299:                         {
300:                                 PType t = d.getType();
301:
302:•                                if (t instanceof AClassType)
303:                                 {
304:                                         AClassType ct = (AClassType) t;
305:
306:•                                        if (ct.getClassdef() instanceof ABusClassDefinition)
307:                                         {
308:                                                 UpdatableValue v = (UpdatableValue) ASystemClassDefinitionRuntime.system.members.get(d.getName());
309:                                                 BUSValue bus = null;
310:
311:•                                                if (!v.isUndefined())
312:                                                 {
313:                                                         bus = (BUSValue) v.deref();
314:
315:                                                         // Set the name and scheduler for the BUS resource, and
316:                                                         // associate the resource with the scheduler.
317:
318:                                                         bus.setup(scheduler, d.getName().getName());
319:                                                 }
320:                                         }
321:                                 }
322:                         }
323:
324:                         // For efficiency, we create a 2D array of CPU-to-CPU bus links
325:                         BUSValue.createMap(initialContext, cpus);
326:
327:                         // Disable the system construction - all objects have not been created and deployed.
328:                         RTOperationMessage.inSystemConstruction = false;
329:                 } catch (ContextException e)
330:                 {
331:                         throw e;
332:                         // FIXME:this exception should be thrown
333:                         // } catch (ValueException e)
334:                         // {
335:                         // throw new ContextException(e, systemClass.getLocation());
336:                 } catch (Exception e)
337:                 {
338:                         throw new ContextException(4135, "Cannot instantiate a system class", systemClass.getLocation(), initialContext);
339:                 }
340:
341:         }
342:         public BUSValue makeVirtualBUS(ValueSet cpus,RootContext initialContext)
343:         {
344:                 try
345:                 {
346:                         return new BUSValue((AClassType) AstFactoryTC.newABusClassDefinition(initialContext.assistantFactory).getType(), cpus);
347:                 } catch (ParserException e)
348:                 {
349:
350:                 } catch (LexException e)
351:                 {
352:
353:                 }
354:                 return null;
355:         }
356: }