1 | package de.uka.ipd.sdq.pcm.transformations.builder.util; |
2 | |
3 | import java.util.Stack; |
4 | |
5 | import de.uka.ipd.sdq.pcm.repository.CollectionDataType; |
6 | import de.uka.ipd.sdq.pcm.repository.CompositeDataType; |
7 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
8 | import de.uka.ipd.sdq.pcm.repository.PrimitiveDataType; |
9 | import de.uka.ipd.sdq.pcm.repository.PrimitiveTypeEnum; |
10 | import de.uka.ipd.sdq.pcm.repository.util.RepositorySwitch; |
11 | |
12 | public class TypesCountingVisitor extends RepositorySwitch<String> { |
13 | |
14 | private PrimitiveTypeEnum type; |
15 | private Stack<String> prefixes = new Stack<String>(); |
16 | |
17 | public TypesCountingVisitor(Parameter p, PrimitiveTypeEnum type) { |
18 | this.type = type; |
19 | prefixes.push(p.getParameterName()); |
20 | } |
21 | |
22 | public TypesCountingVisitor(String name, PrimitiveTypeEnum type) { |
23 | this.type = type; |
24 | prefixes.push(name); |
25 | } |
26 | |
27 | @Override |
28 | public String caseCollectionDataType(CollectionDataType object) { |
29 | String innerStoEx = this.doSwitch(object.getInnerType_CollectionDataType()); |
30 | if (innerStoEx != null) { |
31 | if (innerStoEx.equals("1")){ |
32 | return getPrefix() + ".NUMBER_OF_ELEMENTS"; |
33 | } else { |
34 | //TODO: use sum instead for correct stochastic processing! |
35 | return getPrefix() + ".NUMBER_OF_ELEMENTS * ("+innerStoEx+")"; |
36 | } |
37 | } else |
38 | return null; |
39 | } |
40 | |
41 | private String getPrefix() { |
42 | String result = ""; |
43 | for (String s : prefixes) { |
44 | result += s +"."; |
45 | } |
46 | return result.substring(0, result.length()-1); |
47 | } |
48 | |
49 | @Override |
50 | public String caseCompositeDataType(CompositeDataType object) { |
51 | return super.caseCompositeDataType(object); |
52 | } |
53 | |
54 | @Override |
55 | public String casePrimitiveDataType(PrimitiveDataType object) { |
56 | if (object.getType() == this.type) |
57 | return "1"; |
58 | return super.casePrimitiveDataType(object); |
59 | } |
60 | } |