Backend.jadd 11.4 KB
Newer Older
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
aspect BackendAbstractGrammar {
	public String Program.generateAbstractGrammar() {
		StringBuilder sb = new StringBuilder();
		generateAbstractGrammar(sb);
		return sb.toString();
	}

	public void Program.generateAbstractGrammar(StringBuilder sb) {
		for (TypeDecl td: getTypeDecls()) {
			td.generateAbstractGrammar(sb);
		}
	}

	public void TypeDecl.generateAbstractGrammar(StringBuilder sb) {
		if (getAbstract()) {
			sb.append("abstract ");
		}
		sb.append(getID());
		if (hasSuper()) {
			sb.append(" : " + getSuper());
		}
		
		if (getNumComponent() > 0 || relationComponents().size() > 0) {
			sb.append(" ::=");
		}
		for (Component c: getComponents()) {
			sb.append(" ");
			sb.append(c.generateAbstractGrammar());
		}
		for (RelationComponent c: relationComponents()) {
			sb.append(" ");
			sb.append(c.generateAbstractGrammar());
		}
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
		sb.append(";\n");
	}

	public String Component.generateAbstractGrammar() {
		if (getID().equals(getTypeUse().toString())) {
			return getTypeUse().toString();
		} else {
			return getID() + ":" + getTypeUse();
		}
	}
	public String ListComponent.generateAbstractGrammar() {
		return super.generateAbstractGrammar() + "*";
	}
	public String OptComponent.generateAbstractGrammar() {
		return "[" + super.generateAbstractGrammar() + "]";
	}
	public String TokenComponent.generateAbstractGrammar() {
		return "<" + getID() + ":" + getTypeUse() + ">";
	}

	public String RelationComponent.generateAbstractGrammar() {
56
		return "<" + getImplAttributeName() + ":" + ofTypeDecl() + ">";
57
58
	}
	public String ManyRelationComponent.generateAbstractGrammar() {
59
60
61
62
63
		return "<" + getImplAttributeName() + ":ArrayList<" + ofTypeDecl() + ">>";
	}

	public String RelationComponent.getImplAttributeName() {
		return "_impl_" + getID();
64
	}
65
66
67
68
69
70
71
72
73
74
}

aspect BackendAspect {
	public String Program.generateAspect() {
		StringBuilder sb = new StringBuilder();
		generateAspect(sb);
		return sb.toString();
	}

	public void Program.generateAspect(StringBuilder sb) {
75
		sb.append("import java.util.*;\n");
76
77
78
79
80
81
82
83
84
85
		sb.append("aspect RelAstAPI {\n");
		for (TypeDecl td: getTypeDecls()) {
			if (td.needsConstructor()) {
				td.generateConstructor(sb);
			}
		}
		for (Relation r: getRelations()) {
			r.generateAPI(sb);
		}

86
87
88
89
90
		sb.append(ind(1) + "public void ASTNode.assertNotNull(Object obj) {\n");
		sb.append(ind(2) + "if (obj == null) {\n");
		sb.append(ind(3) + "throw new NullPointerException();\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(1) + "}\n");
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
		sb.append("}\n");
	}

	public void TypeDecl.generateConstructor(StringBuilder sb) {
		sb.append(ind(1) + "public " + getID() + "." + getID() + "(");
		int i = 0;
		for (Component c: componentsTransitive()) {
			sb.append(c.getTypeUse() + " " + c.getID());
			if (++i < componentsTransitive().size()) {
				sb.append(", ");
			}
		}
		sb.append(") {\n");
		for (Component c: componentsTransitive()) {
			sb.append(ind(2) + "set" + c.getID() + "(" + c.getID() + ");\n");
		}
		sb.append(ind(1) + "}\n");
	}
}

aspect BackendAPI {
	public void Relation.generateAPI(StringBuilder sb) {
		sb.append(ind(1) + "// " + prettyPrint() + "\n");
		getDirection().generateAPI(sb);
115
		sb.append("\n");
116
117
	}
	public abstract void Direction.generateAPI(StringBuilder sb);
118
119
120
121
122
123
124
125


	inh Relation Direction.relation();
	eq Relation.getChild().relation() = this;
	eq Program.getChild().relation() = null;

	public String RelationComponent.nameCapitalized() {
		return name().substring(0,1).toUpperCase() + name().substring(1);
126
	}
127
}
128

129
130
131
aspect BackendDirectedAPI {
	public void RightDirection.generateAPI(StringBuilder sb) {
		relation().getLeft().generateDirectedAPI(sb);
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
	public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
	public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
		generateDirectedZeroOneAPI(sb, false);
	}
	public void OptionalRelationComponent.generateDirectedAPI(StringBuilder sb) {
		generateDirectedZeroOneAPI(sb, true);
		
		// has
		sb.append(ind(1) + "public boolean " + toTypeDecl());
		sb.append(".has" + nameCapitalized() + "() {\n");
		sb.append(ind(2) + "return " + name() + "() != null;\n");
		sb.append(ind(1) + "}\n");
	}
	public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb, boolean optional) {
		// Get
		sb.append(ind(1) + "public " + ofTypeDecl() + " " + toTypeDecl());
		sb.append("." + name() + "() {\n");
		sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
		sb.append(ind(1) + "}\n");

		// Set
		sb.append(ind(1) + "public void " + toTypeDecl());
		sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		if (!optional) {
			sb.append(ind(2) + "assertNotNull(o);\n");
		}
		sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
		sb.append(ind(1) + "}\n");
	}

	public void ManyRelationComponent.generateDirectedAPI(StringBuilder sb) {
		// Get
Niklas Fors's avatar
Niklas Fors committed
166
		sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
167
		sb.append("." + name() + "() {\n");
Niklas Fors's avatar
Niklas Fors committed
168
169
		sb.append(ind(2) + "ArrayList<List> l = get" + getImplAttributeName() + "();\n");
		sb.append(ind(2) + "return l != null ? l : Collections.emptyList();\n");
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		sb.append(ind(1) + "}\n");

		// Add
		sb.append(ind(1) + "public void " + toTypeDecl() + ".addTo");
		sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		sb.append(ind(2) + "assertNotNull(o);\n");
		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
		sb.append(ind(2) + "if (list == null) {\n");
		sb.append(ind(3) + "list = new ArrayList<>();\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(2) + "list.add(o);\n");
		sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
		sb.append(ind(1) + "}\n");

		// Remove
		sb.append(ind(1) + "public void " + toTypeDecl() + ".removeFrom");
		sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		sb.append(ind(2) + "assertNotNull(o);\n");
		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
		sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
		sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(1) + "}\n");
	}
194
}
195

196
197
198
199
aspect BackendBidirectionalAPI {
	public void Bidirectional.generateAPI(StringBuilder sb) {
		RelationComponent l = relation().getLeft();
		RelationComponent r = relation().getRight();
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
		if (l.multiplicityOne()) {
			if (r.multiplicityOne()) {
				l.generateBiOne(sb, false);
				r.generateBiOne(sb, false);
			} else if (r.multiplicityOpt()) {
				l.generateBiOne(sb, false);
				r.generateBiOne(sb, true);
			} else if (r.multiplicityMany()) {

			}
		} else if (l.multiplicityOpt()) {
			if (r.multiplicityOne()) {
				l.generateBiOne(sb, true);
				r.generateBiOne(sb, false);
			} else if (r.multiplicityOpt()) {
				l.generateBiOne(sb, true);
				r.generateBiOne(sb, true);
			} else if (r.multiplicityMany()) {
				
			}
		} else if (l.multiplicityMany()) {
			if (r.multiplicityOne()) {

			} else if (r.multiplicityOpt()) {

			} else if (r.multiplicityMany()) {
Niklas Fors's avatar
Niklas Fors committed
227
228
				l.generateBiMany(sb);
				r.generateBiMany(sb);
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
			}
		}
	}

	public void RelationComponent.generateBiOne(StringBuilder sb, boolean isOpt) {
		// Get
		sb.append(ind(1) + "public " + ofTypeDecl() + " " + toTypeDecl());
		sb.append("." + name() + "() {\n");
		sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
		sb.append(ind(1) + "}\n");

		// Set
		sb.append(ind(1) + "public void " + toTypeDecl());
		sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		if (!isOpt) {
			sb.append(ind(2) + "assertNotNull(o);\n");
		}
		sb.append(ind(2) + "if (get" + getImplAttributeName() + "() != null) {\n");
		sb.append(ind(3) + "get" + getImplAttributeName() + "().set" + otherSide().getImplAttributeName() + "(null);\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(2) + "if (o != null && o.get" + otherSide().getImplAttributeName() + "() != null) {\n");
		sb.append(ind(3) + "o.get" + otherSide().getImplAttributeName() + "().set" + getImplAttributeName() + "(null);\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
		if (isOpt) {
			sb.append(ind(2) + "if (o != null) {\n");
			sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
			sb.append(ind(2) + "}\n");
		} else {
			sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
		}
		sb.append(ind(1) + "}\n");

		if (isOpt) {
			// has
			sb.append(ind(1) + "public boolean " + toTypeDecl());
			sb.append(".has" + nameCapitalized() + "() {\n");
			sb.append(ind(2) + "return " + name() + "() != null;\n");
			sb.append(ind(1) + "}\n");
		}
269
	}
Niklas Fors's avatar
Niklas Fors committed
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

	public void RelationComponent.generateBiMany(StringBuilder sb) {
		// Get
		sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
		sb.append("." + name() + "() {\n");
		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> l = get"
			+ getImplAttributeName() + "();\n");
		sb.append(ind(2) + "return l != null ? l : Collections.emptyList();\n");
		sb.append(ind(1) + "}\n");

		// Add
		sb.append(ind(1) + "public void " + toTypeDecl() + ".addTo");
		sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		sb.append(ind(2) + "assertNotNull(o);\n");
		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
		sb.append(ind(2) + "if (list == null) {\n");
		sb.append(ind(3) + "list = new ArrayList<>();\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(2) + "ArrayList<" + otherSide().ofTypeDecl() + "> list2 = o.get"
			+ otherSide().getImplAttributeName() + "();\n");
		sb.append(ind(2) + "if (list2 == null) {\n");
		sb.append(ind(3) + "list2 = new ArrayList<>();\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(2) + "list.add(o);\n");
		sb.append(ind(2) + "list2.add(this);\n");
		sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
		sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
		sb.append(ind(1) + "}\n");

		// Remove
		sb.append(ind(1) + "public void " + toTypeDecl() + ".removeFrom");
		sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
		sb.append(ind(2) + "assertNotNull(o);\n");
		sb.append(ind(2) + "ArrayList<" + ofTypeDecl() + "> list = get" + getImplAttributeName() + "();\n");
		sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
		sb.append(ind(3) + "ArrayList<" + otherSide().ofTypeDecl() + "> list2 = o.get"
			+ otherSide().getImplAttributeName() + "();\n");
		sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n");
		sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
		sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
		sb.append(ind(2) + "}\n");
		sb.append(ind(1) + "}\n");
	}
313
314
}

315

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
aspect PrettyPrint {
	public String Relation.prettyPrint() {
		return "rel "
			+ getLeft().prettyPrint() + " "
			+ getDirection().prettyPrint() + " "
			+ getRight().prettyPrint();
	}
	public String RelationComponent.prettyPrint() {
		if (getID().isEmpty()) {
			return getTypeUse().toString();
		} else {
			return getTypeUse() + "." +  getID();
		}
	}
	public String OptionalRelationComponent.prettyPrint() {
Niklas Fors's avatar
Niklas Fors committed
331
		return super.prettyPrint() + "?";
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	}
	public String ManyRelationComponent.prettyPrint() {
		return super.prettyPrint() + "*";
	}
	abstract public String Direction.prettyPrint();
	public String RightDirection.prettyPrint() {
		return "->";
	}
	public String Bidirectional.prettyPrint() {
		return "<->";
	}

}

aspect Utils {
	public String ASTNode.ind(int n) {
		String s = "";
		for (int i = 0; i < n; i++) {
			s += "  ";
		}
		return s;
	}
354
}