-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_simple.py
More file actions
48 lines (40 loc) · 1.88 KB
/
Copy pathexample_simple.py
File metadata and controls
48 lines (40 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from token import *
from network import *
from logical_node import *
from role_criteria import *
##################################################################################
# Example code that roughly shows how the framework is to be used. Note: the
# relationship between the network and the logical nodes will likely change.
##################################################################################
class MyAwesomeRoleCriteria(RoleCriteria):
def __init__(self, name, happy, excited):
self.name = name
self.happy = happy
self.excited = excited
def evaluate_against(self, node_parameters):
return int(self.happy == node_parameters["happy"] and self.excited == node_parameters["excited"])
# Clients will define their own RoleCriteria, which will expect
# a certain set of parameters to evaluate on
role_criterias = [
MyAwesomeRoleCriteria("very sad", happy=False, excited=False),
MyAwesomeRoleCriteria("just content", happy=True, excited=False),
MyAwesomeRoleCriteria("freaking excited", happy=True, excited=True)
]
nodes = [
LogicalNode(0, { "happy": True, "excited": True }, role_criterias),
LogicalNode(1, { "happy": True, "excited": True }, role_criterias),
LogicalNode(2, { "happy": False, "excited": False }, role_criterias),
LogicalNode(3, { "happy": True, "excited": False }, role_criterias)
]
if __name__ == '__main__':
network = SimulatedNetwork(nodes)
token = nodes[0].begin_logical_assignment()
if token:
print "Error! Some roles couldn't be satisfied"
for role_id in token.unassigned_roles:
print "Role %d: %s" % (role_id, role_criterias[role_id].name)
else:
print "Success! All roles assigned!"
for node in nodes:
if node.assigned_role is not None:
print "Node %d's role: %s" % (node.node_id, role_criterias[node.assigned_role].name)