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

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

Corrected method "synchronize" for the python classes (pythonClass.vm). Now do not lost values
Removed some unnecessary imports

  • 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#set ($index = 0)       
25#foreach($item in $asEnumeration.simpleKeyValues)       
26#if(${vh.isValidPythonName($item.key)})
27${tab}en_${vh.transformToPythonName($item.key)} = "${item.value}"
28#else
29#set ($index = $index + 1)     
30${tab}en_invalid_name_${index} = "${item.value}"
31#end
32#end
33#end
34
35#macro( asCodeList $asCodeList)
36#foreach($item in $asCodeList.simpleKeyValues)
37${tab}cl_${vh.transformToPythonName($item.key)} = "${item.value}"
38#end
39#if ($asCodeList.simpleKeyValues.length() == 0):
40${tab}cl_no_keys = ""   
41#end   
42#end
43
44#macro( asUnion $classModel)
45${tab}types = {#unionDictionary($classModel.attributeModel)}
46#end
47
48#macro (unionDictionary $attributeModel)
49#foreach($item in $attributeModel)
50'${item.name}':${vh.getMappedClass(${item.associatedType.associatedClass})}#if($foreach.hasNext),#end 
51#end
52#end
53
54
55#macro( importing $toImport)
56#foreach( $key in $toImport.keySet())   
57from $key import #foreach( $item in $toImport.get($key)) $item#if($foreach.hasNext),#end #end
58
59#end
60#end
61
62#macro (getter $attr)
63#set ( $methodName = "$display.capitalize( $attr.name )")
64${tab}@property
65${tab}def ${attr.name}(self):   
66#if($attr.getParentName())
67${tab}${tab}return self._${attr.getParentName().toLowerCase()}.${attr.name}
68#elseif($attr.subTypes)
69#if($attr.hasMultiplicity())
70${tab}${tab}try:
71${tab}${tab}${tab}ret = []
72#foreach($type in $attr.subTypes)
73#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
74${tab}${tab}${tab}ret.extend(self._${attribute.name}_$assoClass)
75#end
76${tab}${tab}${tab}if not hasattr(self, '_${attr.name}'):
77${tab}${tab}${tab}${tab}self._${attr.name} = []
78${tab}${tab}${tab}if self._${attr.name} is not None:
79${tab}${tab}${tab}${tab}ret.extend(self._${attr.name})
80${tab}${tab}${tab}del self._${attr.name}[:]
81${tab}${tab}${tab}self._${attr.name}.extend(set(ret))
82${tab}${tab}except Exception as e:
83${tab}${tab}${tab}pass
84${tab}${tab}return self._${attr.name}
85#else
86#foreach($type in $attr.subTypes)
87#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
88${tab}${tab}if(has_value(self._${attribute.name}_$assoClass)):
89${tab}${tab}${tab}return self._${attribute.name}_$assoClass
90#end
91${tab}${tab}return #initAttributeLeft($attr)   
92#end   
93#end
94#end
95
96#macro(saSetter $type, $attr)
97#foreach($stype in $attr.subTypes)
98#if(!$stype.equals($type))
99${tab}${tab}${tab}self._${attr.name}_${stype.associatedClass.name.toLowerCase()} =#initAttributeLeft($stype)
100#end
101#end
102#end
103
104#macro(initAttributeLeft $attrL)
105#if($attrL.hasMultiplicity()) []
106#else None
107#end
108#end
109
110#macro (setter $attr)
111#if($attr.getParentName())
112${tab}@${attr.name}.setter
113${tab}def ${attr.name}(self, value):   
114${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value
115#elseif($attr.subTypes && !$attr.hasMultiplicity())
116${tab}@${attr.name}.setter
117${tab}def ${attr.name}(self, value):   
118${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__)
119#foreach($type in $attr.subTypes)
120#set ( $assoClass = "$type.associatedClass.name")
121#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
122${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}":
123${tab}${tab}${tab}self._${attr.name}_${assoClassL} = value
124## Reinitializes all the other subTypes
125#saSetter($type, $attr)
126#end
127#end   
128#end
129
130
131#macro ( synchronize $attributes, $parents)
132${tab}def synchronize(self):
133##if($parents)
134##foreach( $parent in $parents)
135##${tab}${tab}self._${parent.associatedClass.name.toLowerCase()}.synchronize() 
136##end
137##end
138#foreach($attr in $attributes)
139#if(!$attr.getParentName() && $attr.hasMultiplicity() && $attr.subTypes)
140${tab}${tab}int_${attr.name} = self.${attr.name}       
141#foreach($type in $attr.subTypes)
142#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
143${tab}${tab}del self._${attr.name}_${assoClassL}[:]
144#end
145#end
146#end
147#foreach($attr in $attributes)
148#if(!$attr.getParentName() && $attr.hasMultiplicity() && $attr.subTypes)
149${tab}${tab}for item in int_${attr.name}:       
150${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__)
151#foreach($type in $attr.subTypes)
152#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
153${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${type.associatedClass.name}":
154${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item)
155${tab}${tab}${tab}${tab}continue
156#end
157#end
158#end
159${tab}${tab}pass
160#end
161
162
163#macro ( dogettersetter $attributes)
164#foreach($attribute in $attributes)
165#if($attribute.getParentName() || $attribute.subTypes)
166#getter($attribute)
167
168#setter($attribute)     
169#end
170#end
171#end
172
173#macro ( enlistKeys $attributes)
174#foreach($attribute in $attributes)
175self.${attribute.name}#if($foreach.hasNext), #end
176#end
177#end
178
179#macro( subTypes $attribute)
180## Generates the hidden objects attributes used by the RDBM
181#if($attr.getParentName())
182#else
183#foreach($type in $attribute.subTypes)
184${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute)
185#end
186#end
187## Generates the hidden dummy object oriented instance of the attribute
188${tab}${tab}self._${attribute.name} =#initAttributeLeft($attribute)
189#end
190
191#macro( attributes $attributes, $parents)
192#if($parents)
193#foreach( $parent in $parents)
194${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass($parent.associatedClass)}()       
195#end
196#end
197
198#foreach($attribute in $attributes)
199#if($attribute.subTypes && !$attribute.getParentName())
200#subTypes($attribute)
201#else
202#if(($attribute.getParentName() && !$attribute.hasMultiplicity()) || !$attribute.getParentName())
203${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute)
204#end
205#end
206#end
207#end
208
209
210#importing($imports)
211from ascore.utils import collectionProofHash
212
213'''
214Created on $date
215
216@author: mnagni
217'''
218
219class ${classModel.associatedClass.name}(#superclass($classModel, $stereotype)):
220${tab}'''
221${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
222${tab}${tab}be persisted in a relational database.
223 
224${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
225${tab}'''
226#if($classModel.isEnumeration())
227#asEnumeration($classModel)
228#elseif ($classModel.isCodeList())
229#asCodeList($classModel)
230#elseif ($classModel.isUnion())
231#asUnion($classModel)
232#else
233${tab}'''
234${tab}${tab}Please note that any access to the inner attributes should be done using
235${tab}${tab}the given get/set methods and NOT accessing them directly.
236${tab}'''
237${tab}def __init__(self):
238#end
239#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
240#attributes($attributes, $classModel.parents)
241${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
242#dogettersetter($attributes)
243
244#synchronize($attributes, $classModel.parents)
245
246${tab}def __key(self):
247${tab}${tab}return (#enlistKeys($attributes))
248
249${tab}def __eq__(self, y):
250${tab}${tab}if type(self) != type(y):
251${tab}${tab}${tab}return False
252${tab}${tab}'''
253${tab}${tab}Cannot compare classes which do not define a "__key" attribute
254${tab}${tab}'''
255${tab}${tab}if hasattr(self, '_%s__key' % (type(self).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)):
256${tab}${tab}${tab}return self.__key() == y.__key()
257${tab}${tab}return id(self) == id(y)
258
259${tab}def __hash__(self):
260${tab}${tab}return collectionProofHash(self.__key())
261#end
Note: See TracBrowser for help on using the repository browser.