diff --git a/lib/arch.js b/lib/arch.js index 7b0dd1b..bac6711 100644 --- a/lib/arch.js +++ b/lib/arch.js @@ -360,6 +360,18 @@ module.exports = INHERIT({ }, this).join('') }, + /** + * Dump this arch to Graphviz string for debug purposes. + * + * @returns {String} Graphviz string representation of this arch. + */ + toGraphviz: function() { + return 'digraph G {\n' + + this.findRoots().map(function(r) { + return this.nodeToGraphviz(r) + }, this).join('') + '}\n'; + }, + /** * Dump node with its children to string. * @@ -373,6 +385,27 @@ module.exports = INHERIT({ this.children[id].map(function(c) { return spaces + this.nodeToString(c, spaces + ' ') }, this).join('') + }, + + /** + * Dump node with its children to Graphviz string. + * + * @param {String} id Node ID to dump. + */ + nodeToGraphviz: function(id) { + var _this = this, + thisNode = '"' + id + '"', + s = ' ' + thisNode + ';\n', + children = this.children[id] || []; + + children.forEach(function(child) { + s += ' ' + thisNode + ' -> "' + child + '";\n'; + }); + children.forEach(function(child) { + s += _this.nodeToGraphviz(child); + }); + + return s; } });