From bb7e74ca81efa10e923f04abff3866ed93c845a0 Mon Sep 17 00:00:00 2001 From: Anders Nilsson <anders.nilsson@cs.lth.se> Date: Tue, 14 Feb 2006 14:03:38 +0100 Subject: [PATCH] Can now dump declared classes with their ancestors using DumpClasses.java --- CompilerGeneration.jrag | 6 +++++- DumpClasses.java | 16 ++++++++++++++ DumpClasses.jrag | 20 ++++++++++++++++++ Rewrites.jrag | 19 +++++++++++++++++ Types.jrag | 46 +++++++++++++++++++++++++++++++---------- 5 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 DumpClasses.java create mode 100644 DumpClasses.jrag diff --git a/CompilerGeneration.jrag b/CompilerGeneration.jrag index 3ab9fc7..369f11d 100644 --- a/CompilerGeneration.jrag +++ b/CompilerGeneration.jrag @@ -15,7 +15,11 @@ aspect CompilerGeneration { void OwlClassDecl.genAbsGrammar(PrintStream pStream) { pStream.print("OwlClassDecl: "); - pStream.println(getId()); + pStream.print(getId()); + if (getSuperClass() != null) { + pStream.print(" extends "); + pStream.println(getSuperClass().getId()); + } super.genAbsGrammar(pStream); } diff --git a/DumpClasses.java b/DumpClasses.java new file mode 100644 index 0000000..3e01c60 --- /dev/null +++ b/DumpClasses.java @@ -0,0 +1,16 @@ +// package programs; + +import AST.Start; + +public class DumpClasses extends Parser { + public static void main(String args[]) { + Start ast = parse(args); + + // Dump the AST +// ast.dumpTree(" ", System.out); + + // Should be generated to a file when sufficiently + // implemented. AndersN 060210 + ast.dumpClasses(System.out); + } +} \ No newline at end of file diff --git a/DumpClasses.jrag b/DumpClasses.jrag new file mode 100644 index 0000000..9c7fdf7 --- /dev/null +++ b/DumpClasses.jrag @@ -0,0 +1,20 @@ +/* -*-Java-*- */ + +import java.io.PrintStream; + +aspect DumpClasses { + void ASTNode.dumpClasses(PrintStream pStream) { + for (int i=0; i<getNumChild(); i++) { + getChild(i).dumpClasses(pStream); + } + } + + public void Start.dumpClasses(PrintStream pStream) { + super.dumpClasses(pStream); + } + + void OwlClassDecl.dumpClasses(PrintStream pStream) { + pStream.print(getId()); + pStream.println(" : "+getSuperClass().getId()); + } +} \ No newline at end of file diff --git a/Rewrites.jrag b/Rewrites.jrag index 906b52a..3c85ed2 100644 --- a/Rewrites.jrag +++ b/Rewrites.jrag @@ -56,6 +56,14 @@ aspect MiscUtilities { // well known return "_Unknown_"; } + + ComplexElement ComplexElement.getTopElement() { + ComplexElement e = this; + while (e != null && !(e instanceof RdfDeclaration)) { + e = (ComplexElement) e.getParent().getParent(); + } + return e; + } } aspect RewriteClasses { @@ -69,4 +77,15 @@ aspect RewriteClasses { return new OwlClassUse(new List(), getElementList(), getId()); } } + + rewrite RdfsSubClassOf { + when (getNumAttribute()>0 && getAttribute(0) instanceof RdfResource) + to RdfsSubClassOf { + Value value = getAttribute(0).getValue(); + OwlClass oc = new OwlClass(new List().add(new RdfAbout(value)), + new List()); + return new RdfsSubClassOf(new List(), + new List().add(oc)); + } + } } \ No newline at end of file diff --git a/Types.jrag b/Types.jrag index 3abda03..9902091 100644 --- a/Types.jrag +++ b/Types.jrag @@ -1,15 +1,39 @@ /* -*-Java-*- */ aspect Types { -// syn lazy Class Class.getSuperClass(); -// eq OwlClassDecl.getSuperClass() { -// for (int i=0; i<getNumElement(); i++) { -// if (getElement(i) instanceof RdfsSubClassOf) { -// return getElement(i); -// } -// } -// } -// eq OwlClassUse.getSuperClass() { -// return null; -// } + syn lazy Class Class.getSuperClass(); + eq OwlClassDecl.getSuperClass() { + for (int i=0; i<getNumElement(); i++) { + if (getElement(i) instanceof RdfsSubClassOf) { + RdfsSubClassOf e = (RdfsSubClassOf) getElement(i); + if (e.getElement(0) instanceof Class) { + return ((Class) e.getElement(0)).decl(); + } + } + } + // No super class found, return an owl Thing instead. + return new OwlClassDecl(new List(), new List(), "Thing"); + } + eq OwlClassUse.getSuperClass() { + return null; + } + + syn lazy OwlClassDecl Class.decl(); + eq OwlClassDecl.decl() = this; + eq OwlClassUse.decl() { + ComplexElement top = getTopElement(); + for (int i=0; i<top.getNumElement(); i++) { + if (getId().equals(((ComplexElement) top.getElement(i)).getId())) { + return (OwlClassDecl) top.getElement(i); + } + } + // OK, then let's see if this class is declared by w3.org + String id = getId(); + if (id.endsWith("owl#Thing")) { + return new OwlClassDecl(new List(), new List(), "Thing"); + } + // No decl found, so let's return null. Not sure if that's + // that the right thing to do though. + return null; + } } \ No newline at end of file -- GitLab