有没有办法使用 Java API 从企业架构师模型中获取所有用例图?

Is there a way to fetch all the use case diagrams from an enterprise architect model using Java API?

我在 Enterprise Architect 的模型中定义了许多用例图。这些图在层次结构中处于不同级别。无论图表位于何处,是否有任何方法可以使用 Enterprise Architect Java API?

访问模型中存在的所有用例图(与此相关的任何图表)

Java API 只不过是围绕常规 API 的一层而已,所以我的回答很笼统。

您当然可以在代码中遍历整个模型以获得图表,但在任何非平凡模型中这将花费很长时间。

所以你想做的是

  1. 查询模型以使用 EA.Repository.SQLQuery()
  2. 获取所有图表 GUID
  3. 循环图表 guid 并使用 EA.Repository.GetDiagramByGUID()
  4. 获取每个图表

SQL查询

获取所有用例图 guid 所需的 SQL 查询如下

select d.ea_guid from t_diagram d
where d.Diagram_Type = 'Use Case'

从查询中获取值列表

EA.Repository.SQLQuery() returns 一个 XML 字符串,因此您需要对其进行解析以获取值列表。这个例子是 VBScript 中的一个操作,它完全是这样做的:

function getArrayFromQuery(sqlQuery)
    dim xmlResult
    xmlResult = Repository.SQLQuery(sqlQuery)
    getArrayFromQuery = convertQueryResultToArray(xmlResult)
end function

'converts the query results from Repository.SQLQuery from xml format to a two dimensional array of strings
Public Function convertQueryResultToArray(xmlQueryResult)
    Dim arrayCreated
    Dim i 
    i = 0
    Dim j 
    j = 0
    Dim result()
    Dim xDoc 
    Set xDoc = CreateObject( "MSXML2.DOMDocument" )
    'load the resultset in the xml document
    If xDoc.LoadXML(xmlQueryResult) Then        
        'select the rows
        Dim rowList
        Set rowList = xDoc.SelectNodes("//Row")

        Dim rowNode 
        Dim fieldNode
        arrayCreated = False
        'loop rows and find fields
        For Each rowNode In rowList
            j = 0
            If (rowNode.HasChildNodes) Then
                'redim array (only once)
                If Not arrayCreated Then
                    ReDim result(rowList.Length, rowNode.ChildNodes.Length)
                    arrayCreated = True
                End If
                For Each fieldNode In rowNode.ChildNodes
                    'write f
                    result(i, j) = fieldNode.Text
                    j = j + 1
                Next
            End If
            i = i + 1
        Next
        'make sure the array has a dimension even is we don't have any results
        if not arrayCreated then
            ReDim result(0, 0)
        end if
    end if
    convertQueryResultToArray = result
End Function

根据 guid 获取图表

循环查询结果并使用Repository.GetDiagramByGUID()

在 VBScript 中可能是这样的(假设查询结果存储在变量 guidResults 中)

dim diagram as EA.Diagram
dim diagrams
set diagrams = CreateObject("System.Collections.Arraylist")
dim guid

for each guid in guidResults
    set diagram = Repository.GetDiagramByGuid(guid)
    diagrams.Add diagram
next