1
2
3
4
5
6
7 import copy
8
9
10
11
12
13
14
15
16
17
18
20 "A Class to manage a list of automata"
21
22 - def __init__(self,name,minor=0,major=0):
23 """
24 Initializes the CAutomata class.
25
26 The list of automata is initially empty.
27
28 @type name: string
29 @param name: The name of the Automata
30 @type minor: number
31 @type major: number
32 @return: void
33 """
34
35 self.__name=name
36 self.__minor=minor
37 self.__major=major
38 self.__automata=[]
39
41 """
42 @return: The Name of the Automata
43 """
44 return self.__name
45
47 """
48 Set the name of the Automata list.
49
50 @type name: string
51 @param name: The name of the Automata
52 @return: I{void}
53 """
54 self.__name=name
55
57 """
58 @return: The minor parameter of the Automata
59 """
60 return self.__minor
61
63 """
64 @return: The major parameter of the Automata
65 """
66 return self.__major
67
69 """
70 Returns the automaton instance corresponding to the specified name
71
72 @type name: string
73 @param name: The name of the automaton to retrieve
74 @return: the L{CAutomaton} instance associated to the specified I{name} if it is in the list. I{False} otherwise.
75 """
76 for a in self.__automata:
77 if a.name==name:
78 return a
79 return False
80
82 """
83 Sets the minor parameter of the Automata list.
84
85 @type minor: integer
86 @return: I{void}
87 """
88 self.__minor=minor
89
91 """
92 Sets the major parameter of the Automata list.
93
94 @type major: integer
95 @return: I{void}
96 """
97 self.__major=major
98
100 """
101 Adds an automaton to the automata list
102
103 @type automaton: a L{CAutomaton} instance
104 @param automaton: the automaton instance to add to the current automata
105 @return: I{void}
106 """
107 self.__automata.append(automaton)
108
110 """
111 Returns a copy of the list of automata.
112
113 @return: A copy of the list of automata
114 """
115 return copy.copy(self.__automata)
116
117
118 name=property(fget=getName,fset=setName)
119 minor=property(fget=getMinor,fset=setMinor)
120 major=property(fget=getMajor,fset=setMajor)
121
122
123
124
125
127 "A Class for a Deterministic Finite Automaton DFA"
128
129
130
131
132
134 """
135 A class to implement an event object.
136
137 This class is privately declared inside the L{CAutomaton} class.
138 Every instance will then be created by the L{CAutomaton} public methods (see the L{CAutomaton.addEvent} method).
139 """
140
141 - def __init__(self,automaton,id,name,comment,controllable):
142
143 self.__id=id
144 self.__name=name
145 self.__comment=comment
146 self.__controllable=controllable
147 self.__automaton=automaton
148 self.__prioritylist=set([])
149
151 """
152 @return: The ID of the Event
153 """
154 return self.__id
155
157 """
158 Set the ID of the Event.
159
160 @type id: integer
161 @param id: the ID of the event (univocal key)
162 @return: I{void}
163 """
164 self.__id=id
165
167 """
168 @return: The Name of the Event
169 """
170 return self.__name
171
173 """
174 Set the name of the Event.
175
176 @type name: string
177 @param name: the name of the Event
178 @return: I{void}
179 """
180 self.__name=name
181
183 """
184 @return: The comment associated to the Event
185 """
186 return self.__comment
187
197
199 """
200 Set the controllable flag of the event.
201 A I{True} value means that the event is controllable, a I{False} value that it is not controllable.
202
203 @type controllable: boolean
204 @param controllable: a flag which determines if the Event is controllable or not
205 @return: I{void}
206 """
207 self.__controllable=controllable
208
210 """
211 Add one ore more event for which the current one has top priority.
212
213 The priorities over events are used when an automaton which represents a Supervisor is processed in
214 order to carry out a deterministic Controller (see the L{CAutomaton.toDeterministicController} method)
215
216
217 @type events: string
218 @param events: a list of the names of the less priority events
219 @return: I{True} if the list has been properly added, I{False} if some error has occurred (mostly one of the specified event name is not a valid one for the current automaton)
220 """
221 for e in events:
222 if self._CEvent__automaton.getEvent(e):
223 self._CEvent__prioritylist.add(e)
224 else:
225 return False
226 return True
227
229 """
230 Returns a copy of the list of prioritized events.
231 See the L{setPriorityList} method for further information.
232
233 @return: A copy of the list of prioritized events names
234 """
235 return copy.copy(self._CEvent__prioritylist)
236
238 """
239 Returns I{True} if the specified event has less priority than the current one (than it is Subdued).
240
241 The method looks for if the specified event is in the Priority List of the current event
242 or in the Priority List of one of its subdueds.
243
244 @type event: string
245 @param event: the name of the event to check
246
247 @return: I{True} if the I{event} is subdued, I{False} otherwise
248 """
249 if event in self._CEvent__prioritylist:
250 return True
251 else:
252 for e in self.__prioritylist:
253 if self._CEvent__automaton.getEvent(e).isSubdued(event):
254 return True
255 return False
256
258 """
259 Returns a flag which states if the current Event is controllable or not.
260
261 @return: I{True} if the Event is controllable, I{False} otherwise
262 """
263 return self.__controllable
264
265
266
267 id=property(fget=getId,fset=setId)
268 name=property(fget=getName,fset=setName)
269 comment=property(fget=getComment,fset=setComment)
270 controllable=property(fget=isControllable,fset=setControllable)
271
272
273
275 """
276 A class to implement a state object.
277
278 This class is privately declared inside the L{CAutomaton} class.
279 Every instance will then be created by the L{CAutomaton} public methods (see the L{CAutomaton.addState} method).
280 """
281 - def __init__(self,automaton,id,name,comment,initial,marked):
282
283 self.__id=id
284 self.__name=name
285 self.__comment=comment
286 self.__marked=marked
287 self.__initial=initial
288 self.__weight=-1
289 self.__transitions={}
290 self.__intransitions=[]
291 self.__automaton=automaton
292
294 """
295 @return: The ID of the State
296 """
297 return self.__id
298
300 """
301 Sets the ID of the State.
302
303 @type id: integer
304 @param id: the ID of the state
305 @return: I{void}
306 """
307 self.__id=id
308
310 """
311 @return: The Name of the State
312 """
313 return self.__name
314
316 """
317 Sets the name of the State.
318
319 @type name: number
320 @param name: the name of the State
321 @return: I{void}
322 """
323 self.__name=name
324
326 """
327 @return: The comment associated to the State
328 """
329 return self.__comment
330
340
341
343 """
344 Sets the initial flag for the state.
345 A I{True} value means that the state is initial, a I{False} value that it is not initial.
346
347 @type initial: boolean
348 @param initial: a flag which determines if the State is initial or not
349 @return: I{void}
350 """
351 self.__initial=initial
352
354 """
355 Returns a flag which states if the current State is initial or not.
356
357 @return: I{True} if the State is intial, I{False} otherwise
358 """
359 return self.__initial
360
362 """
363 Sets the marked flag for the state.
364 A I{True} value means that the state is marked, a I{False} value that it is not marked.
365
366 @type marked: boolean
367 @param marked: a flag which determines if the State is marked or not
368 @return: I{void}
369 """
370 self.__marked=marked
371
373 """
374 Returns a flag which states if the current State is marked or not.
375
376 @return: I{True} if the State is marked, I{False} otherwise
377 """
378 return self.__marked
379
381 """
382 Returns a flag which states if the current State is blocking or not.
383
384 @return: I{True} if the State is blocking, I{False} otherwise
385 """
386 return (not(len(self.__transitions)))
387
389 """
390 Returns the weight associated to the state.
391 If a weight has never been set the method returns the B{-1} value.
392
393 The weights are used when an automaton which represents a Supervisor is processed in
394 order to carry out a deterministic Controller (see the L{CAutomaton.toDeterministicController} method).
395
396 @return: The weight associated to the State
397 """
398 return self.__weight
399
401 """
402 Sets a weight to the state.
403
404 The weights are used when an automaton which represents a Supervisor is processed in
405 order to carry out a deterministic Controller (see the L{CAutomaton.toDeterministicController} method).
406
407 The weights are usually assigned by the L{expandWeight} method.
408
409 @type weight: integer
410 @param weight: a flag which determines the weight of the State
411 @return: I{void}
412 """
413 self.__weight=weight
414
416 """
417 Adds an outgoing transition to the State.
418
419 To add the transition the event and the arrival state must already be in the automaton.
420
421 @type event: a L{CAutomaton._CAutomaton__CEvent} instance
422 @param event: the event associated to the transition
423 @type arrivalstate: a L{CAutomaton._CAutomaton__CState} instance
424 @param arrivalstate: the arrival state of the transition
425 @return: I{True} if the transition is successfully added, I{False} if some error has occured (mostly the event or the arrivalstate are not present in the automaton)
426 """
427 if(not(arrivalstate in self.__automaton._CAutomaton__states)):
428 return False
429 if(not(event in self.__automaton._CAutomaton__events)):
430 return False
431 self.__transitions[event.name]=arrivalstate
432 arrivalstate.__intransitions.append([event.name,self])
433 return True
434
436 """
437 Returns the arrival state associated to the specified event for the outgoing transitions.
438
439 @type event: a L{CAutomaton._CAutomaton__CEvent} instance
440 @param event: the event which drives the transition
441 @return: a L{CAutomaton._CAutomaton__CState} instance
442 """
443 return self.__transitions[event.name]
444
446 """
447 Returns the departure state associated to the event for the ingoing transitions.
448
449 @type event: a L{CAutomaton._CAutomaton__CEvent} instance
450 @param event: the event which drives the transition
451 @return: a L{CAutomaton._CAutomaton__CState} instance
452 """
453 for it in self.__intransitions:
454 if it[0]==event.name:
455 return it[1]
456 else: return False
457
458
460 """
461 This method called by the L{CAutomaton.toDeterministicController} method cuts the transitions which made
462 the supervisor non-deterministic by rapport to the control function it should achieve.
463
464 The controllable tranistions for the current state are cut in three steps.
465
466 1. B{Cutting no-WeightGain End event getWeight(nextstate)>=getWeight(currentstate)}
467 - The transitions associated to an I{End} event which don't provide a gain in terms of weight are suppressed.
468 2. B{Cutting lossy controllable events getWeight(nextstate)>getWeight(currentstate)}
469 - The transitions associated to the controllable events which make increase the weight are suppressed.
470 3. B{Using priority to resolve transition conflict}
471 - When a conflict is present between prioritized events the transition associated to the events with less priority are suppressed.
472
473 @return: void
474 """
475 minweight=999999
476 event2del={}
477 for e,s in self.__transitions.iteritems():
478 event2del[e]=False
479 if s.__weight<minweight:
480 minweight=s.__weight
481
482 transitionscopy=copy.copy(self.__transitions)
483
484
485 for e,s in transitionscopy.iteritems():
486 if self._CState__automaton.getEvent(e).isControllable():
487 if (not(s._CState__weight<self._CState__weight) and self._CState__automaton.getEvent(e).name==self._CState__automaton._CAutomaton__endevent):
488 s._CState__intransitions.remove([e,self])
489 del self.__transitions[e]
490
491 transitionscopy=copy.copy(self.__transitions)
492
493
494 for e,s in transitionscopy.iteritems():
495 if self._CState__automaton.getEvent(e).isControllable():
496 if (s._CState__weight>self.__weight):
497 s._CState__intransitions.remove([e,self])
498 del self.__transitions[e]
499
500 transitionscopy=copy.copy(self.__transitions)
501
502
503 for e,s in transitionscopy.iteritems():
504 for ae in event2del:
505 if self._CState__automaton.getEvent(e).isSubdued(ae):
506 event2del[ae]=True
507
508 for e,s in transitionscopy.iteritems():
509 if event2del[e]==True:
510 s._CState__intransitions.remove([e,self])
511 del self.__transitions[e]
512
514 """
515 Returns a copy of the outgoing transitions dictionary.
516 The dictionary has the form ['eventname']:CState.
517
518 @return: a copy of the dictionary of outgoing transitions
519 """
520 return (copy.copy(self.__transitions))
521
523 """
524 Returns a copy of the ingoing transitions list.
525 The list has the form [ ['eventname'][CState] ].
526
527 @return: a copy of the list of ingoing transitions
528 """
529 return (copy.copy(self.__intransitions))