# __ File: GraphGrammarEdit.py ______________________________________________________________ # Implements : class GraphGrammarEdit # Author : Juan de Lara # Description: Window with some widgets to edit a Graph Grammar # Initially generated with ATOM3, added some widgets by hand. # Modified : # - 21 Oct 2001. Header added # ___________________________________________________________________________________________ from ASGNode import * from ATOM3Type import * from ATOM3String import * from ATOM3List import * from ATOM3Constraint import * from ATOM3Constraint import * from GGruleEdit import * import string class GraphGrammarEdit(ASGNode, ATOM3Type): def __init__(self, parent, ATOM3instance): ASGNode.__init__(self) ATOM3Type.__init__(self) self.ATOM3instance = ATOM3instance self.parent = parent self.Name=ATOM3String('') self.Rules=ATOM3List([ 1, 1, 1, 0], GGruleEdit, None, ATOM3instance ) lcobj0=[] self.Rules.setValue(lcobj0) self.InitialAction=ATOM3Constraint() self.InitialAction.setValue(('constraint', (['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.FinalAction=ATOM3Constraint() self.FinalAction.setValue(('const', (['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', ), 'Rules': ('ATOM3List', ), 'InitialAction': ('ATOM3Constraint', ), 'FinalAction': ('ATOM3Constraint', ) } 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='Rules').grid(row=1,column=0,sticky=W) self.Rules.show(self.containerFrame, parentWindowInfo).grid(row=1,column=1,sticky=W) Label(self.containerFrame, text='InitialAction').grid(row=2,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self : ATOM3TypeDialog(x.containerFrame, x.InitialAction)).grid(row=2,column=1,sticky=W) Label(self.containerFrame, text='FinalAction').grid(row=3,column=0,sticky=W) Button( self.containerFrame, text = 'edit', command = lambda x=self : ATOM3TypeDialog(x.containerFrame, x.FinalAction)).grid(row=3,column=1,sticky=W) return self.containerFrame def toString(self, maxWide = None, maxLines = None ): rs = self.Name.toString()+' '+self.Rules.toString()+' '+self.InitialAction.toString()+' '+self.FinalAction.toString() if maxWide: return self.strValue[0:maxWide-3]+'...' else: return rs def getValue(self): return (self.Name.getValue(),self.Rules.getValue(),self.InitialAction.getValue(),self.FinalAction.getValue(),) def setValue(self, value): self.Name.setValue(value[0]) self.Rules.setValue(value[1]) self.InitialAction.setValue(value[2]) self.FinalAction.setValue(value[3]) 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+'= GraphGrammarEdit(None, self)\n') self.Name.writeConstructor2File(file, indent, objName+'.Name', depth, generatingCode) self.Rules.writeConstructor2File(file, indent, objName+'.Rules', depth, generatingCode) self.InitialAction.writeConstructor2File(file, indent, objName+'.InitialAction', depth, generatingCode) self.FinalAction.writeConstructor2File(file, indent, objName+'.FinalAction', 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.Rules.writeValue2File(file, indent, objName+'.Rules', depth, generatingCode) self.InitialAction.writeValue2File(file, indent, objName+'.InitialAction', depth, generatingCode) self.FinalAction.writeValue2File(file, indent, objName+'.FinalAction', depth, generatingCode) def clone(self): cloneObject = GraphGrammarEdit( self.parent, self.ATOM3instance ) cloneObject.Name = self.Name.clone() cloneObject.Rules = self.Rules.clone() cloneObject.InitialAction = self.InitialAction.clone() cloneObject.FinalAction = self.FinalAction.clone() ASGNode.cloneActions(self, cloneObject) return cloneObject def copy(self, other): ATOM3Type.copy(self, other) self.Name = other.Name self.Rules = other.Rules self.InitialAction = other.InitialAction self.FinalAction = other.FinalAction ASGNode.copy(self, other) def destroy(self): self.Name.destroy() self.Rules.destroy() self.InitialAction.destroy() self.FinalAction.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" # 1st. generate code for each rule rules = self.Rules.getValue() # Get the vector of rules... for rule in rules: # for each rule... rule.genCode() # call the code generating method... # now Generate the file with the generated class... fileName = self.Name.toString()+".py" # compose file name if self.ATOM3instance.console: self.ATOM3instance.console.appendText('Generating file: '+fileName+' (main file for transformation).') file = open(self.ATOM3instance.codeGenDir+'/'+fileName, "w+t") # opens the file file.write("# _ "+fileName+" ____________________________________________________________________________\n") file.write("# "+self.Name.toString()+" : a class that subclasifies GraphGrammar. File generated automatically by ATOM3.\n") file.write("# ___________________________________________________________________________________________\n") file.write("from GraphGrammar import *\n") # for each rule in the graph grammar, import its file for rule in rules: file.write("from "+rule.Name.toString()+" import *\n") file.write("class "+self.Name.toString()+" (GraphGrammar):\n") file.write(" def __init__ (self, parent):\n") file.write(" GraphGrammar.__init__(self, [") # add an object of each rule to the list counter = 0 for rule in rules: if counter > 0: file.write(" , ") file.write(rule.Name.toString()+"(parent)") counter = counter + 1 file.write("])\n") # generate the initial action function file.write(" def initialAction(self, graph):\n") name, language, type, event, code = self.InitialAction.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") # generate the final action function file.write(" def finalAction(self, graph):\n") name, language, type, event, code = self.FinalAction.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()