private ProcessEvent getProcessEvent(Process p) { p.Index = GenerateRandomErrorIndex(1, 5, p.Index); switch (p.Index) { case 1: p.Index = selectRand(2, 4, 5); // can jump to return(new ProcessStart(p.ID)); case 2: p.Index = 3; // can jump to return(new ProcessSuspend(p.ID)); case 3: p.Index = selectRand(2, 4, 5); // can jump to return(new ProcessResume(p.ID)); case 4: p.Index = 1; // can jump to return(new ProcessStop(p.ID)); default: p.Index = -1; // mark for remove return(new ProcessComplete(p.ID)); } }
// this is very complex and ugly for simulation purpose protected void Update() { // clean _tasks.RemoveAll(t => t.Index == -1); _tasks.RemoveAll(t => _processes.Any(p => t.ProcessID == p.ID && p.Index == -1)); _processes.RemoveAll(p => p.Index == -1); var rand100 = UniformRandom.Rand.Next(0, 100); if (rand100 < 30) { // process if (rand100 < 10) { // add if (_processes.Count < _processCount) { var p = new Process(); _processes.Add(p); adaptor.SendEvent(getProcessEvent(p)); } } else { if (_processes.Count > 0) { // update var i = UniformRandom.Rand.Next(0, _processes.Count); if (_processes[i].Index == -1) { // Remove completed processes _processes.RemoveAt(i); } var PE = getProcessEvent(_processes[i]); if (PE is ProcessComplete) { foreach (var t in _tasks.Where(x => x.ProcessID == PE.ProcessID)) { adaptor.SendEvent(new TaskComplete(t.ProcessID, t.ID, t.TaskType, t.TaskActor)); } _tasks.RemoveAll(ts => ts.ProcessID == PE.ProcessID); } adaptor.SendEvent(PE); } } } else { //Task if (_processes.Count <= 0) { return; } // parent process var ix = UniformRandom.Rand.Next(0, _processes.Count - 1); if (_processes[ix].Index == -1) { // Remove completed processes _tasks.RemoveAll(ts => ts.ProcessID == _processes[ix].ID); _processes.RemoveAt(ix); return; } var p = _processes[ix]; var tasksOfP = _tasks.Where(z => z.ProcessID == p.ID).ToList(); var count = tasksOfP.Count(); if (rand100 > 90) { // add if (count < _taskCount) { var t = new Task(); t.ProcessID = p.ID; t.TaskType = TaskTypes[UniformRandom.Rand.Next(0, 4)]; t.TaskActor = TaskActors[UniformRandom.Rand.Next(0, 4)]; _tasks.Add(t); adaptor.SendEvent(getTaskEvent(t)); } } else { if (count > 0) { // update var i = UniformRandom.Rand.Next(0, count); if (tasksOfP[i].Index == -1) { // Remove completed taskes _tasks.Remove(tasksOfP[i]); return; } adaptor.SendEvent(getTaskEvent(tasksOfP[i])); } } } }