1 """Group persons for better handling and the ease of use"""
2
3 from heapq import heappush
4
5 __author__ = "F. Ludwig"
6 __maintainer__ = "B. Henne"
7 __contact__ = "henne@dcsec.uni-hannover.de"
8 __copyright__ = "(c) 2010-2011, DCSec, Leibniz Universitaet Hannover, Germany"
9 __license__ = "GPLv3"
13 """Encapsulates a single methods that will be executed by the simulation.
14
15 For example used to implement Person interaction/communication as the zombie infect.
16 @author: F. Ludwig"""
17
18 sim = None
19 """the simulation this Message will be executed by (in mosp.core.Simulation.run)"""
20
21 - def __init__(self, func, args, kwargs, delay):
22 """Initializes Message object and
23 schedules it for execution by mosp.core.Simulation.run()
24 @param func: method to be executed if this Message is called
25 @param args: arguments for func
26 @param kwargs: keyword arguments for func
27 @param delay: tick delay before func is called"""
28 self.func = func
29 self.args = args
30 self.kwargs = kwargs
31 self.time = self.sim.now() + delay
32 heappush(self.sim.messages, self)
33
35 """If Message is finally called, execute self.func
36 with its arguments and keyword arguments"""
37 self.func(*self.args, **self.kwargs)
38
39
40 -class Call(object):
41 """Encapsulates a Call of a method for a group of objects/Persons with a given delay.
42 @author: F. Ludwig"""
43
44 - def __init__(self, group, func_name, delay):
45 """If Call is instantiated, group is set up, func_name and delay is stored."""
46 self.group = group
47 self.func_name = func_name
48 self.delay = delay
49
51 """If Call instance is called, for all members of self.group a Message instance
52 is created and stored to schedule the execution of self.func_name
53 after self.delay ticks.
54 @param args: arguments for the methods called func_name to be executed
55 @param kwargs: keyword arguments for the methods called func_name to be executed"""
56 for pers in self.group:
57 Message(getattr(pers, self.func_name), args, kwargs, self.delay)
58
61 """A grouping object containing a group of things (typically a PersonGroup(set)),
62 which if a generic attribute (method) is requested, returns a Call instance for
63 the group and this attribute=method that shall be called.
64 @author: F. Ludwig"""
66 """Inits the CallGroup, setup group and execution delay.
67 @param group: the group for which a methods shall be executed
68 @param delay: execution delay in ticks"""
69 self.group = group
70 self.delay = delay
71
73 """If any func_name is requested, return an instance of Call
74 with the group, the function name and the defined execution delay as parameter.
75 @param func_name: generic requested function that shall be called for the group"""
76 return Call(self.group, func_name, self.delay)
77
79 """If the CallGroup is called, setup the execution delay and return the CallGroup.
80 @param delay: execution delay in ticks"""
81 self.delay = delay
82 return self
83
86 """A set of Persons.
87 @author: F. Ludwig"""
89 """Filter Persons in PersonGroup by instances' attributes
90
91 Can only filter on equality of key-value-pair."""
92 re = PersonGroup()
93 for pers in self:
94 for key, value in kwargs.items():
95 if hasattr(pers, key) and getattr(pers, key) == value:
96 re.add(pers)
97 return re
98
99 @property
101 """Returns a CallGroup with group=self=this PersonGroup."""
102 return CallGroup(self)
103