...
The result should look something like this (I told you it was going to be hideous):
Visualizing Transitions
Now that we have our states, it's time to visualize transitions as well. It would be nice to just transform transitions leaving a given state while we're transforming that state. Let's do that:
Add the following code right before the
return
statement in the state transformation method:Code Block language java linenumbers true stateNode.outgoingEdges.addAll(state.outgoingTransitions.map[ t | transform(t) ])
Of course, we will have to add a
transform
method for transitions now:Code Block private def KEdge transform(Transition trans) { val transEdge = trans.createEdge().associateWith(trans) return transEdge; }
Again, we need to tell KLighD how to render the label:
Code Block language java linenumbers true transEdge.addPolyline(2).addHeadArrowDecorator();
And the edge also needs a label that describes the transition:
Code Block language java linenumbers true // Add a label to the edge val label = KimlUtil.createInitializedLabel(transEdge); val labelText = trans.trigger + " / " + trans.action + " " + trans.direction + " " + trans.newChar; label.configureCenterEdgeLabel(labelText, KlighdConstants.DEFAULT_FONT_SIZE, KlighdConstants.DEFAULT_FONT_NAME);
What is missing now is to set the edge target to the node that represents the target state. There's a problem here, though: we don't even know if the target state already had a node created for it. Thus, we need to make sure it has:
Code Block language java linenumbers true transEdge.target = transform(trans.targetState);
This, however, results in a different problem: if the target state already had a node created for it, we now create another node. What we would need the state transformation method to do would be to first check if a node has already been created for a given state, and, if so, return that instead of creating a new one. It turns out that Xtend already supports this pattern. Change the method's declaration and its first line to the following:
Code Block language java linenumbers true private def create stateNode : state.createNode() transform(State state) { stateNode.associateWith(state);
Also, remove the return statement. Xtend now checks if we already created a node for the given state and, if not, execute the code in the method.
Start your program again, open your Turing machine file and add an outgoing transition (with pretty much arbitrary properties) to your Wilhelm Tell state. Set the Apple state as the transition's target state. Save the model and fire up a KLighD view. It should look something like this: