source: mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm @ 8085

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm@8085
Revision 8085, 5.5 KB checked in by mnagni, 8 years ago (diff)

Correctly generates DB tables for CEDAEditor
added two Junit tests

Actually can import a configuration file to add further classes to the table generation

In sqlTables.py, still this has to be fixed by hand:

-- Column('role', CI_RoleCode.db_type(), nullable=False) ----> Column('role', CI_RoleCode.db_type())

  • Property svn:executable set to *
Line 
1##########################################################################
2## pythonClass.vm
3## The form expects the following context attributes
4## classModel - an instance of ClassModel class
5## imports    - a Map<String, Set<String>> defining the modules to import
6## stereotype - an instance of UMLClass
7##########################################################################
8
9#set ($object = "object")
10#set ($newline = '\n')
11#macro( superclass $cm, $stereotype)
12#set ($superclasses = $cm.parents)
13#if($superclasses.size() == 0 && !$stereotype)
14$object#elseif($stereotype && $superclasses.size() == 0)
15${stereotype.name}#elseif($superclasses.size() > 0)
16#foreach( $classModel in $vh.getOrderedParents($cm))
17##${classModel.associatedClass.name}#if($foreach.hasNext),#end
18${vh.getMappedClass(${classModel.associatedClass})}#if($foreach.hasNext),#end
19#end
20#end
21#end
22
23#macro( asEnumeration $asEnumeration)
24#foreach($item in $asEnumeration.simpleKeyValues)
25${tab}en_${vh.transformToPythonName($item.key)} = "${item.value}"
26#end
27#end
28
29#macro( asCodeList $asCodeList)
30#foreach($item in $asCodeList.simpleKeyValues)
31${tab}cl_${vh.transformToPythonName($item.key)} = "${item.value}"
32#end
33#end
34
35#macro( asUnion $classModel)
36${tab}types = {#unionDictionary($classModel.attributeModel)}
37#end
38
39#macro (unionDictionary $attributeModel)
40#foreach($item in $attributeModel)
41'${item.name}':${vh.getMappedClass(${item.associatedType.associatedClass})}#if($foreach.hasNext),#end 
42#end
43#end
44
45
46#macro( importing $toImport)
47#foreach( $key in $toImport.keySet())   
48from $key import #foreach( $item in $toImport.get($key)) $item#if($foreach.hasNext),#end #end
49
50#end
51#end
52
53#macro (getter $attr)
54#set ( $methodName = "$display.capitalize( $attr.name )")
55#if($attr.getParentName() || $attr.subTypes)
56${tab}@property
57${tab}def ${attr.name}(self):
58#if($attr.getParentName())
59${tab}${tab}return self._${attr.getParentName().toLowerCase()}.${attr.name}
60#elseif($attr.subTypes)
61#foreach($type in $attr.subTypes)
62#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
63${tab}${tab}if(self._${attribute.name}_$assoClass is not None):
64${tab}${tab}${tab}return self._${attribute.name}_$assoClass
65#end
66${tab}${tab}return None
67#end
68#end
69#end
70
71#macro(saSetter $type, $attr)
72#foreach($stype in $attr.subTypes)
73#if(!$stype.equals($type))
74${tab}${tab}${tab}self._${attr.name}_${stype.associatedClass.name.toLowerCase()}#initAttributeLeft($stype)
75#end
76#end
77#end
78
79#macro(initAttributeLeft $attrL)
80#if($attrL.hasMultiplicity()) = []
81#else = None
82#end
83#end
84
85#macro (setter $attr)
86#set ( $methodName = "$display.capitalize( $attr.name )")
87#if($attr.getParentName() || $attr.subTypes)
88${tab}@${attr.name}.setter
89${tab}def ${attr.name}(self, value):
90#if($attr.getParentName())
91${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value
92#elseif($attr.subTypes)
93${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__)
94#foreach($type in $attr.subTypes)
95#set ( $assoClass = "$type.associatedClass.name")
96#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
97${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}":
98${tab}${tab}${tab}self._${attr.name}_$assoClassL = value
99#saSetter($type, $attr)
100#end
101#end
102#end
103#end
104
105#macro ( dogettersetter $attributes)
106#foreach($attribute in $attributes)
107
108#getter($attribute)
109
110#setter($attribute)
111#end
112#end
113
114#macro ( enlistKeys $attributes)
115#foreach($attribute in $attributes)
116self.${attribute.name}#if($foreach.hasNext), #end
117#end
118#end
119
120#macro( subTypes $attribute)
121#foreach($type in $attribute.subTypes)
122${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()}#initAttributeLeft($attribute)
123#end
124#end
125
126#macro( attributes $attributes, $parents)
127#if($parents)
128#foreach( $parent in $parents)
129${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass($parent.associatedClass)}()       
130#end
131#end
132
133#foreach($attribute in $attributes)
134#if($attribute.subTypes)
135#subTypes($attribute)
136#else
137${tab}${tab}self.${attribute.name}#initAttributeLeft($attribute)
138#end
139#end
140#end
141
142
143#importing($imports)
144
145'''
146Created on $date
147
148@author: mnagni
149'''
150
151class ${classModel.associatedClass.name}(#superclass($classModel, $stereotype)):
152${tab}'''
153${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
154${tab}${tab}be persisted in a relational database.
155 
156${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
157${tab}'''
158#if($classModel.isEnumeration())
159#asEnumeration($classModel)
160#elseif ($classModel.isCodeList())
161#asCodeList($classModel)
162#elseif ($classModel.isUnion())
163#asUnion($classModel)
164#else
165${tab}'''
166${tab}${tab}Please note that any access to the inner attributes should be done using
167${tab}${tab}the given get/set methods and NOT accessing them directly.
168${tab}'''
169${tab}def __init__(self):
170#end
171#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
172#attributes($attributes, $classModel.parents)
173${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
174#dogettersetter($attributes)
175
176${tab}def __key(self):
177${tab}${tab}return (#enlistKeys($attributes))
178
179${tab}def __eq__(x, y):
180${tab}${tab}'''
181${tab}${tab}Cannot compare classes which do not define a "__key" attribute
182${tab}${tab}'''
183${tab}${tab}if hasattr(x, '_%s__key' % (type(x).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)):
184${tab}${tab}${tab}return x.__key() == y.__key()
185${tab}${tab}return id(x) == id(y)
186
187${tab}def __hash__(self):
188${tab}${tab}return hash(self.__key())
189#end
Note: See TracBrowser for help on using the repository browser.