From: <de...@us...> - 2004-01-07 01:48:33
|
Update of /cvsroot/pymerase/smw/smw/scw/Element In directory sc8-pr-cvs1:/tmp/cvs-serv32238/smw/scw/Element Added Files: LayerManager.py SfiManager.py __init__.py python.iml Log Message: Imported version of SMW downloaded on 2003 Apr 14 --- NEW FILE: LayerManager.py --- #!/usr/bin/env python2 __layer__ = 'Element' import unittest from smw.metamodel import UML14 as metamodel from smw.transform.uml2py.uml2py import isNameValid import string def mkTestCaseName(name): """returns name for test case of class 'name'""" tName = string.capitalize(name[0]) + name[1:] tName = unittest.TestLoader.testMethodPrefix + tName return tName mkTestMethodName = mkTestCaseName def mkTestDependencyName(name): """ Returns a name for the dependency between the class and its testclass """ [...1271 lines suppressed...] """ Create a unique name for element and returns it """ if not isinstance(name,str): return None if not(isinstance(element,metamodel.ModelElement)): return None eles=self.returnElements(element) names=[] for k in eles: names.append(k.name) x=1 while(names.count(name+str(x))>0): x=x+1 return name+str(x) --- NEW FILE: SfiManager.py --- #!/usr/bin/env python2 """ This layer can add different elements to the model. Also deleting and renaming elements is added here """ __layer__ = 'Element' from smw.metamodel import UML14 as metamodel from smw.transform.uml2py.uml2py import isNameValid from LayerManager import LayerManager from smw.scw.Project.SfiManager import SfiManager as ProjectSfiManager from LayerManager import mkTestCaseName, mkTestMethodName,mkTestDependencyName def mkUnittestDependencyName(pname): return pname+"UTDep" class SfiManager(ProjectSfiManager): def __init__(self): """ Constructor for a new SfiManager. """ ProjectSfiManager.__init__(self) def setUp(self): ProjectSfiManager.setUp(self) self._layerManager=LayerManager() self._currentElements={} self._unittestPackage={} self._testCaseClass={} def unittestPackage(self): """ Returns the unitest package """ return self._unittestPackage def testCaseClass(self): """ Returns the testcaseclass dictonary """ return self._testCaseClass def layerManager(self): """ returns an instance of the LayerManager class """ return self._layerManager def newProject(self, projectName = None): """ Calls the newProject method in Project layer. Initializes the created project to the currentElements dictionary. """ project=ProjectSfiManager.newProject(self,projectName) if project==None: return None for x in project.ownedElement: if(x.name=="python2_2"): for q in x.ownedElement: if(q.name=="unittest"): self._unittestPackage[project]=q for y in q.ownedElement: if(y.name=="TestCase"): self._testCaseClass[project]=y break self._currentElements[project.name]=self.currentProject() d=self.addUnittestDependency(project) assert(d) return project def deleteProject(self, arg): """ Calls the method in Project layer. Deletes the project from the currentElements dictionary. """ deletedProject=ProjectSfiManager.deleteProject(self,arg) if deletedProject==None: return None # delete the dependency to unittest package # NÄITÄ EI TARVITISI TEHDÄ NYT KUN KÄYTETÄÄN KAIKLLA PROJEKTEILLA OMAA PYTHON PAKKAUSTA for d in deletedProject.clientDependency: for c in d.supplier: if(c==self._unittestPackage[deletedProject]): self.deleteElement2(d) for e in self._layerManager.returnElements(deletedProject): if isinstance(e,metamodel.Class) and self._layerManager.getTestSupplier(e): for g in e.generalization: p=g.parent if p==self._testCaseClass[deletedProject]: self.deleteElement2(g) del self._testCaseClass[deletedProject] del self._unittestPackage[deletedProject] if isinstance(arg,str): del self._currentElements[arg] elif isinstance(arg,metamodel.Model): del self._currentElements[arg.name] return deletedProject def renameProject(self, oldName, newName): """ Calls the method in Project layer. Changes the key in the currentElements dictionary. """ eles=self._layerManager.returnElements(self.currentProject()) pnames=[] for e in eles: if e.oclIsKindOf(metamodel.Package) and not e.oclIsKindOf(metamodel.Model): pnames.append(e.name) if newName in pnames: return rName=ProjectSfiManager.renameProject(self,oldName,newName) if rName==None: return None value=self._currentElements[oldName] del self._currentElements[oldName] self._currentElements[newName]=value return rName def loadProject(self, fname): """ Loads a project. set the current element correctly. """ uModel = ProjectSfiManager.loadProject(self,fname) if uModel: self._currentElements[uModel.name]=uModel for x in range(len(uModel.ownedElement)): e=uModel.ownedElement[x] if (e.oclIsKindOf(metamodel.Package) and e.name=="python2_2"): # Find the loaded packages: # loadedUTPackage=None # loadedTestCaseClass=None for y in e.ownedElement: if(y.name=="unittest"): # loadedUTPackage=y self._unittestPackage[uModel]=y for z in y.ownedElement: if(z.name=="TestCase"): # loadedTestCaseClass=z self._testCaseClass[uModel]=z break packs=[] classes=[] lclclc=self._layerManager.returnElements(uModel) for p in lclclc: if p.oclIsKindOf(metamodel.Package): packs.append(p) for clas in lclclc: if clas.oclIsKindOf(metamodel.Class): classes.append(clas) for c in classes: # mark the class abstract if all it's operations do not have methods for f in c.feature: if f.oclIsKindOf(metamodel.Operation) and not f.method: c.isAbstract=1 # check if the class is in python package ns=c.namespace inpp=None while not ns==uModel: if ns.name=='python2_2': inpp=1 ns=uModel else: ns=ns.namespace if not inpp: if not self._layerManager.isTestClass(c): if not self._layerManager.getTestClient(c): tc=metamodel.Class() tc.name=mkTestCaseName(c.name) tc.namespace=c.namespace self._layerManager.attachTag(tc,'testcase') self.newDependency(c,tc,mkTestDependencyName(c.name)) self.newGeneralization(self._testCaseClass[uModel],tc) # add operations and methods to testClass # test class is made concrete, even if the real class is not ops=c.feature.select(lambda x: x.oclIsKindOf(metamodel.Operation)) for o in ops: to=self._layerManager.newOperation(tc,mkTestMethodName(o.name)) if o.method: for m in o.method: self._layerManager.newMethod(tc,mkTestMethodName(o.name),to) ## else: ## self._layerManager.newMethod(tc,mkTestMethodName(o.name),to) else: tc.isAbstract=1 for p in packs: ns=p inpp=None while not ns==uModel: if ns.name=='python2_2': inpp=1 ns=uModel else: ns=ns.namespace if not inpp: ud=None for d in p.clientDependency: for s in d.supplier: if s==self.unittestPackage()[uModel]: ud=d if not ud: self.newDependency(self.unittestPackage()[uModel],p,mkUnittestDependencyName(p.name)) self.focusOnElement(uModel) return uModel ## lElements=self._layerManager.returnElements(uModel) ## for y in lElements: ## # Create new unittest dependecies (and remove old ones): ## if (y.oclIsKindOf(metamodel.Package)): ## classes=y.ownedElement.select(lambda z: z.oclIsKindOf(metamodel.Class)) ## if (classes): ## deps=y.clientDependency.select(lambda z: z.supplier.select(lambda w: w==loadedUTPackage)) ## if (deps and len(deps)==1): ## d=deps[0] ## s=d.supplier[0] ## c=d.client[0] ## self._layerManager.deleteElement2(d) ## self.newDependency(s,self._unittestPackage, mkUnittestDependencyName(c)) ## # Create new generalizations for test classes (and remove the old ones): ## if (y.oclIsKindOf(metamodel.Class) and not self._layerManager.getTestClient(y)): ## gens=y.generalization.select(lambda z: z.parent==loadedTestCaseClass) ## if gens and len(gens)==1: ## g=gens[0] ## p=g.parent ## c=g.child ## self._layerManager.deleteElement2(g) ## self.newGeneralization(p,self._testCaseClass) ## self.deleteElement2(uModel.ownedElement[x]) ## break ## uModel.ownedElement.append(self.pythonPackage()) def currentElement(self,proj=None): """ Returns the current element of the project given as parameter -if no parameter is given returns the current element of the current project """ if(proj==None): if(len(self.projects())<1): return None else: return self._currentElements[self.currentProject().name] if(isinstance(proj,metamodel.Model)): for x in self.projects().keys(): if(x==proj.name): return self._currentElements[proj.name] return self.currentElement() elif(isinstance(proj,str)): for x in self.projects().keys(): if(x==proj): return self._currentElements[proj] return self.currentElement() else: return self.currentElement() def focusOnElement(self,object): """ Focuses on the given element or project and returns it if -given parameter is of valid type -given parameter exists in manager else returns the previous current element """ owner=object if(isinstance(object,metamodel.Method) or isinstance(object,metamodel.Attribute) or isinstance(object,metamodel.Operation)): owner1=object.owner if(owner1==None): #the object isn't attached to any class or interface owner=owner.namespace if(owner==None):#the object isn't attached to any project return self.currentElement() else: #owner1!=None owner=owner1 if(isinstance(owner,metamodel.Class) or isinstance(owner,metamodel.Interface) or isinstance(owner,metamodel.Generalization) or isinstance(owner,metamodel.Association) or isinstance(owner,metamodel.Dependency) or isinstance(owner,metamodel.Package) or isinstance(owner,metamodel.Model)): #tests if the object is a class diagram element #gets the project in which the owner belongs while(not isinstance(owner,metamodel.Model)): owner=owner.namespace if(owner==None): return self.currentElement() ## #checks wether the project belongs to the manager if(self.projects().has_key(owner.name)): self.focusOnProject(self.projects()[owner.name]) self._currentElements[owner.name]=object return self.currentElement() #else returns the previous current element else: return self.currentElement() ## #else (if parameter type invalid) returns the previous current element else: return self.currentElement() ## def addUnittestDependency(self, package): """ Adds 'unittest' depency to given package (or model) - not created if it allready exists or parameter if not of correct type. Does not change the focus """ if not self.currentProject(): return if not isinstance(package,metamodel.Package): return if isinstance(package,metamodel.Model) and not self._projects.has_key(package.name): return dep=None f=0 for d in package.clientDependency: for s in d.supplier: if s==self._unittestPackage[self.currentProject()]: f=1 if not f: name=mkUnittestDependencyName(package.name) name=self.createUniqueElementName(self.currentProject(),name) dep=self._layerManager.newDependency(self._unittestPackage[self.currentProject()],package,name) return dep def newClass(self, newName=None):# - adding to current element, if it is a project or package if not newName: newName=self.createUniqueElementName(self.currentElement(),"class") createdClass=self._layerManager.newClass(self.currentElement(),newName) if createdClass: testClass=self._layerManager.getTestClient(createdClass) gen=self.newGeneralization(self._testCaseClass[self.currentProject()],testClass) if not gen: raise 'Could not create the test generalization' self.focusOnElement(createdClass) return createdClass def newInterface(self,newName=None):#- adding to current element, if it is a project or package if not newName: newName=self.createUniqueElementName(self.currentElement(),"interface") createdInterface=self._layerManager.newInterface(self.currentElement(),newName) self.focusOnElement(createdInterface) return createdInterface def newGeneralization(self,parent,child,newName=None):#tested, adding to closest common owner, class or interface if not newName: newName=self.createUniqueElementName(self.currentElement(),"generalization") createdGeneralization=self._layerManager.newGeneralization(parent,child,newName) self.focusOnElement(createdGeneralization) return createdGeneralization def newAssociation(self,end1,end2,newName=None):# - adding to the closest common namespace,project or package. if not newName: newName=self.createUniqueElementName(self.currentElement(),"association") createdAssociation=self._layerManager.newAssociation(end1,end2,newName) self.focusOnElement(createdAssociation) return createdAssociation def newDependency(self,supplier,client,newName=None):# - adding to closest namespace, project or package if not newName: newName=self.createUniqueElementName(self.currentElement(),"dependency") createdDependency=self._layerManager.newDependency(supplier,client,newName) self.focusOnElement(createdDependency) return createdDependency def newPackage(self,newName=None):# - adding to current element, if it is project or package if newName==self.currentProject().name: return if not newName: newName=self.createUniqueElementName(self.currentElement(),"package") createdPackage=self._layerManager.newPackage(self.currentElement(),newName) self.focusOnElement(createdPackage) self.addUnittestDependency(createdPackage) return createdPackage def newMethod(self,newName=None, specification=None):# tested adding to currentElement and to the testClass if not newName: newName=self.createUniqueElementName(self.currentElement(),"method") createdMethod=self._layerManager.newMethod(self.currentElement(),newName,specification) self.focusOnElement(createdMethod) return createdMethod def newOperation(self,newName=None): # tested if not newName: newName=self.createUniqueElementName(self.currentElement(),"operation") createdOperation=self._layerManager.newOperation(self.currentElement(),newName) self.focusOnElement(createdOperation) return createdOperation def newAttribute(self,newName=None):#tested-working - adding to currentElement if not newName: newName=self.createUniqueElementName(self.currentElement(),"attribute") createdAttribute=self._layerManager.newAttribute(self.currentElement(),newName) self.focusOnElement(createdAttribute) return createdAttribute ## def deleteElement1(self,element=None): #leaves the relationships hanging ## if (element==None): ## element=self.currentElement() ## if not isinstance(element,metamodel.ModelElement): ## return None ## if(self._layerManager.returnElements(element).count(element)>0): ## if(isinstance(element,metamodel.Attribute) or isinstance(element,metamodel.Method) or isinstance(element,metamodel.Operation)): ## if(element.namespace!=None): ## self.focusOnElement(element.namespace) ## elif(element.owner!=None): ## self.focusOnElement(element.owner) ## # elif(isinstance(element,metamodel.Method)): ## # self.focusOnElement(element.owner) ## else: ## self.focusOnElement(element.namespace) ## deletedElement=self._layerManager.deleteElement1(element) ## return deletedElement def deleteElement2(self,element=None): #deletes the relationships too """ Deletes an element and all its relationships """ if (element==None): element=self.currentElement() if not isinstance(element,metamodel.ModelElement): return None if(self._layerManager.returnElements(element).count(element)>0): if(isinstance(element,metamodel.Attribute) or isinstance(element,metamodel.Method) or isinstance(element,metamodel.Operation)): if(element.namespace!=None): self.focusOnElement(element.namespace) elif(element.owner!=None): self.focusOnElement(element.owner) #elif(isinstance(element,metamodel.Method)): # self.focusOnElement(element.owner) else: self.focusOnElement(element.namespace) removedependency=0 p=None deletedElement=self._layerManager.deleteElement2(element) return deletedElement def renameElement(self, element, newName):#tested-working """ Renames the element to 'newName' and returns the new name of the element. Returns none if either one of the parameters is invalid. """ if isinstance(element,metamodel.Package): if newName==self.currentProject().name: return ns=element if isinstance(element,metamodel.Feature) and element.owner: ns=element.owner while not ns==self.currentProject(): if ns==self.pythonPackage(): return ns=ns.namespace changedName=self._layerManager.renameElement(element,newName) return changedName def createUniqueElementName(self,element,defName=None): """ Create a unique name for element and returns it """ if not defName: defName="element" if not isinstance(defName,str): return None if not(isinstance(element,metamodel.ModelElement)): return None return self.layerManager().createUniqueElementName(element,defName) --- NEW FILE: __init__.py --- from SfiManager import * from LayerManager import * --- NEW FILE: python.iml --- (ismw.metamodel.UML14 Model p1 (dp2 S'isAbstract' p3 I0 sS'isSpecification' p4 I0 sS'__uniqueID__' p5 S'ib351d2ec95de35465f82789cf7689fa7' p6 sS'isRoot' p7 I0 sS'visibility' p8 [...227897 lines suppressed...] p49406 sg54 S'' sbsg4 I0 sg5 S'iaf81dd77ae2741edd072fd3d40fe0e99' p49407 sg56 I0 sg8 I0 sg10 S'_normpath' p49408 sg17 g48793 sg58 I0 sbasbsbasbsbasbsb. |