コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
                }
            }
        }