Select Git revision
CompilerGeneration.jrag
-
Administrator authoredAdministrator authored
CompilerGeneration.jrag 21.67 KiB
/* -*-Java-*- */
/*
* Copyright (C) 2006,2010 Anders Nilsson <anders.nilsson@control.lth.se>
*
* This file is part of OntologyCompiler.
*
* 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/>.
*/
import java.io.*;
import java.util.*;
aspect AbsGrammarGeneration {
void ASTNode.genAbsGrammar(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genAbsGrammar(pStream);
}
}
public void Start.genAbsGrammar(PrintStream pStream) {
pStream.println("// -*-Java-*- ");
pStream.println();
pStream.println("// Automatically generated JastAdd abstract grammar file ");
pStream.println("// Predefined types");
pStream.println("Start ::= Element*;");
pStream.println("abstract Thing : ComplexElement ::=;");
pStream.println("abstract Element;");
pStream.println("ComplexElement : Element ::= OwlIdentifier Attribute* Element*;");
pStream.println("ValueElement : ComplexElement;");
pStream.println("RdfDeclaration : ComplexElement;");
pStream.println("abstract SimpleElement : Element ::= <LITERAL>;");
pStream.println("StringElement : SimpleElement ::=;");
pStream.println("IntElement : SimpleElement ::=;");
pStream.println("FloatElement : SimpleElement ::=;");
pStream.println("ClassUse : Thing ::= /decl:Thing/;");
pStream.println("Attribute ::= Value;");
pStream.println("AbbreviatedIRI : Attribute ::= ;");
pStream.println("Cardinality : Attribute ::= ;");
pStream.println("DatatypeIRI : Attribute ::= ;");
pStream.println("IRI : Attribute ::= ;");
pStream.println("Name : Attribute ::= ;");
pStream.println("OntologyIRI : Attribute ::= ;");
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("XmlnsXsp : Attribute ::= ;");
pStream.println("XmlnsRdfs : Attribute ::= ;");
pStream.println("XmlnsOwl : Attribute ::= ;");
pStream.println("XmlnsSwrl : Attribute ::= ;");
pStream.println("XmlnsSwrlb : Attribute ::= ;");
pStream.println("XmlnsDaml : Attribute ::= ;");
pStream.println("XmlnsP1 : Attribute ::= ;");
pStream.println("XmlnsDc : Attribute ::= ;");
pStream.println("XmlnsExpr : Attribute ::= ;");
pStream.println("XmlnsList : 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>;");
pStream.println("OwlIdentifier ::= <IDENTIFIER>;");
pStream.println("AttrValue ::= <STRING_LITERAL>;");
pStream.println();
pStream.println("// Generated types");
super.genAbsGrammar(pStream);
}
void OwlClassDecl.genAbsGrammar(PrintStream pStream) {
pStream.print(getId());
pStream.print(" : "+getSuperClass().getId());
pStream.print(" ::=");
// getOwnRestrictions().genAbsGrammar(pStream);
getProperties().genAbsGrammar(pStream);
pStream.println(";");
// Properties p = getProperties();
// System.out.println(getId());
// for (int i=0; i<p.getNumProperty(); i++) {
// System.out.println(" "+((ComplexElement) p.getProperty(i)).getId());
// }
}
void ClassDeclaration.genAbsGrammar(PrintStream pStream) {
if (name().equals("Thing")) {
// Thing is handled explicitly
return;
}
System.out.println(name());
dumpTree(" ",System.out);
pStream.print(name());
pStream.print(" : "+getSuperClass().name());
pStream.print(" ::=");
getProperties().genAbsGrammar(pStream);
pStream.println(";");
}
void Properties.genAbsGrammar(PrintStream pStream) {
for (int i=0; i<getNumProperty(); i++) {
pStream.print(" ");
getProperty(i).genAbsGrammarEntry(pStream);
}
}
void OwlProperty.genAbsGrammarEntry(PrintStream pStream) {
pStream.print(name());
pStream.print(":Thing");
// if (allValuesFrom()) {
pStream.print("*");
// }
}
void Element.genAbsGrammarEntry(PrintStream pStream) {}
void ObjectPropertyDomain.genAbsGrammarEntry(PrintStream pStream) {
pStream.print(objectProperty().name());
pStream.print(":Thing*");
}
void DataPropertyDomain.genAbsGrammarEntry(PrintStream pStream) {
pStream.print("<"+dataProperty().name()+">");
// pStream.print(":String*");
}
void Restrictions.genAbsGrammar(PrintStream pStream) {
for (int i=0; i<getNumOwlRestriction(); i++) {
pStream.print(" ");
getOwlRestriction(i).genAbsGrammar(pStream);
}
}
void OwlRestriction.genAbsGrammar(PrintStream pStream) {
pStream.print(name());
pStream.print(":Thing");
if (allValuesFrom()) {
pStream.print("*");
}
}
String OwlRestriction.getRestrictionPropertyId() {
for (int i=0; i<getNumElement(); i++) {
ComplexElement e = (ComplexElement) getElement(i);
if (e instanceof OwlOnProperty) {
return e.getId();
}
}
return "_Unknown_";
}
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_";
}
syn lazy boolean OwlRestriction.allValuesFrom() {
for (int i=0; i<getNumElement(); i++) {
if (getElement(i) instanceof OwlAllValuesFrom) {
return true;
}
}
return false;
}
}
aspect GenAspects {
public void Start.genAspects(PrintStream pStream) {
pStream.println();
pStream.println("import java.io.PrintStream;");
pStream.println();
genRewrites(pStream);
genMisc(pStream);
genPrettyPrinter(pStream);
}
}
aspect GenPrettyPrinter {
public void ASTNode.genPrettyPrinter(PrintStream pStream) {
for (int i=0; i<getNumChild(); i++) {
getChild(i).genPrettyPrinter(pStream);
}
}
public void Start.genPrettyPrinter(PrintStream pStream) {
pStream.println();
pStream.println("aspect PrettyPrinter {");
pStream.println(" public void ASTNode.prettyPrint(String indent, PrintStream pStream) {");
pStream.println(" for (int i=0; i<getNumChild(); i++) {");
pStream.println(" getChild(i).prettyPrint(indent,pStream);");
pStream.println(" }");
pStream.println(" }\n");
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(" }");
pStream.println(" }\n");
pStream.println(" syn String Attribute.id() = \"\";");
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\";");
pStream.println(" eq XmlnsSwrl.id() = \"xmlns:swrl\";");
pStream.println(" eq XmlnsSwrlb.id() = \"xmlns:swrlb\";");
pStream.println(" eq XmlnsXsd.id() = \"xmlns:xsd\";");
pStream.println(" eq XmlnsXsp.id() = \"xmlns:xsp\";");
pStream.println(" public void Attribute.prettyPrint(String indent, PrintStream pStream) {");
pStream.println(" System.out.print(\" \"+id()+\"=\"+getValue().getSTRING_LITERAL());");
pStream.println(" }\n");
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");
// 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");
super.genPrettyPrinter(pStream);
pStream.println("}\n");
}
public void OwlClassDecl.genPrettyPrinter(PrintStream pStream) {
// Generate prettyprinter for class declaration
pStream.print(ind(1)+"public void "+name());
pStream.println(".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("System.out.println(\">\");");
pStream.println(ind(2)+"String childIndent = indent + \" \";");
// 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()+">\");");
// }
pStream.println(ind(2)+"for (int i=0; i<getNumElement(); i++) {");
pStream.println(ind(3)+"getElement(i).prettyPrint(childIndent,pStream);");
pStream.println(ind(2)+"}");
pStream.println(ind(1)+"System.out.println(indent+\"</"+name()+">\");");
pStream.println(ind(1)+"}\n");
}
}
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 {");
pStream.println(ind(1)+"rewrite ComplexElement {");
pStream.println(ind(2)+"when (!(this instanceof ClassUse) && 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)+"}");
// TEMPORARY!!!!!
// Rewrite isSkillOf/hasSkill/hasProperty/isPropertyOf to ClassUse.
// Should probably be auto-generated instead.
pStream.println(ind(1)+"rewrite ComplexElement {");
// 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)+"}");
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)+"}");
// 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)+"}");
pStream.println(ind(1)+"}");
super.genRewrites(pStream);
pStream.println("}");
}
public void OwlClassDecl.genRewrites(PrintStream pStream) {
String ind = " ";
int ix = 0;
// Specialize ComplexElements to the actual class in the ontology
pStream.println(ind(1)+"rewrite ComplexElement {");
pStream.println(ind(2)+"when (!(this instanceof ClassUse) && getOwlIdentifier().getIDENTIFIER().equals(\""+
name()+"\") && !(this instanceof Thing))");
pStream.println(ind(3)+"to "+name()+" {");
pStream.println(ind(3)+name()+" node = new "+name()+"();");
pStream.println(ind(3)+"node.setOwlIdentifier(getOwlIdentifier());");
pStream.println(ind(3)+"node.setAttributeList(getAttributeList());");
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);");
pStream.println(ind(3)+"return node;");
pStream.println(ind(2)+"}");
pStream.println(ind(1)+"}");
pStream.println();
// Rearrange device nodes on the AST to get away from the
// current diagonal style from Protege
pStream.println(ind(1)+"rewrite "+name()+" {");
pStream.println(ind(2)+"when (!isTopElement() && !(getAttribute(0) instanceof RdfId))");
pStream.println(ind(2)+"to ClassUse {");
pStream.println(ind(3)+"ClassUse use = new ClassUse();");
pStream.println(ind(3)+"use.setOwlIdentifier(new OwlIdentifier(\""+name()+"\"));");
pStream.println(ind(3)+"String id = getAttribute(0).getValue().getSTRING_LITERAL();");
// pStream.println(ind(3)+"Value val = new Value(\"\\\"#\"+id.substring(1,id.length()));");
pStream.println(ind(3)+"Value val = new Value(id);");
pStream.println(ind(3)+"use.setAttributeList(new List().add(new RdfResource(val)));");
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)+"}");
}
}
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();
// SimpleElement.value()
pStream.println(ind(1)+"syn String SimpleElement.value() = \"\";");
pStream.println(ind(1)+"eq StringElement.value() = getLITERAL();");
pStream.println(ind(1)+"eq IntElement.value() = getLITERAL();");
pStream.println(ind(1)+"eq FloatElement.value() = getLITERAL();");
// Thing.isTopElement()
pStream.println(ind(1)+"boolean Thing.isTopElement() {");
pStream.println(ind(2)+"return getParent().getParent() instanceof RdfDeclaration;");
pStream.println(ind(1)+"}");
// ASTNode.getStart()
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)+"}");
// ASTNode.getRdfDecl()
pStream.println(ind(1)+"RdfDeclaration ASTNode.getRdfDecl() {");
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)+"}");
// ComplexElement.name()
pStream.println(ind(1)+"public String ComplexElement.name() {");
pStream.println(ind(2)+"return getOwlIdentifier().getIDENTIFIER();");
pStream.println(ind(1)+"}");
pStream.println(ind(1)+"public String Attribute.name() {");
pStream.println(ind(2)+"return getValue().getSTRING_LITERAL();");
pStream.println(ind(1)+"}");
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)+"}");
// ClassUse.decl()
// pStream.println(ind(1)+"syn lazy Thing ClassUse.decl() {");
// pStream.println(ind(2)+
// pStream.println(ind(1)+"}\n");
super.genMisc(pStream);
pStream.println("}");
}
}
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.OWLParser;");
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(" OWLParser parser = new OWLParser(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("}");
}
}