Changeset 8085
- Timestamp:
- 03/02/12 13:45:58 (9 years ago)
- Location:
- mauRepo/xmi2sqlalchemy/trunk/src
- Files:
-
- 4 added
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/NmParserHelper.java
r8017 r8085 32 32 package ndg.services.newmoon; 33 33 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.util.Arrays; 37 import java.util.HashSet; 34 38 import java.util.List; 39 import java.util.Set; 35 40 36 41 import ndg.services.newmoon.xmiModel.UMLAssociation; … … 40 45 import ndg.services.newmoon.xmiModel.UML_ID; 41 46 47 import org.apache.commons.io.IOUtils; 42 48 import org.apache.commons.lang.StringUtils; 43 49 import org.slf4j.Logger; … … 98 104 return node; 99 105 } 106 107 public static Set<String> getCSVFromInputStream(InputStream is) { 108 Set<String> ret = new HashSet<String>(); 109 try { 110 String isText = IOUtils.toString(is); 111 String[] ss = StringUtils.split(isText, ","); 112 ret = new HashSet<String>(Arrays.asList(StringUtils.stripAll(ss))); 113 } catch (IOException e) { 114 if (logger.isWarnEnabled()) { 115 logger.warn("Cannot read the Additional Clases file"); 116 } 117 } 118 return ret; 119 } 100 120 } -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/UmlToRDBM.java
r8084 r8085 35 35 36 36 import java.io.File; 37 import java.io.IOException; 37 38 import java.io.InputStream; 38 import java.util.Array List;39 import java.util.Arrays; 39 40 import java.util.HashMap; 40 41 import java.util.HashSet; … … 52 53 import ndg.services.newmoon.velocity.python.GenerateFromClassModel; 53 54 import ndg.services.newmoon.velocity.python.GenerateTables; 54 import ndg.services.newmoon.xmiModel.UMLClass;55 55 import ndg.services.newmoon.xmiModel.UMLModel; 56 56 57 import org.apache.commons.collections.CollectionUtils; 57 58 import org.apache.commons.io.FileUtils; 59 import org.apache.commons.io.IOUtils; 60 import org.apache.commons.lang.StringUtils; 58 61 import org.slf4j.Logger; 59 62 import org.slf4j.LoggerFactory; … … 82 85 UMLModel umlModel = myModel.get(); 83 86 84 85 87 File outDir = new File(baseDir, params.get(NewmoonManager.NM_PARAM.OUTPUT_DIR)); 86 88 outDir.mkdir(); … … 94 96 substitutesSuperAttributes(cms); 95 97 96 //Generates the python classes 98 //Generates the python classes 97 99 GenerateFromClassModel gen = new GenerateFromClassModel(new GenerateClasses(outDir, umlArchive)); 98 100 gen.execute(umlArchive.getClassModel()); 99 101 100 102 //Generates the sqlalchemy tables 101 GenerateTables gen2 = new GenerateTables(outDir, umlArchive );103 GenerateTables gen2 = new GenerateTables(outDir, umlArchive, NmParserHelper.getCSVFromInputStream(inputAddClass)); 102 104 gen2.execute(umlModel); 103 105 } catch (Exception e) { … … 109 111 } 110 112 } 111 112 113 113 114 private void substitutesSuperAttributes(List<ClassModel> cms) { -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/collect/AttributeModel.java
r8084 r8085 95 95 } 96 96 97 return upperBound > 1 97 return upperBound > 1 98 98 || collectionType.equals(CollectionType.SET) 99 99 || collectionType.equals(CollectionType.SEQUENCE); -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/GenerateTables.java
r8084 r8085 97 97 private final File baseDir; 98 98 private final UMLElementsArchive umlArchive; 99 private final Set<String> additionalClasses = new HashSet<String>(); 99 100 100 101 /** … … 102 103 */ 103 104 public GenerateTables(File baseDir, UMLElementsArchive umlArchive) { 105 this(baseDir, umlArchive, new HashSet<String>()); 106 } 107 108 public GenerateTables(File baseDir, UMLElementsArchive umlArchive, Set<String> additionalClasses) { 104 109 super(); 105 110 this.baseDir = baseDir; 106 111 this.umlArchive = umlArchive; 107 } 108 112 this.additionalClasses.addAll(additionalClasses); 113 } 114 109 115 public void execute(UMLModel umlModel) throws IOException { 110 116 // Extracts the ClassModels belonging only to the given UMLModel … … 114 120 // Adds some customer required ClassModel not directly binded to the 115 121 // given UMLModel 116 processOrphans(ccm); 122 CollectionUtils.forAllDo(additionalClasses, ccm); 123 117 124 118 125 // Generates the TableModels … … 121 128 // then writes the result out 122 129 writeAll(closure); 123 }124 125 private void processOrphans(CollectClassModel ccm) {126 Set<String> externalClasses = new HashSet<String>();127 externalClasses.add("MO_Organization");128 CollectionUtils.forAllDo(externalClasses, ccm);129 130 } 130 131 … … 178 179 Writer sqlTablesWriter = new FileWriter(new File(getHelper().getBaseDir(), "sqlTables.py"), true); 179 180 sqlTablesWriter 180 .append("from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence, event, MetaData\n"); 181 sqlTablesWriter.append("from sqlalchemy.orm import relationship, backref, mapper, clear_mappers\n"); 182 sqlTablesWriter.append("from sqlalchemy.ext.declarative import declarative_base\n"); 183 sqlTablesWriter.append("from sqlalchemy.dialects.postgresql import ARRAY, TEXT, NUMERIC, BOOLEAN, DATE\n"); 184 sqlTablesWriter.append("from sqlalchemy.types import DateTime\n"); 181 .append("from sqlalchemy import Table, Column, ForeignKey, Sequence\n"); 182 sqlTablesWriter.append("from sqlalchemy.orm import relationship, mapper\n"); 185 183 sqlTablesWriter.append("from sqlalchemy.orm.util import class_mapper\n"); 186 sqlTablesWriter.append("from sqlalchemy.schema import ForeignKeyConstraint\n\n"); 187 sqlTablesWriter.append("from ascore.astext import ASText\n\n"); 184 sqlTablesWriter.append("from sqlalchemy.dialects.postgresql import TEXT\n"); 188 185 189 186 sqlTablesWriter.flush(); -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/NmVelocity.java
r8060 r8085 88 88 89 89 public String fillTemplate(Context vcontext, String templateName) { 90 vcontext.put("tab", " "); //four spaces instead of a TAB as required by python standard90 vcontext.put("tab", " "); //four spaces instead of a TAB 91 91 vcontext.put("nl", "\n"); 92 92 vcontext.put("vh", NmVelocityHelper.class); -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/NmVelocityHelper.java
r8084 r8085 83 83 return StringUtils.containsNone(text, "[]<>.,*/|\\@:;+-#!%^£$()"); 84 84 } 85 86 public static boolean validateClass(ClassModel classModel) { 87 for (ClassModel cm : classModel.getParents()) { 88 if (!NmVelocityHelper.isValidPythonName(cm.getAssociatedClass().getName())) { 89 return false; 90 } 91 } 92 return NmVelocityHelper.isValidPythonName(classModel.getAssociatedClass().getName()); 93 } 85 94 86 95 /** … … 275 284 } 276 285 277 //Actually I cannot manage type like Sequ ance<Vector> and for this reason286 //Actually I cannot manage type like Sequence<Vector> and for this reason 278 287 // I set them by default to ARRAY<TEXT> 288 if (clazz.equalsIgnoreCase(substituteDBType("Vector"))){ 289 return "ARRAY(" + substituteDBType("CharacterString") + ")"; 290 } 291 279 292 if (attribute.hasMultiplicity()) { 280 if (clazz.equalsIgnoreCase(substituteDBType("Vector"))) {281 return "ARRAY(" + substituteDBType("CharacterString") + ")";282 } else {283 293 return "ARRAY(" + clazz + ")"; 284 } 285 } 294 } 286 295 return clazz; 287 296 } -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/PythonClassModel.java
r8084 r8085 69 69 || this.getAssociatedClass().getClass().equals(ASCodeList.class) 70 70 || this.getAssociatedClass().getClass().equals(ASUnion.class) 71 || !validateClass(); 72 } 73 74 private boolean validateClass() { 75 for (ClassModel cm : getParents()) { 76 if (!NmVelocityHelper.isValidPythonName(cm.getAssociatedClass().getName())) { 77 return false; 78 } 79 } 80 return NmVelocityHelper.isValidPythonName(this.getAssociatedClass().getName()); 71 || !NmVelocityHelper.validateClass(this); 81 72 } 82 73 -
mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm
r8084 r8085 89 89 ${tab}def ${attr.name}(self, value): 90 90 #if($attr.getParentName()) 91 ${tab}${tab}${tab} 91 ${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value 92 92 #elseif($attr.subTypes) 93 93 ${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__) … … 109 109 110 110 #setter($attribute) 111 #end 112 #end 113 114 #macro ( enlistKeys $attributes) 115 #foreach($attribute in $attributes) 116 self.${attribute.name}#if($foreach.hasNext), #end 111 117 #end 112 118 #end … … 167 173 ${tab}${tab}super(${classModel.associatedClass.name}, self).__init__() 168 174 #dogettersetter($attributes) 175 176 ${tab}def __key(self): 177 ${tab}${tab}return (#enlistKeys($attributes)) 178 179 ${tab}def __eq__(x, y): 180 ${tab}${tab}''' 181 ${tab}${tab}Cannot compare classes which do not define a "__key" attribute 182 ${tab}${tab}''' 183 ${tab}${tab}if hasattr(x, '_%s__key' % (type(x).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)): 184 ${tab}${tab}${tab}return x.__key() == y.__key() 185 ${tab}${tab}return id(x) == id(y) 186 187 ${tab}def __hash__(self): 188 ${tab}${tab}return hash(self.__key()) 169 189 #end
Note: See TracChangeset
for help on using the changeset viewer.