CompilerGeneration.jrag 13.5 KB
Newer Older
1
2
/* -*-Java-*- */

3
4
5
6
7
8
/* 
 * Copyright (C) 2006  Anders Nilsson <anders.nilsson@cs.lth.se>
 *
 * This file is part of OntologyCompiler.
 */

9
import java.io.*;
10
import java.util.*;
11

12
aspect AbsGrammarGeneration {
13
    void ASTNode.genAbsGrammar(PrintStream pStream) {
14
15
16
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genAbsGrammar(pStream);
		}
17
18
    }

19
    public void Start.genAbsGrammar(PrintStream pStream) {
20
		pStream.println("// Predefined types");
Anders Nilsson's avatar
Anders Nilsson committed
21
		pStream.println("Start ::= Element*;");
Anders Nilsson's avatar
Anders Nilsson committed
22
		pStream.println("abstract Thing : ComplexElement ::=;");
23
		pStream.println("abstract Element;");
Anders Nilsson's avatar
Anders Nilsson committed
24
 		pStream.println("ComplexElement : Element ::=  OwlIdentifier Attribute* Element*;");
25
		pStream.println("ValueElement : ComplexElement;");
26
		pStream.println("abstract SimpleElement : Element;");
27
		pStream.println("StringElement : SimpleElement ::= <IDENTIFIER>;");
28
29
		pStream.println("IntElement : SimpleElement ::= <INTEGER_LITERAL>;");
		pStream.println("FloatElement : SimpleElement ::= <FLOAT_LITERAL>;");
Anders Nilsson's avatar
Anders Nilsson committed
30
		pStream.println("abstract ClassUse : ComplexElement  ::= /decl:Thing/;");
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
		pStream.println("Attribute ::= Value;");
		pStream.println("RdfDatatype : Attribute ::= ;");
		pStream.println("RdfId : Attribute ::= ;");
		pStream.println("Xmlns : Attribute ::= ;");
		pStream.println("XmlnsProtege : Attribute ::= ;");
		pStream.println("XmlnsRdf : Attribute ::= ;");
		pStream.println("XmlnsXsd : Attribute ::= ;");
		pStream.println("XmlnsRdfs : Attribute ::= ;");
		pStream.println("XmlnsOwl : Attribute ::= ;");
		pStream.println("XmlnsDaml : Attribute ::= ;");
		pStream.println("XmlnsP1 : Attribute ::= ;");
		pStream.println("XmlnsDc : Attribute ::= ;");
		pStream.println("XmlBase : Attribute ::= ;");
		pStream.println("RdfResource : Attribute ::= ;");
		pStream.println("RdfAbout : Attribute ::= ;");
		pStream.println("RdfParseType : Attribute ::= ;");
		pStream.println("Version : Attribute ::= ;");
		pStream.println("Value ::= <STRING_LITERAL>;");
49
		pStream.println("OwlIdentifier ::= <IDENTIFIER>;");
50
51
52
		pStream.println("AttrValue ::= <STRING_LITERAL>;");
		pStream.println();
		pStream.println("// Generated types");
53
		super.genAbsGrammar(pStream);
54
55
    }

56
    void OwlClassDecl.genAbsGrammar(PrintStream pStream) {
57
58
59
		pStream.print(getId());
		pStream.print(" : "+getSuperClass().getId());	
		pStream.print(" ::=");
60
		getOwnRestrictions().genAbsGrammar(pStream);
61
		pStream.println(";");
Anders Nilsson's avatar
Anders Nilsson committed
62
63
64
		pStream.print(getId()+"Use");
		pStream.print(" : ClassUse");	
		pStream.println(";");
65
66
    }

67
68
69
70
71
72
73
	void Restrictions.genAbsGrammar(PrintStream pStream) {
		for (int i=0; i<getNumOwlRestriction(); i++) {
			pStream.print(" ");
			getOwlRestriction(i).genAbsGrammar(pStream);
		}
	}

74
    void OwlRestriction.genAbsGrammar(PrintStream pStream) {
Anders Nilsson's avatar
Anders Nilsson committed
75
		pStream.print(name());
76
77
78
		if (allValuesFrom()) {
			pStream.print("*");
		}
79
    }
80

81
82
83
84
85
86
87
88
    String OwlRestriction.getRestrictionPropertyId() {
		for (int i=0; i<getNumElement(); i++) {
			ComplexElement e = (ComplexElement) getElement(i);
			if (e instanceof OwlOnProperty) {
				return e.getId(); 
			}
		}
		return "_Unknown_";
89
90
    } 

91
92
93
94
95
96
97
98
99
    String OwlRestriction.getRestrictionClassId() {
		for (int i=0; i<getNumElement(); i++) {
			ComplexElement e = (ComplexElement) getElement(i);
			if (e instanceof OwlAllValuesFrom || 
				e instanceof OwlSomeValuesFrom) {
				return e.getId(); // Ugly as hell!!
			}
		}
		return "_Unknown_";
100
101
102
    } 

    syn lazy boolean OwlRestriction.allValuesFrom() {
103
104
105
106
107
108
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof OwlAllValuesFrom) {
				return true;
			}
		}
		return false;
109
110
    }

111
112
}

113
114
115
116
117
118
aspect GenAspects {
	public void Start.genAspects(PrintStream pStream) {
		pStream.println();
		pStream.println("import java.io.PrintStream;");
		pStream.println();
		genRewrites(pStream);
119
		genMisc(pStream);
120
		genPrettyPrinter(pStream);
121
122
123
	}
}

124
aspect GenPrettyPrinter {
125
	public void ASTNode.genPrettyPrinter(PrintStream pStream) {
126
		for (int i=0; i<getNumChild(); i++) {
127
			getChild(i).genPrettyPrinter(pStream);
128
		}
129
130
	}

131
	public void Start.genPrettyPrinter(PrintStream pStream) {
132
		pStream.println();
133
134
135
		pStream.println("aspect PrettyPrinter {");
		pStream.println("  public void ASTNode.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    for (int i=0; i<getNumChild(); i++) {");
136
		pStream.println("      getChild(i).prettyPrint(indent,pStream);");
137
		pStream.println("    }");
138
		pStream.println("  }\n");
Anders Nilsson's avatar
Anders Nilsson committed
139
140
141
142
		pStream.println("  public void Start.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    for (int i=0; i<getNumElement(); i++) {");
		pStream.println("      getElement(i).prettyPrint(indent,pStream);");
		pStream.println("    }");
143
144
145
		pStream.println("  }\n");
		pStream.println("  syn String Attribute.id() = \"\";");
		pStream.println("  eq RdfId.id() = \"Rdf:ID\";");
146
		pStream.println("  eq RdfDatatype.id() = \"Rdf:datatype\";");
147
148
		pStream.println("  public void Attribute.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    System.out.print(\" \"+id()+\"=\"+getValue().getSTRING_LITERAL());");
Anders Nilsson's avatar
Anders Nilsson committed
149
		pStream.println("  }\n");
150
151
152
153
154
155
156
157
158
		pStream.println(ind(1)+"public void ValueElement.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println(ind(2)+"System.out.print(indent+\"<value \");");
		pStream.println(ind(2)+"for (int i=0; i<getNumAttribute(); i++) {");
		pStream.println(ind(3)+"getAttribute(i).prettyPrint(indent,pStream);");
		pStream.println(ind(2)+"}");
		pStream.println(ind(2)+"System.out.println(\">\");");
		pStream.println(ind(2)+"System.out.println(indent+\"  \"+value());");
		pStream.println(ind(2)+"System.out.println(indent+\"</value>\");");
		pStream.println(ind(1)+"}\n");
159
		super.genPrettyPrinter(pStream);
160
161
162
		pStream.println("}\n");
	}

163
164
	public void OwlClassDecl.genPrettyPrinter(PrintStream pStream) {
		pStream.print(ind(1)+"public void "+name());
165
		pStream.println(".prettyPrint(String indent, PrintStream pStream) {");
166
167
168
169
170
171
		pStream.println(ind(2)+"System.out.print(indent+\"<"+name()+"\");");
		pStream.println(ind(2)+"for (int i=0; i<getNumAttribute(); i++) {");
		pStream.println(ind(3)+"getAttribute(i).prettyPrint(indent,pStream);");
		pStream.println(ind(2)+"}");
		pStream.println("System.out.println(\">\");");
		pStream.println(ind(2)+"String childIndent = indent + \"  \";");
Anders Nilsson's avatar
Anders Nilsson committed
172
		Restrictions restr = getRestrictions();
Anders Nilsson's avatar
Cleanup    
Anders Nilsson committed
173
		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
174
175
176
177
178
179
180
181
182
			OwlRestriction r = restr.getOwlRestriction(i);
			pStream.println(ind(2)+"System.out.println(childIndent+\"<has"+
							r.name()+">\");");
			pStream.println(ind(2)+"for (int i=0; i<getNum"+r.name()+"(); i++) {");
			pStream.println(ind(3)+"get"+r.name()+
							"(i).prettyPrint(childIndent+\"  \",pStream);");
			pStream.println(ind(2)+"}");
			pStream.println(ind(2)+"System.out.println(childIndent+\"</has"+
							r.name()+">\");");
Anders Nilsson's avatar
Anders Nilsson committed
183
		}
184
185
186
		pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
		pStream.println(ind(3)+"getElement(i).prettyPrint(childIndent,pStream);");
		pStream.println(ind(2)+"}");
187
188
		pStream.println(ind(1)+"System.out.println(indent+\"</"+name()+">\");");
		pStream.println(ind(1)+"}\n");		
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	}
}

aspect GenRewrites {

    public void ASTNode.genRewrites(PrintStream pStream) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genRewrites(pStream);
		}
    }

	public void Start.genRewrites(PrintStream pStream) {
		pStream.println();
		pStream.println("aspect Rewrites {");
203
204
205
206
207
208
209
210
211
212
		pStream.println(ind(1)+"rewrite ComplexElement {");
		pStream.println(ind(2)+"when (getOwlIdentifier().getIDENTIFIER().equals(\"value\") && !(this instanceof ValueElement))");
		pStream.println(ind(3)+"to ValueElement {");
		pStream.println(ind(3)+"ValueElement node = new ValueElement();");
		pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
		pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
		pStream.println(ind(3)+"node.setElementList(getElementList());");
		pStream.println(ind(3)+"return node;");
		pStream.println(ind(2)+"}");
		pStream.println(ind(1)+"}");
213
		super.genRewrites(pStream);
214
215
		pStream.println("}");
	}
216

217
218
219
	public void OwlClassDecl.genRewrites(PrintStream pStream) {
		String ind = "  ";
		int ix = 0;
220
		// Specialize ComplexElements to the actual class in the ontology
221
222
223
224
		pStream.println(ind(1)+"rewrite ComplexElement {");
		pStream.println(ind(2)+"when (getOwlIdentifier().getIDENTIFIER().equals(\""+
						name()+"\") && !(this instanceof Thing))");
		pStream.println(ind(3)+"to "+name()+" {");
225

226
227
228
		pStream.println(ind(3)+name()+" node = new "+name()+"();");
		pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
		pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
229
// 		pStream.println(ind(3)+"node.setElementList(getElementList());");
230
		Restrictions restr = getRestrictions();
231
232
		pStream.println(ind(3)+"List l, remain;");
		pStream.println(ind(3)+"remain = new List();");
233
234
		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
			OwlRestriction r = restr.getOwlRestriction(i);
235
236
			pStream.println(ind(3)+"l = new List();");
			pStream.println(ind(3)+"for (int i=0; i<getNumElement(); i++) {");
237
238
			pStream.println(ind(4)+"ComplexElement e = (ComplexElement) getElement(i);");
			pStream.println(ind(4)+"if (e.getNumElement() > 0 && e.getElement(0) instanceof "+r.name()+" ) {");
239
			pStream.println(ind(5)+"l.add(e.getElement(0));");
240
241
			pStream.println(ind(4)+"}");
			pStream.println(ind(3)+"}");
242
			if (r.allValuesFrom()) {
243
				pStream.println(ind(3)+"node.set"+r.name()+"List(l);");
244
245
			} else {
				pStream.println(ind(3)+r.name()+" c"+ix+" = new "+r.name()+"();");
Anders Nilsson's avatar
Anders Nilsson committed
246
				pStream.println(ind(3)+"node.set"+r.name()+"(c"+(ix++)+");");
247
248
			}
		}
249
250
251
252
253
254
		pStream.println(ind(3)+"for (int i=0; i<getNumElement(); i++) {");
		pStream.println(ind(4)+"if (getElement(i) instanceof ValueElement) {");
		pStream.println(ind(5)+"remain.add(getElement(i));");
		pStream.println(ind(4)+"}");
		pStream.println(ind(3)+"}");
  		pStream.println(ind(3)+"node.setElementList(remain);");
255
256
257
		pStream.println(ind(3)+"return node;");
		pStream.println(ind(2)+"}");
		pStream.println(ind(1)+"}");
Anders Nilsson's avatar
Anders Nilsson committed
258
		pStream.println();
259
260
		// Rearrange device nodes on the AST to get away from the
		// current diagonal style from Protege
Anders Nilsson's avatar
Anders Nilsson committed
261
262
263
264
265
266
267
		pStream.println(ind(1)+"rewrite "+name()+" {");
		pStream.println(ind(2)+"when (!isTopElement())");
		pStream.println(ind(2)+"to "+name()+"Use {");
		pStream.println(ind(3)+"getStart().addElement(this);");
		pStream.println(ind(3)+"return new "+name()+"Use();");
		pStream.println(ind(2)+"}");
		pStream.println(ind(1)+"}");
268
	}
Anders Nilsson's avatar
Anders Nilsson committed
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
aspect GenMisc {

    public void ASTNode.genMisc(PrintStream pStream) {
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genMisc(pStream);
		}
    }

	public void Start.genMisc(PrintStream pStream) {
		pStream.println();
		pStream.println("aspect Misc {");
		pStream.println();
		pStream.println(ind(1)+"String ValueElement.value() {");
		pStream.println(ind(2)+"StringBuffer b = new StringBuffer();");
		pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
		pStream.println(ind(3)+"b.append(((SimpleElement) getElement(i)).value());");
		pStream.println(ind(3)+"b.append(\" \");");
		pStream.println(ind(2)+"}");
		pStream.println(ind(2)+"return b.toString().trim();");
		pStream.println(ind(1)+"}");
		pStream.println();
		pStream.println(ind(1)+"syn String SimpleElement.value() = \"\";");
		pStream.println(ind(1)+"eq StringElement.value() = getIDENTIFIER();");
		pStream.println(ind(1)+"eq IntElement.value() = getINTEGER_LITERAL();");
		pStream.println(ind(1)+"eq FloatElement.value() = getFLOAT_LITERAL();");
Anders Nilsson's avatar
Anders Nilsson committed
297
298
299
300
301
302
303
304
305
306
307
308
309
		
		pStream.println(ind(1)+"boolean Thing.isTopElement() {");
		pStream.println(ind(2)+"return getParent().getParent() instanceof Start;");
		pStream.println(ind(1)+"}");

		pStream.println(ind(1)+"Start ASTNode.getStart() {");
		pStream.println(ind(2)+"return getParent().getStart();");
		pStream.println(ind(1)+"}");
		pStream.println(ind(1)+"Start Start.getStart() {");
		pStream.println(ind(2)+"return this;");
		pStream.println(ind(1)+"}");
		

310
311
312
313
314
		super.genMisc(pStream);
		pStream.println("}");
	}

}
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

aspect ParserGen {
	public void Start.genParser(PrintStream pStream) {
		pStream.println("import java.io.FileNotFoundException;");
		pStream.println("import java.io.FileReader;");
		pStream.println("import java.io.InputStreamReader;");
		pStream.println("import java.io.Reader;");
		pStream.println("");
		pStream.println("import AST.SiarasParser;");
		pStream.println("import AST.ParseException;");
		pStream.println("import AST.Start;");
		pStream.println("");
		pStream.println("public class Parser {");
		pStream.println();
		pStream.println("	protected static Start parse(String args[]) {");
		pStream.println("		Reader r = getReader(args);");
		pStream.println("		Start ast = null;");
		pStream.println("		try {");
		pStream.println("			SiarasParser parser = new SiarasParser(r);");
		pStream.println();
		pStream.println("			ast = parser.Start();");
		pStream.println("		} catch (ParseException e) {");
		pStream.println("			System.out.println(e.getMessage());");
		pStream.println("		}");
		pStream.println("		return ast;");
		pStream.println("	}");
		pStream.println();
		pStream.println("	private static Reader getReader(String[] args) {");
		pStream.println("		Reader r = null;");
		pStream.println("		if (args.length != 1) {");
		pStream.println("			r = new InputStreamReader(System.in);");
		pStream.println("		} else {");
		pStream.println("			try {");
		pStream.println("				r = new FileReader(args[0]);");
		pStream.println("			} catch (FileNotFoundException e1) {");
		pStream.println("				System.err.println(\"Dumper: file \" + args[0] + \" not found\");");
		pStream.println("			}");
		pStream.println("		}");
		pStream.println("		return r;");
		pStream.println("	}");
		pStream.println("}");
	}
}