package urban;
import static urban.parser.UrbanParser.parse;
import java.io.File;
import java.io.StringWriter;
import java.util.Map.Entry;
import java.util.Set;
import org.antlr.runtime.RecognitionException;
import org.apache.commons.collections15.Transformer;
import urban.model.Agent;
import urban.model.Model;
import urban.model.Site;
import urban.model.info.ModelInfo;
import urban.transformers.ResolveImports;
import urban.transformers.ShapeToRuleTransformer;
/**
* Urban to diagram transformer
*/
public class UrbanToDotTransformer implements Transformer<String, String> {
private final File baseDir;
/**
* Constructor. Base directory for resolving imports is the current directory.
*/
public UrbanToDotTransformer() {
this.baseDir = null;
}
/**
* Constructor. Base directory for resolving imports is the directory given.
* @param base
*/
public UrbanToDotTransformer(File base) {
this.baseDir = base;
}
@Override
public String transform(String text) {
Model m;
try {
m = parse(text+"\n").model();
m = new ResolveImports(baseDir).transform(m);
m = new ShapeToRuleTransformer().transform(m);
StringWriter out = new StringWriter();
out.write("graph G {\noverlap=false;\nratio=fill;\n");
final ModelInfo modelInfo = new ModelInfo(m.getLines());
for(Agent a : modelInfo.getAgents()){
out.write(a.getName() + "[fontsize=24,width=1.6,height=1.6]\n");
for(Site s : a.getSites()){
if (s.getState() == null){
out.write(s.getAgent() + " -- " + s.getQualifiedName() +" []\n");
out.write(s.getQualifiedName() + "[label="+s.getName()+",fontsize=12,width=0.2,height=0.2]\n");
}
else {
out.write(s.getAgent() + " -- " + s.getQualifiedName() +" []\n");
out.write(s.getQualifiedName() + "[label=\""+s.getName()+"~"+s.getState()+"\",fillcolor=green, shape=triangle, style=filled,fontsize=12]\n");
}
}
}
for(Entry<String, Set<String>> b : modelInfo.getBindings().entrySet()){
for (String s : b.getValue()){
out.write(b.getKey() + "--" + s +"[color=black, dir=none, label=\":\", labelangle=45, labeldistance=1, labelfontsize=6, style=solid]\n");
}
}
out.write("}\n");
return out.toString();
} catch (RecognitionException e) {
e.printStackTrace();
}
return null;
}
}