Package: TraceExpander

TraceExpander

nameinstructionbranchcomplexitylinemethod
TraceExpander(IInterpreterAssistantFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
caseAApplyExpressionTraceCoreDefinition(AApplyExpressionTraceCoreDefinition, Context)
M: 26 C: 144
85%
M: 2 C: 14
88%
M: 2 C: 7
78%
M: 7 C: 35
83%
M: 0 C: 1
100%
caseABracketedExpressionTraceCoreDefinition(ABracketedExpressionTraceCoreDefinition, Context)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAConcurrentExpressionTraceCoreDefinition(AConcurrentExpressionTraceCoreDefinition, Context)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAInstanceTraceDefinition(AInstanceTraceDefinition, Context)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseALetBeStBindingTraceDefinition(ALetBeStBindingTraceDefinition, Context)
M: 32 C: 145
82%
M: 7 C: 15
68%
M: 5 C: 7
58%
M: 8 C: 28
78%
M: 0 C: 1
100%
caseALetDefBindingTraceDefinition(ALetDefBindingTraceDefinition, Context)
M: 0 C: 48
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
caseARepeatTraceDefinition(ARepeatTraceDefinition, Context)
M: 0 C: 34
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseATraceDefinitionTerm(ATraceDefinitionTerm, Context)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createNewReturnValue(INode, Context)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createNewReturnValue(Object, Context)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
defaultPTraceCoreDefinition(PTraceCoreDefinition, Context)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.interpreter.traces;
2:
3: import java.util.List;
4: import java.util.Vector;
5:
6: import org.overture.ast.analysis.AnalysisException;
7: import org.overture.ast.analysis.QuestionAnswerAdaptor;
8: import org.overture.ast.definitions.PDefinition;
9: import org.overture.ast.definitions.traces.AApplyExpressionTraceCoreDefinition;
10: import org.overture.ast.definitions.traces.ABracketedExpressionTraceCoreDefinition;
11: import org.overture.ast.definitions.traces.AConcurrentExpressionTraceCoreDefinition;
12: import org.overture.ast.definitions.traces.AInstanceTraceDefinition;
13: import org.overture.ast.definitions.traces.ALetBeStBindingTraceDefinition;
14: import org.overture.ast.definitions.traces.ALetDefBindingTraceDefinition;
15: import org.overture.ast.definitions.traces.ARepeatTraceDefinition;
16: import org.overture.ast.definitions.traces.ATraceDefinitionTerm;
17: import org.overture.ast.definitions.traces.PTraceCoreDefinition;
18: import org.overture.ast.definitions.traces.PTraceDefinition;
19: import org.overture.ast.expressions.PExp;
20: import org.overture.ast.factory.AstFactory;
21: import org.overture.ast.lex.LexIdentifierToken;
22: import org.overture.ast.node.INode;
23: import org.overture.ast.patterns.PMultipleBind;
24: import org.overture.ast.patterns.PPattern;
25: import org.overture.ast.statements.ACallObjectStm;
26: import org.overture.ast.statements.ACallStm;
27: import org.overture.ast.statements.PStm;
28: import org.overture.config.Settings;
29: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
30: import org.overture.interpreter.runtime.Context;
31: import org.overture.interpreter.runtime.ContextException;
32: import org.overture.interpreter.runtime.ValueException;
33: import org.overture.interpreter.runtime.VdmRuntime;
34: import org.overture.interpreter.values.NameValuePair;
35: import org.overture.interpreter.values.NameValuePairList;
36: import org.overture.interpreter.values.ObjectValue;
37: import org.overture.interpreter.values.Quantifier;
38: import org.overture.interpreter.values.QuantifierList;
39: import org.overture.interpreter.values.Value;
40: import org.overture.interpreter.values.ValueList;
41: import org.overture.parser.lex.LexException;
42: import org.overture.parser.lex.LexTokenReader;
43: import org.overture.parser.syntax.ExpressionReader;
44: import org.overture.parser.syntax.ParserException;
45:
46: /**
47: * This method expands the trace of a core element in the tree.
48: *
49: * @author pvj
50: */
51:•public class TraceExpander extends QuestionAnswerAdaptor<Context, TraceNode>
52: {
53:         protected IInterpreterAssistantFactory af;
54:
55:         public TraceExpander(IInterpreterAssistantFactory af)
56:         {
57:                 this.af = af;
58:         }
59:
60:         @Override
61:         public TraceNode caseAApplyExpressionTraceCoreDefinition(
62:                         AApplyExpressionTraceCoreDefinition core, Context ctxt)
63:                         throws AnalysisException
64:         {
65:                 // return AApplyExpressionTraceCoreDefinitionAssistantInterpreter.expand(core, ctxt);
66:                 List<PExp> newargs = new Vector<PExp>();
67:                 List<PExp> args = null;
68:
69:•                if (core.getCallStatement() instanceof ACallStm)
70:                 {
71:                         ACallStm stmt = (ACallStm) core.getCallStatement();
72:                         args = stmt.getArgs();
73:                 } else
74:                 {
75:                         ACallObjectStm stmt = (ACallObjectStm) core.getCallStatement();
76:                         args = stmt.getArgs();
77:                 }
78:
79:•                for (PExp arg : args)
80:                 {
81:                         Value v = null;
82:                         try
83:                         {
84:                                 v = arg.apply(VdmRuntime.getExpressionEvaluator(), ctxt).deref();
85:                         } catch (AnalysisException e1)
86:                         {
87:                                 e1.printStackTrace();
88:                         }
89:
90:•                        if (v instanceof ObjectValue)
91:                         {
92:                                 newargs.add(arg.clone());
93:                         } else
94:                         {
95:                                 String value = v.toString();
96:                                 
97:                                 // If the value includes mk_R() expressions, we may not be able to resolve the type,
98:                                 // either because the type R is not explicit (like mk_M`R) or because the type may
99:                                 // have been exported from its module as a "struct" and so mk_R(x) is not legal.
100:                                 // Note that a RecordValue's toString does not explicitly expand the module name.
101:                                 // So we exclude anything with mk_R expressions too...
102:                                 
103:•                                if (!value.matches("mk_token\\(.*\\)") && value.matches(".*mk_\\w+\\(.*"))
104:                                 {
105:                                         newargs.add(arg.clone()); // Give up!
106:                                 }
107:                                 else
108:                                 {
109:                                 LexTokenReader ltr = new LexTokenReader(value, Settings.dialect, arg.getLocation());
110:                                 ExpressionReader er = new ExpressionReader(ltr);
111:                                 er.setCurrentModule(core.getCurrentModule());
112:
113:                                 try
114:                                 {
115:                                         newargs.add(er.readExpression());
116:                                 } catch (ParserException e)
117:                                 {
118:                                         newargs.add(arg.clone()); // Give up!
119:                                 } catch (LexException e)
120:                                 {
121:                                         newargs.add(arg.clone()); // Give up!
122:                                 }
123:                                 }
124:                         }
125:                 }
126:
127:                 PStm newStatement = null;
128:
129:•                if (core.getCallStatement() instanceof ACallStm)
130:                 {
131:                         ACallStm stmt = (ACallStm) core.getCallStatement();
132:                         newStatement = AstFactory.newACallStm(stmt.getName().clone(), newargs);
133:                 } else
134:                 {
135:                         ACallObjectStm stmt = (ACallObjectStm) core.getCallStatement();
136:                         ACallObjectStm newCallStatement;
137:
138:•                        if (stmt.getClassname() != null)
139:                         {
140:                                 newCallStatement = AstFactory.newACallObjectStm(stmt.getDesignator().clone(), stmt.getClassname().clone(), newargs);
141:                         } else
142:                         {
143:                                 newCallStatement = AstFactory.newACallObjectStm(stmt.getDesignator().clone(), (LexIdentifierToken) stmt.getFieldname().clone(), newargs);
144:                         }
145:                         
146:•                        if(stmt.getField() != null)
147:                         {
148:                                 newCallStatement.setField(stmt.getField().clone());
149:                         }
150:                         
151:                         newStatement = newCallStatement;
152:                 }
153:
154:                 return new StatementTraceNode(newStatement);
155:         }
156:
157:         @Override
158:         public TraceNode caseABracketedExpressionTraceCoreDefinition(
159:                         ABracketedExpressionTraceCoreDefinition core, Context ctxt)
160:                         throws AnalysisException
161:         {
162:                 // return ABracketedExpressionTraceCoreDefinitionAssitantInterpreter.expand(core, ctxt);
163:                 SequenceTraceNode node = new SequenceTraceNode();
164:
165:•                for (ATraceDefinitionTerm term : core.getTerms())
166:                 {
167:                         // node.nodes.add(ATraceDefinitionTermAssistantInterpreter.expand(term, ctxt));
168:                         node.nodes.add(term.apply(THIS, ctxt));
169:                 }
170:
171:                 return node;
172:         }
173:
174:         @Override
175:         public TraceNode caseAConcurrentExpressionTraceCoreDefinition(
176:                         AConcurrentExpressionTraceCoreDefinition core, Context ctxt)
177:                         throws AnalysisException
178:         {
179:                 // return AConcurrentExpressionTraceCoreDefinitionAssistantInterpreter.expand(core, ctxt);
180:                 ConcurrentTraceNode node = new ConcurrentTraceNode();
181:
182:•                for (PTraceDefinition term : core.getDefs())
183:                 {
184:                         // node.nodes.add(PTraceDefinitionAssistantInterpreter.expand(term, ctxt));
185:                         node.nodes.add(term.apply(THIS, ctxt));
186:                 }
187:
188:                 return node;
189:         }
190:
191:         @Override
192:         public TraceNode caseATraceDefinitionTerm(ATraceDefinitionTerm node,
193:                         Context question) throws AnalysisException
194:         {
195:                 AlternativeTraceNode newNode = new AlternativeTraceNode();
196:
197:•                for (PTraceDefinition term : node.getList())
198:                 {
199:                         newNode.alternatives.add(term.apply(THIS, question));
200:                         // newNode.alternatives.add(PTraceDefinitionAssistantInterpreter.expand(term, ctxt));
201:                 }
202:
203:                 return newNode;
204:         }
205:
206:         @Override
207:         public TraceNode defaultPTraceCoreDefinition(PTraceCoreDefinition node,
208:                         Context question) throws AnalysisException
209:         {
210:•                assert false : "Should not happen";
211:                 return null;
212:         }
213:
214:         @Override
215:         public TraceNode createNewReturnValue(INode node, Context question)
216:                         throws AnalysisException
217:         {
218:                 // TODO Auto-generated method stub
219:                 return null;
220:         }
221:
222:         @Override
223:         public TraceNode createNewReturnValue(Object node, Context question)
224:                         throws AnalysisException
225:         {
226:                 // TODO Auto-generated method stub
227:                 return null;
228:         }
229:
230:         @Override
231:         public TraceNode caseAInstanceTraceDefinition(
232:                         AInstanceTraceDefinition term, Context ctxt)
233:                         throws AnalysisException
234:         {
235:•                assert false : "this one is not in Nicks tree";
236:                 return null;
237:         }
238:
239:         @Override
240:         public TraceNode caseALetBeStBindingTraceDefinition(
241:                         ALetBeStBindingTraceDefinition term, Context ctxt)
242:                         throws AnalysisException
243:         {
244:                 // return ALetBeStBindingTraceDefinitionAssistantInterpreter.expand(term, ctxt);
245:                 AlternativeTraceNode node = new AlternativeTraceNode();
246:
247:                 try
248:                 {
249:                         QuantifierList quantifiers = new QuantifierList();
250:
251:•                        for (PMultipleBind mb : term.getDef().getBindings())
252:                         {
253:                                 ValueList bvals = af.createPMultipleBindAssistant().getBindValues(mb, ctxt, true);        // NB permuted
254:
255:•                                for (PPattern p : mb.getPlist())
256:                                 {
257:                                         Quantifier q = new Quantifier(p, bvals);
258:                                         quantifiers.add(q);
259:                                 }
260:                         }
261:
262:                         quantifiers.init(ctxt, true);
263:
264:•                        if (quantifiers.finished()) // No entries at all
265:                         {
266:                                 node.alternatives.add(new StatementTraceNode(AstFactory.newASkipStm(term.getLocation())));
267:                                 return node;
268:                         }
269:
270:•                        while (quantifiers.hasNext())
271:                         {
272:                                 Context evalContext = new Context(af, term.getLocation(), "TRACE", ctxt);
273:                                 NameValuePairList nvpl = quantifiers.next();
274:                                 boolean matches = true;
275:
276:•                                for (NameValuePair nvp : nvpl)
277:                                 {
278:                                         Value v = evalContext.get(nvp.name);
279:
280:•                                        if (v == null)
281:                                         {
282:                                                 evalContext.put(nvp.name, nvp.value);
283:                                         } else
284:                                         {
285:•                                                if (!v.equals(nvp.value))
286:                                                 {
287:                                                         matches = false;
288:                                                         break; // This quantifier set does not match
289:                                                 }
290:                                         }
291:                                 }
292:
293:•                                if (matches
294:•                                                && (term.getStexp() == null || term.getStexp().apply(VdmRuntime.getExpressionEvaluator(), evalContext).boolValue(ctxt)))
295:                                 {
296:                                         TraceNode exp = term.getBody().apply(THIS, evalContext);
297:                                         exp.addVariables(new TraceVariableList(evalContext, af.createPDefinitionAssistant().getDefinitions(term.getDef())));
298:                                         node.alternatives.add(exp);
299:                                 }
300:                         }
301:                 } catch (AnalysisException e)
302:                 {
303:•                        if (e instanceof ValueException)
304:                         {
305:                                 throw new ContextException((ValueException) e, term.getLocation());
306:                         }
307:                 }
308:
309:                 return node;
310:         }
311:
312:         @Override
313:         public TraceNode caseALetDefBindingTraceDefinition(
314:                         ALetDefBindingTraceDefinition term, Context ctxt)
315:                         throws AnalysisException
316:         {
317:                 // return ALetDefBindingTraceDefinitionAssistantInterpreter.expand(term, ctxt);
318:                 Context evalContext = new Context(af, term.getLocation(), "TRACE", ctxt);
319:
320:•                for (PDefinition d : term.getLocalDefs())
321:                 {
322:                         evalContext.putList(af.createPDefinitionAssistant().getNamedValues(d, evalContext));
323:                 }
324:
325:                 TraceNode node = term.getBody().apply(THIS, evalContext);
326:                 node.addVariables(new TraceVariableList(evalContext, term.getLocalDefs()));
327:                 return node;
328:         }
329:
330:         @Override
331:         public TraceNode caseARepeatTraceDefinition(ARepeatTraceDefinition term,
332:                         Context ctxt) throws AnalysisException
333:         {
334:                 TraceNode body = term.getCore().apply(af.getTraceExpander(),ctxt);
335:                 //expand(term.getCore(), ctxt);
336:
337:•                if (term.getFrom() == 1 && term.getTo() == 1)
338:                 {
339:                         return body;
340:                 } else
341:                 {
342:                         return new RepeatTraceNode(body, term.getFrom(), term.getTo());
343:                 }
344:         }
345:         
346: //                public TraceNode expand(PTraceCoreDefinition core, Context ctxt)
347: //                {
348: //                        try
349: //                        {
350: //                                return core.apply(af.getTraceExpander(), ctxt);
351: //                        } catch (AnalysisException e)
352: //                        {
353: //                                return null;
354: //                        }
355: //                }
356:
357: }