• 1 reply
  • Latest Post - ‏2004-12-06T14:53:00Z by SystemAdmin
3659 Posts

Pinned topic programming on UML class diagram

‏2004-12-04T12:19:46Z |
I would like to write some code(in python or java) which extract relationship information from UML class diagram opened in Rational Rose(on Linux) . Could anyone post a sample program which intereact with rational rose UML diagram and edit the class diagram ..
Updated on 2004-12-06T14:53:00Z at 2004-12-06T14:53:00Z by SystemAdmin
  • SystemAdmin
    3659 Posts

    Re: programming on UML class diagram

    Here’s the initiating method for changing the fill color of classes in a class diagram based on stereotype. It comes from the main form in a VB application that includes a variety of Rose extensions.

    Private Sub mnuDiagramsConvertStereotype_Click()
    Dim ste As New ConvertStereotypeDisplayEvent

    On Error GoTo Handle

    'Rose must already be running
    Set roseApp = GetObject(, ROSE)

    'for this application, a Rose model must be open
    If = NULL_STRING Then
    End If

    'set which application is being invoked = "Convert Diagram Stereotypes"
    Set anEvent = ste
    Set ste = Nothing

    Exit Sub
    Dim anErrorHandler As New ErrorHandler
    Set anErrorHandler = Nothing
    End Sub

    The code from the VB Class that does most of the work follows:

    Option Explicit

    Private m_name As String
    Private m_curDiag As RoseDiagram

    Public Property Get name() As String
    name = m_name
    End Property

    Public Property Let name(n As String)
    m_name = n
    End Property

    Public Property Get curDiag() As RoseDiagram
    Set curDiag = m_curDiag
    End Property

    Public Property Set curDiag(d As RoseDiagram)
    Set m_curDiag = d
    End Property

    Private Sub Class_Terminate()
    name = NULL_STRING
    Set curDiag = Nothing
    End Sub

    Public Sub BackFromModelInterrupt()
    End Sub

    Public Sub OnUnloadModelInterruptForm()
    name = NULL_STRING
    Set curDiag = Nothing
    End Sub

    Public Sub Initiate()
    Set curDiag = roseApp.CurrentModel.GetActiveDiagram()

    If curDiag.GetPropertyClassName() CLASS_DIAGRAM Then
    frmModelInterrupt.Label1.Caption = "Please open the class diagram where the stereotype display is to be changed."
    Exit Sub
    End If

    Set curDiag = Nothing
    End Sub

    Public Sub ConvertDiagram()
    Dim i As Integer
    Dim aView As RoseItemView
    Dim views As RoseItemViewCollection

    Set views = curDiag.ItemViews

    For i = 1 To views.Count
    Set aView = views.GetAt(i)

    If aView.GetPropertyClassName = "Class" Then
    aView.StereotypeDisplay = STEREOTYPE_DISPLAY_NONE

    Select Case aView.Item.Stereotype
    Case "Abstract Class"
    With aView
    .FillColor.Red = 0
    .FillColor.Green = 255
    .FillColor.Blue = 0
    End With
    Case "In Process Class"
    With aView
    .FillColor.Red = 0
    .FillColor.Green = 255
    .FillColor.Blue = 0
    End With
    Case "Deprecated Class"
    With aView
    .FillColor.Red = 191
    .FillColor.Green = 191
    .FillColor.Blue = 191
    End With
    Case "Implemented Class"
    With aView
    .FillColor.Red = 0
    .FillColor.Green = 207
    .FillColor.Blue = 255
    End With
    Case "Modified Class"
    With aView
    .FillColor.Red = 255
    .FillColor.Green = 255
    .FillColor.Blue = 0
    End With
    Case "New Class"
    With aView
    .FillColor.Red = 255
    .FillColor.Green = 95
    .FillColor.Blue = 95
    End With
    End Select
    End If
    Next i

    Set aView = Nothing
    Set views = Nothing
    MsgBox "Done", vbOKOnly,
    End Sub

    Public Sub OnReturnFromModel()
    End Sub

    Here is some code from an REI script. I have not had a chance to add the functionality to the VB application. It retrieves information about associations based on the diagram they are in.

    'Source File: PrintDiagramAssociations.ebs
    'Compiled File: prtdgras.ebx
    ' This script produces a list of associations that belong to
    ' class diagrams in a selected category along with their RoseIDs.
    ' This list can be used in conjunction with the list of Logical
    ' View associations to determine the appropriate package to move
    ' the association to.
    ' Created by Alan Yezierski for Hartford Technology Services Company, LLC
    ' Last modified: May 1, 2000

    Sub ListDiagramAssociations (theCategory As Category)

    'The following three objects are OLE Automation objects.
    Dim ExcelApp As Object
    Dim WorkBook As Object
    Dim Worksheet As Object

    'If an error occurs, then go the Trap1 label
    On Error GoTo Trap1

    'Create an instance of Excel
    Set ExcelApp = CreateObject ("Excel.Application")
    'Make the Excel instance visible
    ExcelApp.Visible = TRUE
    'Add a workbook to the Excel instance
    Set WorkBook = ExcelApp.Application.Workbooks.Add
    'Add a worksheet
    Set Worksheet = WorkBook.WorkSheets.Add

    'Prefill the spreadsheet titles, make the font bold
    Worksheet.Cells (1,1).Value = "Diagram Category"
    Worksheet.Cells (1,2).Value = "Diagram Name"
    Worksheet.Cells (1,3).Value = "Association"
    Worksheet.Cells (1,4).Value = "Client Class"
    Worksheet.Cells (1,5).Value = "Client Category"
    Worksheet.Cells (1,6).Value = "Supplier Class"
    Worksheet.Cells (1,7).Value = "Supplier Category"
    Worksheet.Cells (1,8).Value = "RoseID"
    Worksheet.Cells (1,9).Value = "Association Category"

    Worksheet.Range("a1:i1").Font.Bold = TRUE
    'Underline the cells containing the titles with a double underline
    Worksheet.Range("a1:i1").Borders(4).LineStyle = 9

    'Name the worksheet
    Worksheet.Name = "Output Relations"

    'Entry of data begins in row 2
    Row% = 2

    Dim AllCDs As ClassDiagramCollection
    Set AllCDs = theCategory.ClassDiagrams
    Dim theDiagram As ClassDiagram
    Dim diagAssoc As AssociationCollection
    Dim anAssoc As Association

    For di = 1 To AllCDs.Count
    Set theDiagram = AllCDs.GetAt(di)
    Set diagAssoc = theDiagram.GetAssociations()
    For ai = 1 To diagAssoc.Count
    Set anAssoc = diagAssoc.GetAt(ai)
    Worksheet.Cells (Row%,1).Value = theCategory.Name
    Worksheet.Cells (Row%,2).Value = theDiagram.Name
    Worksheet.Cells (Row%,3).Value = anAssoc.Name
    Worksheet.Cells (Row%,4).Value = anAssoc.Role1.Class.Name
    Worksheet.Cells (Row%,5).Value = anAssoc.Role1.Class.ParentCategory.Name
    Worksheet.Cells (Row%,6).Value = anAssoc.Role2.Class.Name
    Worksheet.Cells (Row%,7).Value = anAssoc.Role2.Class.ParentCategory.Name
    Worksheet.Cells (Row%,8).Value = "'" & anAssoc.GetUniqueID()
    Worksheet.Cells (Row%,9).Value = anAssoc.ParentCategory.Name
    Row% = Row% + 1
    Next ai
    Next di

    'Set column width to 14 with wordwrap
    Worksheet.Columns("A:I").ColumnWidth = 14
    'Format all the cells so they word wrap
    Worksheet.Cells.WrapText = TRUE
    'Set orientation to landscape
    Worksheet.PageSetup.Orientation = 2
    Worksheet.PageSetup.CenterHeader = "Association Output"
    Worksheet.PageSetup.LeftFooter = "Proprietary And Confidential -- Hartford Life"
    Worksheet.PageSetup.CenterFooter = "Page &P"
    Worksheet.PageSetup.PrintGridlines = True
    'Set page printing order to over then down
    Worksheet.PageSetup.Order = 2
    Worksheet.PageSetup.PrintTitleColumns = Worksheet.Columns("A:B").Address
    Worksheet.PageSetup.PrintTitleRows = Worksheet.Rows("1").Address

    Workbook.SaveAs Filename := "diagasso"

    workBookName$ = WorkBook.FullName
    'display full path to report to user
    ExcelApp.Visible = FALSE
    Msgbox "Done. Your report is in: " + workBookName$

    Exit Sub

    MsgBox "Error!" + Err.Description
    Exit Sub

    End Sub

    Sub Main

    Dim theModel As Model
    Set theModel = RoseApp.CurrentModel
    Dim theCategory As Category
    Dim AllCategories As CategoryCollection
    Set AllCategories = theModel.GetAllCategories()
    Dim AllCategoryNames() As String

    ReDim AllCategoryNames(1 To AllCategories.Count)
    For ac = 1 To AllCategories.Count
    AllCategoryNames(ac) = AllCategories.GetAt(ac).Name & " " & AllCategories.GetAt(ac).GetUniqueID()
    Next ac

    ArraySort AllCategoryNames
    Dim catNames(1 To UBound(AllCategoryNames)) As String
    Dim catIDs(1 To UBound(AllCategoryNames)) As String

    For i = 1 To UBound(AllCategoryNames)
    catNames(i) = Left(AllCategoryNames(i), (Len(AllCategoryNames(i)) - 13))
    catIDs(i) = Right(AllCategoryNames(i), 12)
    Next i

    result% = SelectBox("Categories","Select Category for ClassDiagrams",catNames)
    If Not result = 0 Then
    Set theCategory = AllCategories.GetWithUniqueID(catIDS(result%))
    RoseApp.WriteErrorLog "Category = " & theCategory.Name
    Exit Sub
    End If

    RoseApp.WriteErrorLog ""
    RoseApp.WriteErrorLog "Class Diagram Association List"
    ListDiagramAssociations theCategory
    RoseApp.WriteErrorLog "Done"

    End Sub