public static string Serialize(ControlFlowGraph cfg) { using (var stringWriter = new StringWriter()) using (var xmlWriter = new XmlTextWriter(stringWriter)) { xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartElement("DirectedGraph"); xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/vs/2009/dgml"); xmlWriter.WriteStartElement("Nodes"); foreach (var node in cfg.Nodes) { var nodeId = Convert.ToString(node.Id); var label = DGMLSerializer.Serialize(node); xmlWriter.WriteStartElement("Node"); xmlWriter.WriteAttributeString("Id", nodeId); xmlWriter.WriteAttributeString("Label", label); if (node.Kind == CFGNodeKind.Entry || node.Kind == CFGNodeKind.Exit) { xmlWriter.WriteAttributeString("Background", "Yellow"); } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Links"); foreach (var node in cfg.Nodes) { var sourceId = Convert.ToString(node.Id); foreach (var successor in node.Successors) { var targetId = Convert.ToString(successor.Id); xmlWriter.WriteStartElement("Link"); xmlWriter.WriteAttributeString("Source", sourceId); xmlWriter.WriteAttributeString("Target", targetId); xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Styles"); xmlWriter.WriteStartElement("Style"); xmlWriter.WriteAttributeString("TargetType", "Node"); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "FontFamily"); xmlWriter.WriteAttributeString("Value", "Consolas"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "NodeRadius"); xmlWriter.WriteAttributeString("Value", "5"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "MinWidth"); xmlWriter.WriteAttributeString("Value", "0"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Flush(); return(stringWriter.ToString()); } }
public static string Serialize(PointsToGraph ptg) { using (var stringWriter = new StringWriter()) using (var xmlWriter = new XmlTextWriter(stringWriter)) { xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartElement("DirectedGraph"); xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/vs/2009/dgml"); xmlWriter.WriteStartElement("Nodes"); foreach (var variable in ptg.Variables) { var label = variable.Name; xmlWriter.WriteStartElement("Node"); xmlWriter.WriteAttributeString("Id", label); xmlWriter.WriteAttributeString("Label", label); xmlWriter.WriteAttributeString("Shape", "None"); xmlWriter.WriteEndElement(); } foreach (var node in ptg.Nodes) { var nodeId = Convert.ToString(node.Id); var label = DGMLSerializer.Serialize(node); xmlWriter.WriteStartElement("Node"); xmlWriter.WriteAttributeString("Id", nodeId); xmlWriter.WriteAttributeString("Label", label); if (node.Kind == PTGNodeKind.Null) { xmlWriter.WriteAttributeString("Background", "Yellow"); } else if (node.Kind == PTGNodeKind.Unknown) { xmlWriter.WriteAttributeString("Background", "#FFB445"); xmlWriter.WriteAttributeString("StrokeDashArray", "6,6"); } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Links"); foreach (var node in ptg.Nodes) { var targetId = Convert.ToString(node.Id); foreach (var variable in node.Variables) { var sourceId = variable.Name; xmlWriter.WriteStartElement("Link"); xmlWriter.WriteAttributeString("Source", sourceId); xmlWriter.WriteAttributeString("Target", targetId); xmlWriter.WriteEndElement(); } var fieldsBySource = from e in node.Sources from s in e.Value group e.Key by s into g select g; foreach (var g in fieldsBySource) { var sourceId = Convert.ToString(g.Key.Id); var label = DGMLSerializer.GetLabel(g); xmlWriter.WriteStartElement("Link"); xmlWriter.WriteAttributeString("Source", sourceId); xmlWriter.WriteAttributeString("Target", targetId); xmlWriter.WriteAttributeString("Label", label); xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Styles"); xmlWriter.WriteStartElement("Style"); xmlWriter.WriteAttributeString("TargetType", "Node"); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "FontFamily"); xmlWriter.WriteAttributeString("Value", "Consolas"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "NodeRadius"); xmlWriter.WriteAttributeString("Value", "5"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Setter"); xmlWriter.WriteAttributeString("Property", "MinWidth"); xmlWriter.WriteAttributeString("Value", "0"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Flush(); return(stringWriter.ToString()); } }