Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using Alf with Cameo Simulation Toolkit - Part 1: Basics

249 views

Published on

Alf is an OMG-standard textual language with a familiar Java-like syntax, but it is designed specifically to be used in the context of graphical, executable UML and SysML models. This language is available for use in No Magic's Cameo Simulation Toolkit (CST) v18.5 to write expressions and behaviors in executable UML. Behind the scenes, Alf is actually compiled into UML activity models, which are then automatically integrated into your overall model, so they can be seamlessly executed using CST. This presentation gives an introduction to the Alf language, a tutorial on how to use it in CST and hands-on exercises to try yourself. You will be amazed at how much quicker and easier it is to write three lines of Alf code, rather than create by hand an equivalent activity model of 20 or more elements!

Published in: Software
  • Be the first to comment

  • Be the first to like this

Using Alf with Cameo Simulation Toolkit - Part 1: Basics

  1. 1. No Magic World Symposium, Allen TX Ed Seidewitz Model Driven Solutions, Inc. ● http://www.modeldriven.com ed-s@modeldriven.com ● @seidewitz Copyright © 2018 Ed Seidewitz Using the Alf Action Language with Cameo Simulation Toolkit Part 1 – Basics
  2. 2. Page 2 About me Ed Seidewitz Chief Technology Officier, Model Driven Solutions, Inc. ed-s@modeldriven.com ● @seidewitz • 30 years experience as a project manager, architect, developer and modeler • Involved with UML since version 0.8 • Developer of fUML and Alf Reference Implementations and MagicDraw Alf Plugin • Chair of the OMG fUML and Alf Revision Task Forces; member of the UML Revision Task Force Copyright © 2018 Ed Seidewitz
  3. 3. Page 3 Goals Part 1 – Basics (Today) • Learn the basics of the Alf action language for Executable UML. • Use the Alf Plugin for MagicDraw. • Practice using Alf with Cameo Simulation Toolkit. Part 2 – Modeling (Wednesday) • Model simulations using the Alf action language. • Use Alf in class and state machine models in MagicDraw. • Create and run simulations using Alf with Cameo Simulation Toolkit. Copyright © 2018 Ed Seidewitz
  4. 4. Page 4 Prerequisites (for Part 1) • Participant – Basic knowledge of class, activity and state machine modeling using MagicDraw – Some experience with model execution using Cameo Simulation Toolkit – General familiarity with programming/scripting (particularly in a language like C++, Java, JavaScript, etc.) • System (for hands-on exercises) – MagicDraw 18.5 – Cameo Simulation Toolkit 18.5sp3 – Alf plugin 18.5 Install MagicDraw, CST and Alf Plugin on line or from USB stick Copyright © 2018 Ed Seidewitz
  5. 5. Page 5 5 Installing the Alf plugin Copyright © 2018 Ed Seidewitz Plugin documentation is available at: https://docs.nomagic.com/display/ALFP185/Alf+plugin Under Plugins (commercial), download / install the Alf plugin v18.5. Select Help ► Resource/Plugin Manager to open the Resource/ Plugin Manager window.  A new version will be available for the 19.0 release. Or click here to install from USB stick.
  6. 6. Page 6 Background Copyright © 2018 Ed Seidewitz
  7. 7. Page 7 Executable UML Executable UML is a (growing) subset of standard UML that can be used to define, in an executable, operational style, the structural and behavioral semantics of systems. • Foundational UML (structural and activity models) – http://www.omg.org/spec/FUML • Precise Semantics of UML Composite Structure (PSCS) – http://www.omg.org/spec/PSCS • Precise Semantics of UML State Machines (PSSM) – http://www.omg.org/spec/PSSM Copyright © 2018 Ed Seidewitz • Action Language for Foundational UML (Alf) – http://www.omg.org/spec/ALF A textual surface representation for UML modeling elements with the primary purpose of acting as the surface notation for specifying executable (fUML) behaviors within an overall graphical UML model. Alf Plugin
  8. 8. Page 8 Why an action language? • Graphical notations are good for… Copyright © 2018 Ed Seidewitz Structural models High-level behavioral models
  9. 9. Page 9 Why an action language? • …but not so good for detailed behavior Copyright © 2018 Ed Seidewitz Full executability requires complete specification of behavior and computation. This is often much more easy to specify using a textual notation.
  10. 10. Page 10 Why not just use a scripting language? • Scripting language: No standard syntactic or semantic integration with UML • Alf: Full, standardized syntactic and semantic integration with UML Copyright © 2018 Ed Seidewitz this.lineItems->remove(item) this.totalAmount = Subtract(this.totalAmount, item.amount) ALH.removeValue(self, "lineItems", item); arguments = ALH.createList(); arguments.add(ALH.getValue(self, "totalAmount")); arguments.add(ALH.getValue(item, "amount”)); ALH.setValue(self, "totalAmount", ALH.callBehavior("Subtract", arguments)); Example using the MagicDraw- specific Action Language Helper API for JavaScript.
  11. 11. Page 11 The basic idea: Alf maps to fUML Copyright © 2018 Ed Seidewitz activity DoSomething(in input: Integer, out output Integer): Integer { output = A(input); return B(); } Alf behavioral notation maps to fUML activity models. The semantics of the Alf notation is defined by its mapping to fUML
  12. 12. Page 12 Hands On Hello World Copyright © 2018 Ed Seidewitz
  13. 13. Page 13 Create an Alf project Copyright © 2018 Ed Seidewitz In the Alf folder, select the Alf template.  The Alf template automatically loads the Alf Library model and sets Alf as the default language for opaque behaviors, actions and expressions. Under Other, select Project from Template. Select File ► New Project to open the project creation window. Create a Hello World project.
  14. 14. Page 14 Create the Hello World activity Copyright © 2018 Ed Seidewitz Create a new Activity. Enter the Alf code in the Alf editor window. When the code is correct, click Save.  If the Alf editor window is closed, you can open it using Window ► Alf.
  15. 15. Page 15 Executing the activity Copyright © 2018 Ed Seidewitz Right click on the Activity and select Simulation ► Run. Set Animation Speed to the highest level… …and click here to run. Output appears in the console pane.
  16. 16. Page 16 Basic Concepts Copyright © 2018 Ed Seidewitz
  17. 17. Page 17 Assignment as data flow Copyright © 2018 Ed Seidewitz a = +1; b = -a; a = A(a) + B(b); Local names map to forked object flows. Subexpressions are evaluated concurrently. A re-assigned local name actually maps to a new flow.  The literal “1” has type Natural. The expression “+1” has type Integer. The expression “A(a) + B(b)” has type Integer, which is not compatible with Natural. The local name a implicitly gets the type Integer. Statements map to structured activity nodes with control flows to enforce sequential execution. a = 1; a = A(a); ✗ type conformance
  18. 18. Page 18 Using Alf for behaviors Copyright © 2018 Ed Seidewitz lineItem = new LineItem(product, quantity); this.lineItems->add(lineItem); this.totalAmount = this.totalAmount + lineItem.amount; Method of an operation this.lineItems = checkOut.items; Customer_Order.createLink(checkOut.customer, this); this.datePlace = CurrentDate(); this.totalAmount = lineItems.amount->reduce '+'; this.SubmitCharge(checkOut.card); Behavior on a state machine battFrac = battCond / this.maxBattLevel; gThrottle = Max(accelPos * (1-battFrac), this.maxGThrottle); eThrottle = Max(accelPos * battFrac, this.maxEThrottle); Body of an action
  19. 19. Page 19 Using Alf for expressions Copyright © 2018 Ed Seidewitz Activity Edge Guards State Machine Transition Guards
  20. 20. Page 20 Hands On Stopwatch Copyright © 2018 Ed Seidewitz
  21. 21. Page 21 Open the StopWatch sample project Copyright © 2018 Ed Seidewitz … Click Samples on the Welcome Screen Under Simulation, choose the StopWatch sample project.  Select File ► Save Project As… to save a local copy of the project before continuing.
  22. 22. Page 22 Setup the project for Alf Copyright © 2018 Ed Seidewitz Remove the existing Project Usage for fUML_Library.  This part will not be necessary in v19.0. Select Tools ► Alf ► Load Library to load the Alf Library.
  23. 23. Page 23 Open the StopWatch state machine Copyright © 2018 Ed Seidewitz
  24. 24. Page 24 Replace the ready state behavior Copyright © 2018 Ed Seidewitz Open the Specification window for the ready state. Under Entry, change the Behavior Type to Opaque Behavior.  Be sure to select the ready state as a whole, not just the line for the entry behavior.
  25. 25. Page 25 Replace the reset timer activity with Alf code Copyright © 2018 Ed Seidewitz  Be sure to click on the text for the entry behavior, not the entire state. Click on the entry behavior… replaced by  The use of the prefix this is required to access an attribute value in Alf. …and enter the code into the Alf editor window.
  26. 26. Page 26 Show the Alf code on the state machine diagram Copyright © 2018 Ed Seidewitz Open the Symbol Properties window for the ready state. Set the Opaque Behavior Display Mode property to Body.
  27. 27. Page 27 Coming in v19.0! Copyright © 2018 Ed Seidewitz Syntax-aware editing of Alf code in opaque body editors.
  28. 28. Page 28 Replace the Increase time activity with Alf code Copyright © 2018 Ed Seidewitz replaced by  The ++ operator increments its argument.
  29. 29. Page 29 Executing the StopWatch model Copyright © 2018 Ed Seidewitz Right click on the StopWatch class and select Simulation ► Run. Start the simulation, then trigger the start signal. Output is displayed in the console tab. The current state machine configuration is animated.
  30. 30. Page 30 Sequences Copyright © 2018 Ed Seidewitz
  31. 31. Page 31 Sequences Copyright © 2018 Ed Seidewitz activity GetReadings(in sensors: Sensor[*]): Integer[*] { readings = sensors->collect sensor (sensor.reading); return readings; } The input parameter has an unordered set of values (by default). Object flows always carry ordered sequences of values. Values are handled one by one within the expansion region. The read actions happen concurrently. The result is a sequence ordered respective to the input sequence. The return parameter gets an unordered set of values (by default).  The Alf on the left could be written more simply as: return sensors.reading; The local name reading implicitly gets the type Integer and the multiplicity [0..*].  Arbitrary sequences cannot be assigned to local names with multiplicity [0..1]. reading = -1; readings = reading; reading = readings; Implicitly gets the multiplicity [0..1] A single value is really just a sequence of length 1. ✗ multiplicity conformance
  32. 32. Page 32 Null as the empty sequence Copyright © 2018 Ed Seidewitz A LiteralNull is intended to be used to explicitly model the lack of a value. In the context of a MultiplicityElement with a multiplicity lower bound of 0, this corresponds to the empty set (i.e., a set of no values). It is equivalent to specifying no values for the Element. null = any [ ] { } From the UML 2.5 specification (clause 8.2.3): The Alf interpretation: null is the (untyped) empty sequence sensors = null; sensors.readings; WriteLine(null); WriteLine(name ?? "no name"); “null” can be assigned to any target with a multiplicity lower bound of 0. This is not an error. It is equivalent to sensors->collect sensor (sensor.reading); which evaluates to “null”.  An argument for a parameter of multiplicity 1..1 cannot be null. ✗ multiplicity conformance A null-coalescing expression can be used to provide a non-null “default value”.
  33. 33. Page 33 Hands On Reversing a Sequence Copyright © 2018 Ed Seidewitz
  34. 34. Page 34 Create the Reverse project Copyright © 2018 Ed Seidewitz Create a new project using the Alf template, as before.
  35. 35. Page 35 Create the Reverse activity diagram Copyright © 2018 Ed Seidewitz Create a new Activity Diagram.
  36. 36. Page 36 Add activity parameter nodes Copyright © 2018 Ed Seidewitz Leave the parameters untyped.
  37. 37. Page 37 Add Alf code in opaque actions Copyright © 2018 Ed Seidewitz Click on the opaque action to enter code in the Alf editor window. The CompiledRepresentation stereotype records the executable activity compiled from the code. Click on the opaque action and select Symbol Properties to turn off stereotype display. Click here to make this the default. Select Expert mode, if necessary.  If you are using SysML, be careful to us the UML Boolean type, not the SysML Boolean type.
  38. 38. Page 38 Fill in the activity model Copyright © 2018 Ed Seidewitz Using an expression here is a shorthand for the statement result = seq->isEmpty(); Alf in an Opaque Action can access pins as local names.  Guards must be entered as Literal Specifications.
  39. 39. Page 39 Coming in v19.0! Copyright © 2018 Ed Seidewitz Alf activity edge guards using decision input values.
  40. 40. Page 40 Write a TestReverse activity Copyright © 2018 Ed Seidewitz Iterative for loop Sequence construction expression Cast expression Null-coalescing expression  The cast expression is required because the result of Reverse is untyped. The null-coalescing expression is required because a cast can result in null.
  41. 41. Page 41 Testing the Reverse activity Copyright © 2018 Ed Seidewitz Right click on TestReverse and select Simulation ► Run. Click here to execute. Make sure the slider is all the way to the right! This is the expected result.
  42. 42. Page 42 Bonus: Writing Reverse entirely in Alf Copyright © 2018 Ed Seidewitz activity RecursiveReverse(in seq : any[*] sequence) : any[*] sequence { return seq->isEmpty()? null: seq->excludeAt(1)->RecursiveReverse()->including(seq[1]); } activity IterativeReverse(in seq : any[*] sequence) : any[*] sequence { result = null; n = seq->size(); for (i in 0..n-1) { result->add(seq[n-i]); } return result; } The arrow notation (sequence operation expression) can be used with any behavior that has a sequence as its first parameter. Conditional-test expression Remember that null is the empty sequence. add is an “in place” collection function.

×