Top Banner
Introduction to Rational Rose 98i v 5.2 Copyright 1993-1999 Rational Software, all rights reserved 1 Introduction to Rational Rose 98i Module 11: RoseScript
35
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 1

Introduction to Rational Rose 98i

Module 11: RoseScript

Page 2: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 2

Objectives: RoseScript

You will be able to: Understand RoseScript concepts Use RoseScript to:

• Obtain information from a Rose model• Add information to a Rose model• Update a Rose model• Integrate with third-party products

Page 3: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 3

What is RoseScript?

RoseScript is a full-featured Basic scripting language

It is a customized version of Summit Basic Script

Page 4: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 4

Uses of RoseScript Get information out of a model

Extract all or a portion to export Generate custom reports

Perform checks on the model Enforce project/enterprise semantic check Print to a log window

Create information to model Add and/or update model components Create and/or update interaction diagrams and/or

class diagrams Integrate with third-party tools

Page 5: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 5

How are Scripts Executed?

Interactive Executed via the Start button on the Scripting

Window toolbar Compiled

Saved as a file (.ebx) that may be executed Rose menu may be updated to include any

compiled script

Page 6: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 6

How are RoseScript Classes Used?

Each RoseScript class has a defined interface Properties and methods that are used to

access information in the Rose model Example

Application class property Visible

• Controls whether the Rose application is visible on the screen

Application class method OpenModel

• Opens a Rose model

Page 7: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 7

RoseApp

RoseApp is a global application object All RoseScript programs have access to this

object Example

RoseApp.CurrentModel is the currently open model in Rose

• It is an instance of the Model class RoseApp.Height is the height of the main

window RoseApp.CurrentModel.Save will save the

current open model

Page 8: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 8

RoseScripts and Color

Scripts appear in color

Keywords are in blueComments are in greenCode is in redEverything else is black

Page 9: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 9

Some “Basic” Terms

Add a comment ‘This is a comment

Declare a subroutineSub Name (arglist)

Terminate a subroutineEnd Sub or Exit Sub

Declare the main subroutine Main()

Declare a local variable along with its typeDim variable As type

Page 10: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 10

Some “Basic” Terms

Print data to an output devicePrint

Repeat a block of statements for each element in a collection or arrayFor Each member in group

[statements][Exit For][statements]

Next [member]

Page 11: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 11

Some “Basic” Terms Repeat a block of BasicScript statements while

a condition is True or until a condition is TrueDo {While | Until} condition statements Loop

ORDo

statementsLoop {While | Until} condition

ORDo

statementsLoop

Page 12: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 12

The Scripting Toolbar

CutAddWatchEndStartPaste

ProcedureStep

CallsToggleBreakpoint

Break

UndoCopy

SingleStep

Page 13: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 13

The Script Editor

Page 14: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 14

Debugging

Problems arehighlightedin gray

Page 15: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 15

Sample ScriptSub PrintPackage(theModel As Model)

Dim thePackage As CategoryDim PackageID As IntegerDim allPackages As CategoryCollection

Set allPackages = theModel.GetAllCategories()For PackageID = 1 To allPackages.Count

Set thePackage = allPackages.GetAt (PackageID)Print thePackage.Name

Next PackageIDEnd SubSub Main

Viewport.openPrintPackage RoseApp.CurrentModel

End Sub

Page 16: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 16

The Viewport

The Viewport window shows output of BasicScripts run from Rose

Viewport uses Print script output to the Viewport Print debug output to the Viewport

Page 17: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 17

Viewport Methods

Viewport.Clear Clear the open Viewport window

Viewport.Close Close an open Viewport window

Viewport.Open Opens a new Viewport window or switches the

focus to the existing Viewport window

Page 18: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 18

Scripting and Reports

Problem: How do you print out all of the names of all classes in the model?

Page 19: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 19

Print Package and Class Names Script

'The Main statement defines the subroutine where execution begins Sub Main

'Open the Viewport windowViewport.open'Pass the current open model to the DumpModel subroutine DumpModel RoseApp.CurrentModel

'End the subroutineEnd Sub

Page 20: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 20

Print Package and Class Names Script

'DumpModel subroutine expects a model as an argument

Sub DumpModel(theModel As Model)'Declare local variables AllPackages, thePackage and PackageIDDim AllPackages As CategoryCollection Dim thePackage As CategoryDim PackageID As Integer'Set the package collection to all the packages in the modelSet AllPackages = theModel.GetAllCategories()

Page 21: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 21

Print Package and Class Names Script

'Loop through the packagesFor PackageID = 1 to AllPackages.Count'Set the package

Set thePackage = AllPackages.GetAt(PackageID)'Call the DumpPackage subroutine

DumpPackage thePackage'Get the next packageNext PackageID

'End the subroutineEnd Sub

Page 22: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 22

Print Package and Class Names Script

'DumpPackage subroutine expects a package as an argument

Sub DumpPackage(thePackage As Category)'Declare local variables -- theClass and ClassID Dim theClass As ClassDim ClassID As Integer'Prints the name of the package to the Viewport

Print "Package"; thePackage.Name'Prints the number of classes in the package to the ViewportPrint "There are "; thePackage.Classes.Count; " classes"

Page 23: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 23

Print Package and Class Names Script

'Loop through the classes in the packageFor ClassID = 1 To thePackage.Classes.Count

'Set the next classSet theClass = thePackage.Classes.GetAt(ClassID)'Print the name of the class to the ViewportPrint theClass.Name'Set the next classNext ClassID'Print a blank line to the ViewportPrint " "

'End the subroutineEnd Sub

Page 24: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 24

Scripting and Business Problem Solutions

Problem: How can class names be changed? Enforce naming conventions Resolve name clashes Annotate classes to show distribution strategy

A script can be written to change the names of all classes in the model to enforce naming conventions

Page 25: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 25

Creating the Script

Starting point Browse existing scripts as opposed to creating

a new one from scratch Getting help

Rose Extensibility How to Start Scripting from Rose Rose Scripting Online Reference Basic Script Reference

Page 26: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 26

Script Steps

Declare the prefix to be added to the class name

Get the classes from the Model Object Iterate through ClassCollection Check to see that the prefix is not currently

part of the class name If prefix is not present

Assign name = prefix + name

Page 27: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 27

Needed Script Functions

Const Name [As type or type] = expression A statement that declares a constant for use

within the current script

• Const prefix$ As String = “Rose98”Set prefix$ to the string “Rose98”

Left$ (string, length) A function that returns a string that contains the

leftmost NumChars characters contained in the input string

Left$(string,1)

• Return the leftmost character in string

Page 28: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 28

The Prefix Script

' Definition of the prefixConst Prefix$ = "Rose98"

Sub Main' Declare local variablesDim theClasses As ClassCollectionDim theClass As Class

' Set the class collection to all the classes in the model

Set theClasses = RoseApp.CurrentModel.GetAllClasses ()

Page 29: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 29

The Prefix Script

' Loop through the classesFor ClsID = 1 To theClasses.Count

' Get the next classSet theClass = theClasses.GetAt (ClsID)' Get the first n characters of the class where n is equal to the number of characters in the prefix

ClassPrefix$ = Left$ (theClass.Name, Len (Prefix$))

Page 30: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 30

The Prefix Script

' If the prefix has not been used, add the prefix tothe name of the class If ClassPrefix$ <> Prefix$ Then

Viewport.OpenPrint "Renaming ";theClass.Name;" to ";theClass.Name = Prefix$ + theClass.NamePrint theClass.Name

End If' Get the next classNext ClsID

' Create a message box with the message = done!MsgBox "Done!"End Sub

Page 31: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 31

Scripting and Model Updates

Problem: How can packages and classes be added to a model A script can be written to add new packages

and classes to the model Steps

Create the new package Create the class contained in the new package Add the class to the package

Page 32: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 32

' This is the main subroutineSub Main

' Call the CreatePackage subroutineCreatePackage RoseApp.CurrentModel.RootCategory' Create a message box with the message = done!MsgBox "Done!"

End Sub

Script to Add a Package With One Class

Page 33: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 33

Script to Add a Package With One Class

' This subroutine will create a new packageSub CreatePackage (ParentCategory As Category)

' Declare local variablesDim thePackage As Category' Prompt the user to enter the name of the new packagePackageName$ = AskBox$ ("Enter the name for a new package.", "My New Package", "Create A Package")' Add the package to the logical view packageSet thePackage = ParentCategory.AddCategory (PackageName$)' Call the CreateAClass subroutineCreateAClass thePackage

End Sub

Page 34: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 34

Script to Add a Package With One Class' This subroutine will create a new classSub CreateAClass (ParentCategory As Category)

' Declare local variablesDim theClass As Class' Prompt the user to enter the name of the new classClassName$ = AskBox$ ("Enter the name for a new class."_

, "My New Class", "Create A Class")' Add the class to the new packageSet theClass = ParentCategory.AddClass (ClassName$)

End Sub

Page 35: 11script

Introduction to Rational Rose 98i v 5.2Copyright 1993-1999 Rational Software, all rights reserved 35

Exercise

Open the Registration model Run some of the scripts provided with Rose 98i

Modify the Add a Package With One Class script to create a package with a user defined number of classes Hint: Use an AskBox to determine the number

of classes and the name of the classes