Skip to content

Commit

Permalink
(re)done: distribute, factor, downgrade/upgrade, horizontal/vertical;…
Browse files Browse the repository at this point in the history
… improved consistent use of replaceP

the differ improved (almost perfect now), split over several functions
  • Loading branch information
grammarware committed Jun 4, 2012
1 parent 8e4f3a7 commit 9a46d95
Show file tree
Hide file tree
Showing 4 changed files with 504 additions and 337 deletions.
4 changes: 2 additions & 2 deletions shared/rascal/src/Sync.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void main()
bl = readBGF(base+replaceLast(f,".xbgf",".baseline"));
//buffer += "test bool test_<replaceLast(f,".xbgf","")>() { return gdt(transform(<xbgf>,<bgf>),<bl>); }\n";
buffer += "\"<replaceLast(f,".xbgf","")>\": \<<xbgf>,<bgf>,<bl>\>,\n";
buffer2 += "test bool test_<replaceLast(f,".xbgf","")>() { \<xbgf,bgf1,bgf2\> = test_data[\"<replaceLast(f,".xbgf","")>\"]; return gdt(transform(xbgf,bgf1),bgf2); }\n";
buffer3 += "void show_<replaceLast(f,".xbgf","")>() { \<xbgf,bgf1,bgf2\> = test_data[\"<replaceLast(f,".xbgf","")>\"]; println(\"Input \<bgf1\>\");println(\"Transformations: \<xbgf\>\");println(\"Expected output \<bgf2\>\");println(\"Actual output \<transform(xbgf,bgf1)\>\"); }\n";
buffer2 += "test bool test_<replaceLast(f,".xbgf","")>() { \<xbgf,bgf1,bgf2\> = test_data[\"<replaceLast(f,".xbgf","")>\"]; return gdts(transform(xbgf,bgf1),bgf2); }\n";
buffer3 += "void show_<replaceLast(f,".xbgf","")>() { \<xbgf,bgf1,bgf2\> = test_data[\"<replaceLast(f,".xbgf","")>\"]; println(\"Input \<bgf1\>\");println(\"Transformations: \<xbgf\>\");println(\"Expected output \<bgf2\>\");bgf3=transform(xbgf,bgf1);println(\"Actual output \<bgf3\>\"); gdtv(bgf3,bgf2); }\n";
}
writeFile(|project://slps/src/transform/XBGFTest.rsc|, replaceLast(buffer,",","")+");\n\n"+buffer3+"\n\n"+buffer2);
}
Expand Down
70 changes: 64 additions & 6 deletions shared/rascal/src/diff/GDT.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,77 @@ import normal::BGF;
import List;
import IO;

public bool gdt(grammar(rs1,ps1), grammar(rs2,ps2))
// expression equality
public bool eqE(choice([BGFExpression e1]), choice([BGFExpression e2])) = eqE(e1,e2);
public bool eqE(choice(L1), choice(L2))
{
//println("We\'re at eqE with:\n <L1>\nvs\n <L2>...");
for (x <- L1, y <- L2)
if (eqE(x,y))
return eqE(choice(L1 - x), choice(L2 - y));
//println("Unmatched <L1> with <L2> :(");
return false;
}
public bool eqE(sequence(L1), sequence(L2))
{
if (size(L1) != size(L2)) return false;
for (i <- [0..size(L1)-1])
//if (choice(L3) := L1[i] && choice(L4) := L2[i])
if (!eqE(L1[i],L2[i])) return false;
return true;
}
public bool eqE(BGFExpression e1, BGFExpression e2) = e1 == e2; // default
public bool eqP(production(str l,str x, BGFExpression e1), production(l,x, BGFExpression e2)) = eqE(e1,e2);
public bool eqP(BGFExpression p1, BGFExpression p2) = p1 == p2;
// generic differ, returns unmatched production rules
tuple[list[BGFProduction],list[BGFProduction]] gdt(list[BGFProduction] ps1, list[BGFProduction] ps2)
{
ps3 = normalise(ps1);
ps4 = normalise(ps2);
if (toSet(rs1)!=toSet(rs2)) return false;
if (toSet(ps3)==toSet(ps4)) return true;
if (toSet(ps3)==toSet(ps4)) return <[],[]>;
unmatched1 = ps3 - ps4;
unmatched2 = ps4 - ps3;
for (u <- unmatched1)
if (production(l,x,choice(L1)) := u)
for (production(l,x,choice(L2)) <- unmatched2)
if (toSet(L1) == toSet(L2)) {unmatched2 -= production(l,x,choice(L2));unmatched1 -= u;break;}
if (production(str l,str x,BGFExpression e1) := u)
for (production(l,x,BGFExpression e2) <- unmatched2)
if (eqE(e1,e2))
{
unmatched2 -= production(l,x,e2);
unmatched1 -= u;
break;
}
return <unmatched1,unmatched2>;
}
// silent
public bool gdts(grammar(rs1,ps1), grammar(rs2,ps2))
{
if (toSet(rs1)!=toSet(rs2)) return false;
<unmatched1,unmatched2> = gdt(ps1,ps2);
if (isEmpty(unmatched1) && isEmpty(unmatched2)) return true;
// TODO keep trying?
return false;
}
// verbose
public bool gdtv(grammar(rs1,ps1), grammar(rs2,ps2))
{
if (toSet(rs1)!=toSet(rs2))
{
println("Different roots: <rs1> vs <rs2>.");
return false;
}
<unmatched1,unmatched2> = gdt(ps1,ps2);
if (isEmpty(unmatched1) && isEmpty(unmatched2)) return true;
println("Grammars differ!");
for (u <- unmatched1)
println(u);
println("vs");
for (u <- unmatched2)
println(u);
// TODO keep trying?
return false;
}
Loading

0 comments on commit 9a46d95

Please sign in to comment.