如何在 Sparx EA 中修改 ad-hoc Stereotype 的名称

How to modify the name of an ad-hoc Stereotype in Sparx EA

我在 Sparx EA 模型中有许多 "ad-hoc" 刻板印象。也就是说,我只是在元素属性中键入了一个构造型名称,然后在它所应用的其他元素中使用了相同的名称...而不使用配置文件。

然后每一个都出现在 Project --> Settings --> UML Types --> Stereotypes

最终,我添加了 Shape Script 图标等

但是,我对一个 Stereotype 名称造成了一些混淆。我用"Tablespace"作为设计概念来表示"group of related database tables"。由于 Oracle 中 "tablespace" 的物理概念,我们的数据库团队发现这令人困惑。

所以,我想重命名。

如果我从 UML Types --> Stereotypes 执行此操作,则所有现有元素都保留原始名称(例如表空间)并恢复为无 Shape 脚本的外观。如果我访问一个元素并更改为新名称,则会出现 Shape Script 等。

我不喜欢用刻板印象找到每个元素并手动应用新名称。

我是时候学习 EA Scripting 还是有其他方法?

您唯一的出路是使用自动化(或本机数据库访问)。如果您的构造型仅用于对象,您可以迭代

的结果

Repository.SQLQuery("SELECT object_id FROM t_object WHERE stereotype='oldStereo'")

获取所有对象 ID。那么你需要

elem = Repository.GetElementByID(theId)

检索单个元素。最后你可以用

改变刻板印象
elem.stereotype = "newStereo"
elem.update()

您也可以 运行 直接 SQL 与

Repository.Execute("UPDATE t_object SET stereotype='new' WHERE stereotype='old'")

注意后者使用了官方不支持的地雷功能之一

编辑:您还可以 运行 本机 RDBMS 客户端中的最后一个 SQL。使用 EAP 时,您可能需要暂时将其重命名为 .mdb 以假装它是一个 MS Access 数据库(实际上是)。

您可以使用此 VBScript 重命名 EA 中的原型。

在.eap文件上测试过,之所以这么复杂,是因为MS Access SQL语法中缺少replace()

只需转到“脚本”视图,添加一个新的 VBScript 并将此代码粘贴到您的新脚本中。然后修改它以指示 from 和 to 构造型。

option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Script Name: Rename Stereotypes
' Author: Geert Bellekens
' Purpose: Rename stereotypes on all types of elements 
' Environment: Tested on .eap file.
' Date: 13/10/2015
'
sub main
    renameElementStereotypes "FromStereo", "ToStereo"
    renameAttributeStereotypes "FromStereo", "ToStereo"
    renameConnectorStereotypes "FromStereo", "ToStereo"
    renameOperationStereotypes "FromStereo", "ToStereo"
    renameDiagramStereotypes "FromStereo", "ToStereo"
    Repository.RefreshModelView(0)
    msgbox "Finished renaming stereotypes"
end sub

sub renameElementStereotypes(fromStereo, toStereo)
    renameStereotypes "t_object", fromStereo, toStereo
end sub
sub renameAttributeStereotypes(fromStereo, toStereo)
    renameStereotypes "t_attribute", fromStereo, toStereo
end sub
sub renameConnectorStereotypes(fromStereo, toStereo)
    renameStereotypes "t_connector", fromStereo, toStereo
end sub
sub renameOperationStereotypes(fromStereo, toStereo)
    renameStereotypes "t_operation", fromStereo, toStereo
end sub
sub renameDiagramStereotypes(fromStereo, toStereo)
    renameStereotypes "t_diagram", fromStereo, toStereo
end sub

sub renameStereotypes (baseTable, fromStereo, toStereo)
    dim updateSQL
    'first the second part of of t_xref description
    updateSQL = "update (" & baseTable & " o inner join t_xref x on o.[ea_guid] = x.[Client]) "&_
               " set x.Description = MID( x.Description, 1, INSTR(  x.Description, ':" & fromStereo & "') - 1) "&_
                         " + ':" & toStereo & "' "&_
                         " + MID(x.Description,INSTR(  x.Description, ':" & fromStereo & "') "&_ 
                              " + LEN(':" & fromStereo & "'), LEN(x.Description)  "&_
                              " - INSTR(  x.Description, ':" & fromStereo & "') "&_
                              " - LEN(':" & fromStereo & "')+ 1) "&_
               " where o.Stereotype = '" & fromStereo & "' "&_
               "  and x.Name = 'Stereotypes' "&_
               " and INSTR(  x.Description, ':" & fromStereo & "') > 0  "              
    Repository.Execute updateSQL
    'then the first part of t_xref description
    updateSQL = "update (" & baseTable & " o inner join t_xref x on o.[ea_guid] = x.[Client]) "&_
               " set x.Description = MID( x.Description, 1, INSTR(  x.Description, '=" & fromStereo & "') - 1) "&_
                         " + '=" & toStereo & "' "&_
                         " + MID(x.Description,INSTR(  x.Description, '=" & fromStereo & "') "&_ 
                              " + LEN('=" & fromStereo & "'), LEN(x.Description)  "&_
                              " - INSTR(  x.Description, '=" & fromStereo & "') "&_
                              " - LEN('=" & fromStereo & "')+ 1) "&_
               " where o.Stereotype = '" & fromStereo & "' "&_
               "  and x.Name = 'Stereotypes' "&_               
               " and INSTR(  x.Description, '=" & fromStereo & "') > 0  "
    Repository.Execute updateSQL                
    'then the stereotype itself
    updateSQL = " update " & baseTable & " o "&_
                " set o.[Stereotype] = '" & toStereo & "' "&_
                " where o.Stereotype = '" & fromStereo & "' "
    Repository.Execute updateSQL
end sub

main

这是我根据@Thomas Killian 的回答创建的脚本。

改进:

  1. 使用 EAScriptLib 数据库函数获取结果。
  2. Update() 方法的正确案例(不过似乎不是必需的)
  3. 还在 Object/Element 上设置 StereotypeEx 属性。 这似乎很关键...否则,更改将被忽略,或者新的构造型只是添加到对象的构造型列表中

此外,不确定它有多重要,但在最终测试期间 运行 脚本之前没有通过项目浏览器打开组件。这感觉就像巫术。

!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-Database

function RenameStereotypes()
{
    Repository.EnsureOutputVisible( "Script" );

    var objectIDs = DBGetFieldValueArrayString( "object_id" /* : String */, "t_object" /* : String */,  "stereotype='DBTables'" /* : String */ ); /* : Array */

    Session.Output( objectIDs );

    for ( var i = 0; i < objectIDs.length; i++ ) {
        var theId = objectIDs[i];
        var elem = Repository.GetElementByID(theId);
        Session.Output( "Id: " + theId + " Name: " + elem.name + " Stereotype: " + elem.stereotype );
        elem.stereotype = "DBTables";    /* FIRST VISIBLE STEREOTYPE */
        elem.stereotypeEx = "DBTables";  /* CRITICAL - DEEPER LIST OF STEREOTYPES */
        elem.Update();  /* CASE CHANGED, NOT SURE IT MATTERS */
        elem.Refresh(); /* JUST IN CASE */
    }
}

RenameStereotypes();