def __init__(self, url=None, session_id=None): #Record the names of the files created by the plugin so we can #display them to the user. self.files = [] #A mapping from attr ID to attr object. Makes searching for attributes #easier self.attr_id_map = {} self.connection = JsonConnection(url) write_output("Connecting...") if session_id is not None: write_output("Using existing session %s"% session_id) self.connection.session_id=session_id else: self.connection.login() #Dictionaries, keyed on their name, where all the nodes, links & groups will go self.hydra_nodes = {} self.hydra_links = {} self.hydra_groups = {} self.hydra_group_members = {} #Generators of NEGATIVE IDS for the nodes & links. #Negative IDS are used as temporary client-side IDS until the network #is saved, at which point the resources will be given permanent, positive IDS. self.node_ids = temp_ids() self.link_ids = temp_ids() self.group_ids = temp_ids() # A group is an institution. #A mapping from the name of a type to the type itself. self.type_name_map = {} self.num_steps = 3
def get_network_data(self, network_id, scenario_id): """ Retrieve the network, identify the parameters to set, set them and run the model. Then identify the results and set them back on the network. """ write_output("Retrieving Network") write_progress(2, self.num_steps) if network_id is not None: if scenario_id is None: raise HydraPluginError("A scenario ID must be specified.") #The network ID can be specified to get the network... try: network_id = int(network_id) network = self.connection.call( 'get_network', { 'network_id': network_id, 'scenario_ids': [int(scenario_id)] }) write_output("Network retrieved") except Exception, e: log.exception(e) raise HydraPluginError("Network %s not found." % network_id)
def __init__(self, url=None, session_id=None): self.json_net = None self.warnings = [] self.files = [] self.template = None self.attributes = [] self.attr_name_map = {} self.nodes = {} self.links = {} self.groups = {} self.node_id = PluginLib.temp_ids() self.link_id = PluginLib.temp_ids() self.group_id = PluginLib.temp_ids() self.connection = JsonConnection(url) if session_id is not None: write_output("Using existing session %s"% session_id) self.connection.session_id=session_id else: self.connection.login() #3 steps: start, read, save self.num_steps = 3
def __init__(self, url=None, session_id=None): self.json_net = None self.warnings = [] self.files = [] self.template = None self.attributes = [] self.attr_name_map = {} self.nodes = {} self.links = {} self.groups = {} self.node_id = PluginLib.temp_ids() self.link_id = PluginLib.temp_ids() self.group_id = PluginLib.temp_ids() self.connection = JsonConnection(url) if session_id is not None: write_output("Using existing session %s" % session_id) self.connection.session_id = session_id else: self.connection.login() #3 steps: start, read, save self.num_steps = 3
def import_scenario(self): for n in self.network.nodes: self.hydra_nodes[n.name] = n for l in self.network.links: self.hydra_links[l.name] = l for g in self.network.resourcegroups: self.hydra_groups[g.name] = g hydra_group_members = [] for j_inst in self._network.institutions: group_id = self.hydra_groups[j_inst.name].id for n in j_inst.nodes: hydra_group_members.append( { 'ref_key':'NODE', 'ref_id' : self.hydra_nodes[n.name]['id'], 'group_id' : group_id, } ) for l in j_inst.links: hydra_group_members.append( { 'ref_key':'LINK', 'ref_id' : self.hydra_links[l.name]['id'], 'group_id' : group_id, } ) for i in j_inst.institutions: hydra_group_members.append( { 'ref_key':'GROUP', 'ref_id' : self.hydra_groups[i.name]['id'], 'group_id' : group_id, } ) scenario = dict( name = "Baseline", description = "Scenario imported from the import app", network_id = self.network.id, resourcegroupitems = hydra_group_members, resourcescenarios = [] ) write_output("Finished Writing Output.") self.scenario = self.connection.call('add_scenario', { 'network_id': self.network.id, 'scen':scenario}) return self.scenario
def fetch_remote_network(self): """ Request the hobbes network from the hobbes server """ write_output("Fetching Network") write_progress(2, self.num_steps) net_response = requests.get("http://cwn.casil.ucdavis.edu/network/get") #JSON Network #http://cwn.casil.ucdavis.edu/excel/create?prmname=SR_CLE #XLS if net_response.status_code != 200: raise HydraPluginError("A connection error has occurred with status code: %s"%net_response.status_code) self.json_net = json.loads(net_response.content)
def fetch_remote_network(self): """ Request the hobbes network from the hobbes server """ write_output("Fetching Network") write_progress(2, self.num_steps) net_response = requests.get( "http://cwn.casil.ucdavis.edu/network/get") #JSON Network #http://cwn.casil.ucdavis.edu/excel/create?prmname=SR_CLE #XLS if net_response.status_code != 200: raise HydraPluginError( "A connection error has occurred with status code: %s" % net_response.status_code) self.json_net = json.loads(net_response.content)
def __init__(self, url=None, session_id=None): #Record the names of the files created by the plugin so we can #display them to the user. self.files = [] #A mapping from attr ID to attr object. Makes searching for attributes #easier self.attr_id_map = {} self.connection = JsonConnection(url) write_output("Connecting...") if session_id is not None: write_output("Using existing session %s" % session_id) self.connection.session_id = session_id else: self.connection.login() self.network = None self.num_steps = 3
def write_network(self, network, target_dir): write_output("Writing network to file") write_progress(3, self.num_steps) if self.as_xml is False: file_name = "network_%s.json" % (network['name']) self.files.append(os.path.join(target_dir, file_name)) network_file = open(os.path.join(target_dir, file_name), 'w') network_file.write( json.dumps(network, sort_keys=True, indent=4, separators=(',', ': '))) else: file_name = "network_%s.xml" % (network['name']) self.files.append(os.path.join(target_dir, file_name)) network_file = open(os.path.join(target_dir, file_name), 'w') json_network = {'network': network} network_file.write(json2xml(json_network)) write_output("Network Written to %s " % (target_dir))
plug-in connects.''') parser.add_argument( '-c', '--session-id', help='''Session ID. If this does not exist, a login will be attempted based on details in config.''') return parser if __name__ == '__main__': parser = commandline_parser() args = parser.parse_args() jp_runner = ModelRunner(url=args.server_url, session_id=args.session_id) errors = [] try: write_output("Starting App") write_progress(1, jp_runner.num_steps) validate_plugin_xml(os.path.join(__location__, 'plugin.xml')) jp_runner.get_network_data(args.network_id, args.scenario_id) jp_runner.run_model() message = "Model Run Complete" except HydraPluginError as e: message = "An error has occurred" errors = [e.message] log.exception(e) except Exception, e: message = "An error has occurred" log.exception(e) errors = [e]
def import_network_topology(self, project_id=None): """ Read the file containing the network data and send it to the server. """ if self.json_net is None: self.fetch_remote_network() for node in self.json_net: props = node['properties'] node_type = props['type'] node_coords = node['geometry']['coordinates'] tmp_node_id = self.node_id.next() #TODO: HACK. WHy are there 2 coordinates for the node? if isinstance(node_coords[0], list): #log.debug("Using 1st coords of %s (%s)", node_coords, props['type']) node_coords = node_coords[0] #log.info("X=%s, y=%s",node_coords[0], node_coords[1]) node = dict( id = tmp_node_id, name = props['prmname'], x = str(node_coords[0]), #swap these if they are lat-long, not x-y y = str(node_coords[1]), description = props['description'], attributes = [], types = [{'template_id':self.template.id, 'id':node_type}] ) self.nodes[props['prmname']] = node #ATTRIBUTES #Find the matching type for t in self.template.types: if t.name == node_type: node['types'][0]['id'] = t.id #Assign the attributes to the node for tattr in t.typeattrs: node['attributes'].append( {'attr_id':tattr.attr_id} ) inlinks = [o['link_prmname'] for o in props.get('origins', [])] for linkname in inlinks: if linkname not in self.links: link = dict( id=self.link_id.next(), name = linkname, node_2_id = tmp_node_id, attributes = [], description = "", ) self.links[linkname] = link else: link = self.links[linkname] link['node_2_id'] = tmp_node_id outlinks = [o['link_prmname'] for o in props.get('terminals', [])] for linkname in outlinks: if linkname not in self.links: link = dict( id=self.link_id.next(), name = linkname, node_1_id = tmp_node_id, attributes = [], description = "", ) self.links[linkname] = link else: link = self.links[linkname] link['node_1_id'] = tmp_node_id node_groups = props['regions'] project = self.fetch_project(project_id) project_id = project.id write_output("Saving Network") write_progress(3, self.num_steps) for t in self.template.types: if t.name == 'HobbesNetwork': network_type = t.id hydra_network = { 'name' : "HOBBES Network (%s)"%datetime.now(), 'description' : "Hobbes Network, imported directly from the web API", 'nodes': self.nodes.values(), 'links': self.links.values(), 'project_id' : project_id, 'projection':'EPSG:2229', 'groups' : [], 'scenarios': [], 'types' : [{'template_id':self.template.id, 'id':network_type}] } #The network ID can be specified to get the network... self.network = self.connection.call('add_network', {'net':hydra_network}) return self.network
def import_network(self, template_id, project_id): write_output("Writing network to file") write_progress(3, self.num_steps) for j_node in self._network.nodes: log.info("Node: %s", j_node.component_type) node_type = self.type_name_map.get(j_node.component_type) y = j_node.y if y > 800000: y = y - 1000000 elif y > 400000: y = y - 400000 node = dict( id = self.node_ids.next(), name = j_node.name, description = " Node", x = str(j_node.x), y = str(y), attributes = [], types = [{'template_id':int(template_id), 'id':int(node_type.id)}] ) self.hydra_nodes[j_node.name] = node for j_link in self._network.links: link_type = self.type_name_map.get(j_link.component_type) log.info("Link: %s", j_link.component_type) link = dict( id = self.link_ids.next(), name = j_link.name, description = " Link", node_1_id = self.hydra_nodes[j_link.start_node.name]['id'], node_2_id = self.hydra_nodes[j_link.end_node.name]['id'], attributes = [], types = [{'template_id':int(template_id), 'id':link_type.id}] ) self.hydra_links[j_link.name] = link for j_inst in self._network.institutions: group_type = self.type_name_map.get(j_inst.component_type) log.info("Group: %s", j_inst.component_type) group = dict( id = self.group_ids.next(), name = j_inst.name, description = "A Model Institution", attributes = [], types = [{'template_id':int(template_id), 'id':group_type.id}] ) self.hydra_groups[j_inst.name] = group project = self.fetch_project(project_id) project_id = project.id network_type = self.type_name_map.get('Network') hydra_network = { 'name' : " Network (%s)"%datetime.now(), 'description' : " Network, imported directly from the prototype", 'nodes': self.hydra_nodes.values(), 'links': self.hydra_links.values(), 'project_id' : project_id, 'projection':'EPSG:2229', 'resourcegroups': self.hydra_groups.values(), 'scenarios': [], 'types' : [{'template_id':int(template_id), 'id':network_type.id}], } self.network = self.connection.call('add_network', {'net':hydra_network}) return self.network
def import_network_topology(self, project_id=None): """ Read the file containing the network data and send it to the server. """ if self.json_net is None: self.fetch_remote_network() for node in self.json_net: props = node['properties'] node_type = props['type'] node_coords = node['geometry']['coordinates'] tmp_node_id = self.node_id.next() #TODO: HACK. WHy are there 2 coordinates for the node? if isinstance(node_coords[0], list): #log.debug("Using 1st coords of %s (%s)", node_coords, props['type']) node_coords = node_coords[0] #log.info("X=%s, y=%s",node_coords[0], node_coords[1]) node = dict( id=tmp_node_id, name=props['prmname'], x=str( node_coords[0]), #swap these if they are lat-long, not x-y y=str(node_coords[1]), description=props['description'], attributes=[], types=[{ 'template_id': self.template.id, 'id': node_type }]) self.nodes[props['prmname']] = node #ATTRIBUTES #Find the matching type for t in self.template.types: if t.name == node_type: node['types'][0]['id'] = t.id #Assign the attributes to the node for tattr in t.typeattrs: node['attributes'].append({'attr_id': tattr.attr_id}) inlinks = [o['link_prmname'] for o in props.get('origins', [])] for linkname in inlinks: if linkname not in self.links: link = dict( id=self.link_id.next(), name=linkname, node_2_id=tmp_node_id, attributes=[], description="", ) self.links[linkname] = link else: link = self.links[linkname] link['node_2_id'] = tmp_node_id outlinks = [o['link_prmname'] for o in props.get('terminals', [])] for linkname in outlinks: if linkname not in self.links: link = dict( id=self.link_id.next(), name=linkname, node_1_id=tmp_node_id, attributes=[], description="", ) self.links[linkname] = link else: link = self.links[linkname] link['node_1_id'] = tmp_node_id node_groups = props['regions'] project = self.fetch_project(project_id) project_id = project.id write_output("Saving Network") write_progress(3, self.num_steps) for t in self.template.types: if t.name == 'HobbesNetwork': network_type = t.id hydra_network = { 'name': "HOBBES Network (%s)" % datetime.now(), 'description': "Hobbes Network, imported directly from the web API", 'nodes': self.nodes.values(), 'links': self.links.values(), 'project_id': project_id, 'projection': 'EPSG:2229', 'groups': [], 'scenarios': [], 'types': [{ 'template_id': self.template.id, 'id': network_type }] } #The network ID can be specified to get the network... self.network = self.connection.call('add_network', {'net': hydra_network}) return self.network