/// <summary> /// for a given node returns all the child nodes /// </summary> /// <param name="nodeValue"></param> /// <param name="parentNodePath"></param> /// <returns></returns> public static List <Tuple <object, GraphPath> > GetChildTraversalNodes(object nodeValue, GraphPath parentNodePath, Func <object, GraphPath, bool> doNotTraverseFilter = null) { List <Tuple <object, GraphPath> > rv = new List <Tuple <object, GraphPath> >(); //if the node is IEnumerable, recurse here if (nodeValue is IEnumerable && (nodeValue is string) == false) { IEnumerable objEnumerable = nodeValue as IEnumerable; int index = 0; foreach (var each in objEnumerable) { //build the path var path = GraphPath.New(parentNodePath); path.AddSegment(EnumeratedItemSegment.New(index)); if (doNotTraverseFilter != null && doNotTraverseFilter(each, path)) { continue; } rv.Add(new Tuple <object, GraphPath>(each, path)); index++; } } else { //recurse the fields var fields = GraphingUtil.GetNestingNotatedFieldInfos(nodeValue.GetType()); foreach (var field in fields) { //get field value var obj = field.Item2.GetValue(nodeValue); var path = GraphPath.New(parentNodePath); path.AddSegment(GraphSegment.New(field.Item1)); if (doNotTraverseFilter != null && doNotTraverseFilter(obj, path)) { continue; } //build the node and recurse rv.Add(new Tuple <object, GraphPath>(obj, path)); } } return(rv); }
public GraphingTest() : base(LogicOf <Nothing> .New((x) => { //build an object up var id = "id".BuildAsId(); var guid = Guid.NewGuid(); var hasGuid = id.HasGUID(guid); var now = DateTime.Now; var hasDateCreated = hasGuid.HasDateCreated(now); var lastTouchedDate = DateTime.Now; var hasLastTouched = hasDateCreated.HasDateLastTouched(lastTouchedDate); var localMachineName = NetUtil.GetLocalMachineName(); var hasLM = hasLastTouched.HasLocalMachineName(); var ip = NetUtil.GetLocalIPAddresses().First(); var hasIP = hasLM.HasIP(ip); var hasRS = hasIP.HasRandomString("blah"); var hasV = hasRS.HasVersion("v"); //graph it var objState1 = hasV.GraphSerializeWithDefaults(); var readable = LengthEncoder.MakeReadable(objState1, "\t"); readable.WithEach(i => { Debug.WriteLine(i); }); var graph = Graph.Parse(objState1, ValueManagerChainOfResponsibility.NewDefault()); var readable2 = GraphingUtil.ConvertToXML(graph).ToString(); var obj2 = objState1.GraphDeserializeWithDefaults() as HasVersionDecoration; Condition.Requires(obj2.Version).IsEqualTo("v"); Condition.Requires(obj2.As <HasRandomStringDecoration>(true).RandomString).IsEqualTo("blah"); Condition.Requires(obj2.As <HasIPDecoration>(true).IPAddress.ToString()).IsEqualTo(ip.ToString()); Condition.Requires(obj2.As <HasMachineNameDecoration>(true).MachineName).IsEqualTo(localMachineName); Condition.Requires(obj2.As <HasDateLastTouchedDecoration>(true).DateLastTouched.ToString()).IsEqualTo(lastTouchedDate.ToUniversalTime().ToString()); Condition.Requires(obj2.As <HasDateCreatedDecoration>(true).DateCreated.ToString()).IsEqualTo(now.ToUniversalTime().ToString()); Condition.Requires(obj2.As <HasGUIDDecoration>(true).GUID).IsEqualTo(guid); Condition.Requires(obj2.Id.ToString()).IsEqualTo("id"); hasV.Version = "v2"; var objState2 = hasV.GraphSerializeWithDefaults(); var obj3 = objState2.GraphDeserializeWithDefaults() as HasVersionDecoration; Condition.Requires(obj3.Version).IsEqualTo("v2"); })) { }
/// <summary> /// given a parent node and a store of nodes, identifies the children of the parent and wires /// their values to the parent node's value /// </summary> /// <param name="parentNode"></param> /// <param name="nodeStore"></param> private void WireParentToChildren(GraphNode parentNode, IStoreOf <GraphNode> nodeStore) { Condition.Requires(parentNode).IsNotNull(); Condition.Requires(nodeStore).IsNotNull(); List <string> skipFilter = new List <string>() { NullValueManager.ID, DelegateValueManager.ID, PrimitiveValueManager.ID }; if (skipFilter.Contains(parentNode.ValueManagerId)) { return; } var children = nodeStore.GetImmediateChildNodes(parentNode.Path).OrderBy((x) => { return(x.Path.EnumeratedSegmentIndex); }).ToList(); var fields = GraphingUtil.GetNestingNotatedFieldInfos(parentNode.NodeValue.GetType()); //get the manager for this node value var manager = this.ChainOfResponsibility.FindHandlingValueManager(parentNode.NodeValue, this); Condition.Requires(manager).IsNotNull(); var traverseList = manager.GetChildTraversalNodes(parentNode.NodeValue, parentNode.Path); traverseList.WithEach(tuple => { BuildNode(tuple.Item1, tuple.Item2); }); foreach (var each in children) { //add the children if (each.Path.IsEnumeratedSegment) { if (each.NodeValue is IDictionary) { IDictionary dict = parentNode.NodeValue as IDictionary; DictionaryEntry de = (DictionaryEntry)each.NodeValue; dict.Add(de.Key, de.Value); } else if (each.NodeValue is Stack) { Stack stack = parentNode.NodeValue as Stack; stack.Push(each.NodeValue); } else if (each.NodeValue is Queue) { Queue queue = parentNode.NodeValue as Queue; queue.Enqueue(each.NodeValue); } else if (each.NodeValue is IList) { IList list = parentNode.NodeValue as IList; list.Add(each.NodeValue); } } else { //get the matching field GraphSegment gSeg = each.Path.CurrentSegment as GraphSegment; //TODO: refactor implicit contract - that the paths match field names (GraphingUtil.GetNestingNotatedFieldInfos) var matches = fields.Filter((x) => { return(x.Item1 == gSeg.Path); }); Condition.Requires(matches).IsNotNull().HasLength(1); var fi = matches.First(); fi.Item2.SetValue(parentNode.NodeValue, each.NodeValue); } } }