-
Notifications
You must be signed in to change notification settings - Fork 9
/
ATLModule.java
423 lines (355 loc) · 14 KB
/
ATLModule.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import java.util.Vector;
import java.util.Set;
import java.util.HashSet;
/******************************
* Copyright (c) 2003-2023 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
/* Module: ATL */
public class ATLModule
{ String name;
Vector elements; // MatchedRule
java.util.Map interp = null;
Vector attributes = new Vector(); // of Attribute
Vector operations = new Vector(); // of BehaviouralFeature
public ATLModule(String nme)
{ name = nme;
elements = new Vector();
}
public String getName()
{ return name; }
public void addElement(ModuleElement me)
{ elements.add(me); }
public void setElements(Vector elems)
{ elements = elems; }
public void addElements(Vector elems)
{ elements.addAll(elems); }
public MatchedRule getRuleByName(String nme)
{ for (int i = 0; i < elements.size(); i++)
{ ModuleElement me = (ModuleElement) elements.get(i);
if (me instanceof MatchedRule &&
me.getName().equals(nme))
{ return (MatchedRule) me; }
}
return null;
}
public void addAttribute(Attribute att)
{ attributes.add(att); }
public void addOperation(BehaviouralFeature bf)
{ operations.add(bf); }
public void setInterpretation(java.util.Map intp)
{ interp = intp; }
public String toString()
{ String res = "module " + name + ";\n";
res = res + "create OUT : T from IN : S;\n";
for (int i = 0; i < attributes.size(); i++)
{ Attribute att = (Attribute) attributes.get(i);
String attname = att.getName();
Entity ent = att.getEntity();
Type t = att.getType();
Expression e = att.getInitialExpression();
res = res + " helper ";
if (ent != null)
{ res = res + "context " + ent; }
res = res + " def : " + attname + " : " + t + " := " + e + ";\n";
}
for (int i = 0; i < operations.size(); i++)
{ BehaviouralFeature bf = (BehaviouralFeature) operations.get(i);
String attname = bf.getName();
Entity ent = bf.getEntity();
Type t = bf.getResultType();
Expression e = bf.getPost();
res = res + " helper ";
if (ent != null)
{ res = res + "context " + ent; }
res = res + " def : " + bf.getSignature() + " : " + t + "\n { " + e + " };\n\n";
}
for (int i = 0; i < elements.size(); i++)
{ ModuleElement me = (ModuleElement) elements.get(i);
res = res + me + "\n";
}
return res;
}
public int nops()
{ return operations.size(); }
public int nrules()
{ return elements.size(); }
public boolean typeCheck(Vector types, Vector entities)
{ Vector env = new Vector();
for (int j = 0; j < attributes.size(); j++)
{ Attribute att = (Attribute) attributes.get(j);
Vector context = new Vector();
Entity ent = att.getEntity();
if (ent != null)
{ context.add(ent); }
Expression ini = att.getInitialExpression();
ini.typeCheck(types,entities,context,env);
env.add(att);
}
for (int k = 0; k < operations.size(); k++)
{ BehaviouralFeature op = (BehaviouralFeature) operations.get(k);
op.typeCheck(types,entities);
}
for (int i = 0; i < elements.size(); i++)
{ ModuleElement me = (ModuleElement) elements.get(i);
if (me instanceof MatchedRule)
{ MatchedRule rule = (MatchedRule) me;
rule.typeCheck(types,entities);
}
}
return true;
}
public UseCase toUML(Vector types, Vector entities, Vector inits)
{ UseCase res = new UseCase(name,null);
res.addPostconditions(inits);
System.out.println("#Attributes = " + attributes.size());
System.out.println("#Operations = " + operations.size());
java.util.Map interp = getInterpretation();
for (int j = 0; j < attributes.size(); j++)
{ Attribute att = (Attribute) attributes.get(j);
Entity ent = att.getEntity();
if (ent != null)
{ Expression ini = att.getInitialExpression();
Expression resexp = new BasicExpression("result");
resexp.setType(att.getType());
resexp.setUmlKind(Expression.VARIABLE);
Expression post = new BinaryExpression("=", resexp, ini.replaceModuleReferences(res));
BehaviouralFeature bf =
new BehaviouralFeature(att.getName(), new Vector(), true, att.getType());
ent.addOperation(bf);
bf.setPost(post);
bf.setEntity(ent);
bf.setCached(true); // helper attributes act like cached operations in ATL
}
else
{ res.addAttribute(att);
System.out.println(">>> Added attribute " + att + " with initial expression " + att.getInitialExpression());
}
}
for (int k = 0; k < operations.size(); k++)
{ BehaviouralFeature op = (BehaviouralFeature) operations.get(k);
Entity oent = op.getEntity();
System.out.println(">>> operation " + op + " has entity " + oent);
if (oent == null)
{ res.addOperation(op); }
Expression pst = op.getPost();
if (pst != null)
{ Expression newpost = pst.replaceModuleReferences(res);
op.setPost(newpost);
}
Statement act = op.getActivity();
if (act != null)
{ Statement stat = act.replaceModuleReferences(res);
op.setActivity(stat);
}
}
for (int i = 0; i < elements.size(); i++)
{ ModuleElement me = (ModuleElement) elements.get(i);
if (me instanceof MatchedRule)
{ MatchedRule rule = (MatchedRule) me;
rule.typeCheck(types,entities);
if (rule.isLazy() || rule.isCalled())
{ rule.toOperation(types,entities,interp,res); }
else
{ Constraint con =
rule.toConstraint(types,entities,interp,res);
res.addPostcondition(con);
}
}
}
// stat.typeCheck(types,entities,contexts,newparms);
return res;
}
public java.util.Map getInterpretation()
{ java.util.Map res = new java.util.HashMap();
// InE -> OutE for the first input InE and first output OutE of all matched rules
for (int i = 0; i < elements.size(); i++)
{ if (elements.get(i) instanceof MatchedRule)
{ MatchedRule mr = (MatchedRule) elements.get(i);
OutPattern opatt = mr.outPattern;
InPattern inpatt = mr.inPattern;
if (inpatt != null && opatt != null)
{ String t1 = inpatt.firstType();
if (t1 != null)
{ Vector opes = opatt.elements;
if (opes.size() > 0)
{ OutPatternElement ope = (OutPatternElement) opes.get(0);
Entity tt = ope.getEntity();
res.put(t1,tt);
}
}
}
}
}
interp = res;
return res;
}
public Vector dataAnalysis()
{ // returns list of rules that must be split
Vector res = new Vector();
for (int i = 0; i < elements.size(); i++)
{ if (elements.get(i) instanceof MatchedRule)
{ MatchedRule mr = (MatchedRule) elements.get(i);
if (mr.isLazy() || mr.isCalled()) { continue; }
// mr must be split if it reads a target type that is an output of itself or a
// later rule.
OutPattern opatt = mr.outPattern;
Vector streads = opatt.sourceTypesRead();
Statement stat = mr.actionBlock;
if (stat != null)
{ streads.addAll(stat.readFrame()); }
System.out.println(">> Read frame of rule " + i + " is: " + streads);
for (int j = i; j < elements.size(); j++)
{ MatchedRule mr2 = (MatchedRule) elements.get(j);
if (!mr2.isLazy() && !mr2.isCalled() && mr2.hasOutputType(streads))
{ System.err.println(">> Rule " + mr2.getName() + " writes an entity read by earlier rule " + mr);
System.err.println(">> " + mr.getName() + " will be split into two rules/constraints");
MatchedRule mr3 = mr.slice();
if (res.contains(mr3)) { }
else
{ res.add(mr3); } // only add *once*.
}
}
}
}
return res;
}
public int complexity()
{ int result = 0;
for (int i = 0; i < attributes.size(); i++)
{ Attribute att = (Attribute) attributes.get(i);
Expression ini = att.getInitialExpression();
if (ini == null) { continue; }
int acomp = att.syntacticComplexity();
System.out.println("*** Syntactic complexity of helper " + att.getName() + " is " + acomp);
result += acomp;
}
for (int j = 0; j < operations.size(); j++)
{ BehaviouralFeature bf = (BehaviouralFeature) operations.get(j);
int bfc = bf.syntacticComplexity();
System.out.println("*** Syntactic complexity of helper " + bf.getName() + " is " + bfc);
result += bfc;
}
for (int i = 0; i < elements.size(); i++)
{ MatchedRule r = (MatchedRule) elements.get(i);
result = result + r.complexity();
}
System.out.println("*** Number of rules in transformation " + name + " is: " + elements.size());
int hs = operations.size() + attributes.size();
System.out.println("*** Number of helpers in transformation " + name + " is: " + hs);
return result;
}
public int cyclomaticComplexity()
{ int result = 0;
for (int i = 0; i < attributes.size(); i++)
{ Attribute att = (Attribute) attributes.get(i);
Expression ini = att.getInitialExpression();
if (ini == null) { continue; }
int acomp = ini.cyclomaticComplexity();
System.out.println("*** Cyclomatic complexity of helper " + att.getName() + " is " + acomp);
if (acomp > 10)
{ result += 1; }
}
for (int j = 0; j < operations.size(); j++)
{ BehaviouralFeature bf = (BehaviouralFeature) operations.get(j);
int bfc = bf.cyclomaticComplexity();
System.out.println("*** Cyclomatic complexity of helper " + bf.getName() + " is " + bfc);
if (bfc > 10)
{ result += 1; }
}
for (int i = 0; i < elements.size(); i++)
{ MatchedRule r = (MatchedRule) elements.get(i);
int rfc = r.cyclomaticComplexity();
System.out.println("*** Cyclomatic complexity of rule " + r.getName() + " is " + rfc);
if (rfc > 10)
{ result = result + 1; }
}
return result;
}
public Map getCallGraph()
{ // Include also thisModule.resolveTemp(x,v)
Map res = new Map();
String nme = getName();
int opsSize = operations.size();
for (int i = 0; i < opsSize; i++)
{ BehaviouralFeature bf = (BehaviouralFeature) operations.get(i);
Set calledrules = new HashSet();
Vector bfcalls = bf.operationsUsedIn();
for (int j = 0; j < bfcalls.size(); j++)
{ res.add_pair(nme + "::" + bf.getName(), bfcalls.get(j));
calledrules.add(bfcalls.get(j) + "");
}
System.out.println("*** EFO of operation " + bf.getName() + " is: " + calledrules.size());
}
for (int i = 0; i < attributes.size(); i++)
{ Attribute att = (Attribute) attributes.get(i);
Expression ini = att.getInitialExpression();
if (ini == null) { continue; }
Set calledrules = new HashSet();
Vector attcalls = ini.allOperationsUsedIn();
for (int j = 0; j < attcalls.size(); j++)
{ res.add_pair(nme + "::" + att.getName(), attcalls.get(j));
calledrules.add(attcalls.get(j) + "");
}
System.out.println("*** EFO of helper " + att.getName() + " is: " + calledrules.size());
}
int rulesSize = elements.size();
for (int i = 0; i < rulesSize; i++)
{ MatchedRule rr = (MatchedRule) elements.get(i);
Set calledrules = new HashSet();
Vector opuses = rr.operationsUsedIn();
for (int j = 0; j < opuses.size(); j++)
{ res.add_pair(rr.getName(), opuses.get(j));
calledrules.add(opuses.get(j) + "");
}
Vector rts = rr.resolveTempsUsedIn();
if (rts.size() > 0)
{ System.out.println("*** ResolveTemps used in rule " + rr.getName() + ": " + rts);
for (int k = 0; k < rts.size(); k++)
{ BasicExpression rtemp = (BasicExpression) rts.get(k);
Vector pars = rtemp.getParameters();
if (pars != null && pars.size() > 1)
{ Expression esrc = (Expression) pars.get(0);
Expression evar = (Expression) pars.get(1);
System.out.println(esrc + " " + esrc.type);
for (int j = 0; j < rulesSize; j++)
{ MatchedRule rx = (MatchedRule) elements.get(j);
if (rx.calledByResolveTemp(esrc, evar+""))
{ res.remove_pair(rr.getName(), "null::resolveTemp");
res.add_pair(rr.getName(), rx.getName());
}
}
}
}
}
System.out.println("*** EFO of rule " + rr.getName() + " is: " + (calledrules.size() + rts.size()));
}
return res;
}
public int epl()
{ int res = 0;
int rulesSize = elements.size();
for (int i = 0; i < rulesSize; i++)
{ MatchedRule rr = (MatchedRule) elements.get(i);
int repl = rr.epl();
if (repl > 10)
{ System.err.println("*** Rule " + rr.getName() + " has excessive number of parameters/variables: " + repl);
res++;
}
}
return res;
} // and the operations
public int uex()
{ int res = 0;
for (int i = 0; i < elements.size(); i++)
{ MatchedRule r = (MatchedRule) elements.get(i);
if (!r.isLazy() && !r.isCalled())
{ res++; }
}
return (res*(res-1))/2;
}
}