Innoslate's Simulators have three JavaScript functions, which are called during execution and can be overridden by the user to run custom scripts.
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.
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 been 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 its 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 “Loop Probability,” “Loop Iterations,” and “Loop Resource.” 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.
-
Loop Probability 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.
-
Loop Iterations Script
When added to a ‘LOOP,’ this predefined script allows the user to assign exactly how many times the ‘LOOP’ will continue before it exits.
-
Loop Resource 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.
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 the user to choose from a set of predefined choices. |
addTextInput(title, name, value) | Prompts the user to enter text. |
addBigtitleTextInput(title, name, value) | Prompts the user to enter a large amount of text. |
addNumberInput(title, name, value) | Prompts the user to enter a number. |
addBooleanInput(title, name, value) | Prompts the 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 that 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:
Simulator Scripting API
The simulator has a scripting API to allow users 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 the entity’s number. |
Sim.getEntityByNumber(number) | Returns the first entity result based on the entity’s number. |
Sim.getEntitiesByName(name) | Returns a list of entities based on the entity’s name. |
Sim.getEntityByName(name) | Returns the first entity result based on the entity’s name. |
Sim.get(name) | Short-hand alias for GetEntityByName. |
Sim.getEntityById(id) | Returns an entity based on its id. |
Sim.setResourceByName(name, amount) | Sets the resource’s current amount based on the resource’s name. |
Sim.setResourceById(id, amount) | Sets the resource’s current amount based on the resource’s id. |
Sim.getResourceByName(name) | Returns the first resource based on its name. |
Sim.getAmount(name) | Returns the resource amount based on the resource’s name. |
Sim.getAmountById(id) | Returns the resource amount based on the resource’s id. |
Sim.getCurrentCost() | Returns the current cost of the simulation. |
Sim.getIOValueByNumber() | Returns the Input/Output value attribute based on entity number. |
Sim.getIOValueById() | Returns the Input/Output value attribute based on entity id. |
Sim.getIOValueByGlobalId() | Returns the Input/Output value attribute based on entity global id. |
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. |