def changeToRunMode( self ): self.preRunSuccessful= False self.preLoadSuccessful = False # DO VALIDATION first!!! # destroy BoardWindow, save tracerlist if self.theModelEditor.theResultsWindow != None: self.theModelEditor.theResultsWindow.closeBoardWindow() # destroy previous SessionMonitor: if self.theSession != None: # deregister previous callbacks self.theSession = None #save model to temp file if not self.theModelEditor.autoSave(): return fileName = self.theModelEditor.autoSaveName self.theModelEditor.theMainWindow.displayHourglass() #instantiate GtkSessionMonitor self.theSession = GtkSessionMonitor() self.theSession.setMessageMethod( self.message ) self.theSession.registerUpdateCallback( self.updateWindows ) #load model into GtkSessionMonitor self.__preRun() if not self.preLoadSuccessful: dialog = ConfirmWindow(1,"This operation needs loading model into Simulator, but your model is not stable!\nTest load hanged or crashed! Are you sure you want to load model into Simulator?\nIf you choose yes, ModelEditor can hang or crash.\n(Changes are saved)", "CRITICAL" ) if dialog.return_result() != 0: self.theModelEditor.theMainWindow.resetCursor() return False try: self.theSession.loadModel( fileName ) #self.theSession.theSimulator.initialize() except: self.message(' Error while trying to parse model into simulator \n') anErrorMessage = string.join( traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback), '\n' ) self.message(anErrorMessage) self.theModelEditor.theMainWindow.resetCursor() dialog = ConfirmWindow(0,"Sorry, error in parsing model, see message window for details." ) return False self.theMode = ME_RUN_MODE if self.theModelEditor.theResultsWindow != None: self.theModelEditor.theResultsWindow.openBoardWindow(self.theSession) self.theModelEditor.theMainWindow.resetCursor() self.theModelEditor.theMainWindow.updateRunMode() return True
def main(): # ------------------------------------- # initialize file names # ------------------------------------- anEmlFile = None anEssFile = None # ------------------------------------- # gets options # ------------------------------------- try: opts , args = getopt.getopt( sys.argv[1:] , 'he:f:', ["help", "exec=", "file="]) except: usage() sys.exit(1) # ------------------------------------- # checks argument # ------------------------------------- for anOption, anArg in opts: # prints help message if anOption in ( "-h", '--help' ): usage() sys.exit(0) # executes script file (.ess) if anOption in ( "-e", '--exec'): anEssFile = anArg # load model file (.eml) if anOption in ( "-f", '--file' ): anEmlFile = anArg # ------------------------------------- # prohibits to use -e and -f options # ------------------------------------- if anEmlFile != None and anEssFile != None: usage() sys.exit(0) # ------------------------------------- # creates an instance of GtkSession and # creates MainWindow instance # ------------------------------------- aSession = GtkSessionMonitor() # ------------------------------------- # executes options # ------------------------------------- # load model file (.eml) if anEmlFile != None: # check EML File if os.path.isfile( anEmlFile ): pass else: aMessage = " Error ! [%s] No such file. \n" %anEmlFile aSession.message( aMessage ) sys.exit(1) # print message aSession.message("%s is loaded.\n" %anEmlFile ) # load model try: aSession.loadModel( anEmlFile ) except: aSession.message(' can\'t load [%s]' %anEmlFile) anErrorMessage = '\n'.join( traceback.format_exception( sys.exc_type,sys.exc_value,sys.exc_traceback ) ) aSession.message("-----------") aSession.message(anErrorMessage) aSession.message("-----------") # executes script file (.ess) elif anEssFile != None: # check ESS File if os.path.isfile( anEssFile ): pass else: aMessage = " Error ! [%s] No such file. \n" %anEssFile aSession.message( aMessage ) sys.exit(1) # print message on MainWindow aSession.message("%s is being loaded and executed.\n" %anEssFile ) gobject.timeout_add( 1, loadScript, [ aSession, anEssFile ] ) aMainWindow = aSession.openWindow('MainWindow') aSession.updateWindows() # ------------------------------------- # calls gtk.main() # ------------------------------------- aSession.GUI_interact()
class Runtime: def __init__( self, aModelEditor ): #here initialize model self.theMode = ME_DESIGN_MODE self.theSession = None self.theModelEditor = aModelEditor self.testModels = [] def closeModel( self ): self.changeToDesignMode( True ) def createNewProcess( self, testPyFile ): if os.name == 'nt': return os.spawnl( os.P_NOWAIT, ecell3_session_path, '"' + ecell3_session_path + '"', '"' + testPyFile + '"' ) else: return os.spawnl( os.P_NOWAIT, ecell3_session_path, ecell3_session_path , testPyFile ) def killProcess ( self, processID ): if os.name == 'nt': try: win32api.TerminateProcess( processID,0 ) except: pass else: os.kill( processID,9 ) def __preRun( self ): # THIS FUNCTION IS LINUX DEPENDENT!!!! # a possible workaround for using it under Windows is # use os.spawnl instead of os.spawnlp - the concrete path for ecell3-session must be found! # use import winapi32, winapi32.TerminateProcess( pid) instead of os.kill #attempting loading and executing model on a separate thread and check whether it is wise to run it in this thread if self.preRunSuccessful: return True tempDir = os.getcwd() processID = str(os.getpid() ) fileName = self.theModelEditor.autoSaveName testPyFile = tempDir + os.sep + "test" + processID + ".py" testOutFile = tempDir + os.sep + "test" + processID + ".out" testFile = "\ list=[None, None]\n\ delay=10\n\ import gtk\n\ fd=open(r'" + testOutFile + "','w');fd.write('started'+chr(10));fd.flush()\n\ def timerhandler():\n\ fd.write('a');fd.flush()\n\ list[0]=gtk.timeout_add(delay,timerhandler)\n\ def mainfunction():\n\ list[0]=gtk.timeout_add(delay, timerhandler)\n\ try:\n\ loadModel(r'" + fileName + "')\n\ except:\n\ pass\n\ gtk.timeout_remove(list[0])\n\ fd.write(chr(10)+'loaded'+chr(10));fd.flush()\n\ try:\n\ #self.theSimulator.initialize()\n\ step(3)\n\ except:\n\ pass\n\ fd.write('finished'+chr(10));fd.close()\n\ gtk.main_quit()\n\ gtk.timeout_add( 10, mainfunction )\n\ gtk.main()\n\ " fd=open( testPyFile,'w' ) fd.write( testFile ) fd.close() fd=open( testOutFile, 'w' ) fd.close() pid = self.createNewProcess( testPyFile ) # first get started signal startedTime = time.time() startstr = '' a=[] while len(a)<1 and (time.time()-startedTime )< TIME_THRESHOLD: try: fd=open( testOutFile,'r' ) a=fd.readlines( ) fd.close() except: pass if len(a)>0: startstr=a[0].strip() if startstr!="started": try: os.remove( testOutFile ) os.remove( testPyFile ) except: pass return False # must load it!!! lastReadTime = time.time() lastLength = 0 while len(a)<3 and lastReadTime-time.time()<TIME_THRESHOLD: fd=open( testOutFile,'r' ) a=fd.readlines( ) fd.close() if len(a)>1: newLength=len(a[1].strip()) if newLength > lastLength: lastLength = newLength lastReadTime = time.time() if len(a) > 2: self.preLoadSuccessful = True else: try: os.remove( testOutFile ) os.remove( testPyFile ) except: pass self.killProcess( pid) return False loadedTime = time.time() while startstr!='finished' and (time.time()-loadedTime )< TIME_THRESHOLD: fd=open( testOutFile,'r' ) a=fd.readlines( ) fd.close() if len(a)>3: startstr=a[3].strip() try: os.remove( testOutFile ) os.remove( testPyFile ) except: pass if len(a)<4: # hanging or segfault self.killProcess( pid ) return False else: self.preRunSuccessful = True return True def changeToRunMode( self ): self.preRunSuccessful= False self.preLoadSuccessful = False # DO VALIDATION first!!! # destroy BoardWindow, save tracerlist if self.theModelEditor.theResultsWindow != None: self.theModelEditor.theResultsWindow.closeBoardWindow() # destroy previous SessionMonitor: if self.theSession != None: # deregister previous callbacks self.theSession = None #save model to temp file if not self.theModelEditor.autoSave(): return fileName = self.theModelEditor.autoSaveName self.theModelEditor.theMainWindow.displayHourglass() #instantiate GtkSessionMonitor self.theSession = GtkSessionMonitor() self.theSession.setMessageMethod( self.message ) self.theSession.registerUpdateCallback( self.updateWindows ) #load model into GtkSessionMonitor self.__preRun() if not self.preLoadSuccessful: dialog = ConfirmWindow(1,"This operation needs loading model into Simulator, but your model is not stable!\nTest load hanged or crashed! Are you sure you want to load model into Simulator?\nIf you choose yes, ModelEditor can hang or crash.\n(Changes are saved)", "CRITICAL" ) if dialog.return_result() != 0: self.theModelEditor.theMainWindow.resetCursor() return False try: self.theSession.loadModel( fileName ) #self.theSession.theSimulator.initialize() except: self.message(' Error while trying to parse model into simulator \n') anErrorMessage = string.join( traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback), '\n' ) self.message(anErrorMessage) self.theModelEditor.theMainWindow.resetCursor() dialog = ConfirmWindow(0,"Sorry, error in parsing model, see message window for details." ) return False self.theMode = ME_RUN_MODE if self.theModelEditor.theResultsWindow != None: self.theModelEditor.theResultsWindow.openBoardWindow(self.theSession) self.theModelEditor.theMainWindow.resetCursor() self.theModelEditor.theMainWindow.updateRunMode() return True def message( self, text): if type(text) == type(""): if ( text.find( "Start" ) or text.find( "Stop" ) ) and \ text.strip().find("\n") == -1 : self.theModelEditor.printMessage( text ) return utils.showPopupMessage( utils.OK_MODE, text, ME_ERROR ) def isRunning( self ): if self.theSession == None: return False return self.theSession.isRunning() def changeToDesignMode( self, forced = False ): # check whether simulation is running if self.isRunning(): if not forced: dialog = ConfirmWindow(1, "To perform this operation, simulation must be stopped.\n Can simulation be stopped?") if dialog.return_result() == 0: self.theSession.stop() else: return False else: self.theSession.stop() self.theMode = ME_DESIGN_MODE self.theModelEditor.theMainWindow.updateRunMode() return True def createTracerWindow( self,fullPNStringList ): if self.theModelEditor.theResultsWindow == None: self.theModelEditor.createResultsWindow() #pass in number of columns self.theModelEditor.theResultsWindow.createTracer( fullPNStringList ) def attachToTracerWindow( self, aTitle, fullPNStringList ): self.theModelEditor.theResultsWindow.attachToTracerWindow( aTitle, fullPNStringList ) def getTracerList( self ): if self.theModelEditor.theResultsWindow != None: return self.theModelEditor.theResultsWindow.getTracerList() else: return [] def __canRun( self ): if not self.preRunSuccessful : dialog = ConfirmWindow(1,"Your model is not stable for runnig! Test run hanged or crashed!\nAre you sure you want to run in ModelEditor?\nIf you choose yes, ModelEditor can hang or crash.\n(Changes are saved)", "CRITICAL" ) if dialog.return_result() != 0: return False return True def run(self , aTime = 0 ): #do run test if self.__canRun(): self.theSession.run( aTime ) def stop( self ): self.theSession.stop() def step( self, aNum ): if self.__canRun(): self.theSession.step( aNum ) self.updateWindows() def getSession( self ): return self.theSession def checkState (self, passInState): if self.theMode != passInState: if passInState == ME_DESIGN_MODE: return self.changeToDesignMode() else: return self.changeToRunMode() return True def updateWindows( self): self.theModelEditor.theMainWindow.updateRunPanel() # update Propertylists for aStepperWindow in self.theModelEditor.theStepperWindowList: # anID None means all for steppers aStepperWindow.update( None, None ) for anEntityWindow in self.theModelEditor.theEntityListWindowList: anEntityWindow.update( None, None) if self.theModelEditor.theObjectEditorWindow!=None: self.theModelEditor.theObjectEditorWindow.update(None, None) def getSimulationTime( self ): if self.theSession == None: return 0 return self.theSession.getCurrentTime() def createTracerSubmenu( self, aFullPNStringList ): subMenu = gtk.Menu() newMenuItem = gtk.MenuItem( "New Tracer" ) newMenuItem.connect( 'activate', self.__subMenuHandler, ["newtracer",aFullPNStringList] ) if len( aFullPNStringList) == 0: newMenuItem.set_sensitive( False ) subMenu.append( newMenuItem ) for aTracer in self.getTracerList(): tracerMenu = gtk.MenuItem( aTracer ) tracerMenu.connect( 'activate', self.__subMenuHandler, [ aTracer, aFullPNStringList] ) if len( aFullPNStringList) == 0: tracerMenu.set_sensitive( False ) subMenu.append( tracerMenu ) subMenuItem = gtk.MenuItem( "Add to" ) subMenuItem.set_submenu( subMenu ) return subMenuItem def __subMenuHandler( self, menuItem, userData ): if not self.checkState( ME_RUN_MODE ): return if userData[0] == "newtracer": self.createTracerWindow( userData[1] ) else: self.attachToTracerWindow( userData[0], userData[1] )