Package: AllValuesCollector

AllValuesCollector

nameinstructionbranchcomplexitylinemethod
AllValuesCollector(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%
caseABooleanBasicType(ABooleanBasicType, Context)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseAInMapMapType(AInMapMapType, Context)
M: 0 C: 35
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
caseANamedInvariantType(ANamedInvariantType, Context)
M: 3 C: 44
94%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 2 C: 11
85%
M: 0 C: 1
100%
caseANatOneNumericBasicType(ANatOneNumericBasicType, Context)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
caseAOptionalType(AOptionalType, Context)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
caseAParameterType(AParameterType, Context)
M: 53 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
caseAProductType(AProductType, Context)
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%
caseAQuoteType(AQuoteType, Context)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
caseARecordInvariantType(ARecordInvariantType, Context)
M: 61 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
caseAUnionType(AUnionType, 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: 14 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%
defaultSBasicType(SBasicType, Context)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
defaultSInvariantType(SInvariantType, Context)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
defaultSMapType(SMapType, Context)
M: 0 C: 83
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
defaultSNumericBasicType(SNumericBasicType, Context)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
defaultSSetType(SSetType, Context)
M: 45 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
generateNumbers(int, int, Context)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getAllValues(List, Context)
M: 0 C: 88
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 19
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.interpreter.utilities.type;
2:
3: import java.util.Collections;
4: import java.util.List;
5: import java.util.Vector;
6:
7: import org.overture.ast.analysis.AnalysisException;
8: import org.overture.ast.analysis.QuestionAnswerAdaptor;
9: import org.overture.ast.assistant.pattern.PTypeList;
10: import org.overture.ast.factory.AstFactory;
11: import org.overture.ast.lex.LexNameToken;
12: import org.overture.ast.node.INode;
13: import org.overture.ast.patterns.PPattern;
14: import org.overture.ast.types.ABooleanBasicType;
15: import org.overture.ast.types.AFieldField;
16: import org.overture.ast.types.AInMapMapType;
17: import org.overture.ast.types.ANamedInvariantType;
18: import org.overture.ast.types.ANatOneNumericBasicType;
19: import org.overture.ast.types.AOptionalType;
20: import org.overture.ast.types.AParameterType;
21: import org.overture.ast.types.AProductType;
22: import org.overture.ast.types.AQuoteType;
23: import org.overture.ast.types.ARecordInvariantType;
24: import org.overture.ast.types.SSetType;
25: import org.overture.ast.types.AUnionType;
26: import org.overture.ast.types.PType;
27: import org.overture.ast.types.SBasicType;
28: import org.overture.ast.types.SInvariantType;
29: import org.overture.ast.types.SMapType;
30: import org.overture.ast.types.SNumericBasicType;
31: import org.overture.config.Settings;
32: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
33: import org.overture.interpreter.runtime.Context;
34: import org.overture.interpreter.runtime.ValueException;
35: import org.overture.interpreter.values.BooleanValue;
36: import org.overture.interpreter.values.InvariantValue;
37: import org.overture.interpreter.values.MapValue;
38: import org.overture.interpreter.values.NameValuePair;
39: import org.overture.interpreter.values.NameValuePairList;
40: import org.overture.interpreter.values.NilValue;
41: import org.overture.interpreter.values.NumericValue;
42: import org.overture.interpreter.values.ParameterValue;
43: import org.overture.interpreter.values.Quantifier;
44: import org.overture.interpreter.values.QuantifierList;
45: import org.overture.interpreter.values.QuoteValue;
46: import org.overture.interpreter.values.RecordValue;
47: import org.overture.interpreter.values.SetValue;
48: import org.overture.interpreter.values.TupleValue;
49: import org.overture.interpreter.values.Value;
50: import org.overture.interpreter.values.ValueList;
51: import org.overture.interpreter.values.ValueMap;
52: import org.overture.interpreter.values.ValueSet;
53: import org.overture.parser.config.Properties;
54:
55: /**
56: * This class collects all the values exist in a type.
57: *
58: * @author gkanos
59: */
60: public class AllValuesCollector extends
61:                 QuestionAnswerAdaptor<Context, ValueList>
62: {
63:         protected IInterpreterAssistantFactory af;
64:
65:         public AllValuesCollector(IInterpreterAssistantFactory af)
66:         {
67:                 this.af = af;
68:         }
69:
70:         @Override
71:         public ValueList caseABooleanBasicType(ABooleanBasicType type, Context ctxt)
72:                         throws AnalysisException
73:         {
74:                 ValueList v = new ValueList();
75:                 v.add(new BooleanValue(true));
76:                 v.add(new BooleanValue(false));
77:                 return v;
78:         }
79:
80:         @Override
81:         public ValueList defaultSNumericBasicType(SNumericBasicType node,
82:                         Context question) throws AnalysisException
83:         {
84:•                if (Properties.numeric_type_bind_generation)
85:                 {
86:                         return generateNumbers(Properties.minint, Properties.maxint, question);
87:                 }
88:                 return super.defaultSNumericBasicType(node, question);
89:         }
90:
91:         @Override
92:         public ValueList caseANatOneNumericBasicType(ANatOneNumericBasicType node,
93:                         Context question) throws AnalysisException
94:         {
95:•                if (Properties.numeric_type_bind_generation)
96:                 {
97:                         int min = Properties.minint;
98:•                        if (min < 1)
99:                         {
100:                                 min = 1;
101:                         }
102:                         return generateNumbers(min, Properties.maxint, question);
103:                 }
104:                 return super.caseANatOneNumericBasicType(node, question);
105:         }
106:
107:         /**
108:          * Generator method for numeric type bindings.
109:          *
110:          * @param min
111:          * the minimum number
112:          * @param max
113:          * the maximum number
114:          * @param ctxt
115:          * current context
116:          * @return the newly generated list of values between min and max, including.
117:          * @throws ValueException
118:          */
119:         protected ValueList generateNumbers(int min, int max, Context ctxt)
120:                         throws ValueException
121:         {
122:                 ValueList list = new ValueList();
123:•                for (int i = min; i < max + 1; i++)
124:                 {
125:                         list.add(NumericValue.valueOf(i, ctxt));
126:                 }
127:
128:                 return list;
129:         }
130:
131:         @Override
132:         public ValueList defaultSBasicType(SBasicType type, Context ctxt)
133:                         throws AnalysisException
134:         {
135:                 throw new ValueException(4, "Cannot get bind values for type " + type, ctxt);
136:         }
137:
138:         @Override
139:         public ValueList caseANamedInvariantType(ANamedInvariantType type,
140:                         Context ctxt) throws AnalysisException
141:         {
142:                 ValueList raw = type.getType().apply(THIS, ctxt);
143:                 boolean checks = Settings.invchecks;
144:                 Settings.invchecks = true;
145:
146:                 ValueList result = new ValueList();
147:•                for (Value v : raw)
148:                 {
149:                         try
150:                         {
151:                                 result.add(new InvariantValue(type, v, ctxt));
152:                         } catch (ValueException e)
153:                         {
154:                                 // Raw value not in type because of invariant
155:                         }
156:                 }
157:
158:•                if (type.getOrdDef() != null)
159:                 {
160:                         Collections.sort(result);
161:                 }
162:
163:                 Settings.invchecks = checks;
164:                 return result;
165:         }
166:
167:         @Override
168:         public ValueList caseARecordInvariantType(ARecordInvariantType type,
169:                         Context ctxt) throws AnalysisException
170:         {
171:                 List<PType> types = new Vector<PType>();
172:
173:•                for (AFieldField f : type.getFields())
174:                 {
175:                         types.add(f.getType());
176:                 }
177:
178:                 ValueList results = new ValueList();
179:
180:•                for (Value v : getAllValues(types, ctxt))
181:                 {
182:                         try
183:                         {
184:                                 TupleValue tuple = (TupleValue) v;
185:                                 results.add(new RecordValue(type, tuple.values, ctxt));
186:                         } catch (ValueException e)
187:                         {
188:                                 // Value does not match invariant, so ignore it
189:                         }
190:                 }
191:
192:•                if (type.getOrdDef() != null)
193:                 {
194:                         Collections.sort(results);
195:                 }
196:
197:                 return results;
198:         }
199:
200:         @Override
201:         public ValueList defaultSInvariantType(SInvariantType type, Context ctxt)
202:                         throws AnalysisException
203:         {
204:                 throw new ValueException(4, "Cannot get bind values for type " + type, ctxt);
205:         }
206:
207:         @Override
208:         public ValueList caseAInMapMapType(AInMapMapType type, Context ctxt)
209:                         throws AnalysisException
210:         {
211:                 // TODO:Here we have a strange behavior from transforming this call to type.apply(THIS,ctxt)
212:                 ValueList maps = THIS.defaultSMapType(type, ctxt);// ctxt.assistantFactory.createSMapTypeAssistant().getAllValues(type,
213:                                                                                                                         // ctxt);
214:                 ValueList result = new ValueList();
215:
216:•                for (Value map : maps)
217:                 {
218:                         MapValue vm = (MapValue) map;
219:
220:•                        if (vm.values.isInjective())
221:                         {
222:                                 result.add(vm);
223:                         }
224:                 }
225:
226:                 return result;
227:         }
228:
229:         @Override
230:         public ValueList caseAOptionalType(AOptionalType type, Context ctxt)
231:                         throws AnalysisException
232:         {
233:                 ValueList list = type.getType().apply(THIS, ctxt);
234:                 list.add(new NilValue());
235:                 return list;
236:         }
237:
238:         @Override
239:         public ValueList caseAProductType(AProductType type, Context ctxt)
240:                         throws AnalysisException
241:         {
242:                 return getAllValues(type.getTypes(), ctxt);
243:
244:         }
245:
246:         @Override
247:         public ValueList defaultSMapType(SMapType type, Context ctxt)
248:                         throws AnalysisException
249:         {
250:                 PTypeList tuple = new PTypeList();
251:                 tuple.add(type.getFrom());
252:                 tuple.add(type.getTo());
253:
254:                 ValueList results = new ValueList();
255:                 ValueList tuples = getAllValues(tuple, ctxt);
256:                 ValueSet set = new ValueSet();
257:                 set.addAll(tuples);
258:                 List<ValueSet> psets = set.powerSet();
259:
260:•                for (ValueSet map : psets)
261:                 {
262:                         ValueMap result = new ValueMap();
263:
264:•                        for (Value v : map)
265:                         {
266:                                 TupleValue tv = (TupleValue) v;
267:                                 result.put(tv.values.get(0), tv.values.get(1));
268:                         }
269:
270:                         results.add(new MapValue(result));
271:                 }
272:
273:                 return results;
274:         }
275:
276:         @Override
277:         public ValueList caseAQuoteType(AQuoteType type, Context ctxt)
278:                         throws AnalysisException
279:         {
280:                 ValueList v = new ValueList();
281:                 v.add(new QuoteValue(type.getValue().getValue()));
282:                 return v;
283:         }
284:
285:         @Override
286:         public ValueList defaultSSetType(SSetType type, Context ctxt)
287:                         throws AnalysisException
288:         {
289:                 ValueList list = type.getSetof().apply(THIS, ctxt);
290:                 ValueSet set = new ValueSet(list.size());
291:                 set.addAll(list);
292:                 List<ValueSet> psets = set.powerSet();
293:                 list.clear();
294:
295:•                for (ValueSet v : psets)
296:                 {
297:                         list.add(new SetValue(v));
298:                 }
299:
300:                 Collections.sort(list);
301:                 return list;
302:         }
303:
304:         @Override
305:         public ValueList caseAUnionType(AUnionType type, Context ctxt)
306:                         throws AnalysisException
307:         {
308:                 ValueList v = new ValueList();
309:
310:•                for (PType utype : type.getTypes())
311:                 {
312:                         v.addAll(utype.apply(THIS, ctxt));
313:                 }
314:
315:                 return v;
316:         }
317:
318:         @Override
319:         public ValueList caseAParameterType(AParameterType type, Context ctxt)
320:                         throws AnalysisException
321:         {
322:                 Value t = ctxt.lookup(type.getName());
323:
324:•                if (t == null)
325:                 {
326:                         throw new ValueException(4008, "No such type parameter @"
327:                                         + type.getName() + " in scope", ctxt);
328:•                } else if (t instanceof ParameterValue)
329:                 {
330:                         ParameterValue tv = (ParameterValue) t;
331:                         return tv.type.apply(THIS, ctxt);
332:                 }
333:
334:                 throw new ValueException(4009, "Type parameter/local variable name clash, @"
335:                                 + type.getName(), ctxt);
336:         }
337:
338:         @Override
339:         public ValueList createNewReturnValue(INode node, Context question)
340:                         throws AnalysisException
341:         {
342:                 throw new ValueException(4, "Cannot get bind values for type " + node, question);
343:         }
344:
345:         @Override
346:         public ValueList createNewReturnValue(Object node, Context question)
347:                         throws AnalysisException
348:         {
349:                 return null;
350:         }
351:         
352:         public ValueList getAllValues(List<PType> linkedList, Context ctxt)
353:                         throws AnalysisException
354:         {
355:                 QuantifierList quantifiers = new QuantifierList();
356:                 int n = 0;
357:
358:•                for (PType t : linkedList)
359:                 {
360:                         LexNameToken name = new LexNameToken("#", String.valueOf(n), t.getLocation());
361:                         PPattern p = AstFactory.newAIdentifierPattern(name);
362:                         Quantifier q = new Quantifier(p, af.createPTypeAssistant().getAllValues(t, ctxt));
363:                         quantifiers.add(q);
364:                 }
365:
366:                 quantifiers.init(ctxt, true);
367:                 ValueList results = new ValueList();
368:
369:•                while (quantifiers.hasNext())
370:                 {
371:                         NameValuePairList nvpl = quantifiers.next();
372:                         ValueList list = new ValueList();
373:
374:•                        for (NameValuePair nvp : nvpl)
375:                         {
376:                                 list.add(nvp.value);
377:                         }
378:
379:                         results.add(new TupleValue(list));
380:                 }
381:
382:                 return results;
383:         }
384:
385: }