Example #1
0
        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());
                }
        }
Example #2
0
        private static string Serialize(IEnumerable <INamedTypeReference> types)
        {
            using (var stringWriter = new StringWriter())
                using (var xmlWriter = new XmlTextWriter(stringWriter))
                {
                    var allReferencedTypes = new Dictionary <INamedTypeReference, int>();
                    var allDefinedTypes    = new Dictionary <INamedTypeDefinition, int>();
                    var visitedTypes       = new HashSet <INamedTypeDefinition>();
                    var newTypes           = new HashSet <INamedTypeDefinition>();

                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteStartElement("DirectedGraph");
                    xmlWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/vs/2009/dgml");
                    xmlWriter.WriteStartElement("Links");

                    foreach (var type in types)
                    {
                        allReferencedTypes.Add(type, allReferencedTypes.Count);

                        if (type is INamedTypeDefinition)
                        {
                            var namedType = type as INamedTypeDefinition;
                            allDefinedTypes.Add(namedType, allDefinedTypes.Count);
                            newTypes.Add(namedType);
                        }
                    }

                    while (newTypes.Count > 0)
                    {
                        var type = newTypes.First();
                        newTypes.Remove(type);
                        visitedTypes.Add(type);

                        var typeId   = allDefinedTypes[type];
                        var sourceId = Convert.ToString(typeId);
                        var targetId = string.Empty;

                        var fieldsByType = from f in type.Fields
                                           group f by f.Type into g
                                           select g;

                        foreach (var g in fieldsByType)
                        {
                            if (g.Key is INamedTypeReference)
                            {
                                var fieldTypeRef = g.Key as INamedTypeReference;

                                if (!allReferencedTypes.ContainsKey(fieldTypeRef))
                                {
                                    allReferencedTypes.Add(fieldTypeRef, allReferencedTypes.Count);
                                }

                                if (fieldTypeRef is INamedTypeDefinition)
                                {
                                    var fieldTypeDef = fieldTypeRef as INamedTypeDefinition;

                                    if (!allDefinedTypes.ContainsKey(fieldTypeDef))
                                    {
                                        allDefinedTypes.Add(fieldTypeDef, allDefinedTypes.Count);
                                    }

                                    typeId   = allDefinedTypes[fieldTypeDef];
                                    targetId = string.Format("d{0}", typeId);

                                    if (!visitedTypes.Contains(fieldTypeDef))
                                    {
                                        newTypes.Add(fieldTypeDef);
                                    }
                                }
                                else
                                {
                                    typeId   = allReferencedTypes[fieldTypeRef];
                                    targetId = string.Format("r{0}", typeId);
                                }

                                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("Nodes");

                    foreach (var entry in allReferencedTypes)
                    {
                        var typeId = Convert.ToString(entry.Value);
                        var label  = TypeHelper.GetTypeName(entry.Key);

                        xmlWriter.WriteStartElement("Node");
                        xmlWriter.WriteAttributeString("Id", typeId);
                        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());
                }
        }
Example #3
0
        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());
                }
        }