core/state.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  Pushkal Katara (katarapushkal@gmail.com)
19 
20  '''
21 from visualstates.gui.state.guistate import StateGraphicsItem
22 from visualstates.core.transition import Transition
23 from visualstates.core.namespace import Namespace
24 from xml.dom.minidom import Node
25 
26 class State:
27  def __init__(self, id, name, initial, namespace, parent=None):
28  self.parent = parent
29  self.id = id
30  self.name = name
31  self.code = ''
32  self.timeStepDuration = 100
33  self.x = 0
34  self.y = 0
35  self.initial = initial
36  self.children = []
38  self.destTransitions = []
39 
40  self.namespace = namespace
41 
42  self.graphicsItem = None
43  self.isRunning = False
44 
45  def setID(self, id):
46  self.id = id
47 
48  def setName(self, _name):
49  self.name = _name
50 
51  def setNamespace(self, namespace):
52  self.namespace = namespace
53 
54  def getNamespace(self):
55  return self.namespace
56 
57  def getParentNamespace(self):
58  return self.parent.namespace
59 
60  def getID(self):
61  return self.id
62 
63  def getName(self):
64  return self.name
65 
66  def setPos(self, x, y):
67  self.x = x
68  self.y = y
69 
70  def setParent(self, parent):
71  self.parent = parent
72 
73  def getParent(self):
74  return self.parent
75 
76  def addChild(self, child):
77  if child not in self.children:
78  self.children.append(child)
79 
80  def removeChild(self, child):
81  if child in self.children:
82  self.children.remove(child)
83 
84  def getChildren(self):
85  return self.children
86 
88  return self.originTransitions
89 
90  def addOriginTransition(self, tran):
91  if tran not in self.originTransitions:
92  self.originTransitions.append(tran)
93 
94  def removeOriginTransition(self, tran):
95  if tran in self.originTransitions:
96  self.originTransitions.remove(tran)
97 
98  def getDestTransitions(self):
99  return self.destTransitions
100 
101  def addDestTransition(self, tran):
102  if tran not in self.destTransitions:
103  self.destTransitions.append(tran)
104 
105  def removeDestTransition(self, tran):
106  if tran in self.destTransitions:
107  self.destTransitions.remove(tran)
108 
109  def getGraphicsItem(self):
110  if self.graphicsItem == None:
111  self.graphicsItem = StateGraphicsItem(self)
112  self.graphicsItem.posChanged.connect(self.posChanged)
113  self.graphicsItem.setRunning(self.isRunning)
114  return self.graphicsItem
115 
116  def resetGraphicsItem(self):
117  self.graphicsItem = None
118 
119  def posChanged(self, stateItem):
120  scenePos = stateItem.scenePos()
121  self.x = scenePos.x()
122  self.y = scenePos.y()
123 
124  # creates a new copy of the state without children and transitions
125  def getNewCopy(self):
126  copy = State(self.id, self.name, False, self.parent)
127  copy.code = self.code
128  copy.x = self.x
129  copy.y = self.y
130  return copy
131 
132  def parseElement(self, elementName, parentElement):
133  elements = parentElement.getElementsByTagName(elementName)
134  if len(elements) > 0:
135  if len(elements[0].childNodes) > 0:
136  return elements[0].childNodes[0].nodeValue
137  return ''
138 
139  def parse(self, stateElement):
140  # parse attributes of the state
141  for (name, value) in stateElement.attributes.items():
142  if name == 'id':
143  self.id = int(value)
144  elif name == 'initial':
145  self.initial = (value == 'True')
146 
147  self.name = stateElement.getElementsByTagName('name')[0].childNodes[0].nodeValue
148  # print('read name:' + self.name)
149  self.x = float(stateElement.getElementsByTagName('posx')[0].childNodes[0].nodeValue)
150  self.y = float(stateElement.getElementsByTagName('posy')[0].childNodes[0].nodeValue)
151 
152  self.code = self.parseElement('code', stateElement)
153  self.timeStepDuration = int((self.parseElement('timestep', stateElement)))
154 
155  namespace = Namespace('', '')
156  namespace.parse(stateElement.getElementsByTagName('namespace')[0])
157  self.namespace = namespace
158  # recursive child state parsing
159  allChildTransitions = []
160  statesById = {}
161  stateTransitions = []
162 
163  for childNode in stateElement.childNodes:
164  if childNode.nodeType == Node.ELEMENT_NODE:
165  if childNode.tagName == 'state':
166  childState = State(0, 'state', False, self.namespace, self)
167  transitionNodes = childState.parse(childNode)
168 
169  # print('add child:' + childState.name + ' to parent:' + self.name)
170  self.addChild(childState)
171  statesById[childState.id] = childState
172  allChildTransitions = allChildTransitions + transitionNodes
173  elif childNode.tagName == 'transition':
174  stateTransitions.append(childNode)
175 
176  # wire transitions with the states after all the child states are parsed
177  for tranNode in allChildTransitions:
178  transition = Transition(0, 'transition')
179  transition.parse(tranNode, statesById)
180 
181  # return transitions of the state to be able to wire after all states are created
182  return stateTransitions
183 
184  def createElement(self, doc, parentElement=None):
185  stateElement = doc.createElement('state')
186  stateElement.setAttribute('initial', str(self.initial))
187  stateElement.setAttribute('id', str(self.id))
188  posxElement = doc.createElement('posx')
189  posxElement.appendChild(doc.createTextNode(str(self.x)))
190  posyElement = doc.createElement('posy')
191  posyElement.appendChild(doc.createTextNode(str(self.y)))
192  stateElement.appendChild(posxElement)
193  stateElement.appendChild(posyElement)
194  nameElement = doc.createElement('name')
195  nameElement.appendChild(doc.createTextNode(self.name))
196  stateElement.appendChild(nameElement)
197  codeElement = doc.createElement('code')
198  codeElement.appendChild(doc.createTextNode(self.code))
199  stateElement.appendChild(codeElement)
200  timeElement = doc.createElement('timestep')
201  timeElement.appendChild(doc.createTextNode(str(self.timeStepDuration)))
202  stateElement.appendChild(timeElement)
203 
204  stateElement.appendChild(self.namespace.createNode(doc))
205 
206  # create transition elements
207  for tran in self.getOriginTransitions():
208  tranElement = tran.createElement(doc)
209  stateElement.appendChild(tranElement)
210 
211  for child in self.getChildren():
212  child.createElement(doc, stateElement)
213 
214  if parentElement is not None:
215  parentElement.appendChild(stateElement)
216 
217  return stateElement
218 
219  def getCode(self):
220  return self.code
221 
222  def setCode(self, code):
223  self.code = code
224 
225  def getTimeStep(self):
226  return self.timeStepDuration
227 
228  def setTimeStep(self, timestep):
229  self.timeStepDuration = timestep
230 
231  def getInitialChild(self):
232  for child in self.getChildren():
233  if child.initial:
234  return child
235  return None
236 
237  def setInitial(self, initial):
238  self.initial = initial
239 
241  childTransitions = []
242  for child in self.getChildren():
243  childTransitions += child.getOriginTransitions()
244  return childTransitions
245 
246  def setRunning(self, status):
247  self.isRunning = status
248  if self.graphicsItem is not None:
249  self.graphicsItem.setRunning(self.isRunning)
250 
251  if not self.isRunning:
252  for child in self.getChildren():
253  child.setRunning(self.isRunning)
254 
255  def getRunning(self):
256  return self.isRunning
def removeDestTransition(self, tran)
Definition: core/state.py:105
def setTimeStep(self, timestep)
Definition: core/state.py:228
def removeOriginTransition(self, tran)
Definition: core/state.py:94
def __init__(self, id, name, initial, namespace, parent=None)
Definition: core/state.py:27
def posChanged(self, stateItem)
Definition: core/state.py:119
def parseElement(self, elementName, parentElement)
Definition: core/state.py:132
def createElement(self, doc, parentElement=None)
Definition: core/state.py:184
def parse(self, stateElement)
Definition: core/state.py:139
def setInitial(self, initial)
Definition: core/state.py:237
def addOriginTransition(self, tran)
Definition: core/state.py:90
def setNamespace(self, namespace)
Definition: core/state.py:51


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