Exemplo n.º 1
0
 public bool AddWorkableUnitToQueue(string nom, Type model, Coordinates parkingPos, Coordinates workingPos)
 {
     //- L’usine ne peut enregistrer plus de commandes si sa queue est pleine
     //- L’usine ne peut construire plus de robots si son entrepôt est plein
     if ((Queue.Count <= this.QueueCapacity) && ((Queue.Count <= this.StorageCapacity)))
     {
         FactoryQueueElement add = new FactoryQueueElement(nom, model, parkingPos, workingPos);
         Queue.Enqueue(add);
         if (_instanceUnitFactory == null)
         {
             lock (obj)
             {
                 if (_instanceUnitFactory == null)
                 {
                     //  _instanceUnitFactory = add;
                 }
             }
         }
         return(true);
     }
     else
     {
         return(false);
     }
     //- L’usine ne peut construire qu’un robot à la fois.
     //- On peut appeler l’ajout de commande n’importe quand
     //- La méthode doit retourner false si la commande n’est pas enregistrée
     //- La construction doit être active tant que la queue n’est pas vide ou l’entrepôt plein
     //- La construction d’un robot doit être simulée et prendre le temps indiqué par la propriété BuildTime du robot
 }
Exemplo n.º 2
0
        public bool AddWorkableUnitToQueue(Type model, string name, Coordinates parkingPos, Coordinates workingPos)
        {
            OnStatusChangedFactory(new StatusChangedEventArgs("Adding " + name + " to Queue ..."));
            if (this.Queue.Count < this.QueueCapacity && this.Storage.Count < this.StorageCapacity)
            {
                FactoryQueueElement commande = new FactoryQueueElement(name, model, parkingPos, workingPos);
                Queue.Add(commande);
                OnStatusChangedFactory(new StatusChangedEventArgs("test"));

                object       robot       = Activator.CreateInstance(model);
                ITestingUnit robotToTest = Activator.CreateInstance(commande.Model, new object[] { }) as ITestingUnit;

                robotToTest.Model      = name;
                robotToTest.ParkingPos = parkingPos;
                robotToTest.WorkingPos = workingPos;
                Thread th = new Thread(() =>
                {
                    OnStatusChangedFactory(new StatusChangedEventArgs("test"));
                    lock (thisLock)
                    {
                        Thread.Sleep(Convert.ToInt32(robotToTest.BuildTime) * 1000);
                        Storage.Add(robotToTest);
                        Queue.RemoveAt(Queue.Count - 1);
                        QueueTime += DateTime.Now.AddSeconds(Convert.ToInt32(robotToTest.BuildTime)) - DateTime.Now;
                        OnStatusChangedFactory(new StatusChangedEventArgs("test"));
                    }
                    OnStatusChangedFactory(new StatusChangedEventArgs("test 2"));
                });

                th.Start();
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        private void addRobotToStorage()
        {
            StatusChangedEventArgs s = new StatusChangedEventArgs();

            s.NewStatus = "Robot start creation";
            FactoryProgress(this, s);

            FactoryQueueElement first   = _queue.First();
            ITestingUnit        toStore = ( ITestingUnit )Activator.CreateInstance(first.Model);

            toStore.WorkingPos = first.WorkingPos;
            toStore.ParkingPos = first.ParkingPos;
            toStore.Name       = first.Name;

            Thread.Sleep(((int)toStore.BuildTime * 1000));
            _storage.Add(toStore);
            StorageFreeSlots = StorageCapacity - _storage.Count();
            _queue.Remove(first);
            QueueFreeSlots = QueueCapacity - _queue.Count();

            TimeSpan sp = TimeSpan.FromSeconds(toStore.BuildTime);

            QueueTime   = QueueTime.Subtract(sp);
            s.NewStatus = "Robot created";
            FactoryProgress(this, s);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Ajoute un nouvel élément à la liste de commande et relance la production si besoin
        /// </summary>
        /// <param name="inModel">Type de robot à construire</param>
        /// <param name="inUnitName">Nom du robot</param>
        /// <param name="inWorkingPos">Position de travail</param>
        /// <param name="inParkPos">Position de recharge</param>
        /// <returns></returns>
        public bool AddWorkableUnitToQueue(Type inModel, string inUnitName, Coordinates inWorkingPos, Coordinates inParkPos)
        {
            if (m_Queue.Count >= m_QueueCapacity)
            {
                return(false);
            }

            // l'ajoute à la liste
            FactoryQueueElement lOrder = new FactoryQueueElement(inUnitName, inModel, inParkPos, inWorkingPos);

            m_Queue.Add(lOrder);

            //On met à jour l'event
            FactoryProgressEventArgs lNewOrder = new FactoryProgressEventArgs();

            lNewOrder.QueueElement = lOrder;
            OnStatusChanged(lNewOrder);

            // lance la construction si ce n'est pas déjà en cours
            if (!m_IsBuilding)
            {
                BuildWorkableUnit();
            }
            return(true);
        }
Exemplo n.º 5
0
        private void BuildRobot(FactoryQueueElement queueElement)
        {
            if (queueElement == null)
            {
                return;
            }

            this.Queue.Remove(queueElement);
            this.QueueFreeSlots += 1;

            IWorkingUnit workingUnit = (IWorkingUnit)Activator.CreateInstance(queueElement.Model.UnderlyingSystemType, queueElement.Name, 0);

            workingUnit.Model      = queueElement.Model;
            workingUnit.ParkingPos = queueElement.ParkingPos;
            workingUnit.WorkingPos = queueElement.WorkingPos;
            workingUnit.WorkBegins();
            workingUnit.WorkEnds();

            //this.Storage.Add(workingUnit);

            //// Usine signale que la construction d'un robot s'achève
            //if (this.FactoryProgress != null)
            //{ this.FactoryProgress(this, new EventArgs()); }

            TimeSpan ts = TimeSpan.FromMilliseconds(workingUnit.BuildTime);

            this.QueueTime = this.QueueTime.Add(ts);
        }
 public bool CreaInstance(FactoryQueueElement felement)
 {
     lock (this)
     {
         ITestingUnit elementinstan = Construire(felement);
         _storage.Add(elementinstan);
         return(true);
     }
 }
Exemplo n.º 7
0
        private void BuildRobot(FactoryQueueElement newElement)
        {
            FactoryStatusArg factoryStatusArg = new FactoryStatusArg();

            factoryStatusArg.QueueBot = newElement;
            if (FactoryStatus != null)
            {
                FactoryStatus(this, factoryStatusArg);
            }
            BaseUnit robot = (BaseUnit)Activator.CreateInstance(newElement.Model);

            robot.UnitStatusChanged += robotUnitStatusChanged;
            robot.Status             = RobotStatus.Built;
        }
Exemplo n.º 8
0
        /// <summary>
        /// On ajoute une nouveau robot dans la file d'attente
        /// </summary>
        /// <param name="modeleName">Le modèle du robot à ajouter</param>
        /// <param name="unitName">Le nom du robot</param>
        /// <param name="parkingPos">La position de station du robot</param>
        /// <param name="workingPos">La position de travail du robot</param>
        public void AddWorkableUnitToQueue(Type modeleName, string unitName, Coordinates parkingPos, Coordinates workingPos)
        {
            // On vérifie si il reste de la place dans la file d'attente et dans l'entrepot
            if ((_queue.Count() < QueueCapacity) && (_storage.Count() + _queue.Count < StorageCapacity))
            {
                // On ajoute un élément dans la file d'attente
                FactoryQueueElement queueElement = new FactoryQueueElement(modeleName, unitName, parkingPos, workingPos);
                _queue.Enqueue(queueElement);



                //On ajoute du temps dans la file d'attente
                ITestingUnit recupTime = Activator.CreateInstance(queueElement.Model, new object[] { }) as ITestingUnit;
                QueueTime = QueueTime.Add(new TimeSpan(0, 0, Convert.ToInt32(recupTime.BuildTime)));

                //Création d'un thread permettant de construire un robot (un seul robot est créé à la fois)
                Thread threadBuildUnit = new Thread(() =>
                {
                    lock (_lockBuildingUnit)
                    {
                        FactoryQueueElement nextQueueElement = (FactoryQueueElement)_queue.Peek();
                        // On récupére par reflexion les informations sur un robot
                        ITestingUnit newUnit = Activator.CreateInstance(nextQueueElement.Model, new object[] { }) as ITestingUnit;
                        newUnit.Model        = nextQueueElement.Name;
                        newUnit.ParkingPos   = nextQueueElement.ParkingPos;
                        newUnit.WorkingPos   = nextQueueElement.WorkingPos;
                        BuildingUnit(newUnit);
                    }
                });

                threadBuildUnit.Start();
            }
            else
            {
                if (_queue.Count() >= QueueCapacity)
                {
                    MessageBox.Show("Max unit in queue");
                }
                else
                {
                    MessageBox.Show("Max unit in factory");
                }
            }
        }
Exemplo n.º 9
0
        public bool AddWorkableUnitToQueue(Type model, string name, Coordinates parkingPos, Coordinates workingPos)
        {
            var isAdded = false;

            if (Queue.Count < QueueCapacity)
            {
                FactoryQueueElement queueElement = new FactoryQueueElement();
                queueElement.Model      = model;
                queueElement.Name       = name;
                queueElement.ParkingPos = parkingPos;
                queueElement.WorkingPos = workingPos;
                _iFactoryQueueElement   = queueElement;
                Queue.Enqueue(_iFactoryQueueElement);
                isAdded = true;
                var createThread = new Thread(new ThreadStart(CreateUnit));
                createThread.Start();
            }
            return(isAdded);
        }
Exemplo n.º 10
0
        public bool AddWorkableUnitToQueue(Type model, string name, Coordinates parkingPos, Coordinates workingPos)
        {
            if (this.QueueFreeSlots > 0 && this.QueueFreeSlots <= this.QueueCapacity &&
                this.StorageFreeSlots > 0 && this.StorageFreeSlots <= this.StorageCapacity)
            {
                // Création d'un nouveau robot à créer dans la queue
                FactoryQueueElement queueElement = new FactoryQueueElement(name, model, parkingPos, workingPos);
                this.Queue.Add(queueElement);
                this.QueueFreeSlots   -= 1;
                this.StorageFreeSlots -= 1;

                //Parallel.Invoke(() => this.BuildRobot(queueElement));
                Task task = Task.Run(() => this.BuildRobot(queueElement));
                task.Wait();
                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        public bool AddWorkableUnitToQueue(Type model, string name, Coordinates parkingPos, Coordinates workingPos)
        {
            if (Queue.Count > QueueCapacity || Storage.Count > StorageCapacity)
            {
                // Ajouter le retour
                return(false);
            }

            FactoryQueueElement newElement = new FactoryQueueElement();

            newElement.Name       = name;
            newElement.Model      = model;
            newElement.ParkingPos = parkingPos;
            newElement.WorkingPos = workingPos;

            Queue.Enqueue(newElement);
            BuildRobot(newElement);

            return(true);
        }
Exemplo n.º 12
0
        public bool AddWorkableUnitToQueue(Type model, string name, Coordinates parkingpos, Coordinates workingpos)
        {
            QueueFreeSlots   = QueueCapacity - _queue.Count;        // QFS = 5  - x
            StorageFreeSlots = StorageCapacity - _storage.Count;    // SFS = 10 - x

            if ((QueueFreeSlots > 0) && (StorageFreeSlots > 0) && (StorageFreeSlots - _queue.Count > 0))
            {
                var fqe = new FactoryQueueElement(model, name, parkingpos, workingpos);
                _queue.Add(fqe);
                if (!factoryIsUpAndRunning)
                {
                    if (Monitor.TryEnter(obj))
                    {
                        Monitor.Pulse(obj);
                        Monitor.Exit(obj);
                    }
                }
                return(true);
            }

            return(false);
        }
        public ITestingUnit Construire(FactoryQueueElement factoryelement)
        {
            // Create an instance of the ITestingUnit type using
            // Activator.CreateInstance.
            ITestingUnit testUnit = (ITestingUnit)Activator.CreateInstance(factoryelement.Model);

            Thread.Sleep(TimeSpan.FromSeconds(testUnit.BuildTime));

            testUnit.ParkingPos = factoryelement.ParkingPos;
            testUnit.WorkingPos = factoryelement.WorkingPos;
            testUnit.CurrentPos = factoryelement.ParkingPos;
            testUnit.Name       = factoryelement.Name;
            testUnit.Model      = factoryelement.Model.Name;

            OnStatusChanged(testUnit, new StatusChangedEventArgs()
            {
                NewStatus = "I'm building my bot.."
            });
            QueueTime += TimeSpan.FromSeconds(testUnit.BuildTime);

            return(testUnit);
        }
Exemplo n.º 14
0
        /*
         *      private void ConstructingUnitAndAddToStorage()
         *      {
         *          Console.Write("Construction en cours");
         *          //OnStatusChanged(new StatusChangedEventArgs("Starting construction of robots", queue[0], null));
         *          if (storage.Count < queueCapacity)
         *          {
         *              Thread thread = new Thread(() =>
         *              {
         *                  while (queue.Count != 0)
         *                  {
         *                      Object activ = Activator.CreateInstance(queue[0].Model);
         *                      double _buildTime = ((WorkingUnit)activ).BuildTime;
         *                      bool _isWorking = ((WorkingUnit)activ).isWorking;
         *                      string _model = queue[0].Model.Name;
         *                      string _name = queue[0].Name;
         *                      double _speed = ((WorkingUnit)activ).Speed;
         *                      Coordinates _parkingPos = queue[0].ParkingPos;
         *                      Coordinates _workingPos = queue[0].WorkingPos;
         *                      Console.WriteLine("Construction of the thread..., it takes :" + _buildTime);
         *                      Thread.Sleep(TimeSpan.FromSeconds(_buildTime));
         *                      Console.WriteLine("End of construction");
         *                      TestingUnit unit = new TestingUnit(_buildTime, _isWorking, _model, _name, _speed, _parkingPos, _workingPos);
         *                      Console.WriteLine("Adding unit to storage");
         *                      storage.Add(unit);
         *                      Console.WriteLine("Unit " + unit.name + " added to storage");
         *                      queue.Remove(queue[0]);
         *                      //OnStatusChanged(new StatusChangedEventArgs("Starting construction of robots", null, unit));
         *                  }
         *              });
         *
         *              thread.Name = "machine";
         *              thread.Start();
         *
         *              //OnStatusChanged(new StatusChangedEventArgs("Construction of robots is done", queue[0], null));
         *              Console.WriteLine("End of construction");
         *          }
         *          else
         *          {
         *              Console.Write("We cannot add any robot,  storage is full");
         *          }
         *      }
         */

        public void AddWorkableUnitToQueue(Type model, string name, Coordinates parkingPos,
                                           Coordinates workingPos)
        {
            FactoryQueueElement nesfqe = new FactoryQueueElement();

            nesfqe.Model      = model;
            nesfqe.Name       = name;
            nesfqe.ParkingPos = parkingPos;
            nesfqe.WorkingPos = workingPos;

            if (queue.Count < storageCapacity - storage.Count && queue.Count < queueCapacity)
            {
                queue.Add(nesfqe);
                OnStatusChanged(new StatusChangedEventArgs("Adding the unit to the queue ", null, null));
                Console.WriteLine("L'unité est ajouté à la queue");
                //ConstructingUnitAndAddToStorage();
                Thread thread = new Thread(constructionThread);
                thread.Start();
            }
            else
            {
                Console.WriteLine("L'unité ne peut pas être ajouté car il ne reste plus de place");
            }
        }
        public async Task <bool> AddWorkableUnitToQueue(Type model, string name, Coordinates parkingpos, Coordinates workingpos)
        {
            FactoryQueueElement fqe = null;

            QueueFreeSlots   = QueueCapacity - _queue.Count;
            StorageFreeSlots = StorageCapacity - _storage.Count;

            if ((QueueFreeSlots > 0) && (StorageFreeSlots > 0) && (StorageFreeSlots - _queue.Count > 0))
            {
                fqe = new FactoryQueueElement()
                {
                    Name = name, Model = model, ParkingPos = parkingpos, WorkingPos = workingpos
                };
                _queue.Add(fqe);
            }
            bool result = await Task.Run(() => { return(CreaInstance(fqe)); });

            if (_queue.Count() > 0 && result)
            {
                _queue.Remove(fqe);
            }

            return(result);
        }