Writing Your Own JSR-Compliant, Domain-Specific Scripting Language
Subject Specific Training: Scripting September 2008.
-
Upload
dayna-adams -
Category
Documents
-
view
213 -
download
0
Transcript of Subject Specific Training: Scripting September 2008.
Subject Specific Training:Subject Specific Training:ScriptingScripting
September 2008
Learning ObjectiveLearning Objective
• Upon completing this training, the learner will be able to identify the components of a script; explain the process for writing a script including its application in Supply Chain Guru; and develop a simple script, test the script and review results.
AgendaAgenda
• Scripting Basics
• Scripts in Supply Chain Guru
• Script Writing
• Testing the Script
• Check on Learning
• Capstone Exercise
MaterialsMaterials
• Supply Chain Guru installed on computer with proper licenses
• Scripting Training Slides
• Script Types Quick Reference Sheet
• Model Object Dictionary (Excel Version)
Scripting BasicsScripting Basics
• Why do you need to script?
• Simulation Engine
• Model Objects
• Model Variables
Why Do We Script?Why Do We Script?
• Create more detailed models– Real world behavior does not fit in any of the
“pre-defined” behaviors
• Add realistic scenarios
• Perform detailed risk analysis
SimServerSimServer
• External Simulation Engine
• Reads the Model Configuration File (MCF)
• Simulation results loaded into SCG tables
• Ability to use animation
Scripting Basics: Model Scripting Basics: Model ObjectsObjects
• Can be viewed as an independent little machine with a distinct role
• The actions or “operators” on the objects are closely associated with the object
• The data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or "class")
• Characteristics (its attributes, fields or properties)
• Behaviors (the things it can do, or methods, operations or features)
Supply Chain Guru ObjectsSupply Chain Guru Objects
Model
Products Customers Sites Lanes Assets Processes BillofMaterials
ProductSource ProductDestination Customer Product See Next Slide Modes AssetUnits Shipments
ShippingRoute
Location
Waypoints
BOMItems
Site ObjectSite Object
Sites
SiteProduct WorkCenter WorkResource WorkProcess Order ShippingItem
ProductSource ProcessLot WorkStep ProcessLot ProcessRouting
ActivityResource
OrderDetail ShippingItemDetail ShippingItemList
Scripting Basics: VariablesScripting Basics: Variables• Definition: a value that can change, depending on conditions or on
information passed to the program • Variable Scopes
– Model Variables• Permanent
– Cannot be created in script– Sites, Customers, Products
• Dynamic– Change during the simulation
• Exist in Collections– Group of objects that should be kept together
– Global Variables• Cannot be declared in script• Use the global variables table to declare variables
– Local Variables• Pertain only to the executing script
– Context Variables• Contains variables and objects that depend upon the context of the
script
Scripting Basics: Script TypesScripting Basics: Script Types
• Mode Selection Rule• Process Routing Rule• Queue Selection Rule• Sourcing Policy• Make Policy• Inventory Policy• On Simulation
Begins/ Ends
• On Receive Order Begins/ Ends
• On Receive Shipment Begins/ Ends
• Asset Unit Selection Rule
• Transportation Costs Basis Rule
Using Scripts in Supply Chain Using Scripts in Supply Chain GuruGuruTMTM
• Scripts Table
• Global Variables Table
• Debug Level
• Practical Exercise
Using Scripts: Scripts TableUsing Scripts: Scripts Table
• Name• Script• Script Type• Time First• Occurrences• Interval
Scripts Table: Script TypeScripts Table: Script Type
On Simulation EndsOn Receive Order BeginsOn Receive Order EndsOn Receive Shipment BeginsOn Receive Shipment EndsAsset Unit Selection RuleTransportation Costs Basis Rule
Mode Selection RuleProcess Routing RuleQueue Selection RuleSourcing PolicyMake PolicyInventory PolicyOn Simulation Begins
See The Script Types Quick Reference Card for more information
Using Scripts: Global Using Scripts: Global Variables TableVariables Table
• Name• Type• Value• Dimensions • Notes
DebugDebug
• The Debug statement is the most detailed debugging command available. During the simulation run, when a Debug statement is encountered, the model will stop execution and begin to run in a line-by-line serial execution mode, displaying the specific model code being executed at each line.
Setting the Debug LevelSetting the Debug Level
• The model must be set at an equal or higher debug level than what is called in script
• Accessible in Model Options, Simulation Tab
Using Scripts: Practical ExerciseUsing Scripts: Practical Exercise
• Copy a script which will permanently close a site – Determine the steps to close the site– Tell SimServer what to do– Run the Simulation and view the results
Practical Exercise (1)Practical Exercise (1)1. Open the Model:TrainingExercise1_BaseModel_SiteClosedScript
2. In Word or Notepad, write the steps needed to close DC_2 after the second week of the simulation
Practical Exercise (2)Practical Exercise (2)
3. Tell SimServer how to perform each stepHint: Use “//” to keep SimServer from reading your comments
4. Copy to the Script Table
Practical Exercise (3)Practical Exercise (3)• Set the script to run at day 14
• Run the Simulation• View the Script Log
Practical Exercise (4)Practical Exercise (4)
• View Simulation Outputs
Script WritingScript Writing
• 3 Steps
• Scripting Language
• Common Problems
3 Steps to for Script Writing3 Steps to for Script Writing
1. Determine objective
2. Pseudocode: write down each step in list form
3. Tell the computer what to do
Step 1: Determine Script Step 1: Determine Script ObjectiveObjective
• What are you trying to do?– Mode Selection vs. Price Adjustments– Risk Analysis– Refer to the Script Type Quick Reference– What is the context variable?
• Map back to Model Object– Model Object Dictionary– Scripting Quick Reference Chart
Step 2: Write down each stepStep 2: Write down each step
• Use notepad or Microsoft Word
• List each and every event from start to end
• Use simple language, write as you speak
• Recognize if you will need to loop, make a decision, or use a counter during the script execution
Step 3: Tell the computerStep 3: Tell the computer
• Tools– Scripts Table– Model Object Dictionary– Model Object Quick Reference Guide– Script Log
What to do?
When to do it?
When to Stop?
How Often?
Tell the Computer: Sequential Tell the Computer: Sequential InstructionsInstructions
1. Define variables to solve the objective (Start)
2. Devise a method to create a solution (Body)
3. Determine how to put the solution back into the model (End)
Start: Defining VariablesStart: Defining Variables
• Declare A Variable/ Name the Variable – Name Space
• Can contain only alphanumeric and the underscore character
• Must begin with a letter• NOT case sensitive• Do not use reserved words
– All language keywords– All model collections– True and False
• Context namespace– Contains variables and objects
that depend upon the context of the script
• Examples: nDate, nDemand, nSite
• Data Types– Integer– Real– Boolean– String– Date– Object
“Declare Variable As Data Type”
Start: Setting VariablesStart: Setting Variables
• Set your variable to an initial value or expression– Must match the data type that you declared it
to be – Can use Operators (+, -, /, *)– Model Object
• Use Model Object Dictionary
“Set Variable = ”
Body: MethodsBody: Methods• Looping Instructions
– For Next• Repeats a block of script a specific number of timesFor index = intialValue To expression
BlockNext
– For Each … Next– While Wend– Do Until
• Decision Statements– If …Then– If … Then, Else– If … Then, Elseif, Else, End If
• Other Statements– Break: Exits the immediately – Continue: Jumps to the next loop iteration bypassing any further block
statements– Return: Goes back to the start of the loop
Body: Methods (2)Body: Methods (2)
• Built In Functions– Math, String, Date, Debug– Specify namesake prefix and enclose arguments in
parentheses– Examples:SET Cost = Math.Min(Global.MinCost, Local.ActualCost)
The cost will be the smaller cost of the Global and Local variables
SET Elapsed = Date.DateDiff(Global.Now(), Model.StartTime, “s”)
Determines the elapsed time between the start of the model run and the current time in seconds
End the ScriptEnd the Script
• CALL– Example:
CALL Debug.Assert(Not ISNull(THESITE))
Ensures that this site is not empty
• Assert
• Write to Script Log
Testing the ScriptTesting the Script• Script Log
– Run Simulation– Open Notepad– Open the Script log
• Location- In Model Folder• Debug statements help determine script correctness
– Debugging a Script• Syntax Errors
– Misspelling or omitting a command• Run time Errors
– Script runs into unexpected error (such as declaring a name an integer)
• Logic Errors– Typically prevent script and/ or Sim Server from running – Outputs do not make sense
Scripting LanguageScripting Language
• Comments //
• Need to add more here
Common Scripting IssuesCommon Scripting Issues
• Need to add more here
Check on LearningCheck on Learning• Identify components of a script• Explain process for writing a
script• Apply a script to SCG• Develop and test a script• Review results• Explain reasons for scripting• State when you need a script• Define model objects and
variables• Identify key tables for scripting• Select the debug level for a
model
• Plan how to write a script• Define key script writing terms• Apply steps to write a script• Practice writing a script• Open the Scripts table• Write a script• Run a simulation which calls a
script• Perform a DEBUG on a script• Identify simulation outputs and
locations of logs
Capstone ExerciseCapstone Exercise
Create a ModelCreate a Model
• Save model as: Training_Script_A• 1 Product• 3 CZ, 1 MFG• Weekly demand of 10 by each CZ• Each CZ can be sourced from each MFG• LTL Truckload Policy• 1 Asset Types:
– Truck_A: Preferred asset, only available on Mondays– Truck_B: More expensive, runs everyday
Custom Mode Selection ScriptCustom Mode Selection Script
1. Write a custom Mode Selection Script which will ship via Truck_A on Mondays, and via Truck_B at all other times.
2. Run the simulation.
3. Review outputs and analyze model.
QuestionsQuestions