/// <summary> /// Generates the diagram. /// </summary> /// <param name="graph">The graph.</param> /// <param name="parentNode">The parent node.</param> /// <returns>IList<Node>.</returns> internal IList<Node> GenerateDiagram(Graph graph, Node parentNode) { var text = string.IsNullOrWhiteSpace(Description) ? Expression : Description; var node = new Node { Text = text, Category = "Decision", Level = parentNode.Level + 1 }; graph.Nodes.Add(node); graph.Links.Add(new Link { From = parentNode, To = node }); foreach (var trueAction in TrueActions) { var nodes = trueAction.GenerateDiagram(graph, node); foreach (var childNode in nodes) { var link = graph.Links.FirstOrDefault(l => l.From == node && l.To == childNode); if (link != null) link.Text = "Yes"; } } foreach (var falseAction in FalseActions) { var nodes = falseAction.GenerateDiagram(graph, node); foreach (var childNode in nodes) { var link = graph.Links.FirstOrDefault(l => l.From == node && l.To == childNode); if (link != null) link.Text = "No"; } } return new List<Node> { node }; }
/// <summary> /// Generates the diagram JSon. /// </summary> /// <returns>GoJS formatted JSon with nodes and links</returns> public string GenerateDiagram() { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); var graph = new Graph(); var node = new Node { Text = string.IsNullOrWhiteSpace(Name) ? "Start" : Name, Category = "Start", Level = 0 }; graph.Nodes.Add(node); foreach (var rule in Rules) { rule.GenerateDiagram(graph, node); } var terminateNode = new Node { Category = "End", Text = "End" }; graph.Nodes.Add(terminateNode); var maxLevel = 0; foreach (var n in graph.Nodes) { if (n.IsTerminal) graph.Links.Add(new Link { From = n, To = terminateNode }); maxLevel = Math.Max(n.Level, maxLevel); } terminateNode.Level = maxLevel + 1; using (JsonWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; writer.WriteStartObject(); writer.WritePropertyName("class"); writer.WriteValue("go.GraphLinksModel"); writer.WritePropertyName("nodeDataArray"); writer.WriteStartArray(); var key = 0; foreach (var n in graph.Nodes) { writer.WriteStartObject(); writer.WritePropertyName("key"); writer.WriteValue(--key); n.Key = key; if (n.Figure != null) { writer.WritePropertyName("figure"); writer.WriteValue(n.Figure); } if (!string.IsNullOrWhiteSpace(n.Category)) { writer.WritePropertyName("category"); writer.WriteValue(n.Category); } if (n.EscalationLevel > 0) { writer.WritePropertyName("escalationLevel"); writer.WriteValue(n.EscalationLevel.ToString()); } if (!string.IsNullOrWhiteSpace(n.Source)) { writer.WritePropertyName("source"); writer.WriteValue(n.Source); } writer.WritePropertyName("text"); writer.WriteValue(n.Text); writer.WriteEndObject(); } writer.WriteEndArray(); writer.WritePropertyName("linkDataArray"); writer.WriteStartArray(); foreach (var link in graph.Links) { writer.WriteStartObject(); writer.WritePropertyName("from"); writer.WriteValue(link.From.Key); writer.WritePropertyName("to"); writer.WriteValue(link.To.Key); if (!string.IsNullOrWhiteSpace(link.Category)) { writer.WritePropertyName("category"); writer.WriteValue(link.Category); } var text = string.IsNullOrWhiteSpace(link.Text) ? string.Empty : link.Text; writer.WritePropertyName("text"); writer.WriteValue(text); writer.WriteEndObject(); } writer.WriteEndArray(); writer.WriteEndObject(); } return sb.ToString(); }