Skip to content

Instantly share code, notes, and snippets.

@jurgenvinju
Last active February 16, 2017 06:32
Show Gist options
  • Save jurgenvinju/8972255 to your computer and use it in GitHub Desktop.
Save jurgenvinju/8972255 to your computer and use it in GitHub Desktop.
Example code to compute flow graphs for Java and some basic UML diagramming for TU Eindhoven students
module FlowGraphsAndClassDiagrams
import analysis::flow::ObjectFlow;
import lang::java::flow::JavaToObjectFlow;
import List;
import Relation;
import lang::java::m3::Core;
import IO;
import vis::Figure;
import vis::Render;
alias OFG = rel[loc from, loc to];
OFG buildGraph(FlowProgram p)
= { <as[i], fps[i]> | newAssign(x, cl, c, as) <- p.statements, constructor(c, fps) <- p.decls, i <- index(as) }
+ { <cl + "this", x> | newAssign(x, cl, _, _) <- p.statements }
/* + ... etc */
;
OFG prop(OFG g, rel[loc,loc] gen, rel[loc,loc] kill, bool back) {
OFG IN = { };
OFG OUT = gen + (IN - kill);
gi = g<to,from>;
set[loc] pred(loc n) = gi[n];
set[loc] succ(loc n) = g[n];
solve (IN, OUT) {
IN = { <n,\o> | n <- carrier(g), p <- (back ? pred(n) : succ(n)), \o <- OUT[p] };
OUT = gen + (IN - kill);
}
return OUT;
}
public void drawDiagram(M3 m) {
classFigures = [box(text("<cl.path[1..]>"), id("<cl>")) | cl <- classes(m)];
edges = [edge("<to>", "<from>") | <from,to> <- m@extends ];
render(scrollable(graph(classFigures, edges, hint("layered"), std(gap(10)), std(font("Bitstream Vera Sans")), std(fontSize(20)))));
}
public str dotDiagram(M3 m) {
return "digraph classes {
' fontname = \"Bitstream Vera Sans\"
' fontsize = 8
' node [ fontname = \"Bitstream Vera Sans\" fontsize = 8 shape = \"record\" ]
' edge [ fontname = \"Bitstream Vera Sans\" fontsize = 8 ]
'
' <for (cl <- classes(m)) { /* a for loop in a string template, just like PHP */>
' \"N<cl>\" [label=\"{<cl.path[1..] /* a Rascal expression between < > brackets is spliced into the string */>||}\"]
' <} /* this is the end of the for loop */>
'
' <for (<from, to> <- m@extends) {>
' \"N<to>\" -\> \"N<from>\" [arrowhead=\"empty\"]<}>
'}";
}
public void showDot(M3 m) = showDot(m, |home:///<m.id.authority>.dot|);
public void showDot(M3 m, loc out) {
writeFile(out, dotDiagram(m));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment