Mercurial > MadButterfly
comparison pyink/domview.py @ 1467:6927debad4ee
Fix issue and a simple testcase for FSM
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 18 Apr 2011 11:47:52 +0800 |
parents | a3ce8d22d163 |
children | c586981ecf1a |
comparison
equal
deleted
inserted
replaced
1466:1829b0cc9721 | 1467:6927debad4ee |
---|---|
135 class State(object): | 135 class State(object): |
136 name = None | 136 name = None |
137 entry_action = None | 137 entry_action = None |
138 transitions = None | 138 transitions = None |
139 | 139 |
140 def __init__(self, node): | 140 def __init__(self, node=None): |
141 self.node = node | 141 self.node = node |
142 self.transitions = {} | |
142 pass | 143 pass |
143 | 144 |
144 def rename(self, name): | 145 def rename(self, name): |
145 self.name = name | 146 self.name = name |
146 self.node.setAttribute('name', name) | 147 self.node.setAttribute('name', name) |
178 node.setAttribute('name', self.name) | 179 node.setAttribute('name', self.name) |
179 if self.entry_action: | 180 if self.entry_action: |
180 node.setAttribute('entry_action', self.entry_action) | 181 node.setAttribute('entry_action', self.entry_action) |
181 pass | 182 pass |
182 | 183 |
183 transitions = self.transitions or [] | 184 transitions = self.transitions |
184 for trn in transitions: | 185 for trn in transitions: |
185 trn.update_node() | 186 trn.update_node() |
186 pass | 187 pass |
187 pass | 188 pass |
188 | 189 |
219 trn = Transition() | 220 trn = Transition() |
220 trn.condition = cond | 221 trn.condition = cond |
221 trn.target = target | 222 trn.target = target |
222 trn_node = trn.create_node(node.document()) | 223 trn_node = trn.create_node(node.document()) |
223 | 224 |
224 node.addChild(trn_node) | 225 node.appendChild(trn_node) |
225 transitions[cond] = trn | 226 transitions[cond] = trn |
226 pass | 227 pass |
227 | 228 |
228 def rm_transition(self, cond): | 229 def rm_transition(self, cond): |
229 transitions = self.transitions | 230 transitions = self.transitions |
284 | 285 |
285 comp_node.appendChild(scenes_node) | 286 comp_node.appendChild(scenes_node) |
286 pass | 287 pass |
287 pass | 288 pass |
288 | 289 |
290 def _get_comp_node(self): | |
291 return self.node or self._comp_mgr._metadata_node | |
292 | |
289 def name(self): | 293 def name(self): |
290 if self.node: | 294 if self.node: |
291 name = self.node.getAttribute('name') | 295 name = self.node.getAttribute('name') |
292 else: | 296 else: |
293 name = 'main' | 297 name = 'main' |
299 return names | 303 return names |
300 | 304 |
301 def parse_timelines(self): | 305 def parse_timelines(self): |
302 self.timelines[:] = [] | 306 self.timelines[:] = [] |
303 | 307 |
304 if self.node: | 308 if self.node: # main component has None value for self.node |
305 assert self.node.name() == 'ns0:component' | 309 assert self.node.name() == 'ns0:component' |
306 pass | 310 pass |
307 | 311 |
308 comp_node = self.node | 312 comp_node = self._get_comp_node() |
309 for child in comp_node.childList(): | 313 for child in comp_node.childList(): |
310 if child.name() == 'ns0:scenes': | 314 if child.name() == 'ns0:scenes': |
311 tl = Timeline(child) | 315 tl = Timeline(child) |
312 self.timelines.append(tl) | 316 self.timelines.append(tl) |
313 print ' ' + tl.name() | 317 print ' ' + tl.name() |
316 pass | 320 pass |
317 | 321 |
318 ## \brief Parse FSM from a ns0:states node. | 322 ## \brief Parse FSM from a ns0:states node. |
319 # | 323 # |
320 def parse_states(self): | 324 def parse_states(self): |
321 comp_node = self.node | 325 comp_node = self._get_comp_node() |
322 assert (not comp_node) or comp_node.name() == 'ns0:component' | 326 assert (not comp_node) or comp_node.name() == 'ns0:component' |
323 | 327 |
324 states_nodes = [node | 328 states_nodes = [node |
325 for node in comp_node.childList() | 329 for node in comp_node.childList() |
326 if node.name() == 'ns0:states'] | 330 if node.name() == 'ns0:states'] |
361 if self.has_timeline(name): | 365 if self.has_timeline(name): |
362 raise ValueError, \ | 366 raise ValueError, \ |
363 'try add a timeline with duplicated name - %s' % (name) | 367 'try add a timeline with duplicated name - %s' % (name) |
364 | 368 |
365 doc = self._comp_mgr._doc | 369 doc = self._comp_mgr._doc |
366 comp_node = self.node | 370 comp_node = self._get_comp_node() |
367 | 371 |
368 scenes_node = doc.createElement('ns0:scenes') | 372 scenes_node = doc.createElement('ns0:scenes') |
369 scenes_node.setAttribute('name', name) | 373 scenes_node.setAttribute('name', name) |
370 node_id = self._comp_mgr.new_id() | 374 node_id = self._comp_mgr.new_id() |
371 scenes_node.setAttribute('id', node_id) | 375 scenes_node.setAttribute('id', node_id) |
389 pass | 393 pass |
390 | 394 |
391 def rm_timeline(self, name): | 395 def rm_timeline(self, name): |
392 for i, tl in enumerate(self.timelines): | 396 for i, tl in enumerate(self.timelines): |
393 if tl.name() == name: | 397 if tl.name() == name: |
394 comp_node = self.node | 398 comp_node = self._get_comp_node() |
395 comp_node.removeChild(tl.scenes_node) | 399 comp_node.removeChild(tl.scenes_node) |
396 | 400 |
397 del self.timelines[i] | 401 del self.timelines[i] |
398 return | 402 return |
399 pass | 403 pass |
410 def rename(self, new_name): | 414 def rename(self, new_name): |
411 self.node.setAttribute('name', new_name) | 415 self.node.setAttribute('name', new_name) |
412 pass | 416 pass |
413 | 417 |
414 def _create_states_node(self): | 418 def _create_states_node(self): |
415 node = self.node | 419 node = self._get_comp_node() |
416 doc = self._comp_mgr._doc | 420 doc = self._comp_mgr._doc |
417 | 421 |
418 states_node = doc.createElement('ns0:states') | 422 states_node = doc.createElement('ns0:states') |
419 node.addChild(states_node) | 423 node.appendChild(states_node) |
420 self.fsm_states_node = states_node | 424 self.fsm_states_node = states_node |
421 pass | 425 pass |
422 | 426 |
423 def get_start_state_name(self): | 427 def get_start_state_name(self): |
424 return self.fsm_start_state | 428 return self.fsm_start_state |
439 self._create_states_node() | 443 self._create_states_node() |
440 pass | 444 pass |
441 | 445 |
442 doc = self._comp_mgr._doc | 446 doc = self._comp_mgr._doc |
443 | 447 |
444 state = State(name) | 448 state = State() |
445 state.transitions = [] | 449 state.name = name |
446 self.fsm_states[name] = state | 450 self.fsm_states[name] = state |
447 | 451 |
448 state_node = state.create_node(doc) | 452 state_node = state.create_node(doc) |
449 states_node = self.fsm_states_node | 453 states_node = self.fsm_states_node |
450 states_node.addChild(state_node) | 454 states_node.appendChild(state_node) |
451 pass | 455 pass |
452 | 456 |
453 def rename_state(self, state_name, new_name): | 457 def rename_state(self, state_name, new_name): |
454 state = self.fsm_states[state_name] | 458 state = self.fsm_states[state_name] |
455 del self.fsm_states[state_name] | 459 del self.fsm_states[state_name] |