net.ericaro.diezel.core.builder.DiezelImplementationBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of diezel-maven-plugin Show documentation
Show all versions of diezel-maven-plugin Show documentation
An Embedded Domain Specific Language Parser Generator PLugin compiler
The newest version!
package net.ericaro.diezel.core.builder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.util.Graphs;
/** A Diezel Implementation provides implementations code for a Diezel Language
*
* @author eric
*
*/
public class DiezelImplementationBuilder implements DiezelBuilder {
DiezelLanguage language;
String packageName;
List transitions = new ArrayList();
DirectedSparseMultigraph graph;
private String guideName;
private String languageName;
private StateImplementation start;
private String extendClass;
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public void setImplements(String str) {
this.languageName = str;
}
public void setExtends(String extendClass){
this.extendClass = extendClass ;
}
public void setGuideName(String str) {
this.guideName = str;
}
public void addTransitionImplementation(TransitionImplementation impl){
this.transitions.add(impl);
}
public String getImplements() {
return languageName;
}
public void setLanguage(DiezelLanguage language) {
this.language = language;
}
public DiezelImplementation build(){
buildGraph();
DiezelImplementation impl = new DiezelImplementation();
impl.language = language;
impl.graph = Graphs.unmodifiableGraph(graph);
impl.transitions = Collections.unmodifiableList(transitions);
impl.packageName = packageName;
impl.start = start;
impl.extendClass = extendClass;
impl.guideName = guideName;
return impl;
}
/** creates a derivative graph
*
*/
private void buildGraph(){
DirectedGraph pg = language.getGraph();
graph = new DirectedSparseMultigraph(); // the new graph
// build an alias map
Map transitionMap = new HashMap();
for (TransitionImplementation ti : transitions)
transitionMap.put(ti.getAlias(), ti);
// build a map for st to st impl, and add st impl into the graph
Map stateMap = new HashMap();
for( State st: pg.getVertices()){
StateImplementation impl = new StateImplementation(graph, st, guideName);
stateMap.put(st, impl);
graph.addVertex(impl);
if (st == language.start)
start = impl;
}
for( TransitionInstance ti: pg.getEdges()){
// get the three data : edge, src, and dest edges
TransitionImplementation impl = transitionMap.get(ti.getAlias() );
State src = pg.getSource(ti);
State dest = pg.getDest(ti);
// add a new edge
StateImplementation srcImpl =stateMap.get(src);
StateImplementation destImpl = stateMap.get(dest);
graph.addEdge(new TransitionImplementationInstance(graph, impl, ti), srcImpl, destImpl );
}
}
}