public
Created

  • Download Gist
spring-config-graph-by-props.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
using System;
using System.Collections.Generic;
using System.Linq;
using QuickGraph;
using QuickGraph.Algorithms;
using QuickGraph.Graphviz;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
using Spring.Objects.Factory.Xml;
 
namespace q7446068
{
internal class Program
{
private static void Main(string[] args)
{
var configFile = "circular-reference.xml";
IApplicationContext ctx = new XmlApplicationContext(configFile);
 
var myRegistry = GetRegistry(configFile);
 
var graph = GetAdjacencyGraph(myRegistry);
 
var output = GetDotRepresentation(graph);
 
Console.WriteLine("Created dot file: " + output);
if(!graph.IsDirectedAcyclicGraph())
Console.WriteLine("Warning: the configuration contains circular references.");
 
Console.ReadLine();
}
 
private static string GetDotRepresentation(AdjacencyGraph<string, Edge<string>> graph)
{
var graphviz = new GraphvizAlgorithm<string, Edge<string>>(graph);
graphviz.FormatVertex += graphviz_FormatVertex;
var output = graphviz.Generate(new FileDotEngine(), "graph");
return output;
}
 
static void graphviz_FormatVertex(object sender, FormatVertexEventArgs<string> e)
{
e.VertexFormatter.Label = e.Vertex;
}
 
private static AdjacencyGraph<string, Edge<string>> GetAdjacencyGraph(DictionaryRegistry myRegistry)
{
var graph = new QuickGraph.AdjacencyGraph<string, Edge<string>>();
 
foreach (var definition in myRegistry.Definitions.Keys)
graph.AddVertex(definition);
 
foreach (var defId in graph.Vertices)
{
var def = myRegistry.GetObjectDefinition(defId);
foreach (PropertyValue pv in def.PropertyValues.PropertyValues)
{
var objRef = pv.Value as RuntimeObjectReference;
if (objRef == null)
continue;
 
graph.AddEdge(new Edge<string>(defId, objRef.ObjectName));
}
}
 
return graph;
}
 
private static DictionaryRegistry GetRegistry(string configFile)
{
var myRegistry = new DictionaryRegistry();
var rdr = new XmlObjectDefinitionReader(myRegistry);
rdr.LoadObjectDefinitions(configFile);
return myRegistry;
}
}
 
public class ClassA : IObjectNameAware
{
public ClassA MyOtherA { get; set; }
public string ObjectName { get; set; }
}
 
public class DictionaryRegistry : IObjectDefinitionRegistry
{
private readonly Dictionary<string, IObjectDefinition> _definitions = new Dictionary<string, IObjectDefinition>();
public Dictionary<string, IObjectDefinition> Definitions
{
get { return _definitions; }
}
 
#region Implementation of IObjectDefinitionRegistry
 
public bool IsObjectNameInUse(string objectName)
{
return Definitions.ContainsKey(objectName);
}
 
public string[] GetObjectDefinitionNames()
{
return Definitions.Keys.ToArray();
}
 
public bool ContainsObjectDefinition(string name)
{
return Definitions.ContainsKey(name);
}
 
public IObjectDefinition GetObjectDefinition(string name)
{
return Definitions[name];
}
 
public void RegisterObjectDefinition(string name, IObjectDefinition definition)
{
Definitions.Add(name, definition);
}
 
public string[] GetAliases(string name)
{
throw new NotImplementedException();
}
 
public void RegisterAlias(string name, string theAlias)
{
throw new NotImplementedException();
}
 
public int ObjectDefinitionCount
{
get { return Definitions.Count; }
}
 
#endregion
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.