def setup(self, configDictionary=None, configFile=None): if configDictionary is not None: self.configurations = configDictionary if 'host' and 'port' not in configDictionary.keys(): raise e.SimpleException( "configDictionary needs to contain host and port") elif configFile is not None: if os.path.exists(configFile): with open(configFile) as cfile: self.configurations = json.load(cfile) if 'host' and 'port' not in self.configurations.keys(): raise e.SimpleException( "configDictionary needs to contain host and port") else: self.configurations = None try: if self.configurations is None: self.client = MongoClient() else: if self.configurations['port'] is not int: # NITIN : TODO : Handle error and continue to parse it anyway #raise e.SimpleException() self.configurations['port'] = int( self.configurations['port']) self.client = MongoClient(self.configurations['host'], self.configurations['port']) except Exception as ex: print str(ex) raise e.SimpleException( 'problem connecting to the db instance, try firing again!')
def defineRelation(self, _id,_start, _end,_RelationType = "Association", _startUpperVaule="unknown",_endUpperValue="unknown"): if _id in self.Relations : return # NITIN : TODO : handle exceptions without stopping execution of program if not self.ElementDirectory.has_key(_start) : raise e.SimpleException("Starting Element of relation not defined, startId : " + _start) if not self.ElementDirectory.has_key(_end) : raise e.SimpleException("Ending Element of relation not defined, startId : " + _end) if _RelationType == "Association": relation = dt.Association(_id,_start, _end,_RelationType,_startUpperVaule,_endUpperValue) elif _RelationType == "Aggregation": relation = dt.Aggregation(_id,_start, _end,_RelationType,_startUpperVaule,_endUpperValue) #ZHIYUN: add end to start class as an attribute for aggregation relation elemName = str(self.ElementDirectory[_start].ElementName) elemAttributeTypeSetter = dt.SimpleType("string") elemAttributeName=str(self.ElementDirectory[_end].ElementName)+"_id" self.defineSimpleAttribute(elemName, elemAttributeName, elemAttributeTypeSetter) elif _RelationType == "Generalization": relation = dt.Generalization(_id,_start, _end, _RelationType) elif _RelationType == "Composition": relation = dt.Composition(_id,_start, _end,_RelationType,_startUpperVaule,_endUpperValue) else: raise e.SimpleException("Type of relation not defined : " + RelationType) self.Relations.add(_id) self.ElementDirectory[_start].relationsFromThisElement.append(relation) self.ElementDirectory[_end].relationsToThisElement.append(relation)
def DM_File_Analyze(self,PROJECT_DIR, config, filename): if not os.path.exists(PROJECT_DIR): raise e.SimpleException("Project Home Directory not created, run project initialize first.") DM_File_type = config['DM_Input_type'] if DM_File_type == "Simple_XML": retObj = self.SimpleXMLUtil(PROJECT_DIR, filename) if retObj is None: raise e.SimpleException("xml file not provided in the directory, check if file with extension .xml is uploaded") return retObj
def defineComplexAttribute(self, _ElementName , _AttributeName , _AttributeElementName , _AttributeType): # ZHIYUN: format element name _ElementName=_ElementName.lower().replace(" ","_") _AttributeElementName=_AttributeElementName.lower().replace(" ","_") try: assert self.isElementDeclared(_ElementName) except: raise e.SimpleException("No such element declared, check for declaration of element :" + _ElementName) if not self.isElementDeclared(_AttributeElementName) : raise e.SimpleException("Attempt to create attribute of type " + _AttributeElementType + " which is not declared .") id = self.ElementReference[_ElementName] self.ElementDirectory[id].addComplexAttribute(_AttributeName, _AttributeElementName, _AttributeType)
def defineOperation(self , _ElementName, _OperationName, _ReturnValue=[] , _ParameterValue=[]): _ElementName=_ElementName.lower().replace(" ","_") try: assert self.isElementDeclared(_ElementName) except: raise e.SimpleException("No such element declared, check for declaration of element :" + _ElementName) id = self.ElementReference[_ElementName] self.ElementDirectory[id].addOperation(_OperationName, _ReturnValue, _ParameterValue)
def defineSimpleAttribute(self , _ElementName , _AttributeName , _AttributeType): # ZHIYUN: format element name _ElementName=_ElementName.lower().replace(" ","_") try: assert self.isElementDeclared(_ElementName) except: raise e.SimpleException("No such element declared, check for declaration of element :" + _ElementName) id = self.ElementReference[_ElementName] self.ElementDirectory[id].addSimpleAttribute(_AttributeName, _AttributeType)
def SimpleXMLUtil(self,PROJECT_DIR, _dmoName): # NITIN : TODO : check how to handle namepspaces dynamically later namespaces = {"xmi_namespace": "http://schema.omg.org/spec/XMI/2.1"} myDMO = ET.parse(PROJECT_DIR + "/" + _dmoName +".xml") root = myDMO.getroot() documentation=root.find('xmi_namespace:Documentation', namespaces) exporter=documentation.get('exporter') # ZHIYUN: call corresponding parser for xml file if exporter=="Enterprise Architect": return self.EA_XMLUtil(root,namespaces,PROJECT_DIR,_dmoName) elif exporter=="Visual Paradigm": return self.VP_XMLUtil(root,namespaces,PROJECT_DIR,_dmoName) else: raise e.SimpleException("parser for your xml exporter is not provided")
def addComplexAttribute(self, _AttributeName , _AttributeElementName , _AttributeType): if not isinstance(_AttributeType, dt.ComplexType) : raise e.SimpleException("Trying to add a non ComplexType attribute in the function addComplexAttribute .") self.ComplexAttributes[_AttributeName] = (_AttributeElementName, _AttributeType)
def addSimpleAttribute(self, _AttributeName , _AttributeType): if not isinstance(_AttributeType,dt.SimpleType) : raise e.SimpleException("Trying to add a non SimpleType attribute in the function addSimpleAttribute .") self.SimpleAttributes[_AttributeName] = _AttributeType
def extendElement(self, _ElementName, _ExtensionType): _ElementName=_ElementName.lower().replace(" ","_") if not isinstance(_ExtensionType, dt.ExtensionType): raise e.SimpleException("_AttributeType has to be ExtensionType.") id = self.ElementReference[_ElementName] self.ElementReference[id].extendElement(_ExtensionType)