Working with networks

Adding networks

Before a network can be added, a project must first be created. See Working with projects.

Networks are made up of nodes, links and scenarios, and can be created with any number of each.

We use the word resource to describe things in hydra that can have attributes (see Attributes). Resources include Projects, Networks, Nodes, Links and Groups. A group is a grouping of nodes and/or links within a network. Groups are used to categorise sub-sections of a network, for example political jurisdictions.

The simplest way to get started is to add an ‘empty’ network

#Assume we have created a project
project_id      = my_project.id

net             = self.client.factory.create('hyd:Network')

#necessary info:
net.name        = "An empty network"
net.description = "A network with no nodes, links or data (scenarios)"
net.project_id  = project.id

A network also has a ‘layout’ attribute, which can be any XML encoding. This is used to help apps (the user interface) to store app-specific information about the network

#Some helpful stuff for the UI, encoded in XML
net.layout = {'background_colour': 'blue'}

Having defined the network, it is added

#add network
empty_net = client.service.add_network(net)

#The new project now has an ID
print empty_net.id

print empty_net.nodes      # None
print empty_net.links      # None
print empty_net.scenarios  # None
print empty_net.attributes # None

Retrieving Networks

Existing networks can be retrieved in a few ways: The simplest and most common is by ID

my_network = client.service.get_network(my_network_id)

If you don’t know the ID, but know the name, you can still find the network, but you must know the project ID

my_network = client.service.get_network_by_name(my_project_id, "An empty network")

Failing that, just ask for all the networks a given project

my_networks = client.service.get_networks(project_id)

Deleting Networks

If a project is being clogged up with too many networks, the networks can be deleted

client.service.delete_network(network_id)

however, this will not permenantly delete the project. It will still be retrievable using get_network but it will not be returned in get_networks. A network can be reactivated by calling

client.service.activate_network(network_id)

To delete a network peremenantly, use

client.service.purge_network(network_id)

Warning This cannot be undone and all sub-nodes, links, scenarios and attributes will also be purged. This may also include the deletion of data (see Working with datasets).

Sharing Networks

It is not uncommon for multiple people to work on the same network, so to facilitate this, Hydra allows users to share networks with other users

#connect...
my_user_id      = 1
friend_1_user_id = 2
friend_2_user_id = 3
my_network_id      = 999

client.service.share_network(my_network_id, [friend_1_user_id, friend_2_user_id], 'Y', 'N')

In this example, user 1 is sharing network 999 with two other users. The first ‘Y’ parameter indicates that the network is ‘read only’, while the second indicates that the users are not allowed to re-share the network with other users.