Exemplo n.º 1
0
 private void DeleteAllPoint(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.DeleteAllPoint]);
             if (_space != null)
             {
                 lock (this)
                 {
                     if (_space.Remove(message.Owner))
                     {
                         message.Action = MsgAction.Approve;
                     }
                     else
                     {
                         message.Action = MsgAction.Deny;
                     }
                 }
                 SendMessage(message);
             }
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 2
0
 public Agent(string name, Color color, Point startPoint, int senseOfPurpose, Purpose purpose, int worship, Temper temper, int lifeCircle, Engine engine, int timeToStart)
 {
     _active     = true;
     _name       = name;
     _color      = color;
     _startPoint = startPoint;
     InitTimeStep();
     _senseOfPurpose = senseOfPurpose;
     _purpose        = purpose;
     _worship        = worship;
     _temper         = temper;
     LifeCircle      = lifeCircle;
     _timeToStart    = timeToStart;
     Engine          = engine;
     EndOfLife       = false;
     _startTime      = DateTime.Now;
     _idMessage      = 0;
     _oldMessage     = null;
     _newMessage     = null;
     _registred      = false;
     _space          = new List <PointState>();
     Length          = 10;
     CalculateResponceTime();
     InitCountAttempt();
     Environment.EnvironmentMessage += ReceiveMessage;
 }
Exemplo n.º 3
0
 private void CommitPoint(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.GetPoint]);
             if (message.Entity is SendPoint)
             {
                 if (SearchPoint(message.Owner, ((SendPoint)(message.Entity)).ID,
                                 ((SendPoint)(message.Entity)).Point))
                 {
                     int indexPoint = IndexPoint(message);
                     if (_space != null)
                     {
                         lock (this)
                         {
                             _space[message.Owner].Points[indexPoint].State = MsgAction.Commit;
                         }
                     }
                     message.Action = MsgAction.Approve;
                 }
                 else
                 {
                     message.Action = MsgAction.Deny;
                 }
                 SendMessage(message);
             }
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 4
0
 private void AddAgent(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.NewAgent]);
             if (_space != null)
             {
                 lock (this)
                 {
                     if (!_space.ContainsKey(message.Owner))
                     {
                         try
                         {
                             _space.Add(message.Owner,
                                        new FigureAgent(((AgentConfig)(message.Entity)).Color, new List <PointConfig>()));
                         }
                         catch
                         {
                         }
                     }
                 }
             }
             EnvironmentConfig environmentConfig = new EnvironmentConfig(_height, _width, _offsetXY, _response);
             message.Entity = environmentConfig;
             message.Action = MsgAction.Approve;
             SendMessage(message);
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 5
0
 private void SendMessage(EnvironmentMessage message)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         lock (this)
         {
             if (message != null)
             {
                 if ((message.Message == TypeMessege.DeletePoint) ||
                     (message.Message == TypeMessege.DeleteAllPoint) ||
                     (message.Message == TypeMessege.GetPoint))
                 {
                     if (_space != null)
                     {
                         ChangedPoints?.Invoke(_space);
                     }
                 }
                 EnvironmentMessage?.Invoke(message);
             }
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 6
0
 public void Clear()
 {
     _space.Clear();
     _name           = "";
     _color          = new Color();
     _senseOfPurpose = 0;
     _purpose        = null;
     _worship        = 0;
     _temper         = Temper.Сангвінік;
     _lifeCircle     = 0;
     _timePause      = 0;
     responceTime.Clear();
     responceTime  = null;
     _countAttempt = 0;
     _id           = 0;
     _endOfLife    = true;
     _engine       = null;
     _startTime    = DateTime.Now;
     _startPoint   = new Point();
     _space.Clear();
     _space           = null;
     _idMessage       = 0;
     _oldMessage      = null;
     _newMessage      = null;
     _env             = null;
     _registred       = false;
     _denyEnvironment = true;
     _length          = 0;
     listConfig.Clear();
     listConfig = null;
     queue.Clear();
     queue = null;
     GC.Collect();
 }
Exemplo n.º 7
0
 private void RollBackPoint(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.GetPoint]);
             if (_space != null)
             {
                 lock (this)
                 {
                     if (
                         _space[message.Owner].Points.RemoveAll(
                             state => state.Point.Equals(((Point)(message.Entity)))) > 0)
                     {
                         message.Action = MsgAction.Approve;
                     }
                     else
                     {
                         message.Action = MsgAction.Deny;
                     }
                 }
             }
             SendMessage(message);
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 8
0
 private void AddPoint(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.GetPoint]);
             Color color = Color.Red;
             if (message.Entity is AgentConfig)
             {
                 color = ((AgentConfig)(message.Entity)).Color;
             }
             if (!SearchPoint((((SendPoint)(message.Entity)).Point)))
             {
                 if (!SearchAgent(message.Owner))
                 {
                     if (_space != null)
                     {
                         lock (this)
                         {
                             _space.Add(message.Owner, new FigureAgent(color, new List <PointConfig>()));
                         }
                     }
                 }
                 if (_space != null)
                 {
                     lock (this)
                     {
                         if ((_space.Count > 0) && (_space[message.Owner].Points.Count > 0))
                         {
                             Point lastPoint    = _space[message.Owner].Points.Last().Point;
                             Point aditionPoint = ((SendPoint)(message.Entity)).Point;
                             int   length       = Math.Abs(lastPoint.X - aditionPoint.X) +
                                                  Math.Abs(lastPoint.Y - aditionPoint.Y);
                             if (length > 1)
                             {
                                 _space[message.Owner].Points.RemoveAll(state =>
                                                                        state.Point.Equals(lastPoint));
                             }
                         }
                         _space[message.Owner].Points.Add(new PointConfig(((SendPoint)(message.Entity)).ID,
                                                                          new Point(((SendPoint)(message.Entity)).Point.X,
                                                                                    ((SendPoint)(message.Entity)).Point.Y), message.Action));
                     }
                 }
                 message.Action = MsgAction.Approve;
             }
             else
             {
                 message.Action = MsgAction.Deny;
             }
             SendMessage(message);
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 9
0
 public void ReceiveMessage(EnvironmentMessage message)
 {
     if (_active && message.Equals(_newMessage))
     {
         _oldMessage = _newMessage;
         _newMessage = message;
     }
 }
Exemplo n.º 10
0
 private void InitCircleMessage()
 {
     if (_active)
     {
         _oldMessage = null;
         _newMessage = null;
         _idMessage++;
     }
 }
Exemplo n.º 11
0
 private void SendMessage(EnvironmentMessage message)
 {
     // Если на событие AgentMessage кто-нибудь подписан - создаем событие, т.е. посылаем сообщение
     // для нашего Environment-а
     if (_active && (message != null) && (AgentMessage != null))
     {
         AgentMessage(message.Clone());
     }
 }
Exemplo n.º 12
0
        public int CompareTo(EnvironmentMessage obj)
        {
            int result = _id.CompareTo(obj.ID);

            if (result == 0)
            {
                result = _idOwner.CompareTo(obj.Owner);
            }
            return(result);
        }
Exemplo n.º 13
0
        private bool _nextFigure;                                        // Флаг, который указывает осуществляется ли перестройка Engine на другую фигуру

        public RandomEngine()
        {
            rndTurn     = new Random(3);
            _oldMessage = null;
            _newMessage = null;
            _space      = new List <PointState>();
            _cancel     = new List <Point>();
            _stepDown   = new List <VectorPoint>();
            _nextFigure = false;
        }
Exemplo n.º 14
0
 private bool StartDialog()
 {
     if (_active)
     {
         if (_newMessage == null)
         {
             _oldMessage = null;
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 15
0
        public override void NextStep(ref EnvironmentMessage message)
        {
            if ((_oldMessage != null) && (_oldMessage.ID != message.ID))
            {
                _oldMessage = null;
            }
            else
            {
                _oldMessage = _newMessage;
            }
            _newMessage = message;
            if (message.Entity is AgentConfig)
            {
                agentConfig = (AgentConfig)message.Entity;
                _nextFigure = false;
            }
            else if (message.Entity is EnvironmentConfig)
            {
                environmentConfig  = (EnvironmentConfig)_newMessage.Entity;
                _newMessage.Entity = agentConfig;
                if (_oldMessage != null)
                {
                    if (_newMessage.Message == TypeMessege.NewAgent)
                    {
                        if ((_newMessage.Action == MsgAction.Approve) &&
                            (_oldMessage.Action == MsgAction.Invoke))
                        {
                            _newMessage.Action = MsgAction.Commit;
                        }
                        else if ((_newMessage.Action == MsgAction.Approve) &&
                                 (_oldMessage.Action == MsgAction.Commit))
                        {
                            _newMessage = null;
                            _oldMessage = null;
                        }
                    }
                }
            }
            else if ((message.Entity is SendPoint) || (message.Entity == null))
            {
                switch (_newMessage.Message)
                {
                case TypeMessege.GetPoint:
                    NextAction();
                    break;

                case TypeMessege.DeletePoint:
                    CancelPoint();
                    break;
                }
            }
            message = _newMessage;
        }
Exemplo n.º 16
0
 private void InvokePoint()
 {
     if (_active)
     {
         Thread.Sleep(responceTime[TypeMessege.GetPoint]);
         _newMessage = new EnvironmentMessage(_idMessage, _id,
                                              TypeMessege.GetPoint,
                                              MsgAction.Invoke, null);
         _engine.NextStep(ref _newMessage);
         SendMessage(_newMessage);
     }
 }
Exemplo n.º 17
0
 public RealEngine()
 {
     rndTurn                 = new Random(3);
     _oldMessage             = null;
     _newMessage             = null;
     _space                  = new List <PointState>();
     _cancel                 = new List <Point>();
     _stepDown               = new List <VectorPoint>();
     _nextFigure             = false;
     currentConfig           = 0;
     currentPositionOfConfig = 0;
     _endWork                = false;
 }
Exemplo n.º 18
0
 private void RegistredAgent()
 {
     if (_active)
     {
         Thread.Sleep(responceTime[TypeMessege.NewAgent]);
         _registred  = true;
         _newMessage = new EnvironmentMessage(_idMessage, _id, TypeMessege.NewAgent,
                                              MsgAction.Invoke,
                                              new AgentConfig(_name, _color, _senseOfPurpose, _purpose, _worship,
                                                              _temper, _lifeCircle, _countAttempt,
                                                              null, _startPoint, _length, listConfig));
         _engine.NextStep(ref _newMessage);
         SendMessage(_newMessage);
     }
 }
Exemplo n.º 19
0
 private void SendMessageEnvironmentMessage(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         EnvironmentMessage message = new EnvironmentMessage();
         if (message is EnvironmentMessage)
         {
             message = ((EnvironmentMessage)msg).Clone();
         }
         EnvironmentMessage?.Invoke(message);
         _running = tempRunning;
     }
 }
Exemplo n.º 20
0
 private void CommitDeletePoint(object msg)
 {
     if (_active)
     {
         bool tempRunning = _running;
         _running = true;
         if ((msg != null) && (msg is EnvironmentMessage))
         {
             EnvironmentMessage message = (EnvironmentMessage)(msg);
             Thread.Sleep(_response[TypeMessege.DeletePoint]);
             message.Action = MsgAction.Approve;
             SendMessage(message);
         }
         _running = tempRunning;
     }
 }
Exemplo n.º 21
0
        public override bool Equals(object obj)
        {
            bool result = false;
            EnvironmentMessage tempObj = (EnvironmentMessage)obj;

            if (obj != null)
            {
                if (this._id == tempObj.ID)
                {
                    if (this.Owner == tempObj.Owner)
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 22
0
 private bool FirstResponce()
 {
     if (_active)
     {
         if (_newMessage == null)
         {
             _oldMessage = null;
         }
         if ((_oldMessage != null) && (_newMessage != null))
         {
             return((_oldMessage.Action == MsgAction.Invoke) &&
                    ((_newMessage.Action == MsgAction.Approve) ||
                     (_newMessage.Action == MsgAction.Deny)));
         }
     }
     return(false);
 }
Exemplo n.º 23
0
        private void NextAction()
        {
            int i = 0;

            switch (_newMessage.Action)
            {
            case MsgAction.Invoke:
                NextPoint();
                break;

            case MsgAction.Approve:
                switch (_oldMessage.Action)
                {
                case MsgAction.Invoke:
                    int index = _space.Count;
                    _space.Add(new PointState(index,
                                              new Point(((SendPoint)(_newMessage.Entity)).Point.X,
                                                        ((SendPoint)(_newMessage.Entity)).Point.Y),
                                              _oldMessage.Action));
                    _newMessage.Action = MsgAction.Commit;
                    break;

                case MsgAction.Commit:
                    i = IndexPoint(((SendPoint)(_newMessage.Entity)).Point);
                    if (i >= 0)
                    {
                        _space[IndexPoint(((SendPoint)(_newMessage.Entity)).Point)].State = MsgAction.Commit;
                    }
                    _newMessage = null;
                    break;
                }
                break;

            case MsgAction.Deny:
                if (_oldMessage.Action == MsgAction.Commit)
                {
                    _space.RemoveAll(state => state.Point.Equals(((SendPoint)(_newMessage.Entity)).Point));
                }
                _cancel.Add(((SendPoint)(_newMessage.Entity)).Point);
                _newMessage = null;
                break;
            }
        }
Exemplo n.º 24
0
 private void EndOfLifeAgent(object idThread)
 {
     if (_active)
     {
         Thread.Sleep(responceTime[TypeMessege.DeleteAllPoint]);
         InitCircleMessage();
         _id = (int)idThread;
         // Первое собитие (сообщение) о том, что анегт прекращает свое существование, а это значит,
         // что все точки и соединение между ними, т.е.полученная фигура должна быть вытерта из хранилища
         // вреды окружения.
         EnvironmentMessage message =
             new EnvironmentMessage(_id, _idMessage,
                                    TypeMessege.DeleteAllPoint,
                                    MsgAction.Invoke, null);
         SendMessage(message);
         //Invoke(_ReceiveMsg, message);
         while (!message.AproveForAgent(_oldMessage))
         {
         }
         _endOfLife = true;
     }
 }
Exemplo n.º 25
0
        private int IndexPoint(EnvironmentMessage msg)
        {
            int i = -1;

            if (_active)
            {
                bool tempRunning = _running;
                _running = true;
                if ((msg != null) && (msg.Entity is SendPoint))
                {
                    if (_space != null)
                    {
                        lock (this)
                        {
                            i = _space[msg.Owner].Points.FindIndex(x => x.Point.Equals(((SendPoint)(msg.Entity)).Point));
                        }
                    }
                }
                _running = tempRunning;
            }
            return(i);
        }
Exemplo n.º 26
0
        private void CancelPoint()
        {
            switch (_newMessage.Action)
            {
            case MsgAction.Approve:
                switch (_oldMessage.Action)
                {
                case MsgAction.Invoke:
                    _space.RemoveAll(state => state.Point.Equals(((SendPoint)(_newMessage.Entity)).Point));
                    _newMessage.Action = MsgAction.Commit;
                    break;

                case MsgAction.Commit:
                    _newMessage = null;
                    break;
                }
                break;

            case MsgAction.Deny:
                _space.RemoveAll(state => state.Point.Equals(((SendPoint)(_newMessage.Entity)).Point));
                _newMessage = null;
                break;
            }
        }
Exemplo n.º 27
0
 public bool AproveForAgent(EnvironmentMessage oldMessage)
 {
     return(Equals(oldMessage) && (this.Action == MsgAction.Approve));
 }
Exemplo n.º 28
0
        /// <summary>
        /// Обработчик событий, поступивших от агента. Метод, который слушает сообщения Agent. Он должен быть подписан на события AgentMessage.
        /// </summary>
        /// <param name="message"></param>
        public void ReceiveMessage(EnvironmentMessage message)
        {
            if (_active)
            {
                bool tempRunning = _running;
                _running = true;
                //int i;
                switch (message.Message)
                {
                case TypeMessege.GetPoint:
                    switch (message.Action)
                    {
                    case MsgAction.Invoke:         // Запускаем поток реакции на сообщение MsgAction.Invoke
                        //lock (this)
                        //{
                        //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(AddPoint)));
                        //i = _environmenThreads.Count - 1;
                        //_environmenThreads[i].IsBackground = true;
                        //_environmenThreads[i].Start(message);
                        AddPoint(message);
                        //}
                        break;

                    case MsgAction.Commit:         // Запускаем поток реакции на сообщение MsgAction.Commit
                        //lock (this)
                        //{
                        //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(CommitPoint)));
                        //i = _environmenThreads.Count - 1;
                        //_environmenThreads[i].IsBackground = true;
                        //_environmenThreads[i].Start(message);
                        CommitPoint(message);
                        //}
                        break;

                    case MsgAction.Rollback:         // Запускаем поток реакции на сообщение MsgAction.Rollback
                        //lock (this)
                        //{
                        //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(RollBackPoint)));
                        //i = _environmenThreads.Count - 1;
                        //_environmenThreads[i].IsBackground = true;
                        //_environmenThreads[i].Start(message);
                        RollBackPoint(message);
                        //}
                        break;
                    }
                    break;

                case TypeMessege.DeletePoint:
                    switch (message.Action)
                    {
                    case MsgAction.Invoke:         // Запускаем поток реакции на сообщение MsgAction.Invoke
                        //lock (this)
                        //{
                        //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(DeletePoint)));
                        //i = _environmenThreads.Count - 1;
                        //_environmenThreads[i].IsBackground = true;
                        //_environmenThreads[i].Start(message);
                        DeletePoint(message);
                        //}
                        break;

                    case MsgAction.Commit:         // Запускаем поток реакции на сообщение MsgAction.Commit
                        lock (this)
                        {
                            //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(CommitDeletePoint)));
                            //i = _environmenThreads.Count - 1;
                            //_environmenThreads[i].IsBackground = true;
                            //_environmenThreads[i].Start(message);
                            CommitDeletePoint(message);
                        }
                        break;

                    case MsgAction.Rollback:         // Запускаем поток реакции на сообщение MsgAction.Rollback
                        //lock (this)
                        //{
                        //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(RollBackDeletePoint)));
                        //i = _environmenThreads.Count - 1;
                        //_environmenThreads[i].IsBackground = true;
                        //_environmenThreads[i].Start(message);
                        RollBackDeletePoint(message);
                        //}
                        break;
                    }
                    break;

                case TypeMessege.DeleteAllPoint:     // Запускаем поток реакции на сообщение MsgAction.DeleteAllPoint
                    //lock (this)
                    //{
                    //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(DeleteAllPoint)));
                    //i = _environmenThreads.Count - 1;
                    //_environmenThreads[i].IsBackground = true;
                    //_environmenThreads[i].Start(message);
                    DeleteAllPoint(message);
                    //}
                    break;

                case TypeMessege.NewAgent:     // Запускаем поток реакции на сообщение MsgAction.NewAgent
                    //lock (this)
                    //{
                    //_environmenThreads.Add(new Thread(new ParameterizedThreadStart(AddAgent)));
                    //i = _environmenThreads.Count - 1;
                    //_environmenThreads[i].IsBackground = true;
                    //_environmenThreads[i].Start(message);
                    AddAgent(message);
                    //}
                    break;
                }
                _running = tempRunning;
            }
        }
Exemplo n.º 29
0
        private void NextPoint()
        {
            lock (this)
            {
                try
                {
                    if ((agentConfig.Attempt > 0) &&
                        !((_space.Count > 0) &&
                          (_space.Last().Point.Equals(((Point)(agentConfig.Purpose.Entity))))))


                    {
                        Point point = new Point();
                        if (_nextFigure)
                        {
                            point = agentConfig.ListConfig[currentConfig + 1][_space.Count - 1];
                            if (
                                _space.Last()
                                .Point.Equals(
                                    agentConfig.ListConfig[currentConfig + 1][_space.Count - 1]))
                            {
                                _nextFigure = false;
                                agentConfig.Attempt--;
                                currentConfig++;
                                if (currentConfig == agentConfig.ListConfig.Count)
                                {
                                    agentConfig.Attempt = 0;
                                    currentConfig       = agentConfig.ListConfig.Count - 1;
                                }
                                _oldMessage = null;
                                _newMessage = null;
                            }
                            else
                            {
                                AddStepDownPoint();
                                DeletePoint();
                            }
                        }
                        else
                        {
                            if (_space.Count < (agentConfig.ListConfig[currentConfig].Count))
                            {
                                point = agentConfig.ListConfig[currentConfig][_space.Count];
                                if (!PostActionApprove(point))
                                {
                                    _nextFigure = true;
                                    if ((currentConfig == agentConfig.ListConfig.Count - 1) ||
                                        (agentConfig.Attempt == 1))
                                    {
                                        agentConfig.Attempt = 0;
                                        currentConfig       = agentConfig.ListConfig.Count - 1;
                                        _oldMessage         = null;
                                        _newMessage         = null;
                                    }
                                    else
                                    {
                                        AddStepDownPoint();
                                        if (_space.Count > 1)
                                        {
                                            DeletePoint();
                                        }
                                        else
                                        {
                                            _stepDown.Clear();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _nextFigure = true;
                                if ((currentConfig == agentConfig.ListConfig.Count - 1) ||
                                    (agentConfig.Attempt == 1))
                                {
                                    agentConfig.Attempt = 0;
                                    currentConfig       = agentConfig.ListConfig.Count - 1;
                                    _oldMessage         = null;
                                    _newMessage         = null;
                                }
                                else
                                {
                                    AddStepDownPoint();
                                    if (_space.Count > 1)
                                    {
                                        DeletePoint();
                                    }
                                    else
                                    {
                                        _stepDown.Clear();
                                    }
                                }
                            }
                        }
                    }

                    else
                    {
                        _oldMessage = null;
                        _newMessage = null;
                        if (!_endWork)
                        {
                            _endWork = true;
                            DoEndWork($"{agentConfig.Name} завершив свою побудову.");
                        }
                    }
                }
                catch (Exception ee)
                {}
                if ((_newMessage != null) && (_newMessage.Entity == null))
                {
                    _oldMessage = null;
                    _newMessage = null;
                }
            }
        }
Exemplo n.º 30
0
 private int IndexPoint(EnvironmentMessage msg)
 {
     return((_active) ? _space.FindIndex(state => state.Point.Equals(((SendPoint)(msg.Entity)).Point)) : -1);
 }