def on_message(self, message):
        """ Receiving a message from the websocket, parse, and act accordingly. """

        msg = tornado.escape.json_decode(message)

        if msg['type'] == 'config_file':

            if self.application.verbose:
                print(msg['data'])

            self.config = list(yaml.load_all(msg['data']))

            if len(self.config) > 1:
                error = 'Please, provide only one configuration.'
                if self.application.verbose:
                    logger.error(error)
                self.write_message({'type': 'error',
                    'error': error})
                return

            self.config = self.config[0]
            self.send_log('INFO.' + self.simulation_name,
                          'Using config: {name}'.format(name=self.config['name']))

            if 'visualization_params' in self.config:
                self.write_message({'type': 'visualization_params',
                    'data': self.config['visualization_params']})
            self.name = self.config['name']
            self.run_simulation()

            settings = []
            for key in self.config['environment_params']: 
                if type(self.config['environment_params'][key]) == float or type(self.config['environment_params'][key]) == int:
                    if self.config['environment_params'][key] <= 1:
                        setting_type = 'number'
                    else:
                        setting_type = 'great_number'
                elif type(self.config['environment_params'][key]) == bool:
                    setting_type = 'boolean'
                else:
                    setting_type = 'undefined'

                settings.append({
                    'label': key,
                    'type': setting_type,
                    'value': self.config['environment_params'][key]
                })

            self.write_message({'type': 'settings',
                'data': settings})

        elif msg['type'] == 'get_trial':
            if self.application.verbose:
                logger.info('Trial {} requested!'.format(msg['data']))
            self.send_log('INFO.' + __name__, 'Trial {} requested!'.format(msg['data']))
            self.write_message({'type': 'get_trial',
                                'data': self.get_trial(int(msg['data']))})

        elif msg['type'] == 'run_simulation':
            if self.application.verbose:
                logger.info('Running new simulation for {name}'.format(name=self.config['name']))
            self.send_log('INFO.' + self.simulation_name, 'Running new simulation for {name}'.format(name=self.config['name']))
            self.config['environment_params'] = msg['data']
            self.run_simulation()

        elif msg['type'] == 'download_gexf':
            G = self.trials[ int(msg['data']) ].history_to_graph()
            for node in G.nodes():
                if 'pos' in G.node[node]:
                    G.node[node]['viz'] = {"position": {"x": G.node[node]['pos'][0], "y": G.node[node]['pos'][1], "z": 0.0}}
                    del (G.node[node]['pos'])
            writer = nx.readwrite.gexf.GEXFWriter(version='1.2draft')
            writer.add_graph(G)
            self.write_message({'type': 'download_gexf',
                'filename': self.config['name'] + '_trial_' + str(msg['data']),
                'data': tostring(writer.xml).decode(writer.encoding) })

        elif msg['type'] == 'download_json':
            G = self.trials[ int(msg['data']) ].history_to_graph()
            for node in G.nodes():
                if 'pos' in G.node[node]:
                    G.node[node]['viz'] = {"position": {"x": G.node[node]['pos'][0], "y": G.node[node]['pos'][1], "z": 0.0}}
                    del (G.node[node]['pos'])
            self.write_message({'type': 'download_json',
                'filename': self.config['name'] + '_trial_' + str(msg['data']),
                'data': nx.node_link_data(G) })

        else:
            if self.application.verbose:
                logger.info('Unexpected message!')
def on_message ( self , message ) : msg = tornado . escape . json_decode ( message ) if msg [ 'type' ] == 'config_file' : if self . application . verbose : print ( msg [ 'data' ] ) self . config = list ( yaml . load_all ( msg [ 'data' ] ) ) if len ( self . config ) > 1 : error = 'Please, provide only one configuration.' if self . application . verbose : logger . error ( error ) self . write_message ( { 'type' : 'error' , 'error' : error } ) return self . config = self . config [ 0 ] self . send_log ( 'INFO.' + self . simulation_name , 'Using config: {name}' . format ( name = self . config [ 'name' ] ) ) if 'visualization_params' in self . config : self . write_message ( { 'type' : 'visualization_params' , 'data' : self . config [ 'visualization_params' ] } ) self . name = self . config [ 'name' ] self . run_simulation ( ) settings = [ ] for key in self . config [ 'environment_params' ] : if type ( self . config [ 'environment_params' ] [ key ] ) == float or type ( self . config [ 'environment_params' ] [ key ] ) == int : if self . config [ 'environment_params' ] [ key ] <= 1 : setting_type = 'number' else : setting_type = 'great_number' elif type ( self . config [ 'environment_params' ] [ key ] ) == bool : setting_type = 'boolean' else : setting_type = 'undefined' settings . append ( { 'label' : key , 'type' : setting_type , 'value' : self . config [ 'environment_params' ] [ key ] } ) self . write_message ( { 'type' : 'settings' , 'data' : settings } ) elif msg [ 'type' ] == 'get_trial' : if self . application . verbose : logger . info ( 'Trial {} requested!' . format ( msg [ 'data' ] ) ) self . send_log ( 'INFO.' + __name__ , 'Trial {} requested!' . format ( msg [ 'data' ] ) ) self . write_message ( { 'type' : 'get_trial' , 'data' : self . get_trial ( int ( msg [ 'data' ] ) ) } ) elif msg [ 'type' ] == 'run_simulation' : if self . application . verbose : logger . info ( 'Running new simulation for {name}' . format ( name = self . config [ 'name' ] ) ) self . send_log ( 'INFO.' + self . simulation_name , 'Running new simulation for {name}' . format ( name = self . config [ 'name' ] ) ) self . config [ 'environment_params' ] = msg [ 'data' ] self . run_simulation ( ) elif msg [ 'type' ] == 'download_gexf' : G = self . trials [ int ( msg [ 'data' ] ) ] . history_to_graph ( ) for node in G . nodes ( ) : if 'pos' in G . node [ node ] : G . node [ node ] [ 'viz' ] = { "position" : { "x" : G . node [ node ] [ 'pos' ] [ 0 ] , "y" : G . node [ node ] [ 'pos' ] [ 1 ] , "z" : 0.0 } } del ( G . node [ node ] [ 'pos' ] ) writer = nx . readwrite . gexf . GEXFWriter ( version = '1.2draft' ) writer . add_graph ( G ) self . write_message ( { 'type' : 'download_gexf' , 'filename' : self . config [ 'name' ] + '_trial_' + str ( msg [ 'data' ] ) , 'data' : tostring ( writer . xml ) . decode ( writer . encoding ) } ) elif msg [ 'type' ] == 'download_json' : G = self . trials [ int ( msg [ 'data' ] ) ] . history_to_graph ( ) for node in G . nodes ( ) : if 'pos' in G . node [ node ] : G . node [ node ] [ 'viz' ] = { "position" : { "x" : G . node [ node ] [ 'pos' ] [ 0 ] , "y" : G . node [ node ] [ 'pos' ] [ 1 ] , "z" : 0.0 } } del ( G . node [ node ] [ 'pos' ] ) self . write_message ( { 'type' : 'download_json' , 'filename' : self . config [ 'name' ] + '_trial_' + str ( msg [ 'data' ] ) , 'data' : nx . node_link_data ( G ) } ) else : if self . application . verbose : logger . info ( 'Unexpected message!' )
Receiving a message from the websocket, parse, and act accordingly.