Ignore:
Timestamp:
17/04/12 16:47:01 (8 years ago)
Author:
mnagni
Message:

Creates cleaner python classes and more able to manage lists

Location:
mauRepo/xmi2sqlalchemy/trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/GenerateTables.java

    r8146 r8243  
    182182                Writer sqlTablesWriter = new FileWriter(new File(getHelper().getBaseDir(), "sqlTables.py"), true); 
    183183                sqlTablesWriter 
    184                                 .append("from sqlalchemy import Table, Column, ForeignKey, Sequence\n"); 
     184                                .append("from sqlalchemy import Table, Column, ForeignKey, Sequence, event\n"); 
    185185                sqlTablesWriter.append("from sqlalchemy.orm import relationship, mapper\n");                     
    186186                sqlTablesWriter.append("from sqlalchemy.orm.util import class_mapper\n"); 
    187187                sqlTablesWriter.append("from sqlalchemy.dialects.postgresql import TEXT\n");                     
     188                sqlTablesWriter.append("import inspect\n");              
    188189 
    189190                sqlTablesWriter.flush(); 
     
    199200                sqlTablesWriter.append("    return connection.execute(seq)\n"); 
    200201                sqlTablesWriter.append("\n\n"); 
     202                sqlTablesWriter.append("def my_after_attach(session, instance):\n"); 
     203                sqlTablesWriter.append("    for item in inspect.getmembers(instance):\n");               
     204                sqlTablesWriter.append("        if item[0] == 'synchronize':\n"); 
     205                sqlTablesWriter.append("                instance.synchronize()\n");              
     206                sqlTablesWriter.append("\n\n"); 
     207                sqlTablesWriter.append("def attachEvents(session):\n"); 
     208                sqlTablesWriter.append("    event.listen(session, 'after_attach', my_after_attach)\n"); 
     209                sqlTablesWriter.append("\n\n");          
    201210                sqlTablesWriter.append("def doTables(metadata):\n");             
    202211                sqlTablesWriter.append(sqlTablesStrBuffer.toString()); 
  • mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm

    r8156 r8243  
    22## pythonClass.vm 
    33## The form expects the following context attributes 
    4 ## classModel - an instance of ClassModel class 
     4## classModel - an instance of PythonClassModel class 
    55## imports    - a Map<String, Set<String>> defining the modules to import 
    6 ## stereotype - an instance of UMLClass 
     6## stereotype - an instance of UMLClass representing the classModel 
     7## attributes - a Set<PythonAttributeModel> 
    78########################################################################## 
    89 
     
    5859#elseif($attr.subTypes) 
    5960#if($attr.hasMultiplicity()) 
    60 ${tab}${tab}ret = [] 
     61${tab}${tab}try: 
     62${tab}${tab}${tab}ret = [] 
    6163#foreach($type in $attr.subTypes) 
    6264#set ( $assoClass = "$type.associatedClass.name.toLowerCase()") 
    63 ${tab}${tab}for item in self._${attribute.name}_$assoClass: 
    64 ${tab}${tab}${tab}ret.append(item) 
    65 #end 
    66 ${tab}${tab}return ret 
     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} 
    6776#else 
    6877#foreach($type in $attr.subTypes) 
     
    91100 
    92101#macro (setter $attr) 
    93 #set ( $methodName = "$display.capitalize( $attr.name )") 
     102#if($attr.getParentName()) 
    94103${tab}@${attr.name}.setter 
    95104${tab}def ${attr.name}(self, value):     
    96 #if($attr.getParentName()) 
    97105${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value 
    98 #elseif($attr.subTypes) 
    99 #if($attr.hasMultiplicity()) 
    100 ${tab}${tab}if isinstance(value, list): 
    101 #foreach($type in $attr.subTypes) 
    102 #set ( $assoClass = "$type.associatedClass.name") 
    103 #set ( $assoClassL = "$type.associatedClass.name.toLowerCase()") 
    104 ${tab}${tab}${tab}self._${attr.name}_${assoClassL}[:] 
    105 #end 
    106 ${tab}${tab}${tab}for item in value: 
    107 ${tab}${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__) 
    108 #foreach($type in $attr.subTypes) 
    109 #set ( $assoClass = "$type.associatedClass.name") 
    110 #set ( $assoClassL = "$type.associatedClass.name.toLowerCase()") 
    111 ${tab}${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}": 
    112 ${tab}${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item) 
    113 ${tab}${tab}${tab}${tab}${tab}continue 
    114 #end 
    115 #else 
     106#elseif($attr.subTypes && !$attr.hasMultiplicity()) 
     107${tab}@${attr.name}.setter 
     108${tab}def ${attr.name}(self, value):     
    116109${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__) 
    117110#foreach($type in $attr.subTypes) 
     
    120113${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}": 
    121114${tab}${tab}${tab}self._${attr.name}_${assoClassL} = value 
     115## Reinitializes all the other subTypes 
    122116#saSetter($type, $attr)  
     117#end 
    123118#end     
    124119#end 
    125 #end 
    126 #end 
    127  
    128  
     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 
    129151 
    130152 
     
    132154#foreach($attribute in $attributes) 
    133155#if($attribute.getParentName() || $attribute.subTypes) 
    134 ${vh.showAttribute($attribute)} 
    135156#getter($attribute) 
    136157 
     
    147168 
    148169#macro( subTypes $attribute) 
     170## Generates the hidden objects attributes used by the RDBM 
     171#if($attr.getParentName()) 
     172#else 
    149173#foreach($type in $attribute.subTypes) 
    150174${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute) 
    151175#end 
     176#end 
     177## Generates the hidden dummy object oriented instance of the attribute 
     178${tab}${tab}self._${attribute.name} =#initAttributeLeft($attribute) 
    152179#end 
    153180 
     
    160187 
    161188#foreach($attribute in $attributes) 
    162 #if($attribute.subTypes) 
     189#if($attribute.subTypes && !$attribute.getParentName()) 
    163190#subTypes($attribute) 
    164191#else 
     192#if(($attribute.getParentName() && !$attribute.hasMultiplicity()) || !$attribute.getParentName()) 
    165193${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute) 
     194#end 
    166195#end 
    167196#end 
     
    202231${tab}${tab}super(${classModel.associatedClass.name}, self).__init__() 
    203232#dogettersetter($attributes) 
     233 
     234#synchronize($attributes, $classModel.parents) 
    204235 
    205236${tab}def __key(self): 
  • mauRepo/xmi2sqlalchemy/trunk/src/main/resources/xmiMaps.properties

    r8017 r8243  
    5252GeoSciML=https://www.seegrid.csiro.au/subversion/GeoSciML/model/geosciml.xml 
    5353 
    54 UpComingISO=http://proj.badc.rl.ac.uk/svn/moles/trunk/ConceptualModel/UpComingISO.xml 
    55 MOLES3.4=http://proj.badc.rl.ac.uk/svn/moles/trunk/ConceptualModel/MOLES3_4.xml 
     54#UpComingISO=http://proj.badc.rl.ac.uk/svn/moles/trunk/ConceptualModel/UpComingISO.xml 
     55#MOLES3.4=http://proj.badc.rl.ac.uk/svn/moles/trunk/ConceptualModel/MOLES3_4.xml 
     56UpComingISO=http://proj.badc.rl.ac.uk/svn/moles/branches/v194/ConceptualModel/UpComingISO.xml 
     57MOLES3.4=http://proj.badc.rl.ac.uk/svn/moles/branches/v194/ConceptualModel/MOLES3_4.xml 
     58 
  • mauRepo/xmi2sqlalchemy/trunk/src/main/resources/xmiToSQLMap.properties

    r8048 r8243  
    22CharacterString=sqlalchemy.types.Text 
    33Character=sqlalchemy.types.String 
     4URL=sqlalchemy.types.String 
    45Integer=sqlalchemy.types.Integer 
    56Boolean=sqlalchemy.types.BOOLEAN 
     
    78Vector=sqlalchemy.dialects.postgresql.ARRAY 
    89Number=sqlalchemy.types.NUMERIC 
     10Decimal=sqlalchemy.types.NUMERIC 
    911 
    1012#Date=DATE 
  • mauRepo/xmi2sqlalchemy/trunk/src/test/java/ngn/services/newmoon/DOMParser.java

    r8165 r8243  
    4949                 
    5050         
    51         //@Test 
     51        @Test 
    5252        public final void testParseXmlFile() { 
    5353                //InputStream inputXML = NmParser.class.getClassLoader().getResourceAsStream("CEDA_Model/CEDA_ModelXMI.xml"); 
  • mauRepo/xmi2sqlalchemy/trunk/src/test/java/ngn/services/newmoon/GMLValidatorTest.java

    r8165 r8243  
    1515                 
    1616         
    17         @Test 
     17        //@Test 
    1818        public final void testParseXmlFile() { 
    1919                //InputStream inputXML = NmParser.class.getClassLoader().getResourceAsStream("CEDA_Model/CEDA_ModelXMI.xml"); 
  • mauRepo/xmi2sqlalchemy/trunk/src/test/resources/CEDA_Model/constraints.txt

    r8146 r8243  
    33MO_Observation:result:CEDA_Result 
    44MO_Observation:inSupportOf:CEDA_Project 
     5MO_Project:subProject:CEDA_Project 
    56OM_Observation:procedure:CEDA_Process 
     7OM_Observation:phenomenonTime:TM_GeometricPrimitive 
Note: See TracChangeset for help on using the changeset viewer.