def GetDiagram(canvas, D, parent_block=None): ''' Build the DEVSimpy diagram with the creation of the block models (atomic and coupled) and ports (input and output) ''' if D != {}: for k, v in D.items(): if v.has_key('components'): ### coupled model name = k.attributes['name'].value temp = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp.write(WizardGUI.coupledCode('CoupledModel')) temp.seek(0) nbi, nbo = map(len, GetNbPort(k)) cp_block = Components.BlockFactory.CreateBlock( x=100, y=100, inputs=nbi, outputs=nbo, name=name, python_file=temp.name, canvas=canvas) cp_block.label = name parent_block.AddShape(cp_block) #print cp_block for d in v['components']: if isinstance(d, dict): GetDiagram(canvas, {d['node']: d}, cp_block) else: GetDiagram(canvas, {d: {}}, cp_block) return parent_block else: ### atomic model name = k.attributes['name'].value temp = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp.write(WizardGUI.atomicCode('AtomicModel')) temp.seek(0) nbi, nbo = map(len, GetNbPort(k)) am_block = Components.BlockFactory.CreateBlock( x=250, y=100 * (1 + parent_block.nbCodeBlock), inputs=nbi, outputs=nbo, name=name, python_file=temp.name, canvas=canvas) am_block.label = name #print am_block parent_block.AddShape(am_block) return True
def AddLayer(self, d, l): """ Add the diagram d at level l """ if l in self.layers: self.SetDiagramByLevel(d, l) else: ### add new diagram according the new layer self.layers[l] = d ### add new DAM and UAM according to new layer self.SetUAM(l, WizardGUI.atomicCode('UAM%d' % l)) self.SetDAM(l, WizardGUI.atomicCode('DAM%d' % l))
def AddLayer(self, d, l): """ Add the diagram d at level l """ if l in self.layers: self.SetDiagramByLevel(d, l) else: ### add new diagram according the new layer self.layers[l] = d ### add new DAM and UAM according to new layer self.SetUAM(l, WizardGUI.atomicCode('UAM%d'%l)) self.SetDAM(l, WizardGUI.atomicCode('DAM%d'%l))
def GetDiagram(canvas, D, parent_block=None): ''' Build the DEVSimpy diagram with the creation of the block models (atomic and coupled) and ports (input and output) ''' if D != {}: for k,v in D.items(): if v.has_key('components'): ### coupled model name = k.attributes['name'].value temp = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp.write(WizardGUI.coupledCode('CoupledModel')) temp.seek(0) nbi,nbo = map(len,GetNbPort(k)) cp_block = Components.BlockFactory.CreateBlock(x=100, y=100, inputs = nbi, outputs = nbo, name=name, python_file=temp.name, canvas=canvas) cp_block.label = name ### if True, the flag for bad python file is activated #cp_block.bad_filename_path_flag = True parent_block.AddShape(cp_block) #print cp_block for d in v['components']: if isinstance(d, dict): GetDiagram(canvas,{d['node']:d}, cp_block) else: GetDiagram(canvas,{d:{}}, cp_block) return parent_block else: ### atomic model name = k.attributes['name'].value temp = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp.write(WizardGUI.atomicCode('AtomicModel')) temp.seek(0) nbi,nbo = map(len,GetNbPort(k)) am_block = Components.BlockFactory.CreateBlock(x=250, y=100*(1+parent_block.nbCodeBlock), inputs = nbi, outputs = nbo, name=name, python_file=temp.name, canvas=canvas) am_block.label = name ### if True, the flag for bad python file is activated #am_block.bad_filename_path_flag = True #print am_block parent_block.AddShape(am_block) return True
def getDiagramFromXML(xml_file="", name="", canvas=None, D={}): """ """ import WizardGUI xmldoc = minidom.parse(xml_file) ### all item [2:] for id 0 and 1 itemlist = xmldoc.getElementsByTagName('mxCell')[2:] ### item corresponding to the block blocklist = [] ### item corresponding to the connection connectionlist = [] for s in itemlist: if s.attributes.has_key('source') and s.attributes.has_key('target'): connectionlist.append(s) else: blocklist.append(s) #mxGraphModel = xmldoc.getElementsByTagName('mxGraphModel')[0] #dx = int(mxGraphModel.attributes['dx'].value) #dy = int(mxGraphModel.attributes['dy'].value) ### parent of all block is canvas D['1'] = canvas ### make block (atomic or coupled model) while(blocklist!=[]): s = blocklist[0] name = s.attributes['value'].value #print name if s.attributes.has_key('style'): ### coupled model have swimlane style or *couple{d}* in value filed if s.attributes['style'].value == 'swimlane' or re.match('[a-zA-Z0-9_ ]*[c|C]oupl[ed|e|é][a-zA-Z0-9_ ]*',name, re.IGNORECASE): attr = s.getElementsByTagName('mxGeometry')[0].attributes temp = tempfile.NamedTemporaryFile(suffix='.py') temp.write(WizardGUI.coupledCode('CoupledModel')) temp.seek(0) block = Components.BlockFactory.CreateBlock(x=int(attr['x'].value), y=int(attr['y'].value), name=name, python_file=temp.name, canvas=canvas) block.label = name parent_id = s.attributes['parent'].value id = str(s.attributes['id'].value) if parent_id == '1': canvas.AddShape(block) D[id] = block del blocklist[0] #print block elif parent_id in D.keys(): canvas_parent = D[parent_id] canvas_parent.AddShape(block) D[id] = block del blocklist[0] #print block else: blocklist.insert(len(blocklist),blocklist.pop(0)) elif re.match('[a-zA-Z0-9_ ]*[a|A]tomi[c|que][a-zA-Z0-9_ ]*',name, re.IGNORECASE): attr = s.getElementsByTagName('mxGeometry')[0].attributes temp = tempfile.NamedTemporaryFile(suffix='.py') temp.write(WizardGUI.atomicCode('AtomicModel')) temp.seek(0) block = Components.BlockFactory.CreateBlock(x=int(attr['x'].value), y=int(attr['y'].value), name=name, python_file=temp.name, canvas=canvas) block.label = name parent_id = s.attributes['parent'].value id = str(s.attributes['id'].value) if parent_id == '1': canvas.AddShape(block) D[id] = block del blocklist[0] #print block elif parent_id in D.keys(): canvas_parent = D[parent_id] canvas_parent.AddShape(block) D[id] = block del blocklist[0] else: blocklist.insert(len(blocklist),blocklist.pop(0)) elif s.attributes.has_key('vertex'): if s.attributes['vertex'].value == '1' or re.match('[a-zA-Z0-9_ ]*[a|A]tomi[c|que][a-zA-Z0-9_ ]*',name, re.IGNORECASE): attr = s.getElementsByTagName('mxGeometry')[0].attributes temp = tempfile.NamedTemporaryFile(suffix='.py') temp.write(WizardGUI.atomicCode('AtomicModel')) temp.seek(0) block = Components.BlockFactory.CreateBlock(x=int(attr['x'].value), y=int(attr['y'].value), name=name, python_file=temp.name, canvas=canvas) block.label = name parent_id = s.attributes['parent'].value id = str(s.attributes['id'].value) if parent_id == '1': canvas.AddShape(block) D[id] = block del blocklist[0] #print block elif parent_id in D.keys(): canvas_parent = D[parent_id] canvas_parent.AddShape(block) D[id] = block del blocklist[0] else: blocklist.insert(len(blocklist),blocklist.pop(0)) else: sys.stdout.write(_('Element not considered!\n')) ### make connection while(connectionlist != []): s = connectionlist[0] source_id = s.attributes['target'].value target_id = s.attributes['source'].value parent_id = s.attributes['parent'].value #style = s.attributes['style'].value.split(';') source = D[source_id] target = D[target_id] c = D[parent_id] if source in canvas.diagram.shapes and target in canvas.diagram.shapes: print source.label, target.label a,b = canvas.GetNodeLists(source, target) if a == [] or b == []: a,b = canvas.GetNodeLists(target,source) canvas.sourceNodeList, canvas.targetNodeList = a,b #print canvas.sourceNodeList, canvas.targetNodeList if canvas.sourceNodeList != [] and canvas.targetNodeList !=[]: #print source.label, target.label #print canvas.sourceNodeList, canvas.targetNodeList canvas.makeConnectionShape(canvas.sourceNodeList[0], canvas.targetNodeList[0]) del connectionlist[0]