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

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

Creates cleaner python classes and more able to manage lists

  • Property svn:executable set to *
Line 
1##########################################################################
2## pythonClass.vm
3## The form expects the following context attributes
4## classModel - an instance of PythonClassModel class
5## imports    - a Map<String, Set<String>> defining the modules to import
6## stereotype - an instance of UMLClass representing the classModel
7## attributes - a Set<PythonAttributeModel>
8##########################################################################
9
10#set ($object = "object")
11#set ($newline = '\n')
12#macro( superclass $cm, $stereotype)
13#set ($superclasses = $cm.parents)
14#if($superclasses.size() == 0 && !$stereotype)
15$object#elseif($stereotype && $superclasses.size() == 0)
16${stereotype.name}#elseif($superclasses.size() > 0)
17#foreach( $classModel in $vh.getOrderedParents($cm))
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${tab}@property
56${tab}def ${attr.name}(self):   
57#if($attr.getParentName())
58${tab}${tab}return self._${attr.getParentName().toLowerCase()}.${attr.name}
59#elseif($attr.subTypes)
60#if($attr.hasMultiplicity())
61${tab}${tab}try:
62${tab}${tab}${tab}ret = []
63#foreach($type in $attr.subTypes)
64#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
65${tab}${tab}${tab}ret.extend(self._${attribute.name}_$assoClass)
66#end
67${tab}${tab}${tab}if not hasattr(self, '_${attr.name}'):
68${tab}${tab}${tab}${tab}self._${attr.name} = []
69${tab}${tab}${tab}if self._${attr.name}:
70${tab}${tab}${tab}${tab}ret.extend(self._${attr.name})
71${tab}${tab}${tab}del self._${attr.name}[:]
72${tab}${tab}${tab}self._${attr.name}.extend(set(ret))
73${tab}${tab}except Exception as e:
74${tab}${tab}${tab}pass
75${tab}${tab}return self._${attr.name}
76#else
77#foreach($type in $attr.subTypes)
78#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
79${tab}${tab}if(has_value(self._${attribute.name}_$assoClass)):
80${tab}${tab}${tab}return self._${attribute.name}_$assoClass
81#end
82${tab}${tab}return #initAttributeLeft($attr)   
83#end   
84#end
85#end
86
87#macro(saSetter $type, $attr)
88#foreach($stype in $attr.subTypes)
89#if(!$stype.equals($type))
90${tab}${tab}${tab}self._${attr.name}_${stype.associatedClass.name.toLowerCase()} =#initAttributeLeft($stype)
91#end
92#end
93#end
94
95#macro(initAttributeLeft $attrL)
96#if($attrL.hasMultiplicity()) []
97#else None
98#end
99#end
100
101#macro (setter $attr)
102#if($attr.getParentName())
103${tab}@${attr.name}.setter
104${tab}def ${attr.name}(self, value):   
105${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value
106#elseif($attr.subTypes && !$attr.hasMultiplicity())
107${tab}@${attr.name}.setter
108${tab}def ${attr.name}(self, value):   
109${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__)
110#foreach($type in $attr.subTypes)
111#set ( $assoClass = "$type.associatedClass.name")
112#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
113${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}":
114${tab}${tab}${tab}self._${attr.name}_${assoClassL} = value
115## Reinitializes all the other subTypes
116#saSetter($type, $attr)
117#end
118#end   
119#end
120
121
122#macro ( synchronize $attributes, $parents)
123${tab}def synchronize(self):
124##if($parents)
125##foreach( $parent in $parents)
126##${tab}${tab}self._${parent.associatedClass.name.toLowerCase()}.synchronize() 
127##end
128##end
129#foreach($attr in $attributes)
130#if(!$attr.getParentName() && $attr.hasMultiplicity())
131#foreach($type in $attr.subTypes)
132#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
133${tab}${tab}del self._${attr.name}_${assoClassL}[:]
134#end
135#end
136#end
137#foreach($attr in $attributes)
138#if(!$attr.getParentName() && $attr.hasMultiplicity() && $attr.subTypes)
139${tab}${tab}for item in self.${attr.name}:     
140#foreach($type in $attr.subTypes)
141#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
142${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__)
143${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${type.associatedClass.name}":
144${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item)
145${tab}${tab}${tab}${tab}continue
146#end
147#end
148#end
149${tab}${tab}pass
150#end
151
152
153#macro ( dogettersetter $attributes)
154#foreach($attribute in $attributes)
155#if($attribute.getParentName() || $attribute.subTypes)
156#getter($attribute)
157
158#setter($attribute)     
159#end
160#end
161#end
162
163#macro ( enlistKeys $attributes)
164#foreach($attribute in $attributes)
165self.${attribute.name}#if($foreach.hasNext), #end
166#end
167#end
168
169#macro( subTypes $attribute)
170## Generates the hidden objects attributes used by the RDBM
171#if($attr.getParentName())
172#else
173#foreach($type in $attribute.subTypes)
174${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute)
175#end
176#end
177## Generates the hidden dummy object oriented instance of the attribute
178${tab}${tab}self._${attribute.name} =#initAttributeLeft($attribute)
179#end
180
181#macro( attributes $attributes, $parents)
182#if($parents)
183#foreach( $parent in $parents)
184${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass($parent.associatedClass)}()       
185#end
186#end
187
188#foreach($attribute in $attributes)
189#if($attribute.subTypes && !$attribute.getParentName())
190#subTypes($attribute)
191#else
192#if(($attribute.getParentName() && !$attribute.hasMultiplicity()) || !$attribute.getParentName())
193${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute)
194#end
195#end
196#end
197#end
198
199
200#importing($imports)
201from ascore.utils import has_value, collectionProofHash
202
203'''
204Created on $date
205
206@author: mnagni
207'''
208
209class ${classModel.associatedClass.name}(#superclass($classModel, $stereotype)):
210${tab}'''
211${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
212${tab}${tab}be persisted in a relational database.
213 
214${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
215${tab}'''
216#if($classModel.isEnumeration())
217#asEnumeration($classModel)
218#elseif ($classModel.isCodeList())
219#asCodeList($classModel)
220#elseif ($classModel.isUnion())
221#asUnion($classModel)
222#else
223${tab}'''
224${tab}${tab}Please note that any access to the inner attributes should be done using
225${tab}${tab}the given get/set methods and NOT accessing them directly.
226${tab}'''
227${tab}def __init__(self):
228#end
229#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
230#attributes($attributes, $classModel.parents)
231${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
232#dogettersetter($attributes)
233
234#synchronize($attributes, $classModel.parents)
235
236${tab}def __key(self):
237${tab}${tab}return (#enlistKeys($attributes))
238
239${tab}def __eq__(self, y):
240${tab}${tab}if type(self) != type(y):
241${tab}${tab}${tab}return False
242${tab}${tab}'''
243${tab}${tab}Cannot compare classes which do not define a "__key" attribute
244${tab}${tab}'''
245${tab}${tab}if hasattr(self, '_%s__key' % (type(self).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)):
246${tab}${tab}${tab}return self.__key() == y.__key()
247${tab}${tab}return id(self) == id(y)
248
249${tab}def __hash__(self):
250${tab}${tab}return collectionProofHash(self.__key())
251#end
Note: See TracBrowser for help on using the repository browser.