Simulation Scripts

Last modified on January 31st, 2017.


The simulator has three JavaScript functions, shown below, which are called during execution and can be overridden by the user to run custom scripts. A script can have one or all three of these functions in addition to any other helper functions necessary.

Edit Script Dialog

Each of these functions executes at a different time during the course of a construct being executed. The "onStart" function is executed after all the 'Input/Output' triggers have completed but before any 'Resource' consumption and/or Asset utilization. The "prompt" function is executed after the 'Resource' consumption and/or Asset utilization but before the construct begins it's Duration. The "onEnd" function executes after the construct's Duration has ended.

Note: Any other functions must be called from within one of these three functions or they will not be executed by the simulator.

 

Predefined Scripts

'LOOP' and 'OR' constructs have a special 'Edit Script' dialog which gives the user the option to choose one of the predefined scripts in addition to the option to write a custom script. The available predefined scripts include "Probability (Loop)", "Resource (Loop)", and "Loop Iterations". These predefined scripts allow the user to quickly create a script for three of the most common use-cases without having to write any code.

 

Probability (Loop) Script

When added to a 'LOOP', shown below, this predefined script allows the user to assign the probability the 'LOOP' will continue or exit. When added to an 'OR', it allows the user to assign the probability that each branch of the 'OR' will be executed.

Edit Script Probability (Loop) Option

 

Resource (Loop) Script

When added to a 'LOOP', shown below, this predefined script allows the user to assign a condition on a 'Resource' which will cause the 'LOOP' to exit when the condition has been met. When added to an 'OR', it allows the user to assign a condition on a 'Resource' for each branch of the 'OR' so that when the condition has been met, that particular branch of the 'OR' will execute.

Edit Script Resource (Loop) Option

 

Loop Iterations Script

When added to a 'LOOP', shown below, this predefined script allows the user to assign exactly how many times the 'LOOP' will continue before it exits.

Edit Script Loop Iterations Option

 

Scripting Prompts

Prompts allow the simulator to get input directly from the user. A prompt will cause the simulator to wait until input from the user has been received. Prompts are used in 'OR' and 'LOOP' constructs if no scripts are found and the simulator is in "Prompt Mode". Prompts can be created by using one or more of these functions inside the "prompt" function. The title variable sets the title of the prompt. The name variable is the key that the response from the user will be stored under. The choices variable is the array of options for the user to choose from. The value variable is the default value entered in the prompt field. Below is a table of the types of prompts available to be scripted:

Function Description
addEnumInput(title, name, choices, value) Prompts user to choose from set of predefined choices.
addTextInput(title, name, value) Prompts user to enter text.
addBigtitleTextInput(title, name, value) Prompts user to enter a large amount of text.
addNumberInput(title, name, value) Prompts user to enter a number.
addBooleanInput(title, name, value) Prompts user to choose from True or False.
setTitle(title) Sets the title of the prompt.

The "onEnd" function will receive the response from a scripted prompt in the form of a map. Below is an example of a simple prompt script which prompts for the user's name and then prints their name from the response map to the web browser's console within the "onEnd" function:

Edit Script Prompt Example

 

Simulator Scripting API

The simulator has an scripting API to allow user to gain access to data contained in the model being executed. The table below identifies each of the functions available in the API and what they return:

Function Description
Sim.getEntitiesByNumber(number) Returns a list of entities based on entity's number.
Sim.getEntityByNumber(number) Returns the first entity result based on entity's number.
Sim.getEntitiesByName(name) Returns a list of entities based on entity's name.
Sim.getEntityByName(name) Returns the first entity result based on entity's name.
Sim.get(name) Short-hand alias for GetEntityByName.
Sim.getEntityById(id) Returns an entity based on entity's id.
Sim.setResourceByName(name, amount) Sets the resource's current amount based on resource's name.
Sim.setResourceById(id, amount) Sets the resource's current amount based on resource's id.
Sim.getResourceByName(name) Returns the first resource based on resource's name.
Sim.getAmount(name) Returns the resource amount based on on resource's name.
Sim.getAmountById(id) Returns the resource amount based on on resource's id.
Sim.getCurrentCost() Returns the current cost of the simulation.
Sim.getCurrentTime() Returns the current time of the simulation.
Sim.httpGet(url) Allows the user to do a synchronous GET request which will get data from an external server.
Sim.httpPost(url) Allows the user to do a synchronous POST request which will create data on an external server.
Sim.httpPut(url) Allows the user to do a synchronous PUT request which will update data on an external server.

Simulation Scripts

Last modified on January 31st, 2017. 


The simulator has three JavaScript functions, shown below, which are called during execution and can be overridden by the user to run custom scripts. A script can have one or all three of these functions in addition to any other helper functions necessary.

Edit Script Dialog

Each of these functions executes at a different time during the course of a construct being executed. The “onStart” function is executed after all the ‘Input/Output’ triggers have completed but before any ‘Resource’ consumption and/or Asset utilization. The “prompt” function is executed after the ‘Resource’ consumption and/or Asset utilization but before the construct begins it’s Duration. The “onEnd” function executes after the construct’s Duration has ended.

Note: Any other functions must be called from within one of these three functions or they will not be executed by the simulator.

 

Predefined Scripts

‘LOOP’ and ‘OR’ constructs have a special ‘Edit Script’ dialog which gives the user the option to choose one of the predefined scripts in addition to the option to write a custom script. The available predefined scripts include “Probability (Loop)”, “Resource (Loop)”, and “Loop Iterations”. These predefined scripts allow the user to quickly create a script for three of the most common use-cases without having to write any code.

 

Probability (Loop) Script

When added to a ‘LOOP’, shown below, this predefined script allows the user to assign the probability the ‘LOOP’ will continue or exit. When added to an ‘OR’, it allows the user to assign the probability that each branch of the ‘OR’ will be executed.

Edit Script Probability (Loop) Option

 

Resource (Loop) Script

When added to a ‘LOOP’, shown below, this predefined script allows the user to assign a condition on a ‘Resource’ which will cause the ‘LOOP’ to exit when the condition has been met. When added to an ‘OR’, it allows the user to assign a condition on a ‘Resource’ for each branch of the ‘OR’ so that when the condition has been met, that particular branch of the ‘OR’ will execute.

Edit Script Resource (Loop) Option

 

Loop Iterations Script

When added to a ‘LOOP’, shown below, this predefined script allows the user to assign exactly how many times the ‘LOOP’ will continue before it exits.

Edit Script Loop Iterations Option

 

Scripting Prompts

Prompts allow the simulator to get input directly from the user. A prompt will cause the simulator to wait until input from the user has been received. Prompts are used in ‘OR’ and ‘LOOP’ constructs if no scripts are found and the simulator is in “Prompt Mode”. Prompts can be created by using one or more of these functions inside the “prompt” function. The title variable sets the title of the prompt. The name variable is the key that the response from the user will be stored under. The choices variable is the array of options for the user to choose from. The value variable is the default value entered in the prompt field. Below is a table of the types of prompts available to be scripted:

Function Description
addEnumInput(title, name, choices, value) Prompts user to choose from set of predefined choices.
addTextInput(title, name, value) Prompts user to enter text.
addBigtitleTextInput(title, name, value) Prompts user to enter a large amount of text.
addNumberInput(title, name, value) Prompts user to enter a number.
addBooleanInput(title, name, value) Prompts user to choose from True or False.
setTitle(title) Sets the title of the prompt.

The “onEnd” function will receive the response from a scripted prompt in the form of a map. Below is an example of a simple prompt script which prompts for the user’s name and then prints their name from the response map to the web browser’s console within the “onEnd” function:

Edit Script Prompt Example

 

Simulator Scripting API

The simulator has an scripting API to allow user to gain access to data contained in the model being executed. The table below identifies each of the functions available in the API and what they return:

Function Description
Sim.getEntitiesByNumber(number) Returns a list of entities based on entity’s number.
Sim.getEntityByNumber(number) Returns the first entity result based on entity’s number.
Sim.getEntitiesByName(name) Returns a list of entities based on entity’s name.
Sim.getEntityByName(name) Returns the first entity result based on entity’s name.
Sim.get(name) Short-hand alias for GetEntityByName.
Sim.getEntityById(id) Returns an entity based on entity’s id.
Sim.setResourceByName(name, amount) Sets the resource’s current amount based on resource’s name.
Sim.setResourceById(id, amount) Sets the resource’s current amount based on resource’s id.
Sim.getResourceByName(name) Returns the first resource based on resource’s name.
Sim.getAmount(name) Returns the resource amount based on on resource’s name.
Sim.getAmountById(id) Returns the resource amount based on on resource’s id.
Sim.getCurrentCost() Returns the current cost of the simulation.
Sim.getCurrentTime() Returns the current time of the simulation.
Sim.httpGet(url) Allows the user to do a synchronous GET request which will get data from an external server.
Sim.httpPost(url) Allows the user to do a synchronous POST request which will create data on an external server.
Sim.httpPut(url) Allows the user to do a synchronous PUT request which will update data on an external server.