Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Add parameterization and online library import export #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/visualstates/core/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
Authors : Pushkal Katara ([email protected])

'''
from visualstates.core.parameter import Parameter

class Namespace:
def __init__(self, functions, variables):
def __init__(self, functions, variables, params=[]):
self.functions = functions
self.variables = variables
self.params = params

def createNode(self, doc, globalNamespace=False):
if globalNamespace:
Expand All @@ -33,6 +36,10 @@ def createNode(self, doc, globalNamespace=False):
variablesElement = doc.createElement('variables')
variablesElement.appendChild(doc.createTextNode(self.variables))
namespaceElement.appendChild(variablesElement)
paramsElement = doc.createElement('parameters')
for param in self.params:
paramsElement.appendChild(param.createDocFromParam(doc))
namespaceElement.appendChild(paramsElement)
return namespaceElement

def parseElement(self, elementName, parentElement):
Expand All @@ -42,9 +49,20 @@ def parseElement(self, elementName, parentElement):
return elements[0].childNodes[0].nodeValue
return ''

def parseParamElement(self, parentElement):
params = []
paramsElement = parentElement.getElementsByTagName('parameters')
if len(paramsElement) > 0:
for paramElement in paramsElement[0].getElementsByTagName('param'):
param = Parameter()
param.parseElement(paramElement)
params.append(param)
return params

def parse(self, namespaceElement):
self.functions = self.parseElement('functions', namespaceElement)
self.variables = self.parseElement('variables', namespaceElement)
self.params = self.parseParamElement(namespaceElement)

def getVariables(self):
return self.variables
Expand Down Expand Up @@ -80,3 +98,12 @@ def addFunctions(self, functions):

def setFunctions(self, functions):
self.functions = functions

def addParams(self, params):
self.params += params

def getParams(self):
return self.params

def setParams(self, params):
self.params = params
88 changes: 88 additions & 0 deletions src/visualstates/core/parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'''
Copyright (C) 1997-2019 JDERobot Developers Team

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.

Authors : Baidyanath Kundu ([email protected])

'''

class Parameter:
def __init__(self, name='', type='', value='', desc=''):
self.name = name
self.type = type
self.value = value
self.desc = desc

def setName(self, name):
self.name = name

def setType(self, type):
self.type = type

def setValue(self, value):
self.value = value

def setDesc(self, desc):
self.desc = desc

def createDocFromParam(self, doc):
paramElement = doc.createElement('param')
paramElement.setAttribute('type', self.type)
paramElement.setAttribute('name', self.name)

valueElement = doc.createElement('value')
valueElement.appendChild(doc.createTextNode(self.value))
paramElement.appendChild(valueElement)
descElement = doc.createElement('description')
descElement.appendChild(doc.createTextNode(self.desc))
paramElement.appendChild(descElement)

return paramElement

def parseElement(self, element):
for (name, value) in element.attributes.items():
if name == 'name':
self.name = str(value)
elif name == 'type':
self.type = str(value)

if len(element.getElementsByTagName('value')[0].childNodes) > 0:
self.value = str(element.getElementsByTagName('value')[0].childNodes[0].nodeValue)
else:
self.value = ''
if len(element.getElementsByTagName('description')[0].childNodes) > 0:
self.desc = str(element.getElementsByTagName('description')[0].childNodes[0].nodeValue)
else:
self.desc = ''

def isTypeEqualValue(type, value):
if type == 'Boolean' and not (value == 'True' or value == 'False' or value == 'true' or value == 'false'):
return False
elif type == 'Integer' and not value.isdigit():
return False
elif type == 'Float' and not (value.replace('.', '', 1).isdigit()):
return False
elif type == 'Character' and not len(value) == 1:
return False
else:
return True

def isParamName(name):
if name == '':
return False
elif name.replace('_', '').isalnum() and name[0].isalpha():
return True
else:
return False
2 changes: 1 addition & 1 deletion src/visualstates/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def parse(self, stateElement):
self.code = self.parseElement('code', stateElement)
self.timeStepDuration = int((self.parseElement('timestep', stateElement)))

namespace = Namespace('', '')
namespace = Namespace('', '', [])
namespace.parse(stateElement.getElementsByTagName('namespace')[0])
self.namespace = namespace
# recursive child state parsing
Expand Down
34 changes: 20 additions & 14 deletions src/visualstates/generators/cpprosgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def generate(self, projectPath, projectName):
self.generateStateClasses(stringList)
self.generateTransitionClasses(stringList)
stringList.append('#endif')
sourceCode = ''.join(stringList)
fp = open(projectPath + os.sep + 'src' + os.sep + projectName + '.h', 'w')
fp.write(sourceCode)
fp.close()
headerSourceCode = ''.join(stringList)

stringList = []
self.generateHeadersForCpp(stringList, projectName)
Expand All @@ -56,28 +53,37 @@ def generate(self, projectPath, projectName):
self.generateMain(stringList, projectName)
sourceCode = ''.join(stringList)

stringList = []
self.generateRunTimeGui(stringList)
guiSourceCode = ''.join(stringList)

stringList = []
self.generateCmake(stringList, projectName, self.config)
cmakeString = ''.join(stringList)

xmlDoc = self.generatePackageXml(self.config, projectName)
xmlStr = xmlDoc.toprettyxml(indent=' ')

# writing to files
fp = open(projectPath + os.sep + 'src' + os.sep + projectName + '.h', 'w')
fp.write(headerSourceCode)
fp.close()


fp = open(projectPath + os.sep + 'src' + os.sep + projectName + '.cpp', 'w')
fp.write(sourceCode)
fp.close()

stringList = []
self.generateRunTimeGui(stringList)
sourceCode = ''.join(stringList)
fp = open(projectPath + os.sep + projectName + '_runtime.py', 'w')
fp.write(sourceCode)
fp.write(guiSourceCode)
fp.close()
# make runtime gui python file executable
os.chmod(projectPath + os.sep + projectName + '_runtime.py', stat.S_IEXEC | stat.S_IXOTH | stat.S_IWRITE | stat.S_IREAD)

stringList = []
self.generateCmake(stringList, projectName, self.config)
cmakeString = ''.join(stringList)
fp = open(projectPath + os.sep + 'CMakeLists.txt', 'w')
fp.write(cmakeString)
fp.close()

xmlDoc = self.generatePackageXml(self.config, projectName)
xmlStr = xmlDoc.toprettyxml(indent=' ')
with open(projectPath + os.sep + 'package.xml', 'w') as f:
f.write(xmlStr)

Expand Down Expand Up @@ -177,7 +183,7 @@ def generateStateNamespaceClass(self, classStr):
classStr.append('class Namespace' + str(state.id) + ' {\n')
classStr.append('public:\n')
classStr.append('\tGlobalNamespace* globalNamespace;\n')
types, varNames, initialValues = CPPParser.parseVariables(state.getNamespace().variables)
types, varNames, initialValues = CPPParser.parseVariables(state.getNamespace().variables, state.getNamespace().params)
for i in range(len(types)):
classStr.append(types[i] + ' ' + varNames[i] + ';\n')
classStr.append('\n')
Expand Down
Loading