1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Synapses, cell variants which exist only within C{L{RuleCell}}s and
21 which mediate the propogation of another cell's value. For instance, a
22 cell C{a} could use cell C{b}'s value, but only if it had changed more
23 than 5% since the last time C{a} used it.
24
25 @var DEBUG: Turns on debugging messages for the synapse module.
26 """
27
28 import cells, cell
29
30 DEBUG = False
31
37
39 """
40 A very specialized Cell variant. Synapses are filters for
41 Cells. They can be applied to any type of Cell, and they simply
42 process the cell to build a value which is provided to dependent
43 cells as the filtered cell's value. They live within the cell
44 which defines them, and as such are only referenceable in that
45 rule.
46 """
47 initialized = False
48
49 - def __new__(cls, owner, name=None, **kwargs):
50 """
51 Create a new synapse instance within the calling Cell, if
52 neccessary.
53
54 @param cls: Class this synapse is being called from.
55
56 @param owner: Model instance this synapse is being created in
57 / retrieved from
58
59 @param name: Name of the synapse to retrieve; used as a lookup
60 on the enclosing Cell's synapse space.
61
62 @param kwargs: standard C{L{Cell}} keyword arguments.
63 """
64
65
66 if not owner.synapse_space.has_key(name):
67
68 debug("building new synapse '" + name + "' in", str(owner))
69 owner.synapse_space[name] = cell.Cell.__new__(cls, owner,
70 name=name, **kwargs)
71
72
73 return owner.synapse_space[name]
74
75 - def __init__(self, owner, name=None, **kwargs):
76 """
77 Initialize the synapse Cell, if neccessary.
78
79 @param owner: The model instance to pass to this synapse's rule
80
81 @param name: This synapse's name
82
83 @param kwargs: Standard C{L{Cell}} keyword arguments
84 """
85
86
87
88 if not self.initialized:
89 debug("(re)initializing", name)
90 cell.Cell.__init__(self, owner, name=name, **kwargs)
91 self.initialized = True
92
94 """
95 Run C{L{Cell.get}(self)} when a synapse is called as a function.
96 """
97 return self.getvalue()
98
100 """
101 Slightly modified version of C{L{Cell.run}()}.
102 """
103 debug(self.name, "running")
104
105 oldcurr = cells.cellenv.curr
106 cells.cellenv.curr = self
107
108
109
110 for cell in self.calls_list():
111 debug(self.name, "removing c-b link from", cell.name)
112 cell.remove_cb(self)
113 self.reset_calls()
114
115 self.dp = cells.cellenv.dp
116 newvalue = self.rule(self.owner, self.value)
117 self.bound = True
118
119
120 cells.cellenv.curr = oldcurr
121
122
123 if self.unchanged_if(self.value, newvalue):
124 debug(self.name, "unchanged.")
125 return False
126 else:
127 debug(self.name, "changed.")
128 self.last_value = self.value
129 self.value = newvalue
130
131 return True
132
133 - def rule(self, owner, oldvalue):
134 return None
135
137 """A very simple filter. Only returns the new value when it's
138 changed by the passed delta
139 """
140 - def __init__(self, owner, name=None, read=None, delta=None, **kwargs):
146
148 debug("running ChangeSynapse rule")
149 newval = self.readvar.getvalue()
150 if not oldvalue or abs(newval - oldvalue) > self.delta:
151 debug("returning new value", str(newval))
152 return newval
153 else:
154 debug("returning old value", str(oldvalue))
155 return oldvalue
156