         }
     }
 }
+
+
+/// Helper function used when generating DOT digraph strings.
+private func label<T: DOTLabel>(x: T) -> String {
+    return x.DOTLabel
+}

        for fromState in states {
            for event in events {
                if let (toState, _) = transitionLogic(fromState, event) {
                    let fromIndex = stateIndexesByLabel[fromState.DOTLabel]!
                    let toIndex = stateIndexesByLabel[toState.DOTLabel]!

                    digraph += "    \(fromIndex) -> \(toIndex) [label=\"\(event.DOTLabel)\"]\n"
                }
            }
        }

    }

    private static func DOTDigraphGivenTransitionLogic(transitionLogic: (State, Event) -> (State, (Subject -> ())?)?, initialState: State, states: [State], events: [Event]) -> String {
        var stateIndexesByLabel: [String: Int] = [:]
        for (i, state) in enumerate(states) {
            stateIndexesByLabel[label(state)] = i + 1
        }

        func index(state: State) -> Int {
            return stateIndexesByLabel[label(state)]!
        }

        var digraph = "digraph {\n    graph [rankdir=LR]\n\n    0 [label=\"\", shape=plaintext]\n    0 -> \(index(initialState)) [label=\"START\"]\n\n"

        for state in states {
            digraph += "    \(index(state)) [label=\"\(label(state))\"]\n"
        }

        digraph += "\n"
