def multipleStereotypes(profilePackage, stereotypePackage, customPackage, energon, unusedSterPack, unusedCustPack, allStereotypes, allElements, allCustomizations): multipleSter = [] baseClass = getElements(allElements, profilePackage) baseClass = filter(lambda element: isinstance(element, Classifier), baseClass) gl.log('VALIDATION: Elements with Multiple Stereotypes') for b in baseClass: if b.getName() != "": ster =StereotypesHelper.getAllAssignedStereotypes([b]) sterName = [] for s in ster: sterName.append(s.getName()) sterInProfile = filter(lambda element: element.getOwner() == stereotypePackage, ster) if len(sterInProfile) > 1: multipleSter.append(b) for d in multipleSter: whichSter = StereotypesHelper.getAllAssignedStereotypes([d]) whichSter = filter(lambda element: element.getOwner() == stereotypePackage, whichSter) whichSterName = [] for w in whichSter: whichSterName.append(w.getName()) gl.log('\t\t' +d.getName() + ' has multiple stereotypes: ' + str(whichSterName)) gl.log('\t\t\t Location:' + d.getQualifiedName()) gl.log("=====Validate Multiple Applied Stereotypes Summary") gl.log(str(len(multipleSter))+ ' elements have multiple stereotypes applied') gl.log("*Validate Mulitplie Stereotypes Complete") return [multipleSter]
def nameMismatch(profilePackage, stereotypePackage, customPackage, energon, unusedSterPack, unusedCustPack, allStereotypes, allElements, allCustomizations): mismatch = [] queueProps = ["priority", "location in queue", "time in queue", "timestamp", "read capability", "next", "event time", "minClosed", "maxClosed", "minClosed1", "maxClosed1", "start event", "time interval", "duration interval", "end event"] baseClass = getElements(allElements, profilePackage) baseClass = filter(lambda element: isinstance(element, Classifier), baseClass) for b in baseClass: ster =StereotypesHelper.getAllAssignedStereotypes([b]) sterName = [] for s in ster: sterName.append(s.getName()) if b.getName() != "": if not StereotypesHelper.hasStereotype(b, b.getName()) and len(ster)>0 and "ValueType" not in sterName and not StereotypesHelper.hasStereotype(b, StereotypesHelper.getStereotype(project, "Customization")): mismatch.append(b) gl.log('VALIDATION: Element Name, Applied Stereotype Name Mismatch') elMismatch = 0 for m in mismatch: whichSter = StereotypesHelper.getAllAssignedStereotypes([m]) whichSterName = [] for w in whichSter: if w.getName() != "ValueType": whichSterName.append(w.getName()) if m.getName() not in queueProps: elMismatch +=1 gl.log('\t\t' + m.getName() + ' does not match applied stereotype name:' + str(whichSterName)) gl.log('\t\t\t Location:' + m.getQualifiedName()) gl.log("=====Validate Name Mismatch Summary") gl.log(str(elMismatch) + ' elements have applied stereotypes not matching name') gl.log('*Validate Name Mismatch Complete') return [elMismatch]
def createStereotype(owner, element): if isinstance(element, Package): if StereotypesHelper.hasStereotype(element, StereotypesHelper.getStereotype(project, "View")): package = element.getName().find("View") if package == -1: element.setName(element.getName() + " View") else: package = element.getName().find("Package") if package == -1: element.setName(element.getName() + " View") if not isinstance(element, EnumerationLiteral): eSter = StereotypesHelper.getAllAssignedStereotypes([element]) eSter = filter(lambda element: element != StereotypesHelper.getStereotype(project, "Diagram Info"), eSter) newSter = StereotypesHelper.createStereotype(owner, element.getName(), [StereotypesHelper.getBaseClass(element)]) StereotypesHelper.addStereotypeByString(element, element.getName()) for e in eSter: newgen = ef.createGeneralizationInstance() gl.log(e.getQualifiedName()) gl.log(newSter.getQualifiedName()) newgen.setGeneral(e) newgen.setSpecific(newSter) newgen.setOwner(newSter) icon = filter(lambda element: isinstance(element, Image), e.getOwnedElement()) if icon: icon = icon[0] newicon= ef.createImageInstance() newicon.setLocation(icon.getLocation()) newicon.setContent(icon.getContent()) newicon.setFormat(icon.getFormat()) newicon.setOwner(newSter) return
def validateBaseClass(profilePackage, stereotypePackage, customPackage, energon, unusedSterPack, unusedCustPack, allStereotypes, allElements, allCustomizations): gl.log("**VALIDATION: Base Class") queueProps = ["priority", "location in queue", "time in queue", "timestamp", "read capability", "next", "event time", "minClosed", "maxClosed", "minClosed1", "maxClosed1", "start event", "time interval", "duration interval", "end event"] baseClass = getElements(allElements, profilePackage) # elementNoStereotype = getMissing(baseClass, allStereotypes) # elementNoStereotype = filter(lambda element: not isinstance(element, Association), elementNoStereotype) # elementNoStereotype = filter(lambda element: not isinstance(element, Dependency),elementNoStereotype) # elementNoStereotype = filter(lambda element: not isinstance (element, InstanceSpecification), elementNoStereotype) # elementNoStereotype = filter(lambda element: element.getOwner() != customPackage, elementNoStereotype) elNoSter = 0 classifiers = [] properties = [] operations=[] misc = [] diagrams = [] for e in baseClass: eSter = StereotypesHelper.getAllAssignedStereotypes([e]) if not eSter: if e.getName() != "" and e.getName() not in queueProps: if isinstance(e, Classifier): classifiers.append(e) elif isinstance(e, Property): properties.append(e) elif isinstance(e, Diagram): diagrams.append(e) elif isinstance(e, Operation): operations.append(e) else: misc.append(e) elNoSter+=1 gl.log('======Classifiers with no Stereotype') for c in classifiers: gl.log('\t' + str(c.getName()) + ' has no stereotype') gl.log('======Properties with no Stereotype') for p in properties: gl.log('\t' + str(p.getName()) + ' has no stereotype') gl.log("======Diagrams with no Stereotype") for d in diagrams: gl.log('\t' + d.getName() + ' has no stereotype') gl.log("=====Operations with no Stereotype") for o in operations: gl.log('\t' + o.getName() + ' has no stereotype') gl.log('======Others with no Stereotype:****') for m in misc: gl.log('\t' + str(m.getName()) + ' has no stereotype') gl.log('* ' + str(elNoSter) + ' stereotypes are missing') gl.log('======Validate Base Class Summary') gl.log(str(len(classifiers))+ ' classifiers have no stereotype') gl.log(str(len(properties))+ ' properties have no stereotype') gl.log(str(len(diagrams)) + ' diagrams have no stereotype') gl.log(str(len(operations))+ ' operations have no stereotype') gl.log(str(len(misc))+' other base class elements have no stereotype') gl.log("*Validate Elements Complete") return [classifiers, properties, diagrams, operations, misc]
def unapplyStereotypes(elements): for e in elements: eSter = StereotypesHelper.getAllAssignedStereotypes([e]) suffix = [' part property', ' constraint property', ' reference property',' shared property', ' value property', ' shared property', ' distributed property', ' port', ' flow property'] names = [] for s in suffix: names.append(e.getName() + s) names.append(e.getName()) names.append('Customization') for s in eSter: if s.getName() not in names: StereotypesHelper.removeStereotype(e, s) return
def getMissing(A, B): missing =[] nameMatch = {} metaMismatch=[] applyMismatch=[] for a in A: aName = a.getName() if isinstance(a, Property): aName = propertyCheck(a) elif isinstance(a, Stereotype) and StereotypesHelper.getBaseClassesAsClasses(a)[0] == Property: aGens = filter(lambda element: isinstance(element, Generalization), a.get_directedRelationshipOfSource()) if len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "ConstraintProperty"), aGens))!=0: if a.getName().find("constraint property")==-1: aName = a.getName() + " constraint property" elif len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "SharedProperty"), aGens))!=0: if a.getName().find("shared property")==-1: aName = a.getName() + " shared property" elif len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "DistributedProperty"), aGens))!=0: if a.getName().find("distributed property")==-1: aName = a.getName() + " distributed property" elif len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "FlowProperty"), aGens))!=0: if a.getName().find("flow property")==-1: aName = a.getName() + " flow property" elif len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "ValueProperty"), aGens))!=0: if a.getName().find("value property")==-1: aName = a.getName() + " value property" elif len(filter(lambda element: element.getGeneral() == StereotypesHelper.getStereotype(project, "ReferenceProperty"), aGens))!=0: if a.getName().find("reference property")==-1: aName = a.getName() + " reference property" else: if a.getName().find("part property")==-1: aName=a.getName() + " part property" elif isinstance(a, Stereotype) and StereotypesHelper.getBaseClassAsClasses(a)[0] == Port: if a.getName().find("port")==-1: aName = a.getName() + " port" bExist = 0 for b in B: if aName == b.getName(): nameMatch[a]=b bExist = 1 if b not in StereotypesHelper.getAllAssignedStereotypes([a]): noApply[a]=b if bExist == 0: missing.append(a) for n in nameMatch: ###get generalizations of stereotype stereotype = nameMatch[n] nGeneral = filter(lambda element: isinstance(element, Generalization), stereotype.get_directedRelationshipOfSource()) general = [] for g in nGeneral: general.append(g.getGeneral()) ###keep only SysML generalizations gSys = filter(lambda element: StereotypesHelper.getProfileForStereotype(element) == StereotypesHelper.getProfile(project, "SysML"), general) if gSys and not StereotypesHelper.checkForAllDerivedStereotypes(n, gSys[0]): applyMismatch.append(n) ###check if same if n.getName() != "_Unused Stereotypes": if not isinstance(n, StereotypesHelper.getClassOfMetaClass(StereotypesHelper.getBaseClasses(nameMatch[n])[0])): metaMismatch.append(n) return [missing, applyMismatch, metaMismatch]
def validateStereotypes(profilePackage, stereotypePackage, customPackage, energon, unusedSterPack, unusedCustPack, allStereotypes, allElements, allCustomizations): gl.log("**VALIDATION: Stereotypes**") baseClass = getElements(allElements, profilePackage) stereotypeNoCustomization = getMissing(allStereotypes, allCustomizations) appliedStereotypes = StereotypesHelper.getAllAssignedStereotypes(baseClass) stereotypeNoElement=[] for s in allStereotypes: if s not in appliedStereotypes: stereotypeNoElement.append(s) gl.log('======Stereotypes With No Customizations') for s in stereotypeNoCustomization: gl.log('\t' + str(s.getName()) + " has no customization") gl.log('======Stereotypes With No Element in Base Class') for s in stereotypeNoElement: sSter = StereotypesHelper.getAllAssignedStereotypes([s]) sProfSter = filter(lambda element: element.getOwner() == stereotypePackage, sSter) if len(sProfSter) == 0: gl.log('\t' + str(s.getName()) + " has no element in the Base Class. ") gl.log("======Validate Stereotypes Summary") gl.log(str(len(stereotypeNoCustomization)) + ' stereotypes don\'t have customizations') gl.log(str(len(stereotypeNoElement)) + ' stereotypes don\'t have elements in the Base Class') gl.log("*Validate Stereotypes Complete*") return [stereotypeNoCustomization, stereotypeNoElement]
def createStereotype(owner, element, stereotypePackage): '''append Package to any package element, this is an OpsRev naming convention ''' if isinstance(element, Package): package = element.getName().find("Package") if package == -1: element.setName(element.getName() + " Package") '''stereotypes should not be made for enumeration literals''' if not isinstance(element, EnumerationLiteral): eSter = StereotypesHelper.getAllAssignedStereotypes([element]) elementSterName = propertyCheck(element) newSter = StereotypesHelper.createStereotype(owner, elementSterName, [StereotypesHelper.getBaseClass(element)]) StereotypesHelper.addStereotype(element, newSter) for e in eSter: if StereotypesHelper.getProfileForStereotype(e) == StereotypesHelper.getProfile(project, "SysML") or StereotypesHelper.getProfileForStereotype(e) == StereotypesHelper.getProfile(project, "additional_stereotypes"): newgen = ef.createGeneralizationInstance() newgen.setGeneral(e) newgen.setSpecific(newSter) newgen.setOwner(newSter) icon = filter(lambda element: isinstance(element, Image), e.getOwnedElement()) if icon: icon = icon[0] newicon= ef.createImageInstance() newicon.setLocation(icon.getLocation()) newicon.setContent(icon.getContent()) newicon.setFormat(icon.getFormat()) newicon.setOwner(newSter) if isinstance(element, Class) and e.getOwner() == stereotypePackage: newgen = ef.createGeneralizationInstance() newgen.setGeneral(e) newgen.setSpecific(newSter) newgen.setOwner(newSter) icon = filter(lambda element: isinstance(element, Image), e.getOwnedElement()) if icon: icon = icon[0] newicon = ef.createImageInstance() newicon.setLocation(icon.getLocation()) newicon.setContent(icon.getContent()) newicon.setFormat(icon.getFormat()) newicon.setOwner(newSter) return
def getElements(allels, profilePackage): sterPack = filter(lambda element: isinstance(element, Package) and element.getName() == "_Stereotypes", profilePackage.getOwnedElement()) custPack = filter(lambda element:isinstance(element, Package) and element.getName() == "_Customizations", profilePackage.getOwnedElement()) depPack = filter(lambda element: isinstance(element, Package) and element.getName() == "_Deprecated Stereotypes", profilePackage.getOwnedElement()) depCust = filter(lambda element: isinstance(element, Package) and element.getName() == "_Deprecated Customizations", profilePackage.getOwnedElement()) unusedSter = filter(lambda element: isinstance(element, Package) and element.getName() == '_Unused Stereotypes', profilePackage.getOwnedElement()) baseClass = filter(lambda element: element.getOwner() != sterPack, allels) baseClass = filter(lambda element: element.getOwner() != custPack, baseClass) baseClass = filter(lambda element: element.getOwner()!=depPack, baseClass) baseClass = filter(lambda element: element.getOwner()!= depCust, baseClass) baseClass = filter(lambda element: element.getOwner()!=unusedSter, baseClass) baseClass = filter(lambda element: isinstance(element, NamedElement), baseClass) baseClass = filter(lambda element: element.getName() != "_Stereotypes" and element.getName() != "_Customizations" and element.getName() != "_Deprecated Stereotypes" and element.getName() != "_Deprecated Customizations", baseClass) baseClass = filter(lambda element: not isinstance(element, Stereotype), baseClass) baseClass = filter(lambda element: StereotypesHelper.getStereotype(project, "Customization") not in StereotypesHelper.getAllAssignedStereotypes([element]), baseClass) # baseClass = filter(lambda element: not isinstance(element, Diagram), baseClass) baseClass = filter(lambda element: not isinstance(element, Comment), baseClass) baseClass = filter(lambda element: StereotypesHelper.getStereotype(project, "FrameworkValidation.Profile") not in StereotypesHelper.getAllAssignedStereotypes([element]), baseClass) # baseClass = filter(lambda element: not isinstance(element, Property), baseClass) baseClass = filter(lambda element: not isinstance(element, InputPin), baseClass) baseClass = filter(lambda element: not isinstance(element, OutputPin), baseClass) baseClass = filter(lambda element: not isinstance(element, ActivityParameterNode), baseClass) baseClass = filter(lambda element: not isinstance(element, Parameter), baseClass) baseClass = filter(lambda element: not isinstance(element, CallBehaviorAction), baseClass) # baseClass = filter(lambda element: not isinstance(element, Operation), baseClass) baseClass = filter(lambda element: not isinstance(element, EnumerationLiteral), baseClass) baseClass = filter(lambda element: not isinstance(element, ObjectFlow), baseClass) baseClass = filter(lambda element: not isinstance(element, Interaction), baseClass) # baseClass = filter(lambda element: not isinstance(element, StateMachine), baseClass) baseClass = filter(lambda element: element.getName() != "", baseClass) baseClass = filter(lambda element: 'base_' not in element.getName(), baseClass) baseClass = filter(lambda element: 'extension_' not in element.getName(), baseClass) return baseClass
def deprecateStereotypes(modelPackage, validateOnly): if not isinstance(modelPackage, Profile): profilePackage = VP.getProfilePackage(modelPackage) if not profilePackage: gl.log( '***ERROR: The code is not being run on a Profile. Stopping Execution***' ) return else: profilePackage = modelPackage energon = filter( lambda element: isinstance(element, Package) and element.getName() == "_Energon", profilePackage.getOwnedElement()) # if energon: # energon = energon[0] # energon = filter(lambda element: isinstance(element, Package) and element.getName() =="_Stereotypes", energon.getOwnedElement())[0] # else: # gl.log("INFORMATION: Energon Package not present.") # gl.log('**FIX: Deprecating Stereotypes') allElements = VP.getEverything(modelPackage) allCustomizations = filter( lambda element: StereotypesHelper.hasStereotype( element, "Customization"), allElements) unusedSterPack = filter( lambda element: isinstance(element, Package) and "_Deprecated Stereotypes" in element.getName(), profilePackage.getOwnedElement()) if not unusedSterPack: underscore = filter( lambda element: isinstance(element, Package) and "_" == element. getName(), profilePackage.getOwnedElement()) if underscore: unusedSterPack = filter( lambda element: isinstance(element, Package) and "_Deprecated Stereotypes" in element.getName(), underscore[0].getOwnedElement())[0] else: return ['no deprecated package'] else: unusedSterPack = unusedSterPack[0] '''find unused stereotypes''' baseClass = filter(lambda element: isinstance(element, NamedElement), allElements) baseClass = filter( lambda element: "_Stereotypes" not in element.getName( ) or "_Customizations" not in element.getName() or "_Deprecated Stereotypes" not in element.getName(), baseClass) baseClass = filter( lambda element: not StereotypesHelper.hasStereotype( element, "Customization"), baseClass) if profilePackage != modelPackage: baseClass.append(modelPackage) baseClass = VP.flatten(baseClass) properties = filter( lambda element: isinstance(element, Property) and element.getName() != "", baseClass) '''Validate Package Org: all stereotypes in one top level stereotypes package; all customizations in one top level customizations package.''' infraPack = filter( lambda element: isinstance(element, Package) and "_" == element. getName(), profilePackage.getOwnedElement()) stereotypeIpack = [] customIpack = [] userScriptsIpack = [] if len(infraPack) != 0: infraPack = infraPack[0] stereotypeIpack = filter( lambda element: isinstance(element, Package) and "_Stereotypes" in element.getName(), infraPack.getOwnedElement()) customIpack = filter( lambda element: isinstance(element, Package) and "_Customizations" in element.getName(), infraPack.getOwnedElement()) userScriptsIpack = filter( lambda element: isinstance(element, Package) and "_UserScripts" in element.getName(), infraPack.getOwnedElement()) stereotypePackage = filter( lambda element: isinstance(element, Package) and "_Stereotypes" in element.getName(), profilePackage.getOwnedElement()) customPackage = filter( lambda element: isinstance(element, Package) and "_Customizations" in element.getName(), profilePackage.getOwnedElement()) userScriptsPack = filter( lambda element: isinstance(element, Package) and "_UserScripts" in element.getName(), profilePackage.getOwnedElement()) if not stereotypePackage: if len(stereotypeIpack) > 0: stereotypePackage = stereotypeIpack[0] else: gl.log( '======No Stereotype Package Exists for This Profile! All stereotypes are in the wrong place. Stopping Execution; Run Validate Location set to "fix"======' ) return ['no stereotype Package'] else: stereotypePackage = stereotypePackage[0] if not customPackage: if len(customIpack) > 0: customPackage = customIpack[0] elif len(customIpack) == 0: gl.log( '======No Customization Package Exists for This Profile! All customizations are in the wrong place. Stopping Execution; Run Validation Location set to "fix"======' ) return ['no customization Package'] else: customPackage = customPackage[0] if not userScriptsPack: if len(userScriptsIpack) > 0: userScriptsPack = userScriptsIpack[0] elif len(userScriptsIpack) == 0: gl.log( '=======Warning! No UserScripts Package Exists for this Profile.' ) appliedStereotypes = StereotypesHelper.getAllAssignedStereotypes(baseClass) unassignedStereotypes = filter( lambda element: element not in appliedStereotypes, filter(lambda element: isinstance(element, Stereotype), stereotypePackage.getOwnedElement())) if not userScriptsPack: unassignedStereotypes = filter( lambda element: element.getOwner() != userScriptsPack, unassignedStereotypes) for u in unassignedStereotypes: if validateOnly != True: gl.log('FIX: moving ' + u.getQualifiedName() + ' to deprecated stereotypes package') depr = u.getName().find("zz") if depr == -1: u.setName("zz" + u.getName()) u.setOwner(unusedSterPack) gl.log( "Number of stereotypes in profile not being applied in base class: " + str(len(unassignedStereotypes))) if validateOnly != True: killCust(unusedSterPack, customPackage) gl.log('======Deprecate Stereotypes Summary') if not stereotypePackage: gl.log( '*WARNING: No Stereotype Package Exists for This Profile! All stereotypes are in the wrong place*' ) if not customPackage: gl.log( '*WARNING: No Customization Package Exists for This Profile! All stereotypes are in the wrong place*' ) gl.log("======Deprecation Summary=======") gl.log( str(len(unassignedStereotypes)) + ' stereotypes have no matching element. They were moved to unused stereotypes package, customizations deleted' ) gl.log("***Deprecation Complete***") return unassignedStereotypes