# __ File: GGruleEdit.py ______________________________________________________________ # Implements : class GGruleEdit # Author : Juan de Lara # Description: Window with some widgets to edit a Graph Grammar rule # Generated automatically with ATOM3, added some code by hand. # Modified : # - 21 Oct 2001. Header added # ___________________________________________________________________________________________ from ASGNode import * from ATOM3Type import * from ATOM3String import * from ATOM3Integer import * from ATOM3String import * from ATOM3String import * from ATOM3Constraint import * from ATOM3Constraint import * from graph_GGruleEdit import * class GGruleEdit(ASGNode, ATOM3Type): def __init__(self, parent, ATOM3instance ): ASGNode.__init__(self) ATOM3Type.__init__(self) self.ATOM3instance = ATOM3instance self.parent = parent self.Name=ATOM3String('rule') self.Order=ATOM3Integer(1) # get the Meta-Model that is being used currently by the ATOM3 instance currMetaModelName = ATOM3instance.ASGroot.getClass() print "epp: currMetaModelName = "+repr(currMetaModelName) exec "from "+currMetaModelName+" import *\n" currMetaModel = eval(currMetaModelName) self.LHS=currMetaModel(ATOM3instance, None) self.RHS=currMetaModel(ATOM3instance, None) self.Condition=ATOM3Constraint() self.Condition.setValue(('condition', (['Python', 'OCL'], 1), (['PREcondition', 'POSTcondition'], 1), (['EDIT', 'SAVE', 'CREATE', 'CONNECT', 'DELETE', 'DISCONNECT', 'TRANSFORM', 'SELECT', 'DRAG', 'DROP', 'MOVE OBJECT'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 'return 1\n')) self.Action=ATOM3Constraint() self.Action.setValue(('action', (['Python', 'OCL'], 1), (['PREcondition', 'POSTcondition'], 1), (['EDIT', 'SAVE', 'CREATE', 'CONNECT', 'DELETE', 'DISCONNECT', 'TRANSFORM', 'SELECT', 'DRAG', 'DROP', 'MOVE OBJECT'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), None)) self.generatedAttributes = {'Name': ('ATOM3String', ), 'Order': ('ATOM3Integer', ), 'LHS': ('ATOM3String', ), 'RHS': ('ATOM3String', ), 'Condition': ('ATOM3Constraint', ), 'Action': ('ATOM3Constraint', ) } def setGGEditLHS(self, AT3Dialog, ATOM3instance, semanticObject): ATOM3instance.editGGLabel = self.INLHS ATOM3instance.reDrawGGLabels() def setGGEditRHS(self, AT3Dialog, ATOM3instance, semanticObject): ATOM3instance.editGGLabel = self.INRHS ATOM3instance.reDrawGGLabels() ATOM3instance.addCopyFromLHSButton(self) def show(self, parent, parentWindowInfo): ATOM3Type.show(self, parent, parentWindowInfo) self.containerFrame = Frame(parent) Label(self.containerFrame, text='Name').grid(row=0,column=0,sticky=W) self.Name.show(self.containerFrame, parentWindowInfo).grid(row=0,column=1,sticky=W) Label(self.containerFrame, text='Order').grid(row=1,column=0,sticky=W) self.Order.show(self.containerFrame, parentWindowInfo).grid(row=1,column=1,sticky=W) Label(self.containerFrame, text='LHS').grid(row=2,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self: x.showLeftHandSide()).grid(row=2,column=1,sticky=W) Label(self.containerFrame, text='RHS').grid(row=3,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self: x.showRightHandSide()).grid(row=3,column=1,sticky=W) Label(self.containerFrame, text='Condition').grid(row=4,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self : ATOM3TypeDialog(x.containerFrame, x.Condition)).grid(row=4,column=1,sticky=W) Label(self.containerFrame, text='Action').grid(row=5,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self : ATOM3TypeDialog(x.containerFrame, x.Action)).grid(row=5,column=1,sticky=W) return self.containerFrame def showLeftHandSide (self): dial = ATOM3TypeDialog(self.containerFrame, self.LHS, ATOM3TypeDialog.OPEN, (None, self.setGGEditLHS)) if dial.result_ok: self.LHS = dial.myWidget.ASGroot def showRightHandSide (self): dial = ATOM3TypeDialog(self.containerFrame, self.RHS, ATOM3TypeDialog.OPEN, (None, self.setGGEditRHS)) if dial.result_ok: self.RHS = dial.myWidget.ASGroot def toString(self, maxWide = None, maxLines = None ): #rs = self.Name.toString()+' '+self.Order.toString()+' '+self.LHS.toString()+' '+self.RHS.toString()+' '+self.Condition.toString()+' '+self.Action.toString() rs = self.Name.toString()+' '+self.Order.toString() if maxWide: return self.strValue[0:maxWide-3]+'...' else: return rs def getValue(self): return (self.Name.getValue(),self.Order.getValue(),self.LHS.getValue(),self.RHS.getValue(),self.Condition.getValue(),self.Action.getValue(),) def setValue(self, value): self.Name.setValue(value[0]) self.Order.setValue(value[1]) self.LHS.setValue(value[2]) self.RHS.setValue(value[3]) self.Condition.setValue(value[4]) self.Action.setValue(value[5]) def writeConstructor2File(self, file, indent, objName="at", depth = 0, generatingCode = 0): "Method that writes into a file the constructor and the value of the object. Must be overriden in children" file.write(indent+objName+'= GGruleEdit(None, self)\n') self.Name.writeConstructor2File(file, indent, objName+'.Name', depth, generatingCode) self.Order.writeConstructor2File(file, indent, objName+'.Order', depth, generatingCode) # import necessary node classes... classes2Import = [] # create a list with the necessary classes to import... for c2i in self.LHS.listNodes.keys(): # add node names to the list... if not c2i in classes2Import: classes2Import.append(c2i) for c2i in self.RHS.listNodes.keys(): # add node names to the list... if not c2i in classes2Import: classes2Import.append(c2i) file.write("\n") for c2i in classes2Import: file.write(indent+"from "+c2i+" import *\n") file.write("\n") # call constructor for LHS graph appropriately! file.write(indent+objName+".LHS = "+self.LHS.getClass()+"(self)\n") # ey! we can have nested graphs!, generate the appropriate code for nesting... for merged_asg in self.LHS.mergedASG: file.write(indent+objName+".LHS.merge("+merged_asg.getClass()+"(self))\n") if generatingCode: self.LHS.writeGraph2File(file, 1, 0, objName+".LHS", indent, 0, '', ASGNode.INLHS, "parent") else: self.LHS.writeGraph2File(file, 1, 0, objName+".LHS", indent, 0, '', ASGNode.INLHS, "self") # call constructor for RHS graph appropriately! file.write(indent+objName+".RHS = "+self.RHS.getClass()+"(self)\n") # ey! we can have nested graphs!, generate the appropriate code for nesting... for merged_asg in self.RHS.mergedASG: file.write(indent+objName+".RHS.merge("+merged_asg.getClass()+"(self))\n") if generatingCode: self.RHS.writeGraph2File(file, 1, 0, objName+".RHS", indent, 0, '', ASGNode.INRHS, "parent") else: self.RHS.writeGraph2File(file, 1, 0, objName+".RHS", indent, 0, '', ASGNode.INRHS, "self") self.Condition.writeConstructor2File(file, indent, objName+'.Condition', depth, generatingCode) self.Action.writeConstructor2File(file, indent, objName+'.Action', depth, generatingCode) def writeValue2File(self, file, indent, objName="at", depth = 0, generatingCode = 0): "Method that writes into a file the the value of the object. Must be overriden in children" self.Name.writeValue2File(file, indent, objName+'.Name', depth, generatingCode) self.Order.writeValue2File(file, indent, objName+'.Order', depth, generatingCode) self.LHS.writeValue2File(file, indent, objName+'.LHS', depth, generatingCode) self.RHS.writeValue2File(file, indent, objName+'.RHS', depth, generatingCode) self.Condition.writeValue2File(file, indent, objName+'.Condition', depth, generatingCode) self.Action.writeValue2File(file, indent, objName+'.Action', depth, generatingCode) def clone(self): cloneObject = GGruleEdit( self.parent, self.ATOM3instance ) cloneObject.Name = self.Name.clone() cloneObject.Order = self.Order.clone() cloneObject.LHS = self.LHS.clone() cloneObject.RHS = self.RHS.clone() cloneObject.Condition = self.Condition.clone() cloneObject.Action = self.Action.clone() ASGNode.cloneActions(self, cloneObject) return cloneObject def copy(self, other): ATOM3Type.copy(self, other) self.Name = other.Name self.Order = other.Order self.LHS = other.LHS self.RHS = other.RHS self.Condition = other.Condition self.Action = other.Action ASGNode.copy(self, other) def destroy(self): self.Name.destroy() self.Order.destroy() #self.LHS.destroy() #self.RHS.destroy() self.Condition.destroy() self.Action.destroy() self.containerFrame = None def cardinalityCheck(self, selfPosition): return None def checkConnectedObjectType(self, selfPosition): if selfPosition == 'SOURCE': last=self.out_connections_[len(self.out_connections_)-1] else: last=self.in_connections_[len(self.in_connections_)-1] return None def preCondition (self, actionID, * params): if self.graphObject_: return self.graphObject_.preCondition(actionID, params) else: return None def postCondition (self, actionID, * params): if actionID == self.CONNECT: res = self.checkConnectedObjectType(params[0]) if res: return res if self.graphObject_: return self.graphObject_.preCondition(actionID, params) else: return None def genCode(self): "Generates code for this class" fileName = self.Name.toString()+".py" # compose the file name if self.ATOM3instance.console: self.ATOM3instance.console.appendText('Generating file: '+fileName+' (file for rule).') file = open(self.ATOM3instance.codeGenDir+'/'+fileName, "w+t") # opens the file file.write("# _ "+fileName+" ____________________________________________________________________________\n") file.write("# File generated automatically by ATOM3. Graph Grammar Rule\n") file.write("# ___________________________________________________________________________________________\n") file.write("from GGrule import *\n") # import the necessary files for LHS and RHS graphs importedFiles = [] # create a list, in order to not repeat file names! for importFile in self.LHS.listNodes.keys(): # import all node types of LHS if not importFile in importedFiles: file.write("from "+importFile+" import *\n") importedFiles.append(importFile) for importFile in self.RHS.listNodes.keys(): # # import all node types of RHS if not importFile in importedFiles: file.write("from "+importFile+" import *\n") importedFiles.append(importFile) file.write("class "+self.Name.toString()+" (GGrule):\n\n") file.write(" def __init__(self, parent):\n") file.write(" GGrule.__init__(self, "+self.Order.toString()+")\n") # write LHS and RHS graphs... # 1st call appropriate constructor for LHS... file.write(" self.LHS = "+self.LHS.getClass()+"(parent)\n") # ey! check if we have to merge the graph with some other... for merged_asg in self.LHS.mergedASG: file.write(" self.LHS.merge("+merged_asg.getClass()+"(parent))\n") self.LHS.writeGraph2File( file, 1, 0, "self.LHS", " ", 0, '', ASGNode.INLHS, "parent") # 1st call appropriate constructor for RHS... file.write(" self.RHS = "+self.RHS.getClass()+"(parent)\n") for merged_asg in self.RHS.mergedASG: file.write(" self.RHS.merge("+merged_asg.getClass()+"(parent))\n") self.RHS.writeGraph2File( file, 1, 0, "self.RHS", " ", 0, '', ASGNode.INRHS, "parent") file.write(" def condition(self, graphID, isograph, atom3i):\n") name, language, type, event, code = self.Condition.getValue() if code != None: # if there is some code in the method... file.write(" "+string.replace( code, '\n', '\n ')+"\n\n") else: file.write(" pass\n\n") file.write(" def action(self, graphID, isograph, atom3i):\n") name, language, type, event, code = self.Action.getValue() if code != None: # if there is some code in the method... file.write(" "+string.replace( code, '\n', '\n ')+"\n\n") else: file.write(" pass\n\n") file.close()