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

3
/* 
4
 * Copyright (C) 2006,2010  Anders Nilsson <anders.nilsson@control.lth.se>
5 6
 *
 * This file is part of OntologyCompiler.
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * OntologyCompiler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * OntologyCompiler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with OntologyCompiler.  If not, see <http://www.gnu.org/licenses/>.
20 21
 */

22
import java.io.*;
23
import java.util.*;
24

25
aspect AbsGrammarGeneration {
26
    void ASTNode.genAbsGrammar(PrintStream pStream) {
27 28 29
		for (int i=0; i<getNumChild(); i++) {
			getChild(i).genAbsGrammar(pStream);
		}
30 31
    }

32
    public void Start.genAbsGrammar(PrintStream pStream) {
33 34 35
        pStream.println("// -*-Java-*- ");
        pStream.println();
        pStream.println("// Automatically generated JastAdd abstract grammar file ");
36
		pStream.println("// Predefined types");
Anders Nilsson's avatar
Anders Nilsson committed
37
		pStream.println("Start ::= Element*;");
Anders Nilsson's avatar
Anders Nilsson committed
38
		pStream.println("abstract Thing : ComplexElement ::=;");
39
		pStream.println("abstract Element;");
Anders Nilsson's avatar
Anders Nilsson committed
40
 		pStream.println("ComplexElement : Element ::=  OwlIdentifier Attribute* Element*;");
41
		pStream.println("ValueElement : ComplexElement;");
Anders Nilsson's avatar
Anders Nilsson committed
42
		pStream.println("RdfDeclaration : ComplexElement;");
Anders Nilsson's avatar
Anders Nilsson committed
43 44 45 46
		pStream.println("abstract SimpleElement : Element ::= <LITERAL>;");
		pStream.println("StringElement : SimpleElement ::=;");
		pStream.println("IntElement : SimpleElement ::=;");
		pStream.println("FloatElement : SimpleElement ::=;");
47
		pStream.println("ClassUse : Thing  ::= /decl:Thing/;");
48
		pStream.println("Attribute ::= Value;");
Anders Nilsson's avatar
Anders Nilsson committed
49 50 51 52 53 54
		pStream.println("AbbreviatedIRI : Attribute ::= ;");
		pStream.println("Cardinality : Attribute ::= ;");
		pStream.println("DatatypeIRI : Attribute ::= ;");
		pStream.println("IRI : Attribute ::= ;");
		pStream.println("Name : Attribute ::= ;");
		pStream.println("OntologyIRI : Attribute ::= ;");
55 56 57 58 59 60
		pStream.println("RdfDatatype : Attribute ::= ;");
		pStream.println("RdfId : Attribute ::= ;");
		pStream.println("Xmlns : Attribute ::= ;");
		pStream.println("XmlnsProtege : Attribute ::= ;");
		pStream.println("XmlnsRdf : Attribute ::= ;");
		pStream.println("XmlnsXsd : Attribute ::= ;");
Anders Nilsson's avatar
Anders Nilsson committed
61
		pStream.println("XmlnsXsp : Attribute ::= ;");
62 63
		pStream.println("XmlnsRdfs : Attribute ::= ;");
		pStream.println("XmlnsOwl : Attribute ::= ;");
Anders Nilsson's avatar
Anders Nilsson committed
64 65
		pStream.println("XmlnsSwrl : Attribute ::= ;");
		pStream.println("XmlnsSwrlb : Attribute ::= ;");
66 67 68
		pStream.println("XmlnsDaml : Attribute ::= ;");
		pStream.println("XmlnsP1 : Attribute ::= ;");
		pStream.println("XmlnsDc : Attribute ::= ;");
69 70
		pStream.println("XmlnsExpr : Attribute ::= ;");
		pStream.println("XmlnsList : Attribute ::= ;");
71 72 73 74 75 76
		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>;");
77
		pStream.println("OwlIdentifier ::= <IDENTIFIER>;");
78 79 80
		pStream.println("AttrValue ::= <STRING_LITERAL>;");
		pStream.println();
		pStream.println("// Generated types");
81
		super.genAbsGrammar(pStream);
82 83
    }

Anders Nilsson's avatar
Anders Nilsson committed
84 85
	

86
    void OwlClassDecl.genAbsGrammar(PrintStream pStream) {
87 88 89
		pStream.print(getId());
		pStream.print(" : "+getSuperClass().getId());	
		pStream.print(" ::=");
Anders Nilsson's avatar
Anders Nilsson committed
90 91
// 		getOwnRestrictions().genAbsGrammar(pStream);
		getProperties().genAbsGrammar(pStream);
92
		pStream.println(";");
Anders Nilsson's avatar
Anders Nilsson committed
93 94 95 96 97 98 99
// 		Properties p = getProperties();
// 		System.out.println(getId());
// 		for (int i=0; i<p.getNumProperty(); i++) {
// 			System.out.println("  "+((ComplexElement) p.getProperty(i)).getId());
// 		}
    }

100
	void ClassDeclaration.genAbsGrammar(PrintStream pStream) {
Anders Nilsson's avatar
Anders Nilsson committed
101 102 103 104
		if (name().equals("Thing")) {
			// Thing is handled explicitly
			return;
		}
105 106
		// System.out.println(name());
		// dumpTree(" ",System.out);
107 108 109
		pStream.print(name());
		pStream.print(" : "+getSuperClass().name());	
		pStream.print(" ::=");
110
		getProperties().genAbsGrammar(pStream);
111 112 113
		pStream.println(";");
	}

Anders Nilsson's avatar
Anders Nilsson committed
114 115 116
	void Properties.genAbsGrammar(PrintStream pStream) {
		for (int i=0; i<getNumProperty(); i++) {
			pStream.print(" ");
117
			getProperty(i).genAbsGrammarEntry(pStream);
Anders Nilsson's avatar
Anders Nilsson committed
118 119 120 121 122
		}
	}

    void OwlProperty.genAbsGrammarEntry(PrintStream pStream) {
		pStream.print(name());
123 124 125 126
		pStream.print(":"+range().type()+"*");
// 		pStream.print(":Thing");
// // 		if (allValuesFrom()) {
// 			pStream.print("*");
Anders Nilsson's avatar
Anders Nilsson committed
127
// 		}
128 129
    }

130 131 132 133 134 135
    void OwlDatatypeProperty.genAbsGrammarEntry(PrintStream pStream) {
		pStream.print("<"+name());
		pStream.print(":String>");
		// pStream.print(":"+range().type()+"*");
    }

136 137 138 139
	void Element.genAbsGrammarEntry(PrintStream pStream) {}

	void ObjectPropertyDomain.genAbsGrammarEntry(PrintStream pStream) {
		pStream.print(objectProperty().name());
140 141
		pStream.print(domain().name()+"*");
		// pStream.print(":Thing*");
142 143
	}

Anders Nilsson's avatar
Anders Nilsson committed
144 145 146 147 148
	void DataPropertyDomain.genAbsGrammarEntry(PrintStream pStream) {
		pStream.print("<"+dataProperty().name()+">");
		// pStream.print(":String*");
	}

149 150 151 152 153 154 155
	void Restrictions.genAbsGrammar(PrintStream pStream) {
		for (int i=0; i<getNumOwlRestriction(); i++) {
			pStream.print(" ");
			getOwlRestriction(i).genAbsGrammar(pStream);
		}
	}

156
    void OwlRestriction.genAbsGrammar(PrintStream pStream) {
Anders Nilsson's avatar
Anders Nilsson committed
157
		pStream.print(name());
158
		pStream.print(":Thing");
159 160 161
		if (allValuesFrom()) {
			pStream.print("*");
		}
162
    }
163

164 165 166 167 168 169 170 171
    String OwlRestriction.getRestrictionPropertyId() {
		for (int i=0; i<getNumElement(); i++) {
			ComplexElement e = (ComplexElement) getElement(i);
			if (e instanceof OwlOnProperty) {
				return e.getId(); 
			}
		}
		return "_Unknown_";
172 173
    } 

174 175 176 177 178 179 180 181 182
    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_";
183 184 185
    } 

    syn lazy boolean OwlRestriction.allValuesFrom() {
186 187 188 189 190 191
		for (int i=0; i<getNumElement(); i++) {
			if (getElement(i) instanceof OwlAllValuesFrom) {
				return true;
			}
		}
		return false;
192 193
    }

194 195
}

196 197 198 199 200 201
aspect GenAspects {
	public void Start.genAspects(PrintStream pStream) {
		pStream.println();
		pStream.println("import java.io.PrintStream;");
		pStream.println();
		genRewrites(pStream);
202
		genMisc(pStream);
203
		genPrettyPrinter(pStream);
204 205 206
	}
}

207
aspect GenPrettyPrinter {
208
	public void ASTNode.genPrettyPrinter(PrintStream pStream) {
209
		for (int i=0; i<getNumChild(); i++) {
210
			getChild(i).genPrettyPrinter(pStream);
211
		}
212 213
	}

214
	public void Start.genPrettyPrinter(PrintStream pStream) {
215
		pStream.println();
216 217 218
		pStream.println("aspect PrettyPrinter {");
		pStream.println("  public void ASTNode.prettyPrint(String indent, PrintStream pStream) {");
		pStream.println("    for (int i=0; i<getNumChild(); i++) {");
219
		pStream.println("      getChild(i).prettyPrint(indent,pStream);");
220
		pStream.println("    }");
221
		pStream.println("  }\n");
Anders Nilsson's avatar
Anders Nilsson committed
222 223 224 225
		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("    }");
226 227
		pStream.println("  }\n");
		pStream.println("  syn String Attribute.id() = \"\";");
228 229 230 231 232 233 234 235 236 237 238 239 240 241
		pStream.println("  eq RdfAbout.id() = \"rdf:about\";");
		pStream.println("  eq RdfDatatype.id() = \"rdf:datatype\";");
		pStream.println("  eq RdfId.id() = \"rdf:ID\";");
		pStream.println("  eq RdfParseType.id() = \"rdf:parseType\";");
		pStream.println("  eq RdfResource.id() = \"rdf:resource\";");
		pStream.println("  eq XmlBase.id() = \"xml:base\";");
		pStream.println("  eq Xmlns.id() = \"xmlns\";");
		pStream.println("  eq XmlnsDaml.id() = \"xmlns:daml\";");
		pStream.println("  eq XmlnsDc.id() = \"xmlns:dc\";");
		pStream.println("  eq XmlnsOwl.id() = \"xmlns:owl\";");
		pStream.println("  eq XmlnsP1.id() = \"xmlns:p1\";");
		pStream.println("  eq XmlnsProtege.id() = \"xmlns:protege\";");
		pStream.println("  eq XmlnsRdf.id() = \"xmlns:rdf\";");
		pStream.println("  eq XmlnsRdfs.id() = \"xmlns:rdfs\";");
Anders Nilsson's avatar
Anders Nilsson committed
242 243
		pStream.println("  eq XmlnsSwrl.id() = \"xmlns:swrl\";");
		pStream.println("  eq XmlnsSwrlb.id() = \"xmlns:swrlb\";");
244
		pStream.println("  eq XmlnsXsd.id() = \"xmlns:xsd\";");
Anders Nilsson's avatar
Anders Nilsson committed
245
		pStream.println("  eq XmlnsXsp.id() = \"xmlns:xsp\";");
246 247
		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
248
		pStream.println("  }\n");
249 250 251 252 253 254 255 256 257
		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");
258 259 260 261 262 263 264 265
		// Generate prettyprinter for ClassUse
		pStream.println(ind(1)+"public void ClassUse.prettyPrint(String indent, PrintStream pStream) {");
		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(ind(2)+"System.out.println(\"/>\");");
		pStream.println(ind(1)+"}\n");
266
		super.genPrettyPrinter(pStream);
267 268 269
		pStream.println("}\n");
	}

270
	public void OwlClassDecl.genPrettyPrinter(PrintStream pStream) {
271
		// Generate prettyprinter for class declaration
272
		pStream.print(ind(1)+"public void "+name());
273
		pStream.println(".prettyPrint(String indent, PrintStream pStream) {");
274 275 276 277 278 279
		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
280 281 282 283 284 285 286 287 288 289 290 291
// 		Restrictions restr = getRestrictions();
// 		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
// 			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()+">\");");
// 		}
292 293 294
		pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
		pStream.println(ind(3)+"getElement(i).prettyPrint(childIndent,pStream);");
		pStream.println(ind(2)+"}");
295 296
		pStream.println(ind(1)+"System.out.println(indent+\"</"+name()+">\");");
		pStream.println(ind(1)+"}\n");		
297 298 299 300 301 302 303 304 305 306 307 308 309 310
	}
}

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 {");
311
		pStream.println(ind(1)+"rewrite ComplexElement {");
312
		pStream.println(ind(2)+"when (!(this instanceof ClassUse) && getOwlIdentifier().getIDENTIFIER().equals(\"value\") && !(this instanceof ValueElement))");
313 314 315 316 317 318 319 320
		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)+"}");
Anders Nilsson's avatar
Anders Nilsson committed
321 322

		// TEMPORARY!!!!!
Anders Nilsson's avatar
Anders Nilsson committed
323 324
		// Rewrite isSkillOf/hasSkill/hasProperty/isPropertyOf to ClassUse.
		// Should probably be auto-generated instead.
Anders Nilsson's avatar
Anders Nilsson committed
325
		pStream.println(ind(1)+"rewrite ComplexElement {");
326 327 328 329 330 331 332 333 334 335 336
// 		pStream.println(ind(2)+"when ((name().equals(\"hasSkill\")");
// 		pStream.println(ind(2)+"|| name().equals(\"hasProperty\"))");
// 		pStream.println("&& getNumAttribute() > 0 && getAttribute(0) instanceof RdfResource)");
// 		pStream.println(ind(2)+"to ClassUse {");
// 		pStream.println(ind(3)+"ClassUse use = new ClassUse();");
// 		pStream.println(ind(3)+"String att = getAttribute(0).name();");
// 		pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(att.substring(att.indexOf('#')+1,att.indexOf('_'))));");
// 		pStream.println(ind(3)+"use.setAttributeList(getAttributeList());");
// 		pStream.println(ind(3)+"use.setElementList(new List());");
// 		pStream.println(ind(3)+"return use;");
// 		pStream.println(ind(2)+"}");
Anders Nilsson's avatar
Anders Nilsson committed
337 338 339 340 341 342
		pStream.println(ind(2)+"when (name().equals(\"rdf:RDF\") && !(this instanceof RdfDeclaration))");
		pStream.println(ind(2)+"to RdfDeclaration {");
		pStream.println(ind(3)+"return new RdfDeclaration(getOwlIdentifier(),getAttributeList(),getElementList());");
		pStream.println(ind(2)+"}");


343 344 345 346 347 348 349 350 351 352 353
// 		pStream.println(ind(2)+"when ((name().equals(\"isSkillOf\")");
// 		pStream.println(ind(2)+"|| name().equals(\"isPropertyOf\"))");
// 		pStream.println("&& getNumAttribute() > 0 && getAttribute(0) instanceof RdfResource)");
// 		pStream.println(ind(2)+"to ClassUse {");
// 		pStream.println(ind(3)+"ClassUse use = new ClassUse();");
// 		pStream.println(ind(3)+"String att = getAttribute(0).name();");
// 		pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(att.substring(att.indexOf('#')+1,att.indexOf('_'))));");
// 		pStream.println(ind(3)+"use.setAttributeList(getAttributeList());");
// 		pStream.println(ind(3)+"use.setElementList(new List());");
// 		pStream.println(ind(3)+"return use;");
// 		pStream.println(ind(2)+"}");
Anders Nilsson's avatar
Anders Nilsson committed
354
		pStream.println(ind(1)+"}");
Anders Nilsson's avatar
Anders Nilsson committed
355 356
	  

357
		super.genRewrites(pStream);
358 359
		pStream.println("}");
	}
360

361 362 363
	public void OwlClassDecl.genRewrites(PrintStream pStream) {
		String ind = "  ";
		int ix = 0;
364
		// Specialize ComplexElements to the actual class in the ontology
365
		pStream.println(ind(1)+"rewrite ComplexElement {");
366
		pStream.println(ind(2)+"when (!(this instanceof ClassUse) && getOwlIdentifier().getIDENTIFIER().equals(\""+
367 368
						name()+"\") && !(this instanceof Thing))");
		pStream.println(ind(3)+"to "+name()+" {");
369

370 371 372
		pStream.println(ind(3)+name()+" node = new "+name()+"();");
		pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
		pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
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
 		pStream.println(ind(3)+"node.setElementList(getElementList());");
// 		Restrictions restr = getRestrictions();
// 		pStream.println(ind(3)+"List l, remain;");
// 		pStream.println(ind(3)+"remain = new List();");
// 		for (int i=0; i<restr.getNumOwlRestriction(); i++) {
// 			OwlRestriction r = restr.getOwlRestriction(i);
// 			pStream.println(ind(3)+"l = new List();");
// 			pStream.println(ind(3)+"for (int i=0; i<getNumElement(); i++) {");
// 			pStream.println(ind(4)+"ComplexElement e = (ComplexElement) getElement(i);");
// 			pStream.println(ind(4)+"if (e.getNumElement() > 0 && e.getElement(0) instanceof "+r.name()+" ) {");
// 			pStream.println(ind(5)+"l.add(e.getElement(0));");
// 			pStream.println(ind(4)+"}");
// 			pStream.println(ind(3)+"}");
// 			if (r.allValuesFrom()) {
// 				pStream.println(ind(3)+"node.set"+r.name()+"List(l);");
// 			} else {
// 				pStream.println(ind(3)+r.name()+" c"+ix+" = new "+r.name()+"();");
// 				pStream.println(ind(3)+"node.set"+r.name()+"(c"+(ix++)+");");
// 			}
// 		}
// 		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);");
399 400 401
		pStream.println(ind(3)+"return node;");
		pStream.println(ind(2)+"}");
		pStream.println(ind(1)+"}");
402
		pStream.println();
403 404
		// Rearrange device nodes on the AST to get away from the
		// current diagonal style from Protege
Anders Nilsson's avatar
Anders Nilsson committed
405
		pStream.println(ind(1)+"rewrite "+name()+" {");
Anders Nilsson's avatar
Anders Nilsson committed
406
		pStream.println(ind(2)+"when (!isTopElement() && !(getAttribute(0) instanceof RdfId))");
Anders Nilsson's avatar
Anders Nilsson committed
407 408 409
		pStream.println(ind(2)+"to ClassUse {");
		pStream.println(ind(3)+"ClassUse use = new ClassUse();");
		pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(\""+name()+"\"));");
410
		pStream.println(ind(3)+"String id = getAttribute(0).getValue().getSTRING_LITERAL();");
Anders Nilsson's avatar
Anders Nilsson committed
411 412
// 		pStream.println(ind(3)+"Value val = new Value(\"\\\"#\"+id.substring(1,id.length()));");
		pStream.println(ind(3)+"Value val = new Value(id);");
413
		pStream.println(ind(3)+"use.setAttributeList(new List().add(new RdfResource(val)));");
Anders Nilsson's avatar
Anders Nilsson committed
414 415 416 417 418
		pStream.println(ind(3)+"use.setElementList(new List());");
		pStream.println(ind(3)+"getRdfDecl().addElement(this);");
		pStream.println(ind(3)+"return use;");
		pStream.println(ind(2)+"}");
		pStream.println(ind(1)+"}");
419
	}
Anders Nilsson's avatar
Anders Nilsson committed
420

421
}
422

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
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();
Anders Nilsson's avatar
Anders Nilsson committed
444 445

		// SimpleElement.value()
446
		pStream.println(ind(1)+"syn String SimpleElement.value() = \"\";");
447 448 449
		pStream.println(ind(1)+"eq StringElement.value() = getLITERAL();");
		pStream.println(ind(1)+"eq IntElement.value() = getLITERAL();");
		pStream.println(ind(1)+"eq FloatElement.value() = getLITERAL();");
450
		
Anders Nilsson's avatar
Anders Nilsson committed
451
		// Thing.isTopElement()
452
		pStream.println(ind(1)+"boolean Thing.isTopElement() {");
Anders Nilsson's avatar
Anders Nilsson committed
453
		pStream.println(ind(2)+"return getParent().getParent() instanceof RdfDeclaration;");
454 455
		pStream.println(ind(1)+"}");

Anders Nilsson's avatar
Anders Nilsson committed
456
		// ASTNode.getStart()
457 458 459 460 461 462 463
		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)+"}");
		
Anders Nilsson's avatar
Anders Nilsson committed
464
		// ASTNode.getRdfDecl()
Anders Nilsson's avatar
Anders Nilsson committed
465
		pStream.println(ind(1)+"RdfDeclaration ASTNode.getRdfDecl() {");
Anders Nilsson's avatar
Anders Nilsson committed
466 467 468 469 470 471
		pStream.println(ind(2)+"return getParent().getRdfDecl();");
		pStream.println(ind(1)+"}");
		pStream.println(ind(1)+"RdfDeclaration RdfDeclaration.getRdfDecl() {");
		pStream.println(ind(2)+"return this;");
		pStream.println(ind(1)+"}");
		
Anders Nilsson's avatar
Anders Nilsson committed
472
		// ComplexElement.name()
473
		pStream.println(ind(1)+"public String ComplexElement.name() {");
474 475
		pStream.println(ind(2)+"return getOwlIdentifier().getIDENTIFIER();");
		pStream.println(ind(1)+"}");
476
		pStream.println(ind(1)+"public String Attribute.name() {");
Anders Nilsson's avatar
Anders Nilsson committed
477 478
		pStream.println(ind(2)+"return getValue().getSTRING_LITERAL();");
		pStream.println(ind(1)+"}");
479 480 481 482 483 484 485 486 487 488
		pStream.println(ind(1)+"public String Attribute.value() {");
		pStream.println(ind(2)+"String s = getValue().getSTRING_LITERAL();");
		pStream.println(ind(2)+"int hash = s.indexOf('#');");
		pStream.println(ind(2)+"int firstCit = s.indexOf('\"');");
		pStream.println(ind(2)+"int lastCit = s.lastIndexOf('\"');");
		pStream.println(ind(2)+"hash = hash < 0 ? 0 : hash;");
		pStream.println(ind(2)+"firstCit = firstCit < 0 ? 0 : firstCit;");
		pStream.println(ind(2)+"lastCit = lastCit > firstCit ? lastCit : s.length()-1;");
		pStream.println(ind(2)+"return s.substring(Math.max(hash,firstCit)+1,lastCit);");
		pStream.println(ind(1)+"}");
489

Anders Nilsson's avatar
Anders Nilsson committed
490
		// ClassUse.decl()
491 492 493
// 		pStream.println(ind(1)+"syn lazy Thing ClassUse.decl() {");
// 		pStream.println(ind(2)+
// 		pStream.println(ind(1)+"}\n");
494

495 496 497 498 499
		super.genMisc(pStream);
		pStream.println("}");
	}

}
500 501 502 503 504 505 506 507

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("");
Anders Nilsson's avatar
Anders Nilsson committed
508
		pStream.println("import AST.OWLParser;");
509 510 511 512 513 514 515 516 517
		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 {");
Anders Nilsson's avatar
Anders Nilsson committed
518
		pStream.println("			OWLParser parser = new OWLParser(r);");
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
		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("}");
	}
}