param.py 7.72 KB
Newer Older
1
2
3
4
5
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

Daniel Brown's avatar
Daniel Brown committed
6
7
import abc
import pykat.exceptions as pkex
8
9
import weakref
    
Daniel Brown's avatar
Daniel Brown committed
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
class putable(object):
    """
    Objects that inherit this should be able to have something `put` to it.
    Essentially this means you could write Finesse commands like
    
    put this parameter value
    """
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, component_name, parameter_name, isPutable=True):
        self._parameter_name = parameter_name
        self._component_name = component_name
        self._putter = None
        self._isPutable  = isPutable
    
    @property
    def isPutable(self): return self._isPutable
    
    def put(self, var):
    
        if not isinstance(var, putter):
            raise pkex.BasePyKatException("var was not something that can be `put` as a value")
        
        if self._putter != None:
            self._putter.put_count -= 1
35
            self._putter.putees.remove(self)
Daniel Brown's avatar
Daniel Brown committed
36
37
        
        self._putter = var
38
39
40
41
        
        if var != None:
            self._putter.put_count += 1
            self._putter.putees.append(self)
Daniel Brown's avatar
Daniel Brown committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        
    def _getPutFinesseText(self):
        rtn = []
        # if something is being put to this 
        if self._putter != None:
            rtn.append("put {comp} {param} ${value}".format(comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
        
        return rtn
            
class putter(object):
    """
    If an object can be put to something that is putable it should inherit this
    object.
    """
    
    def __init__(self, put_name, isPutter=True):
        self._put_name = put_name
        self.put_count = 0
        self._isPutter = isPutter
61
        self.putees = [] # list of params that this puts to
Daniel Brown's avatar
Daniel Brown committed
62
63
64
65
66
67
68
69
70
    
    @property
    def isPutter(self): return self._isPutter
    
    def put_name(self): return self._put_name
    
        
class Param(putable, putter):

71
    def __init__(self, name, owner, value, canFsig=False, fsig_name=None, isPutable=True, isPutter=True, isTunable=True, var_name=None):
Daniel Brown's avatar
Daniel Brown committed
72
        self._name = name
73
        self._owner = weakref.ref(owner)
Daniel Brown's avatar
Daniel Brown committed
74
75
76
        self._value = value
        self._isPutter = isPutter
        self._isTunable = isTunable
77
        self._owner()._register_param(self)
78
79
80
81
82
        self._canFsig = False
        
        if canFsig:
            self._canFsig = True

83
            if fsig_name is None:
84
85
86
                raise pkex.BasePyKatException("If parameter is a possible fsig target the fsig_name argument must be set")

            self.__fsig_name = fsig_name
Daniel Brown's avatar
Daniel Brown committed
87
88
        
        if isPutter:
89
            if var_name is None:
Daniel Brown's avatar
Daniel Brown committed
90
91
                var_name = "var_{0}_{1}".format(owner.name, name)
                
92
        putter.__init__(self, var_name, isPutter)
Daniel Brown's avatar
Daniel Brown committed
93
            
94
        putable.__init__(self, owner.name, name, isPutable)
95
96
97
98
99
100
101
102
103

    def _updateOwner(self, newOwner):
        """
        This updates the internal weak reference to link a parameter to who owns it.
        Should only be called by the __deepcopy__ component method to ensure things
        are kept up to date.
        """
        del self._owner
        self._owner = weakref.ref(newOwner)
104
        
105
106
107
    @property
    def canFsig(self): return self._canFsig
    
108
109
110
    @property
    def owner(self): return self._owner()
    
111
112
113
    @property
    def fsig_name(self): return self.__fsig_name
    
114
115
116
    @property
    def fsigName(self): return self.__fsig_name
    
Daniel Brown's avatar
Daniel Brown committed
117
118
119
120
121
122
123
    @property
    def name(self): return self._name
    
    @property
    def isTuneable(self): return self._isTunable
    
    @property
124
125
126
127
128
129
    def value(self):
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
        else:
            return self._value
    
Daniel Brown's avatar
Daniel Brown committed
130
131
    @value.setter
    def value(self, value):
132
133
134
135
136
137
138
139
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
        else:
            self._value = value
    
    def __str__(self):
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
140
141
        elif type(self.value) == float:
            return repr(self.value)
142
143
144
145
146
147
148
        else:
            return str(self.value)
            
    def __float__(self):
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
        else:
149
            return float(self.value)
Daniel Brown's avatar
Daniel Brown committed
150
151
        
    def getFinesseText(self):
152
153
154
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
            
Daniel Brown's avatar
Daniel Brown committed
155
156
157
158
159
160
161
        rtn = []
        
        if self.isPutable: rtn.extend(self._getPutFinesseText())
        
        # if this parameter is being put somewhere then we need to
        # set it as a variable
        if self.isPutter and self.put_count > 0:
162
            rtn.append("set {put_name} {comp} {param}".format(put_name=self.put_name(), comp=self._owner().name, param=self.name))
Daniel Brown's avatar
Daniel Brown committed
163
164
        
        return rtn
165
166
167
168
169
    
    def _onOwnerRemoved(self):
        #if this param can be put somewhere we need to check if it is
        if self.isPutable:
            for a in self.putees:
170
                print ("Removing put from {0} {1} to {2} {3}".format(self.owner.name, self.name, a.owner.name, a.name))
171
172
173
174
175
176
177
178
179
                a._putter = None
                self.put_count -= 1
                
            # delete any references left over
            del self.putees[:]
        
        # check if we have anything being put to us
        if self.isPutter:
            if self._putter != None:
180
                print ("Removing put from {0} {1} to {2} {3}".format(self._putter.owner.name, self._putter.name, self.owner.name, self.name))
181
182
183
184
185
                self._putter.put_count -= 1
                self._putter.putees.remove(self)
                self._putter = None
       
       
Daniel Brown's avatar
Daniel Brown committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    def __mul__(self, a):
        return self.value * a
    
    def __imul__(self, a):
        return self.value * (a)
        
    __rmul__ = __mul__
    
    def __add__(self, a):
        return self.value + (a)
    
    def __iadd__(self, a):
        return self.value + (a)
        
    __radd__ = __add__
    
    def __sub__(self, a):
        return self.value - (a)
    
    def __isub__(self, a):
        return self.value - (a)
        
208
209
    def __rsub__(self, a):
        return (a) - self.value
Daniel Brown's avatar
Daniel Brown committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    
    def __div__(self, a):
        return self.value / (a)
    
    def __idiv__(self, a):
        return self.value / complex(a)
        
    def __pow__(self, q):
        return  self.value**q

    def __neg__(self):
        return -self.value
        
    def __eq__(self, q):
        return (q) == self.value
    def __ne__(self, q):
        return (q) != self.value
    def __lt__(self, q):
        return (q) > self.value
    def __gt__(self, q):
        return (q) < self.value        
        
class AttrParam(Param):
    """
    Certain parameters of a component are set using the Finesse `attr` command.
    
    This inherits directly from a Param object so can be set whether this attribute
    is putable or a putter.
    
    If the value pf the parameter is not 0 the attr command will be printed.
    """
    def getFinesseText(self):
242
243
244
        if self._owner().removed:
            raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))

Daniel Brown's avatar
Daniel Brown committed
245
246
        rtn = []
        
247
        if self.value != None:
248
            rtn.append("attr {0} {1} {2}".format(self._owner().name, self.name, self.value))
Daniel Brown's avatar
Daniel Brown committed
249
250
251
            
        rtn.extend(super(AttrParam, self).getFinesseText())
        
252
        return rtn
253
254