/
SystemArchitectureAnalysis.java
261 lines (220 loc) · 7.41 KB
/
SystemArchitectureAnalysis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.overture.codegen.vdm2c.distribution;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newAssignment;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newIdentifier;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newInternalMethod;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.overture.codegen.ir.IRStatus;
import org.overture.codegen.ir.PIR;
import org.overture.codegen.ir.SExpIR;
import org.overture.codegen.ir.SStmIR;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
import org.overture.codegen.ir.declarations.AFieldDeclIR;
import org.overture.codegen.ir.declarations.AMethodDeclIR;
import org.overture.codegen.ir.declarations.ASystemClassDeclIR;
import org.overture.codegen.ir.expressions.AEnumSetExpIR;
import org.overture.codegen.ir.expressions.ANewExpIR;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.statements.ACallObjectStmIR;
import org.overture.codegen.ir.statements.AReturnStmIR;
import org.overture.codegen.ir.types.AClassTypeIR;
import org.overture.codegen.ir.types.AVoidTypeIR;
public class SystemArchitectureAnalysis
{
// Maps that are produced by the distribution analysis
public static HashMap<String, Set<SExpIR>> distributionMap = new HashMap<String, Set<SExpIR>>();
public static Map<String, AEnumSetExpIR> connectionMap = new HashMap<String, AEnumSetExpIR>();
public static LinkedList<AFieldDeclIR> systemDeployedObjects = new LinkedList<AFieldDeclIR>();
public static Map<String, LinkedList<Boolean>> DM = new HashMap<String, LinkedList<Boolean>>();
// String versions of the maps
public static LinkedList<String> systemDeployedObjectsStr = new LinkedList<String>();
public static Map<String, Set<String>> connectionMapStr = new HashMap<String, Set<String>>();
public static HashMap<String, Set<String>> distributionMapStr = new HashMap<String, Set<String>>();
public static Set<String> systemClasses = new HashSet<String>();
public static String systemName;
/** Function used to initialise the distribution map */
public void initDistributionMap(String cpuName)
{
HashSet<SExpIR> set = new HashSet<SExpIR>();
distributionMap.put(cpuName, set);
}
public void analyseSystem(List<IRStatus<PIR>> statuses)
{
IRStatus<PIR> status = null;
for (IRStatus<PIR> irStatus : statuses)
{
if (irStatus.getIrNode() instanceof ASystemClassDeclIR)
{
status = irStatus;
}
}
// If there is a system class
if (status != null)
{
ASystemClassDeclIR systemDef = (ASystemClassDeclIR) status.getIrNode().clone();
systemName = systemDef.getName();
// Analyse CPUs, and how they are connected
for (AFieldDeclIR f : systemDef.getFields())
{
if (f.getType() instanceof AClassTypeIR)
{
AClassTypeIR type = (AClassTypeIR) f.getType();
if (type.getName().equals("CPU"))
{
// Initialise the distribution Map
initDistributionMap(f.getName());
} else if (type.getName().equals("BUS"))
{
// Initialise the connection Map
if (f.getInitial() instanceof ANewExpIR)
{
ANewExpIR init = (ANewExpIR) f.getInitial();
SExpIR args = init.getArgs().get(2);
if (args instanceof AEnumSetExpIR)
{
AEnumSetExpIR cpu_args = (AEnumSetExpIR) args;
connectionMap.put(f.getName(), cpu_args);
}
}
} else
{
systemDeployedObjects.add(f);
}
}
}
// Analyse the deployment of objects
for (AMethodDeclIR m : systemDef.getMethods())
{
if (m.getIsConstructor())
{
if (m.getBody() instanceof ABlockStmIR)
{
ABlockStmIR body = (ABlockStmIR) m.getBody();
for (SStmIR s : body.getStatements())
{
if (s instanceof ACallObjectStmIR)
{
ACallObjectStmIR o = (ACallObjectStmIR) s;
String cpuName = o.getDesignator().toString();
if (!distributionMap.keySet().contains(cpuName))
continue;
Set<SExpIR> depSet = distributionMap.get(cpuName);
for (SExpIR arg : o.getArgs())
{
depSet.add(arg);
distributionMap.put(cpuName, depSet);
}
}
}
}
}
}
}
// Based on analysis produce the maps
generateDM();
generateMapStrVer();
}
public void generateDM()
{
for (String cpu : distributionMap.keySet())
{ // pr. cpu
LinkedList<Boolean> li = new LinkedList<Boolean>();
li.add(true); // first value is always true
for (AFieldDeclIR obj : systemDeployedObjects)
{ // pr. deployed object
Set<SExpIR> deployedObjs = distributionMap.get(cpu);
Boolean val = false;
for (SExpIR dep : deployedObjs)
if (dep.toString().equals(obj.getName().toString()))
val = true;
li.add(val);
}
DM.put(cpu, li);
}
}
public void generateMapStrVer()
{
for (AFieldDeclIR field : systemDeployedObjects)
{
systemDeployedObjectsStr.add(field.getName().toString());
}
for (String bus : connectionMap.keySet())
{
AEnumSetExpIR cpus = connectionMap.get(bus);
Set<String> cpuList = new HashSet<String>();
for (SExpIR c : cpus.getMembers())
{
cpuList.add(c.toString());
}
connectionMapStr.put(bus, cpuList);
// systemDeployedObjectsStr.add(field.getName().toString());
}
for (String cpu : distributionMap.keySet())
{
Set<String> objList = new HashSet<String>();
for (SExpIR obj : distributionMap.get(cpu))
{
objList.add(obj.toString());
}
distributionMapStr.put(cpu, objList);
}
}
/** This function generates an initialization function for a specific CPU **/
static void createInitMethod(ADefaultClassDeclIR node)
throws AnalysisException
{
ABlockStmIR body = new ABlockStmIR();
String cpuName = node.getTag().toString();
for (AFieldDeclIR field : node.getFields())
{
if (field.getFinal() && field.getInitial() != null)
{
body.getStatements().add(newAssignment(newIdentifier(field.getName(), null), field.getInitial()));
}
}
// if(node.getMethods().get(0).getBody().clone() instanceof ABlockStmIR)
ABlockStmIR consMethodBody = (ABlockStmIR) node.getMethods().get(0).getBody().clone();
for (SStmIR s : consMethodBody.getStatements())
{
if (s instanceof ACallObjectStmIR)
{
ACallObjectStmIR o = (ACallObjectStmIR) s;
String objName = o.getDesignator().toString();
if (distributionMapStr.get(cpuName).contains(objName))
body.getStatements().add(s.clone());
}
}
// In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());
// Emit init function even if no value fields are present. Simplifies FMU export.
AMethodDeclIR method = newInternalMethod(node.getTag().toString()
+ "_init", body, new AVoidTypeIR(), false);
method.setAccess("public");
node.getMethods().add(method);
}
/** This function generates an initialization function for each CPU **/
public static void addCpuInitMethod(List<IRStatus<PIR>> statuses)
{
for (IRStatus<PIR> irStatus : statuses)
{
if (irStatus.getIrNode() instanceof ADefaultClassDeclIR)
{
ADefaultClassDeclIR cl = (ADefaultClassDeclIR) irStatus.getIrNode();
if (cl.getTag() != null)
try
{
createInitMethod(cl);
} catch (AnalysisException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}