core/transition.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 from visualstates.gui.transition.transitiontype import TransitionType
21 from visualstates.gui.transition.guitransition import TransitionGraphicsItem
22 from PyQt5.QtCore import QPointF
23 
24 class Transition:
25  def __init__(self, id, name, origin=None, dest=None):
26  self.id = id
27  self.name = name
28  self.transitionType = TransitionType.TEMPORAL
29  self.code = ''
30  self.temporalTime = 0
31  self.condition = ''
32  self.x = 0
33  self.y = 0
34  self.isPosChanged = False
35 
36  self.origin = None
37  self.destination = None
38 
39  # set transitions on the state if origin and dest are not None
40  if origin:
41  self.origin = origin
42  self.origin.addOriginTransition(self)
43  if dest:
44  self.destination = dest
45  self.destination.addDestTransition(self)
46 
48 
49  self.graphicsItem = None
50 
51  def setPos(self, x, y):
52  self.x = x
53  self.y = y
54  self.isPosChanged = True
55 
56  def addOriginState(self, origin):
57  if origin != self.origin:
58  self.origin = origin
59  self.origin.addOriginTransition(self)
60 
61  def addDestinationState(self, dest):
62  if dest != self.destination:
63  self.destination = dest
64  self.destination.addDestTransition(self)
65 
66  def getGraphicsItem(self):
67  if self.graphicsItem is None:
68  self.graphicsItem = TransitionGraphicsItem(self)
69  self.graphicsItem.posChanged.connect(self.posChanged)
70 
71  if self.isPosChanged:
72  self.graphicsItem.updateMiddlePoints(QPointF(self.x, self.y))
73  self.graphicsItem.createMiddleHandle()
74  self.isPosChanged = False
75 
76  return self.graphicsItem
77 
78  def resetGraphicsItem(self):
79  self.graphicsItem = None
80  self.isPosChanged = True
81 
82  def getTemporalTime(self):
83  return self.temporalTime
84 
85  def getNamespace(self):
86  return self.origin.getNamespace()
87 
88  def setTemporalTime(self, time):
89  self.temporalTime = int(time)
90 
91  def getCondition(self):
92  return self.condition
93 
94  def setCondition(self, cond):
95  self.condition = cond
96 
97  def getType(self):
98  return self.transitionType
99 
100  def setType(self, type):
101  if type == TransitionType.TEMPORAL or type == TransitionType.CONDITIONAL:
102  self.transitionType = type
103 
104  def getCode(self):
105  return self.code
106 
107  def setCode(self, code):
108  self.code = code
109 
110  def posChanged(self, tranItem):
111  self.isPosChanged = True
112  self.x = tranItem.midPointX
113  self.y = tranItem.midPointY
114 
116  if self.origin is not None and self.destination is not None:
117  self.x = (self.origin.x + self.destination.x)/2
118  self.y = (self.origin.y + self.destination.y)/2
119 
120  def createElement(self, doc):
121  tranElement = doc.createElement('transition')
122  tranElement.setAttribute('id', str(self.id))
123  typeElement = doc.createElement('type')
124  typeElement.appendChild(doc.createTextNode(str(self.getType())))
125  tranElement.appendChild(typeElement)
126  if self.getType() == TransitionType.CONDITIONAL:
127  condElement = doc.createElement('condition')
128  condElement.appendChild(doc.createTextNode(self.getCondition()))
129  tranElement.appendChild(condElement)
130  elif self.getType() == TransitionType.TEMPORAL:
131  timeElement = doc.createElement('time')
132  timeElement.appendChild(doc.createTextNode(str(self.getTemporalTime())))
133  tranElement.appendChild(timeElement)
134  tposxElement = doc.createElement('posx')
135  tposxElement.appendChild(doc.createTextNode(str(self.x)))
136  tranElement.appendChild(tposxElement)
137  tposyElement = doc.createElement('posy')
138  tposyElement.appendChild(doc.createTextNode(str(self.y)))
139  tranElement.appendChild(tposyElement)
140  nameElement = doc.createElement('name')
141  nameElement.appendChild(doc.createTextNode(self.name))
142  tranElement.appendChild(nameElement)
143  originElement = doc.createElement('originid')
144  originElement.appendChild(doc.createTextNode(str(self.origin.id)))
145  tranElement.appendChild(originElement)
146  destinElement = doc.createElement('destinationid')
147  destinElement.appendChild(doc.createTextNode(str(self.destination.id)))
148  tranElement.appendChild(destinElement)
149  codeElement = doc.createElement('code')
150  codeElement.appendChild(doc.createTextNode(self.getCode()))
151  tranElement.appendChild(codeElement)
152  return tranElement
153 
154  def parse(self, transitionElement, statesById):
155  for (name, value) in transitionElement.attributes.items():
156  if name == 'id':
157  self.id = int(value)
158  self.transitionType = int(transitionElement.getElementsByTagName('type')[0].childNodes[0].nodeValue)
159  if self.transitionType == TransitionType.TEMPORAL:
160  self.setTemporalTime(int(transitionElement.getElementsByTagName('time')[0].childNodes[0].nodeValue))
161  elif self.transitionType == TransitionType.CONDITIONAL:
162  self.setCondition(transitionElement.getElementsByTagName('condition')[0].childNodes[0].nodeValue)
163  if len(transitionElement.getElementsByTagName('name')[0].childNodes) > 0:
164  self.name = transitionElement.getElementsByTagName('name')[0].childNodes[0].nodeValue
165  else:
166  self.name = ""
167  self.x = float(transitionElement.getElementsByTagName('posx')[0].childNodes[0].nodeValue)
168  self.y = float(transitionElement.getElementsByTagName('posy')[0].childNodes[0].nodeValue)
169  # parse optinal code tag
170  if len(transitionElement.getElementsByTagName('code')[0].childNodes) > 0:
171  self.setCode(transitionElement.getElementsByTagName('code')[0].childNodes[0].nodeValue)
172  originId = int(transitionElement.getElementsByTagName('originid')[0].childNodes[0].nodeValue)
173  self.addOriginState(statesById[originId])
174  destinationId = int(transitionElement.getElementsByTagName('destinationid')[0].childNodes[0].nodeValue)
175  self.addDestinationState(statesById[destinationId])
176  self.isPosChanged = True
177 
178  def setID(self, id):
179  self.id = id
180 
181  def getID(self):
182  return self.id
def parse(self, transitionElement, statesById)
def __init__(self, id, name, origin=None, dest=None)


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20