Skip to content

Commit

Permalink
fixed the O/X mapping megamodel; implemented some validity/well-forme…
Browse files Browse the repository at this point in the history
…dness constraints
  • Loading branch information
grammarware committed Oct 20, 2012
1 parent c4a818f commit 3769ce1
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 25 deletions.
3 changes: 0 additions & 3 deletions topics/mega/examples/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion topics/mega/lib/ox/.gitignore
@@ -1,3 +1,3 @@
*.back.megal
*.back
*.dot
*.pdf
2 changes: 0 additions & 2 deletions topics/mega/lib/ox/configuration.megal
Expand Up @@ -2,8 +2,6 @@

megamodel capabilities/Object_XML_mapping/configuration .

include capabilities/Object_XML_mapping/values .

variable Language OxConfiguration .
variable File config elementOf OxConfiguration .

Expand Down
2 changes: 0 additions & 2 deletions topics/mega/lib/ox/deserialization.megal
Expand Up @@ -2,8 +2,6 @@

megamodel capabilities/Object_XML_mapping/deserialization .

include capabilities/Object_XML_mapping/values .

local Function deserialize : XML -> ObjectGraphs .
variable File xmlInputDoc elementOf XML .
variable ObjectGraph intialObj elementOf ObjectGraphs .
Expand Down
9 changes: 9 additions & 0 deletions topics/mega/lib/ox/root.megal
@@ -0,0 +1,9 @@
{- include ALL the megamodels! -}

megamodel capabilities/Object_XML_mapping/root .

include capabilities/Object_XML_mapping/annotation .
include capabilities/Object_XML_mapping/configuration .
include capabilities/Object_XML_mapping/generation .
include capabilities/Object_XML_mapping/read .
include capabilities/Object_XML_mapping/write .
2 changes: 0 additions & 2 deletions topics/mega/lib/ox/serialization.megal
Expand Up @@ -2,8 +2,6 @@

megamodel capabilities/Object_XML_mapping/serialization .

include capabilities/Object_XML_mapping/values .

local Function serialize : ObjectGraphs -> XML .
variable File xmlOutputDoc elementOf XML .
variable ObjectGraph finalObj elementOf ObjectGraphs .
Expand Down
2 changes: 1 addition & 1 deletion topics/mega/src/MegaParser.rsc
Expand Up @@ -18,7 +18,7 @@ public void main(list[str] args)
println("Getting an abstract syntax tree of <f>...");
AMegaModel ast = readAST(base+f);
println("Serialising an abstract syntax tree of <f>...");
writeAST(base+(f+".back.megal"),ast);
writeAST(base+(f+".back"),ast);
println("Generating a Graphviz digraph for <f>...");
writeDot(base+(f+".dot"),ast);
}
Expand Down
10 changes: 5 additions & 5 deletions topics/mega/src/backend/MegaImplode.rsc
Expand Up @@ -5,16 +5,16 @@ import structure::MegaADT;
import structure::MegaGrammar;
import String;

AMegaModel mapmegal((MegaModel)`<MegaHeader h><MegaInclude* ins><MegaDecl+ ds>`) = makemegal("<h.name>","",ins,ds);
AMegaModel mapmegal((MegaModel)`<MegaDesc d><MegaHeader h><MegaInclude* ins><MegaDecl+ ds>`) = makemegal("<h.name>",trim("<d.s>"),ins,ds);
AMegaModel mapmegal((MegaModel)`<MegaHeader h><MegaInclude* ins><MegaDecl* ds>`) = makemegal("<h.name>","",ins,ds);
AMegaModel mapmegal((MegaModel)`<MegaDesc d><MegaHeader h><MegaInclude* ins><MegaDecl* ds>`) = makemegal("<h.name>",trim("<d.s>"),ins,ds);
default AMegaModel mapmegal(MegaModel m) = megamodel("","",[],[],[]);

AMegaModel makemegal(str name, str desc, MegaInclude* ins, MegaDecl+ ds)
AMegaModel makemegal(str name, str desc, MegaInclude* ins, MegaDecl* ds)
= megamodel(name,desc,collectIncludes(ins),collectDecls(ds),collectRels(ds));

list[str] collectIncludes(MegaInclude* ins) = ["<i.name>" | MegaInclude i <- ins];
list[MegaDeclaration] collectDecls(MegaDecl+ ds) = [*mapdecl(d) | MegaDecl d <- ds];
list[MegaRelation] collectRels(MegaDecl+ ds) = [*maprel(d) | MegaDecl d <- ds];
list[MegaDeclaration] collectDecls(MegaDecl* ds) = [*mapdecl(d) | MegaDecl d <- ds];
list[MegaRelation] collectRels(MegaDecl* ds) = [*maprel(d) | MegaDecl d <- ds];

list[MegaDeclaration] mapdecl((MegaDecl)`<MegaModifier? mm><MegaEntity e><MegaDot d>`) = [map1decl(e,mapmod(mm),"<e.id>",getComment(d))];
//list[MegaDeclaration] mapdecl((MegaDecl)`<MegaModifier? mm><MegaEntity e><MegaDot d>`) = [map2decl(mapmod(mm),"<e.id>")];
Expand Down
18 changes: 18 additions & 0 deletions topics/mega/src/backend/MegaManipulate.rsc
@@ -0,0 +1,18 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module backend::MegaManipulate

import structure::MegaADT;

import List;
import Set;

public MegaDeclaration getByName(str name, AMegaModel m)
{
list[MegaDeclaration] mine = getAllByName(name,m);
if(size(mine)==1) return mine[0];
else throw "Declaration for <name> not found or not unique: <mine>";
}

public list[MegaDeclaration] getAllByName(str name, AMegaModel m) = [d | d <- m.decls, d.id == name];

public list[&T] mergeLists(list[&T] xs, list[&T] ys) = toList(toSet(xs)+toSet(ys));
27 changes: 19 additions & 8 deletions topics/mega/src/io/MegaL.rsc
Expand Up @@ -8,10 +8,12 @@ import backend::GraphML2MegaL;
import backend::MegaImplode;
import backend::MegaExport;
import backend::MegaDot;
import backend::MegaManipulate;

import ParseTree;
import String;
import List;
import Set;
import IO;
import Exception;

Expand All @@ -30,17 +32,26 @@ public AMegaModel readYEd(loc f) = backend::GraphML2MegaL::readGraphML(f);

public AMegaModel readFlat(loc f)
{
AMegaModel a = readAST(f);
list[str] includes = a.incs;
AMegaModel a = readAST(f), b;
list[str] includes = a.incs, done = [];
a.incs = [];
while(!isEmpty(includes))
while(!isEmpty(includes - done))
for (i <- includes)
{
//try
b = readAST(f.parent+(substring(i,findLast(i,"/")+1)+".megal"));
//catch:
// continue;
a = megamodel(a.name,a.desc,[],a.decls + b.decls, a.rels + b.rels);
name = f.parent+(substring(i,findLast(i,"/")+1)+".megal");
if (name in done) continue;
try
{
b = readAST(name);
done += i;
}
catch IO:
{
println("Cannot find <name>");
done += i;
continue;
}
a = megamodel(a.name,a.desc,[],mergeLists(a.decls,b.decls), mergeLists(a.rels, b.rels));
includes += b.incs;
}
//println(includes);
Expand Down
29 changes: 29 additions & 0 deletions topics/mega/src/structure/MegaConstraints.rsc
@@ -0,0 +1,29 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module structure::MegaConstraints

import structure::MegaADT;
import backend::MegaManipulate;

import io::MegaL;
import IO; // println
import List; // size

public void main()
{
AMegaModel m = readFlat(|project://megal/lib/ox/root.megal|);
println(m);
println({*{r.x,r.y} | r <- m.rels});
for (n <- {*{r.x,r.y} | r <- m.rels})
println("<n> ::= <getAllByName(n,m)>;");
println(areAllDefined(m));
println(allAmbiguous(m));
println(allUndefined(m));
}

public bool isValid(AMegaModel m) = areAllDefined(m);

public bool areAllDefined(AMegaModel m) = (true | it && size(getAllByName(name,m))==1 | name <- {*{r.x,r.y} | r <- m.rels});

public list[str] allUndefined(AMegaModel m) = ([] | size(getAllByName(name,m))==0?(it+name):it | name <- {*{r.x,r.y} | r <- m.rels});

public list[str] allAmbiguous(AMegaModel m) = ([] | size(getAllByName(name,m))>1?(it+name):it | name <- {*{r.x,r.y} | r <- m.rels});
2 changes: 1 addition & 1 deletion topics/mega/src/structure/MegaGrammar.rsc
@@ -1,7 +1,7 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module structure::MegaGrammar

start syntax MegaModel = MegaDesc? MegaHeader MegaInclude* MegaDecl+;
start syntax MegaModel = MegaDesc? MegaHeader MegaInclude* MegaDecl*;
syntax MegaHeader = "megamodel" MegaURI name MegaDot;
syntax MegaURI = {ID "/"}+;
syntax MegaInclude = "include" MegaURI name MegaDot;
Expand Down

0 comments on commit 3769ce1

Please sign in to comment.