/// <summary>
        ///
        /// </summary>
        /// <param name="tuple"></param>
        /// <param name="toNode"></param>
        /// <returns></returns>
        public IList <MessageEntity> Produce(MessageTuple tuple, NodeDescriptor toNode)
        {
            if (tuple == null)
            {
                throw new ArgumentNullException("tuple");
            }
            if (toNode == null)
            {
                throw new ArgumentNullException("toNode");
            }
            if (!IsProduce(tuple.Context, toNode))
            {
                return(new List <MessageEntity>());
            }
            var list    = new List <MessageEntity>();
            var builder = new CommandBuilder(toNode);
            var result  = builder.Build(tuple);

            if (result != null)
            {
                list.Add(result);
            }

            return(list);
        }
        /// <summary>
        /// 根据给定的成功接收的命令消息创建待分发命令
        /// </summary>
        /// <param name="tuple">成功接收的命令</param>
        /// <returns></returns>
        public IList <MessageEntity> Produce(MessageTuple tuple)
        {
            if (tuple == null)
            {
                throw new ArgumentNullException("tuple");
            }
            var list = new List <MessageEntity>();

            foreach (CommandBuilder builder in this.CommandBuilders(tuple.Context.Host))
            {
                // 不闭合
                if (IsProduce(tuple.Context, builder.ToNode))
                {
                    var result = builder.Build(tuple);
                    if (result != null)
                    {
                        list.Add(result);
                    }
                }
            }

            return(list);
        }
            /// <summary>
            /// 根据给定的接收成功的命令建造待分发命令
            /// </summary>
            /// <param name="tuple">已成功接收的命令类型的命令描述对象</param>
            /// <exception cref="AnycmdException">
            /// 当<seealso cref="ToNode"/>是自己时发生,不能建造分发向自己的命令消息
            /// </exception>
            /// <returns>待分发命令集合,可能为null</returns>
            public MessageEntity Build(MessageTuple tuple)
            {
                if (tuple == null)
                {
                    throw new ArgumentNullException("tuple");
                }
                #region 演出
                if (ToNode == ToNode.AcDomain.NodeHost.Nodes.ThisNode)
                {
                    throw new AnycmdException("不能建造分发向自己的命令消息");
                }
                if (tuple.Context.ClientAgent == ToNode)
                {
                    throw new AnycmdException("不能基于来源节点的命令建造分发向来源节点的命令");
                }
                var actionCode = tuple.Context.Command.Verb;
                if (actionCode == Verb.Create || actionCode == Verb.Update)
                {
                    var infoValueCares = new List <DataItem>();
                    foreach (var item in tuple.Context.InfoTuplePair.ValueTuple)
                    {
                        if (item.Element != tuple.Context.Ontology.IdElement &&
                            !item.Element.IsRuntimeElement &&
                            ToNode.IsCareforElement(item.Element))
                        {
                            infoValueCares.Add(new DataItem(item.Key, item.Value));
                        }
                    }
                    DataItem[] infoIdItems = null;
                    if (tuple.Tuple == null ||
                        ToNode.GetInfoIdElements().Any(a =>
                                                       a != tuple.Context.Ontology.IdElement &&
                                                       a.Element.IsInfoIdItem &&
                                                       !tuple.Tuple.Any(b => a.Element.Code.Equals(b.Key, StringComparison.OrdinalIgnoreCase))))
                    {
                        if (actionCode == Verb.Create)
                        {
                            var selectElements = new OrderedElementSet();
                            foreach (var item in ToNode.GetInfoIdElements())
                            {
                                if (item.Element.IsInfoIdItem)
                                {
                                    selectElements.Add(item);
                                }
                            }
                            infoIdItems = tuple.Context.Ontology.EntityProvider.GetTopTwo(tuple.Context.Ontology,
                                                                                          new InfoItem[] { InfoItem.Create(tuple.Context.Ontology.IdElement, tuple.Context.LocalEntityId) },
                                                                                          selectElements).SingleInfoTuple.Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        }
                        else
                        {
                            infoIdItems = tuple.Context.TowInfoTuple.SingleInfoTuple
                                          .Where(e => e.Element.Element.IsInfoIdItem)
                                          .Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        }
                    }
                    else
                    {
                        infoIdItems = tuple.Tuple.Where(e => e.Element.Element.IsInfoIdItem && ToNode.IsInfoIdElement(e.Element)).ToArray();
                    }
                    DataItemsTuple dataTuple = DataItemsTuple.Create(
                        ToNode.AcDomain,
                        infoIdItems,
                        infoValueCares.ToArray(),
                        tuple.Context.Command.DataTuple.QueryList,
                        tuple.Context.Command.DataTuple.InfoFormat);

                    if (infoValueCares.Count > 0)
                    {
                        return(new MessageEntity(MessageTypeKind.Distribute, Guid.NewGuid(), dataTuple)
                        {
                            ClientId = ToNode.Node.Id.ToString(),
                            Verb = tuple.Context.Command.Verb,
                            LocalEntityId = tuple.Context.LocalEntityId,
                            CatalogCode = tuple.Context.CatalogCode,
                            ReceivedOn = tuple.Context.Command.ReceivedOn,
                            CreateOn = DateTime.Now,
                            Ontology = tuple.Context.Command.Ontology,
                            ClientType = tuple.Context.Command.ClientType,
                            TimeStamp = tuple.Context.Command.TimeStamp,
                            MessageType = tuple.Context.Command.MessageType,
                            MessageId = tuple.Context.Command.MessageId,
                            Status = tuple.Context.Result.Status,
                            ReasonPhrase = tuple.Context.Result.ReasonPhrase,
                            Description = tuple.Context.Result.Description,
                            EventSourceType = tuple.Context.Command.EventSourceType,
                            EventSubjectCode = tuple.Context.Command.EventSubjectCode,
                            UserName = tuple.Context.Command.UserName,
                            IsDumb = tuple.Context.Command.IsDumb,
                            Version = tuple.Context.Command.Version
                        });
                    }
                }
                else if (actionCode == Verb.Delete)
                {
                    if (ToNode.IsCareForOntology(tuple.Context.Ontology))
                    {
                        DataItem[] infoIdItems = tuple.Context.TowInfoTuple.SingleInfoTuple
                                                 .Where(e => e.Element.Element.IsInfoIdItem)
                                                 .Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        DataItemsTuple dataTuple = DataItemsTuple.Create(ToNode.AcDomain, infoIdItems, null, tuple.Context.Command.DataTuple.QueryList, tuple.Context.Command.DataTuple.InfoFormat);
                        return(new MessageEntity(MessageTypeKind.Distribute, Guid.NewGuid(), dataTuple)
                        {
                            ClientId = ToNode.Node.Id.ToString(),
                            Verb = tuple.Context.Command.Verb,
                            LocalEntityId = tuple.Context.LocalEntityId,
                            CatalogCode = tuple.Context.CatalogCode,
                            ReceivedOn = tuple.Context.Command.ReceivedOn,
                            CreateOn = DateTime.Now,
                            Ontology = tuple.Context.Command.Ontology,
                            ClientType = tuple.Context.Command.ClientType,
                            TimeStamp = tuple.Context.Command.TimeStamp,
                            MessageType = tuple.Context.Command.MessageType,
                            MessageId = tuple.Context.Command.MessageId,
                            Status = tuple.Context.Result.Status,
                            ReasonPhrase = tuple.Context.Result.ReasonPhrase,
                            Description = tuple.Context.Result.Description,
                            EventSubjectCode = tuple.Context.Command.EventSubjectCode,
                            EventSourceType = tuple.Context.Command.EventSourceType,
                            UserName = tuple.Context.Command.UserName,
                            IsDumb = tuple.Context.Command.IsDumb,
                            Version = tuple.Context.Command.Version
                        });
                    }
                }
                #endregion

                return(null);
            }
            /// <summary>
            /// 根据给定的接收成功的命令建造待分发命令
            /// </summary>
            /// <param name="tuple">已成功接收的命令类型的命令描述对象</param>
            /// <exception cref="AnycmdException">
            /// 当<seealso cref="ToNode"/>是自己时发生,不能建造分发向自己的命令消息
            /// </exception>
            /// <returns>待分发命令集合,可能为null</returns>
            public MessageEntity Build(MessageTuple tuple)
            {
                if (tuple == null)
                {
                    throw new ArgumentNullException("tuple");
                }
                #region 演出
                if (ToNode == ToNode.AcDomain.NodeHost.Nodes.ThisNode)
                {
                    throw new AnycmdException("不能建造分发向自己的命令消息");
                }
                if (tuple.Context.ClientAgent == ToNode)
                {
                    throw new AnycmdException("不能基于来源节点的命令建造分发向来源节点的命令");
                }
                var actionCode = tuple.Context.Command.Verb;
                if (actionCode == Verb.Create || actionCode == Verb.Update)
                {
                    var infoValueCares = new List<DataItem>();
                    foreach (var item in tuple.Context.InfoTuplePair.ValueTuple)
                    {
                        if (item.Element != tuple.Context.Ontology.IdElement
                            && !item.Element.IsRuntimeElement
                            && ToNode.IsCareforElement(item.Element))
                        {
                            infoValueCares.Add(new DataItem(item.Key, item.Value));
                        }
                    }
                    DataItem[] infoIdItems = null;
                    if (tuple.Tuple == null
                        || ToNode.GetInfoIdElements().Any(a =>
                            a != tuple.Context.Ontology.IdElement
                            && a.Element.IsInfoIdItem
                            && !tuple.Tuple.Any(b => a.Element.Code.Equals(b.Key, StringComparison.OrdinalIgnoreCase))))
                    {
                        if (actionCode == Verb.Create)
                        {
                            var selectElements = new OrderedElementSet();
                            foreach (var item in ToNode.GetInfoIdElements())
                            {
                                if (item.Element.IsInfoIdItem)
                                {
                                    selectElements.Add(item);
                                }
                            }
                            infoIdItems = tuple.Context.Ontology.EntityProvider.GetTopTwo(tuple.Context.Ontology,
                                new InfoItem[] { InfoItem.Create(tuple.Context.Ontology.IdElement, tuple.Context.LocalEntityId) },
                                selectElements).SingleInfoTuple.Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        }
                        else
                        {
                            infoIdItems = tuple.Context.TowInfoTuple.SingleInfoTuple
                                .Where(e => e.Element.Element.IsInfoIdItem)
                                .Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        }
                    }
                    else
                    {
                        infoIdItems = tuple.Tuple.Where(e => e.Element.Element.IsInfoIdItem && ToNode.IsInfoIdElement(e.Element)).ToArray();
                    }
                    DataItemsTuple dataTuple = DataItemsTuple.Create(
                        ToNode.AcDomain,
                        infoIdItems,
                        infoValueCares.ToArray(),
                        tuple.Context.Command.DataTuple.QueryList,
                        tuple.Context.Command.DataTuple.InfoFormat);

                    if (infoValueCares.Count > 0)
                    {
                        return new MessageEntity(MessageTypeKind.Distribute, Guid.NewGuid(), dataTuple)
                        {
                            ClientId = ToNode.Node.Id.ToString(),
                            Verb = tuple.Context.Command.Verb,
                            LocalEntityId = tuple.Context.LocalEntityId,
                            CatalogCode = tuple.Context.CatalogCode,
                            ReceivedOn = tuple.Context.Command.ReceivedOn,
                            CreateOn = DateTime.Now,
                            Ontology = tuple.Context.Command.Ontology,
                            ClientType = tuple.Context.Command.ClientType,
                            TimeStamp = tuple.Context.Command.TimeStamp,
                            MessageType = tuple.Context.Command.MessageType,
                            MessageId = tuple.Context.Command.MessageId,
                            Status = tuple.Context.Result.Status,
                            ReasonPhrase = tuple.Context.Result.ReasonPhrase,
                            Description = tuple.Context.Result.Description,
                            EventSourceType = tuple.Context.Command.EventSourceType,
                            EventSubjectCode = tuple.Context.Command.EventSubjectCode,
                            UserName = tuple.Context.Command.UserName,
                            IsDumb = tuple.Context.Command.IsDumb,
                            Version = tuple.Context.Command.Version
                        };
                    }
                }
                else if (actionCode == Verb.Delete)
                {
                    if (ToNode.IsCareForOntology(tuple.Context.Ontology))
                    {
                        DataItem[] infoIdItems = tuple.Context.TowInfoTuple.SingleInfoTuple
                            .Where(e => e.Element.Element.IsInfoIdItem)
                            .Select(e => new DataItem(e.Key, e.Value)).ToArray();
                        DataItemsTuple dataTuple = DataItemsTuple.Create(ToNode.AcDomain, infoIdItems, null, tuple.Context.Command.DataTuple.QueryList, tuple.Context.Command.DataTuple.InfoFormat);
                        return new MessageEntity(MessageTypeKind.Distribute, Guid.NewGuid(), dataTuple)
                        {
                            ClientId = ToNode.Node.Id.ToString(),
                            Verb = tuple.Context.Command.Verb,
                            LocalEntityId = tuple.Context.LocalEntityId,
                            CatalogCode = tuple.Context.CatalogCode,
                            ReceivedOn = tuple.Context.Command.ReceivedOn,
                            CreateOn = DateTime.Now,
                            Ontology = tuple.Context.Command.Ontology,
                            ClientType = tuple.Context.Command.ClientType,
                            TimeStamp = tuple.Context.Command.TimeStamp,
                            MessageType = tuple.Context.Command.MessageType,
                            MessageId = tuple.Context.Command.MessageId,
                            Status = tuple.Context.Result.Status,
                            ReasonPhrase = tuple.Context.Result.ReasonPhrase,
                            Description = tuple.Context.Result.Description,
                            EventSubjectCode = tuple.Context.Command.EventSubjectCode,
                            EventSourceType = tuple.Context.Command.EventSourceType,
                            UserName = tuple.Context.Command.UserName,
                            IsDumb = tuple.Context.Command.IsDumb,
                            Version = tuple.Context.Command.Version
                        };
                    }
                }
                #endregion

                return null;
            }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tuple"></param>
        /// <param name="toNode"></param>
        /// <returns></returns>
        public IList<MessageEntity> Produce(MessageTuple tuple, NodeDescriptor toNode)
        {
            if (tuple == null)
            {
                throw new ArgumentNullException("tuple");
            }
            if (toNode == null)
            {
                throw new ArgumentNullException("toNode");
            }
            if (!IsProduce(tuple.Context, toNode))
            {
                return new List<MessageEntity>();
            }
            var list = new List<MessageEntity>();
            var builder = new CommandBuilder(toNode);
            var result = builder.Build(tuple);
            if (result != null)
            {
                list.Add(result);
            }

            return list;
        }
        /// <summary>
        /// 根据给定的成功接收的命令消息创建待分发命令
        /// </summary>
        /// <param name="tuple">成功接收的命令</param>
        /// <returns></returns>
        public IList<MessageEntity> Produce(MessageTuple tuple)
        {
            if (tuple == null)
            {
                throw new ArgumentNullException("tuple");
            }
            var list = new List<MessageEntity>();
            foreach (CommandBuilder builder in this.CommandBuilders(tuple.Context.Host))
            {
                // 不闭合
                if (IsProduce(tuple.Context, builder.ToNode))
                {
                    var result = builder.Build(tuple);
                    if (result != null)
                    {
                        list.Add(result);
                    }
                }
            }

            return list;
        }