def OnViewLog(self, event): """ When View button is clicked """ # The simulation verbose event occurs pluginmanager.trigger_event("START_SIM_VERBOSE", parent=self) # The simulation verbose event occurs pluginmanager.trigger_event("VIEW_ACTIVITY_REPORT", parent=self, master=self.current_master)
def simulate(self, T=sys.maxint): """ """ master = self._simulator.getMaster() send = self._simulator.send #clock = master.myTimeAdvance # Initialize the model --- set the simulation clock to 0. send(master, (0, [], 0)) clock = master.myTimeAdvance ### ref to cpu time evaluation t_start = time.time() ### if suspend, we could store the future ref old_cpu_time = 0 ### stoping condition depend on the ntl (no time limit for the simulation) condition = lambda clock: HasActiveChild(getFlatImmChildrenList(master, [])) if self._simulator.ntl else clock <= T #self._simulator.s = shelve.open('toto.db',writeback = True) #self._simulator.s['s'] = {} #self._simulator.s.close() # Main loop repeatedly sends $(*,\,t)$ messages to the model's root DEVS. while condition(clock) and self._simulator.end_flag == False: ##Optional sleep if self._simulator.thread_sleep: time.sleep(self._simulator._sleeptime) elif self._simulator.thread_suspend: ### Optional suspend while self._simulator.thread_suspend: time.sleep(1.0) old_cpu_time = self._simulator.cpu_time t_start = time.time() else: # The SIM_VERBOSE event occurs pluginmanager.trigger_event("SIM_VERBOSE", clock=clock) send(master, (1, {}, clock)) clock = master.myTimeAdvance self._simulator.cpu_time = old_cpu_time + (time.time() - t_start) #self.Trace(clock, model) #q.put((clock,self._simulator)) ### for back simulation process #self._simulator.s = shelve.open('toto.db',writeback = True) #self._simulator.s['s'][str(clock)] = self._simulator #self._simulator.s.close() self._simulator.terminate()
def simulate(self, T=sys.maxint): """ """ ### ref to cpu time evaluation t_start = time.time() ### if suspend, we could store the future ref old_cpu_time = 0 ### stoping condition depend on the ntl (no time limit for the simulation) condition = lambda clk: HasActiveChild(getFlatPriorityList(self.master, [])) if self._simulator.ntl else clk <= T ### simualtion time and list of flat models ordered by devs priority self.ts.Set(min([m.myTimeAdvance for m in self.flat_priority_list if m.myTimeAdvance < INFINITY])) formated_priority_list = [(1 + i / 10000.0, m, execIntTransition) for i, m in enumerate(self.flat_priority_list)] while condition(self.ts.Get()) and self._simulator.end_flag == False: ### Optional sleep if self._simulator.thread_sleep: time.sleep(self._simulator._sleeptime) elif self._simulator.thread_suspend: ### Optional suspend while self._simulator.thread_suspend: time.sleep(1.0) old_cpu_time = self._simulator.cpu_time t_start = time.time() else: ### The SIM_VERBOSE event occurs pluginmanager.trigger_event("SIM_VERBOSE", self.master, None, clock=self.ts.Get()) ### tree-like data structure ordered by devsimpy priority priority_scheduler = filter(lambda a: self.ts.Get() == a[1].myTimeAdvance, formated_priority_list) heapq.heapify(priority_scheduler) ### TODO: execute with process of model are parallel ! while priority_scheduler: ### get most priority model and apply its internal trnasition priority, model, transition_fct = heapq.heappop(priority_scheduler) apply(transition_fct, (model,)) ### update simulation time self.ts.Set(min([m.myTimeAdvance for m in self.flat_priority_list])) ### just for progress bar self.master.timeLast = self.ts.Get() if self.ts.Get() != INFINITY else self.master.timeLast self._simulator.cpu_time = old_cpu_time + (time.time() - t_start) self._simulator.terminate()
def send(self, d, msg): """ Dispatch messages to the right method. """ if isinstance(d, DEVS.CoupledDEVS): CS = CoupledSolver() r = CS.receive(d, msg) else: AS = AtomicSolver() r = AS.receive(d, msg) pluginmanager.trigger_event("SIM_BLINK", model=d, msg=msg) pluginmanager.trigger_event("SIM_TEST", model=d, msg=msg) return r
def execIntTransition(m): """ """ ts = m.ts.Get() if m.timeNext != INFINITY: m.outputFnc() m.elapsed = ts - m.timeLast m.intTransition() m.timeLast = ts m.myTimeAdvance = m.timeAdvance() m.timeNext = m.timeLast + m.myTimeAdvance if m.myTimeAdvance != INFINITY: m.myTimeAdvance += ts m.elapsed = 0.0 # The SIM_VERBOSE event occurs pluginmanager.trigger_event("SIM_VERBOSE", model=m, msg=0) pluginmanager.trigger_event("SIM_BLINK", model=m, msg=[1]) pluginmanager.trigger_event("SIM_TEST", model=m, msg=[1])
def receive(aDEVS, msg): # For any received message, the time {\tt t} (time at which the message # is sent) is the second item in the list {\tt msg}. t = msg[2] # $(*,\,t)$ message --- triggers internal transition and returns # $(y,\,t)$ message for parent coupled-DEVS: if msg[0] == 1: if t != aDEVS.timeNext: Error("Bad synchronization...1", 1) # First call the output function, which (amongst other things) rebuilds # the output dictionnary {\tt myOutput}: aDEVS.myOutput = {} aDEVS.outputFnc() aDEVS.elapsed = t - aDEVS.timeLast aDEVS.intTransition() aDEVS.timeLast = t aDEVS.myTimeAdvance = aDEVS.timeAdvance() aDEVS.timeNext = aDEVS.timeLast + aDEVS.myTimeAdvance if aDEVS.myTimeAdvance != INFINITY: aDEVS.myTimeAdvance += t aDEVS.elapsed = 0 # The SIM_VERBOSE event occurs pluginmanager.trigger_event("SIM_VERBOSE", model=aDEVS, msg=0) # Return the DEVS' output to the parent coupled-DEVS (rather than # sending $(y,\,t)$ message). return aDEVS.myOutput # ${x,\,t)$ message --- triggers external transition, where $x$ is the # input dictionnary to the DEVS: elif isinstance(msg[0], dict): if not (aDEVS.timeLast <= t <= aDEVS.timeNext): Error("Bad synchronization...2", 1) aDEVS.myInput = msg[0] # update elapsed time. This is necessary for the call to the external # transition function, which is used to update the DEVS' state. aDEVS.elapsed = t - aDEVS.timeLast aDEVS.extTransition() # Udpate time variables: aDEVS.timeLast = t aDEVS.myTimeAdvance = aDEVS.timeAdvance() aDEVS.timeNext = aDEVS.timeLast + aDEVS.myTimeAdvance if aDEVS.myTimeAdvance != INFINITY: aDEVS.myTimeAdvance += t aDEVS.elapsed = 0 # The SIM_VERBOSE event occurs pluginmanager.trigger_event("SIM_VERBOSE", model=aDEVS, msg=1) # $(i,\,t)$ message --- sets origin of time at {\tt t}: elif msg[0] == 0: aDEVS.timeLast = t - aDEVS.elapsed aDEVS.myTimeAdvance = aDEVS.timeAdvance() aDEVS.timeNext = aDEVS.timeLast + aDEVS.myTimeAdvance if aDEVS.myTimeAdvance != INFINITY: aDEVS.myTimeAdvance += t else: Error("Unrecognized message", 1)
def OnOk(self, event): """ When Run button is clicked """ assert (self.master is not None) from Core.Components.Container import Diagram if self._value.GetValidator().Validate(self._value) or self.ntl: ### pour prendre en compte les simulations multiples sans relancer un SimulationDialog ### si le thread n'est pas lance (pas pendant un suspend) if self.thread is not None and not self.thread.thread_suspend: diagram = self.master.getBlockModel() diagram.Clean() self.current_master = Diagram.makeDEVSInstance(diagram) else: self.current_master = self.master if isinstance(self.parent, wx.Panel): # redirection du stdout ici dans le cas du Panel (sinon dans OnSimulation) mainW = self.parent.GetTopLevelParent() sys.stdout = mainW.stdioWin ### test si le modele et bien charge if (self.current_master is None) or (self.current_master.componentSet == []): return self.MsgBoxEmptyModel() ### dont erase the gauge if ntl if not self.ntl: # stockage du temps de simulation dans le master self.current_master.FINAL_TIME = float(self._value.GetValue()) self._gauge.SetValue(0) ### if _gauge is wx.Slider #self._gauge.SetMax(self.current_master.FINAL_TIME) self.statusbar.SetBackgroundColour('') self.statusbar.SetStatusText("", 1) if self.statusbar.GetFieldsCount() > 2: self.statusbar.SetStatusText("", 2) if (self.thread is None) or (not self.timer.IsRunning()): pluginmanager.trigger_event("START_BLINK", parent=self, master=self.current_master) pluginmanager.trigger_event("START_TEST", parent=self, master=self.current_master) ### The START_ACTIVITY_TRACKING event occurs pluginmanager.trigger_event("START_ACTIVITY_TRACKING", parent=self, master=self.current_master) ### The START_CONCURRENT_SIMULATION event occurs pluginmanager.trigger_event("START_CONCURRENT_SIMULATION", parent=self, master=self.current_master) ### clear all log file for fn in filter(lambda f: f.endswith('.devsimpy.log'), os.listdir(gettempdir())): os.remove(os.path.join(gettempdir(), fn)) self.thread = SimulationThread(self.current_master, self.selected_strategy, self.prof, self.ntl) self.thread.setName(self.title) ### si le modele n'a pas de couplage, ou si pas de generateur: alors pas besoin de simuler if self.thread.end_flag: self.OnTimer(event) else: self.timer.Start(100) else: #print self.thread.getAlgorithm().trace ### for back simulation #self.thread.s = shelve.open(self.thread.f.name+'.db',flag = 'r') #self.thread.model = self.thread.s['s'][str(float(self._gauge.GetValue()))] ### restart the hiding gauge if self.ntl: self._gauge.Show() ### restart thread self.thread.resume_thread() self.Interact(False) if self.count >= 100: return