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

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

Now generate the inner class attributes and table relations which manage the attribute subtyping (at least for no more than the same subtyping attribute for each class)

  • 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 $superclasses, $stereotype)
12#if($superclasses.size() == 0 && !$stereotype)
13$object#elseif($stereotype && $superclasses.size() == 0)
14${stereotype.name}#elseif($superclasses.size() > 0)
15#foreach( $classModel in $superclasses)
16##${classModel.associatedClass.name}#if($foreach.hasNext),#end
17${vh.getMappedClass(${classModel.associatedClass.name})}#if($foreach.hasNext),#end
18#end
19#end
20#end
21
22#macro( asEnumeration $asEnumeration)
23#foreach($item in $asEnumeration.simpleKeyValues)
24${tab}en_${vh.transformToPythonName($item.key)} = "${item.value}"
25#end
26#end
27
28#macro( asCodeList $asCodeList)
29#foreach($item in $asCodeList.simpleKeyValues)
30${tab}cl_${vh.transformToPythonName($item.key)} = "${item.value}"
31#end
32#end
33
34#macro( asUnion $classModel)
35${tab}types = {#unionDictionary($classModel.attributeModel)}
36#end
37
38#macro (unionDictionary $attributeModel)
39#foreach($item in $attributeModel)
40'${item.name}':${vh.getMappedClass(${item.associatedType.associatedClass.name})}#if($foreach.hasNext),#end 
41#end
42#end
43
44
45#macro( importing $toImport)
46#foreach( $key in $toImport.keySet())   
47from $key import #foreach( $item in $toImport.get($key)) $item#if($foreach.hasNext),#end #end
48
49#end
50#end
51
52#macro (getter $attribute)
53#set ( $methodName = "$display.capitalize( $attribute.name )")
54#if($attribute.getParentName())
55${tab}@property
56${tab}def ${attribute.name}(self):
57${tab}${tab}return self._${attribute.getParentName().toLowerCase()}.${attribute.name}
58#end
59#if($attribute.subTypes)
60${tab}@property
61${tab}def ${attribute.name}(self):
62#foreach($type in $attribute.subTypes)
63#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
64${tab}${tab}if(self._$assoClass is not None):
65${tab}${tab}${tab}return self._$assoClass
66#end
67${tab}${tab}return None
68#end
69#end
70
71#macro(saSetter $type, $sTypes)
72#foreach($stype in $sTypes)
73#if(!$stype.equals($type))
74${tab}${tab}${tab}self._${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 $attribute)
86#set ( $methodName = "$display.capitalize( $attribute.name )")
87#if($attribute.getParentName())
88${tab}@${attribute.name}.setter
89${tab}def ${attribute.name}(self, value):
90${tab}${tab}${tab} self._${attribute.getParentName().toLowerCase()}.${attribute.name} = value
91#end
92#if($attribute.subTypes)
93${tab}@${attribute.name}.setter
94${tab}def ${attribute.name}(self, value):
95#foreach($type in $attribute.subTypes)
96#set ( $assoClass = "$type.associatedClass.name")
97#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
98${tab}${tab}if isinstance(value, $assoClass):
99${tab}${tab}${tab}self._$assoClassL = value
100#saSetter($type, $attribute.subTypes)
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( subTypes $subTypes)
115#foreach($type in $subTypes)
116${tab}${tab}self._${type.associatedClass.name.toLowerCase()}#initAttributeLeft($type)
117#end
118#end
119
120#macro( attributes $attributes, $parents)
121#if($parents)
122#foreach( $parent in $parents)
123${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass(${parent.associatedClass.name})}()         
124#end
125#end
126
127#foreach($attribute in $attributes)
128#if($attribute.subTypes)
129#subTypes($attribute.subTypes)
130#else
131${tab}${tab}self.${attribute.name}#initAttributeLeft($attribute)
132#end
133#end
134#end
135
136
137#importing($imports)
138
139'''
140Created on $date
141
142@author: mnagni
143'''
144
145class ${classModel.associatedClass.name}(#superclass($classModel.parents, $stereotype)):
146${tab}'''
147${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
148${tab}${tab}be persisted in a relational database.
149 
150${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
151${tab}'''
152#if($classModel.isEnumeration())
153#asEnumeration($classModel)
154#elseif ($classModel.isCodeList())
155#asCodeList($classModel)
156#elseif ($classModel.isUnion())
157#asUnion($classModel)
158#else
159${tab}'''
160${tab}${tab}Please note that any access to the inner attributes should be done using
161${tab}${tab}the given get/set methods and NOT accessing them directly.
162${tab}'''
163${tab}def __init__(self):
164#end
165#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
166#attributes($attributes, $classModel.parents)
167${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
168#dogettersetter($attributes)
169#end
Note: See TracBrowser for help on using the repository browser.