Ejemplo n.º 1
0
 private void Go()
 {
     lock (this)
     {
         if (_storage.Count < StorageCapacity)
         {
             IFactoryQueueElement element = _queue[0];
             //construction d'un element testable
             _instance = Activator.CreateInstance(element.Model);
             ITestingUnit testingUnit = (ITestingUnit)_instance;
             testingUnit.Name       = element.Name;
             testingUnit.WorkingPos = element.WorkingPos;
             testingUnit.ParkingPos = element.ParkingPos;
             _buildTime             = testingUnit.BuildTime;
             Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}: Le temps de construction = {_buildTime}");
             OnStatusChanged(new StatusChangedEventArgs()
             {
                 NewStatus = "Start", QueueElement = _queue[0]
             });
             Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}: Construction en cours...");
             Thread.Sleep(TimeSpan.FromSeconds(_buildTime));
             Console.WriteLine($"{DateTime.UtcNow.ToLongTimeString()}: La construction est terminée après {_buildTime} secondes");
             queueTime = TimeSpan.FromSeconds(_buildTime);
             _queue.Remove(element);
             _storage.Add(testingUnit);
             OnStatusChanged(new StatusChangedEventArgs()
             {
                 NewStatus = "End", TestingUnit = testingUnit
             });
         }
     }
 }
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo n.º 4
0
 private void CreateUnit()
 {
     lock (_mutex)
     {
         if (Storage.Count < StorageCapacity)
         {
             IFactoryQueueElement elementToCreate = Queue.Peek();
             var          modelInstance           = Activator.CreateInstance(elementToCreate.Model);
             ITestingUnit testingUnit             = (ITestingUnit)modelInstance;
             testingUnit.Name       = elementToCreate.Name;
             testingUnit.ParkingPos = elementToCreate.ParkingPos;
             testingUnit.WorkingPos = elementToCreate.WorkingPos;
             FactoryProgressEventArgs arg = new FactoryProgressEventArgs();
             arg.NewStatus    = "Creation start";
             arg.QueueElement = elementToCreate;
             OnStatusChanged(arg);
             var buildTime = testingUnit.BuildTime;
             Thread.Sleep(TimeSpan.FromSeconds(buildTime));
             QueueTime = TimeSpan.FromSeconds(buildTime);
             Queue.Dequeue();
             Storage.Add(testingUnit);
             arg             = new FactoryProgressEventArgs();
             arg.NewStatus   = "Creation end";
             arg.TestingUnit = testingUnit;
             OnStatusChanged(arg);
         }
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Construit les robots qui sont dans la liste de commande
        /// </summary>
        private async void BuildWorkableUnit()
        {
            // Pseudo mutex
            m_IsBuilding = true;
            ITestingUnit lUnit = null;

            // On teste que l'on a pas atteint la capacité max et qu'il reste des éléments dans la Queue

            while (m_Storage.Count <= StorageCapacity && m_Queue.Count > 0)
            {
                lUnit            = Activator.CreateInstance(m_Queue[0].Model, new string[] { m_Queue[0].Name }) as ITestingUnit;
                lUnit.WorkingPos = m_Queue[0].WorkingPos;
                lUnit.ParkingPos = m_Queue[0].ParkingPos;
                await Task.Delay((int)(500d * lUnit.BuildTime));

                m_Queue.RemoveAt(0);
                m_Storage.Add(lUnit);

                //On met à jour l'event
                FactoryProgressEventArgs lNewUnit = new FactoryProgressEventArgs();
                lNewUnit.TestingUnit = lUnit;
                OnStatusChanged(lNewUnit);
            }

            // previent que les constructions sont finies
            m_IsBuilding = false;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Construction d'un robot
 /// </summary>
 /// <param name="newUnit">Le robot à construire</param>
 private void BuildingUnit(ITestingUnit newUnit)
 {
     OnStatusChangedFactory(new StatusChangedEventArgs(string.Format("Start build unit {0} : {1}", newUnit.Model, newUnit.Name)));
     Thread.Sleep(Convert.ToInt32(newUnit.BuildTime) * 1000);
     _storage.Add(newUnit);
     _queue.Dequeue();
     QueueTime = QueueTime.Add(new TimeSpan(0, 0, Convert.ToInt32(-newUnit.BuildTime)));
     OnStatusChangedFactory(new StatusChangedEventArgs(string.Format("End build unit {0} : {1}", newUnit.Model, newUnit.Name)));
 }
 public bool CreaInstance(FactoryQueueElement felement)
 {
     lock (this)
     {
         ITestingUnit elementinstan = Construire(felement);
         _storage.Add(elementinstan);
         return(true);
     }
 }
Ejemplo n.º 8
0
 //This thread constructs a bot
 private void Construction(ITestingUnit bot)
 {
     lock (Storage)
     {
         Thread.Sleep((int)bot.BuildTime * 1000);
         Debug.WriteLine("Robot Construit");
         Monitor.Pulse(Storage);
     }
 }
Ejemplo n.º 9
0
        public void Construire()
        {
            Monitor.Enter(obj);
            lock (obj)
            {
                // Create an instance of the ITestingUnit type using
                // Activator.CreateInstance.
                while (!factoryIsUpAndRunning)
                {
                    if (factoryIsUpAndRunning || (Queue.Count == 0))
                    {
                        OnStatusChanged(this, new StatusChangedEventArgs()
                        {
                            NewStatus = "hello................."
                        });
                    }
                    else
                    {
                        factoryIsUpAndRunning = true;

                        IFactoryQueueElement factoryelement = _queue[0];
                        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);


                        OnStatusChanged(testUnit, new StatusChangedEventArgs()
                        {
                            NewStatus = "Add to Storage.."
                        });
                        _storage.Add(testUnit);
                        _queue.Remove(factoryelement);

                        factoryIsUpAndRunning = false;
                    }
                }
            }
        }
Ejemplo n.º 10
0
 //This thread constructs a bot
 private void Construction(ITestingUnit bot)
 {
     if (Storage.Count < StorageCapacity)
     {
         lock (Storage)
         {
             Thread.Sleep((int)bot.BuildTime * 1000);
             Debug.WriteLine("Robot Construit");
             Storage.Add(bot);
             StorageFreeSlots--;
             QueueFreeSlots++;
             Monitor.Pulse(Storage);
         }
     }
 }
Ejemplo n.º 11
0
        private void robotUnitStatusChanged(object robot, EventArgs statusArgs)
        {
            StatusChangedEventArgs status = (StatusChangedEventArgs)statusArgs;

            if (status.NewStatus == RobotStatus.Built.ToString())
            {
                ITestingUnit robotCast = (ITestingUnit)robot;
                Storage.Add(robotCast);
                Queue.Dequeue();
                FactoryStatusArg factoryStatusArg = new FactoryStatusArg();
                factoryStatusArg.Robot = robotCast;
                if (FactoryStatus != null)
                {
                    FactoryStatus(this, factoryStatusArg);
                }
            }
        }
Ejemplo n.º 12
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");
                }
            }
        }
Ejemplo n.º 13
0
        private void AddUnitToQueue_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ModelsList.SelectedIndex >= 0 && !String.IsNullOrEmpty(UnitName.Text))
            {
                Type item = (Type)ModelsList.SelectedItem;

                ITestingUnit newUnit = Activator.CreateInstance(item, new object[] { }) as ITestingUnit;

                string stringToDisplay = string.Format("Vous avez choisi de construire le robot {0} qui a pour temps de construction {1} secondes, Etes-vous sûr de vouloir construire ce robot ?", newUnit.Name, newUnit.BuildTime);

                var name = UnitName.Text;
                if (MessageBox.Show(stringToDisplay, "", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    _dataContext.Builder.AddWorkableUnitToQueue(item, name, new Coordinates(0, 0), new Coordinates(10, 10));
                    //MessageBox.Show(new StatusChangedEventArgs("blaal").NewStatus);
                    _dataContext.ForceUpdate();
                }
            }
        }
        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);
        }
Ejemplo n.º 15
0
 public void SetUnitToTest(ITestingUnit unit)
 {
     _unitDataContext.IBot = unit;
 }
Ejemplo n.º 16
0
 public StatusChangedEventArgs(string _newStatus, IFactoryQueueElement _firstQE, ITestingUnit _testunit)
 {
     this.newStatus = _newStatus;
     this.firstQE   = _firstQE;
     this.testunit  = _testunit;
 }