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

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

Stable release 0.1.0

  • 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}:
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())
140#foreach($type in $attr.subTypes)
141#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
142${tab}${tab}del self._${attr.name}_${assoClassL}[:]
143#end
144#end
145#end
146#foreach($attr in $attributes)
147#if(!$attr.getParentName() && $attr.hasMultiplicity() && $attr.subTypes)
148${tab}${tab}for item in self.${attr.name}:     
149#foreach($type in $attr.subTypes)
150#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
151${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__)
152${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${type.associatedClass.name}":
153${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item)
154${tab}${tab}${tab}${tab}continue
155#end
156#end
157#end
158${tab}${tab}pass
159#end
160
161
162#macro ( dogettersetter $attributes)
163#foreach($attribute in $attributes)
164#if($attribute.getParentName() || $attribute.subTypes)
165#getter($attribute)
166
167#setter($attribute)     
168#end
169#end
170#end
171
172#macro ( enlistKeys $attributes)
173#foreach($attribute in $attributes)
174self.${attribute.name}#if($foreach.hasNext), #end
175#end
176#end
177
178#macro( subTypes $attribute)
179## Generates the hidden objects attributes used by the RDBM
180#if($attr.getParentName())
181#else
182#foreach($type in $attribute.subTypes)
183${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute)
184#end
185#end
186## Generates the hidden dummy object oriented instance of the attribute
187${tab}${tab}self._${attribute.name} =#initAttributeLeft($attribute)
188#end
189
190#macro( attributes $attributes, $parents)
191#if($parents)
192#foreach( $parent in $parents)
193${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass($parent.associatedClass)}()       
194#end
195#end
196
197#foreach($attribute in $attributes)
198#if($attribute.subTypes && !$attribute.getParentName())
199#subTypes($attribute)
200#else
201#if(($attribute.getParentName() && !$attribute.hasMultiplicity()) || !$attribute.getParentName())
202${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute)
203#end
204#end
205#end
206#end
207
208
209#importing($imports)
210from ascore.utils import has_value, collectionProofHash
211
212'''
213Created on $date
214
215@author: mnagni
216'''
217
218class ${classModel.associatedClass.name}(#superclass($classModel, $stereotype)):
219${tab}'''
220${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
221${tab}${tab}be persisted in a relational database.
222 
223${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
224${tab}'''
225#if($classModel.isEnumeration())
226#asEnumeration($classModel)
227#elseif ($classModel.isCodeList())
228#asCodeList($classModel)
229#elseif ($classModel.isUnion())
230#asUnion($classModel)
231#else
232${tab}'''
233${tab}${tab}Please note that any access to the inner attributes should be done using
234${tab}${tab}the given get/set methods and NOT accessing them directly.
235${tab}'''
236${tab}def __init__(self):
237#end
238#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
239#attributes($attributes, $classModel.parents)
240${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
241#dogettersetter($attributes)
242
243#synchronize($attributes, $classModel.parents)
244
245${tab}def __key(self):
246${tab}${tab}return (#enlistKeys($attributes))
247
248${tab}def __eq__(self, y):
249${tab}${tab}if type(self) != type(y):
250${tab}${tab}${tab}return False
251${tab}${tab}'''
252${tab}${tab}Cannot compare classes which do not define a "__key" attribute
253${tab}${tab}'''
254${tab}${tab}if hasattr(self, '_%s__key' % (type(self).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)):
255${tab}${tab}${tab}return self.__key() == y.__key()
256${tab}${tab}return id(self) == id(y)
257
258${tab}def __hash__(self):
259${tab}${tab}return collectionProofHash(self.__key())
260#end
Note: See TracBrowser for help on using the repository browser.