示例#1
0
        public Buffer(IEnumerable <Consumer <T> > someConsumers) : base("BufferEmpty", null)
        {
            CheckArg.IEnumerable(someConsumers);
            foreach (var item in someConsumers)
            {
                ConsList.Enqueue(item);
                item.Buffer = this;
            }

            AddBehavior(new Behavior <Work <T> >(t =>
            {
                this.SendMessage(InternalCurrentState, t);
            }
                                                 ));

            AddBehavior(new FsmBehavior <string, Work <T> >("BufferEmpty", "BufferNotEmpty",
                                                            t =>
            {
                if (ConsList.Count != 0)
                {
                    var cons = ConsList.Dequeue();
                    cons.SendMessage(t);
                }
                else
                {
                    WorkList.Enqueue(t);
                }
            }, t => true));

            AddBehavior(new FsmBehavior <string, Work <T> >("BufferNotEmpty", "BufferNotEmpty",
                                                            t =>
            {
                if (ConsList.Count != 0)
                {
                    var cons = ConsList.Dequeue();
                    cons.SendMessage(t);
                }
                else
                {
                    WorkList.Enqueue(t);
                }
            },
                                                            t => WorkList.Count != 0));

            AddBehavior(new Behavior <Consumer <T> >(t =>
            {
                if (WorkList.Count == 0)
                {
                    ConsList.Enqueue(t);
                    InternalCurrentState = "BufferEmpty";
                }
                else
                {
                    t.SendMessage(WorkList.Dequeue());
                }
            }));
        }
示例#2
0
        public static TKey Calc(IEnumerable <TKey> keys)
        {
            CheckArg.IEnumerable(keys);
            var dic = new Dictionary <HashKey, TKey>();

            foreach (var k in keys)
            {
                dic[HashKey.ComputeHash(k.ToString())] = k;
            }
            var elected = dic.Keys.OrderBy(h => h.ToString()).FirstOrDefault();

            return(dic[elected]);
        }
示例#3
0
        public Buffer(IEnumerable <Consumer <T> > someConsumers) : base()
        {
            CheckArg.IEnumerable(someConsumers);
            foreach (Consumer <T> item in someConsumers)
            {
                _consList.Enqueue(item);
                item.Buffer = this;
            }

            FsmBehaviors <string, Work <T> > bhv = new FsmBehaviors <string, Work <T> >();

            bhv
            .AddRule("BufferEmpty", null,
                     t =>
            {
                if (_consList.Count != 0)
                {
                    Consumer <T> cons = _consList.Dequeue();
                    cons.SendMessage(t);
                }
                else
                {
                    _workList.Enqueue(t);
                }
            }, "BufferNotEmpty")
            .AddRule("BufferNotEmpty", _ => _workList.Count != 0,
                     t =>
            {
                if (_consList.Count != 0)
                {
                    Consumer <T> cons = _consList.Dequeue();
                    cons.SendMessage(t);
                }
                else
                {
                    _workList.Enqueue(t);
                }
            },
                     "BufferNotEmpty");
        }