Scenario - ProcessBuilder
Scenario.ProcessBuilderA builder to define a process for simulation.
Example
const cash = new ResourceBuilder("cash")
.mapFromValue(1.0)
.assert({
predicate: x => GreaterEqual(x, 0),
message: "Cash must be positive",
});
const sales = new ProcessBuilder("sales")
.resource(cash)
.value("amount", FloatType)
.set("cash", (props, resources) => Add(resources.cash, props.amount))
.mapFromValue({ date: new Date(0), amount: 42.0, });
const scenario = new ScenarioBuilder('my_scenario')
.resource(cash)
.process(sales);
Type parameters
Name | Type |
---|---|
Name | extends string |
Values | extends Record = |
Properties | extends Record = { date : Variable } |
Resources | extends Record = |
Processes | extends Record <string , Record > = |
MLs | extends Record = |
Procs | extends Record = |
Hierarchy
-
AbstractProcessBuilder
↳
ProcessBuilder
Function
clear
▸ clear(collection
):
ProcessBuilder
Delete all elements from an array, set or dictionary. The collection will be mutated in place.
Parameters
Name | Type | Description |
---|---|---|
collection | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction <SetType | ArrayType | DictType > | a function returning the array, set or dictionary to clear |
Returns
ProcessBuilder
Example
// create a resource for inventory items
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]))
// add the inventory resource and clear all values
const dump_inventory = new ProcessBuilder("dump_inventory")
.resource(inventory)
.clear((props, resources) => resources.inventory)
.mapFromValue({
date: new Date(0),
});
Process
constructor
• new ProcessBuilder(name
, module?
):
ProcessBuilder
Construct a process with a given name.
Type parameters
Name | Type |
---|---|
Name | extends string |
Values | extends Record = |
Properties | extends Record = { date : Variable } |
Resources | extends Record = |
Processes | extends Record <string , Record > = |
MLs | extends Record = |
Procs | extends Record = |
Parameters
Name | Type | Description |
---|---|---|
name | Name | the name for the ProcessBuilder |
module? | ModulePath | ModuleBuilder | - |
Returns
ProcessBuilder
Example
const cash = new ResourceBuilder("cash")
.mapFromValue(1.0)
.assert({
predicate: x => GreaterEqual(x, 0),
message: "Cash must be positive",
});
const sales = new ProcessBuilder("sales")
.resource(cash)
.value("amount", FloatType)
.set("cash", (props, resources) => Add(resources.cash, props.amount))
.mapFromValue({ date: new Date(0), amount: 42.0, });
const scenario = new ScenarioBuilder('my_scenario')
.resource(cash)
.process(sales);
Overrides
AbstractProcessBuilder<Name, Values, Properties, Resources, MLs, Procs>.constructor
assign
▸ assign(name
, expression
):
ProcessBuilder
Reassign an existing property value with the result of an expression of existing properties.
Type parameters
Name | Type |
---|---|
PropertyName | extends string | number | symbol |
Expr | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => { ast_type : AstType ; type : SubType } |
Parameters
Name | Type | Description |
---|---|---|
name | PropertyName | the name of the property |
expression | Expr | the function of defining the new property value |
Returns
ProcessBuilder
Remarks
You must overwrite an existing property, and the value must be of the same type as that property. The "date"
property cannot be modified.
Example
const my_process = new ProcessBuilder("my_process")
// let x = 5
.let("x", () => Const(5))
// x = x + 1
.assign("x", props => Add(props.x, 1))
delete
▸ delete(collection
, key
):
ProcessBuilder
Delete an existing element from an array, set or dictionary. The collection will be mutated in place.
For arrays, you can delete the "first"
or "last"
element (i.e. pop the front or end of the array).
For sets, you can delete an existing key. If the key doesn't exist an error will result.
For dictionaries, you can delete an existing key (and associated value). If the key doesn't exist an error will result.
Type parameters
Name | Type |
---|---|
C | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | C | a function returning the array, set or dictionary to update |
key | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction <ReturnType ["type" ]["value" ]["key" ]> | the key of the item to delete |
Returns
ProcessBuilder
Example
// create a resource
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]))
// unlist an item from inventory
const unlist = new ProcessBuilder("unlist")
.resource(inventory)
.value("qty", IntegerType)
.value("item", StringType)
.delete(
(props, resources) => resources.inventory,
(props) => props.item,
)
.mapFromValue({
date: new Date(0),
item: "socks",
});
describe
▸ describe(description
):
ProcessBuilder
Add a human-readable description of the process (as a whole).
Parameters
Name | Type | Description |
---|---|---|
description | string | the description string. |
Returns
ProcessBuilder
Example
// create the sales process with some properties
const sales = new ProcessBuilder("sales")
.describe("Process a sale event, determining the profit from revenue and cost")
.let("revenue", () => Const(5))
.let("cost", () => Const(6))
.let("profit", (props) => Subtract(props.revenue, props.cost))
.mapFromValue({ date: new Date(0) });
endSimulation
▸ endSimulation(predicate?
):
ProcessBuilder
End the entire simulation immediately. A user-defined predicate may be provided to control when this does or does not occur.
Parameters
Name | Type | Description |
---|---|---|
predicate? | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | a function if set to true will end the entire simulation |
Returns
ProcessBuilder
Remarks
Any later statements in this process instance will not be executed.
Example
// create the sale process
const sales = new ProcessBuilder("sales")
// create another sale
.execute(
"sales",
(props) => Struct({
date: AddDuration(
props.date,
RandomExponential(),
'hour'
),
})
)
// end after an hour
.endSimulation((props) => Greater(props.date, new Date(3600000)))
// start simulating from a date
.mapFromValue({ date: new Date(0) });
error
▸ error(message
):
ProcessBuilder
Throw an error with a provided error message.
Parameters
Name | Type | Description |
---|---|---|
message | string | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | the error message |
Returns
ProcessBuilder
Example
// create the sales process with some properties
const sales = new ProcessBuilder("sales")
// create random qty
.let("qty", () => RandomUniform(0, 1))
.if(
(props) => Less(props.qty, 0.5),
block => block
.error((props) => StringJoin`value ${props.qty} <= 0.5`)
)
.mapFromValue({ date: new Date(0) });
execute
▸ execute(name
, values
):
ProcessBuilder
Execute a new process instance at a specified date.
Type parameters
Name | Type |
---|---|
V | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction <StructType <{ date : DateTimeType } & { [K in string | number | symbol]: EastType }>> |
Parameters
Name | Type | Description |
---|---|---|
name | Name | the ProcessBuilder name to execute |
values | V & StructType <{ date : DateTimeType } & Values > extends ReturnType ["type" ] ? unknown : never | the values to execute the ProcessBuilder with |
Returns
ProcessBuilder
Remarks
The values
must be an object with at least date
, but also any value
property define in the
Example
// create the process to be added
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.log({
message: (props) => StringJoin`Recieved ${props.qty} items`
})
// add a process to another and execute an hour after
const sales = new ProcessBuilder("sales")
.process(receive_items)
.execute("receive items", (props) => Struct({
date: AddDuration(props.date, 1, 'hour'),
qty: Const(5)
}))
.mapFromValue({ date: new Date(0) });
Example
// execute the sales process recursively every minute until an hour passes
const sales = new ProcessBuilder("sales")
.execute("sales", (props) => Struct({
date: AddDuration(props.date, 1, 'minute'),
}))
// note that `new Date(3600000)` will be constant in the simulation
.endSimulation((props) => Greater(props.date, new Date(3600000)))
.mapFromValue({ date: new Date(0) });
forArray
▸ forArray(collection
, loop_block
):
ProcessBuilder
Perform a block of statements for each item in an array.
Type parameters
Name | Type |
---|---|
Collection | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection | a function that returns an array expression of properties and resources |
loop_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >, value : Variable <ReturnType ["type" ]["value" ]>, key : Variable ) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed for each element of a collection. |
Returns
ProcessBuilder
Example
// log out all indices and values of an array
new ProcessBuilder("my_process")
.let("array", _ => Const(["a", "b", "c"]))
.forArray(
props => props.array,
(block, value, key) => block
.log({
message: () => StringJoin`Index ${key} contains string ${value}`
})
)
forDict
▸ forDict(collection
, loop_block
):
ProcessBuilder
Perform a block of statements for each item in a dictionary.
Type parameters
Name | Type |
---|---|
Collection | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection | a function that returns a dictionary expression of properties and resources |
loop_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >, value : Variable <ReturnType ["type" ]["value" ]["value" ]>, key : Variable <ReturnType ["type" ]["value" ]["key" ]>) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed for each element of a collection. |
Returns
ProcessBuilder
Example
// log out all keys and values of an dictionary
new ProcessBuilder("my_process")
.let("dict", _ => Const(new Map([["a", 1n], ["b", 2n], ["c", 3n]]))
.forDict(
props => props.dict,
(block, value, key) => block
.log({
message: () => StringJoin`Key ${key} contains value ${value}`
})
)
forSet
▸ forSet(collection
, loop_block
):
ProcessBuilder
Perform a block of statements for each item in a set.
Type parameters
Name | Type |
---|---|
Collection | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | Collection | a function that returns an set expression of properties and resources |
loop_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >, key : Variable <ReturnType ["type" ]["value" ]>) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed for each element of a collection. |
Returns
ProcessBuilder
Example
// log out all keys of a set
new ProcessBuilder("my_process")
.let("set", _ => Const(new Set(["a", "b", "c"])))
.forSet(
props => props.set,
(block, key) => block
.log({
message: () => StringJoin`Key ${key}`
})
)
if
▸ if(predicate
, true_block
, false_block?
):
ProcessBuilder
Perform blocks of statements conditional on some predicate (Boolean) expression.
Parameters
Name | Type | Description |
---|---|---|
predicate | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | a function that returns a BooleanType expression of properties and resources |
true_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed if the predicate is true . |
false_block? | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | an optional function that returns a block of statements to be executed if the predicate is false . |
Returns
ProcessBuilder
Example
// stock level of products to sell
const stock = new ResourceBuilder("stock")
.fromValue(100.0)
// sales reduces stock level
const sales = new ProcessBuilder("sales")
.resource(stock)
.value("qty", FloatType)
// only deduct stock if qty is non-zero
.if(
props => Greater(qty, 0.0),
block => block
.set("stock", (props, resources) => Subtract(resources.stock, props.qty))
)
ifNull
▸ ifNull(input
, null_block
, value_block?
):
ProcessBuilder
Execute blocks of statements conditional on some input being null or not.
Type parameters
Name | Type |
---|---|
I | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
input | I | a function that returns a Nullable expression of properties and resources |
null_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed if the input is null . |
value_block? | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >, value : Variable <NonNullable <ReturnType ["type" ]>>) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | an optional function that returns a block of statements to be executed if the input is not null . The input value is available as a non-nullable variable. |
Returns
ProcessBuilder
Example
// stock level of products to sell
const stock = new ResourceBuilder("stock")
.fromValue(100.0)
// sales reduces stock level (but only qty is not null)
const sales = new ProcessBuilder("sales")
.resource(stock)
.value("qty", Nullable(FloatType))
// only deduct stock if qty is non-zero
.ifNull(
props => qty,
block => block,
block => block,
.set("stock", (props, resources) => Subtract(resources.stock, props.qty))
)
insert
▸ insert(collection
, key
, value
):
ProcessBuilder
Insert a new entry into an array, set or dictionary.
For arrays, you can insert the "first"
or "last"
element (i.e. push to the front or end of the array).
For sets, you can insert a new key. If the key already exists an error will result.
For dictionaries, you can insert a new key and associated value. If the key already exists an error will result (see also
Type parameters
Name | Type |
---|---|
C | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | C | a function returning the array, set or dictionary to insert into |
key | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction <ReturnType ["type" ]["value" ]["key" ]> | the key of the item to insert |
value | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => { ast_type : AstType ; type : SubType <ReturnType ["type" ]["value" ]["value" ]> } | the value to insert (for arrays and dictionaries only) |
Returns
ProcessBuilder
Example
// create a resource
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]))
// add the inventory resource and insert a value
const delivery = new ProcessBuilder("delivery")
.resource(inventory)
.value("qty", IntegerType)
.value("item", StringType)
.insert(
(props, resources) => resources.inventory,
(props) => props.item,
(props) => Struct({ qty: props.qty }),
)
.mapFromValue({
date: new Date(0),
item: "frame",
qty: 54n
});
let
▸ let(name
, expression
):
ProcessBuilder
<Name
, Values
, PropertyName
extends keyof Properties
? Omit
: Properties
& { [K in string]: Variable<ReturnType["type"]> }, Resources
, Processes
, MLs
, Procs
>
Compute a property as an expression of existing properties.
Type parameters
Name | Type |
---|---|
PropertyName | extends string |
Expr | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
name | PropertyName | the name of the property |
expression | Expr | the function of defining the property |
Returns
ProcessBuilder
<Name
, Values
, PropertyName
extends keyof Properties
? Omit
: Properties
& { [K in string]: Variable<ReturnType["type"]> }, Resources
, Processes
, MLs
, Procs
>
Remarks
you may overwrite an existing property of the same name with a new value of any type.
Example
// create the sales process with some properties
const sales = new ProcessBuilder("sales")
.let("revenue", () => Const(5))
.let("cost", () => Const(6))
.let("profit", (props) => Subtract(props.revenue, props.cost))
.mapFromValue({ date: new Date(0) });
log
▸ log(message
):
ProcessBuilder
Produce a log message.
Parameters
Name | Type | Description |
---|---|---|
message | string | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | the error message |
Returns
ProcessBuilder
Example
// create the sales process with some properties
const sales = new ProcessBuilder("sales")
// create random qty
.let("qty", () => RandomUniform(0, 1))
.if(
(props) => Less(props.qty, 0.5),
block => block
.log((props) => StringJoin`value ${props.qty} <= 0.5`)
)
.mapFromValue({ date: new Date(0) });
mapFromPipeline
▸ mapFromPipeline(pipeline
): MappedProcessBuilder
Create a instance of a process from a
PipelineBuilder.Parameters
Name | Type |
---|---|
pipeline | (builder : PipelineBuilder ) => GenericPipelineBuilder <StructType <{ date : DateTimeType } & Values >, Record > |
Returns
MappedProcessBuilder
Remarks
The pipeline
must output a
date
value, but also any value
property define in the ProcessBuilder
Example
// a stream to map from
const data = new SourceBuilder("receive items data")
.value({
value: {
date: new Date(0),
qty: 3
}
})
// create the process to map
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapFromPipeline(builder => builder
.from(data.outputStream())
);
mapFromStream
▸ mapFromStream(stream
): MappedProcessBuilder
Create an instance of a process from a
Stream.Parameters
Name | Type | Description |
---|---|---|
stream | Stream <StructType <{ date : DateTimeType } & Values >> | the stream to map from. |
Returns
MappedProcessBuilder
Remarks
The stream
must be a
date
value, but also any value
property define in the ProcessBuilder
Example
// a stream to map from
const data = new SourceBuilder("receive items data")
.value({
value: {
date: new Date(0),
qty: 3
}
})
// create the process to map
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapFromStream(data.outputStream());
mapFromValue
▸ mapFromValue(value
): MappedProcessBuilder
Create a single instance of a process from a
Value.Parameters
Name | Type | Description |
---|---|---|
value | { [K in string | number | symbol]: ValueTypeOf } | the value to map from. |
Returns
MappedProcessBuilder
Remarks
The value
must be an
date
value, but also any value
property define in the ProcessBuilder
Example
// map a process from a value
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapFromValue({
date: new Date(0),
qty: 10
});
mapManyFromPipeline
▸ mapManyFromPipeline(pipeline
): MappedProcessBuilder
Create many instances of a process from a
Pipeline.Parameters
Name | Type |
---|---|
pipeline | (builder : PipelineBuilder ) => TabularPipelineBuilder <DictType <StringType , StructType <{ date : DateTimeType } & Values >>, Record > |
Returns
MappedProcessBuilder
Remarks
The pipeline
must output
date
value, but also any value
property define in the ProcessBuilder
Example
// a stream to map from
const data = new SourceBuilder("receive items data")
.value({
value: new Map([
["one", {
date: new Date(0),
qty: 10
}],
["two", {
date: new Date(0),
qty: 5
}]
])
})
// create the process to map
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapManyFromPipeline(builder => builder
.from(data.outputStream())
);
mapManyFromStream
▸ mapManyFromStream(stream
): MappedProcessBuilder
Create many instances of a process from a
Stream.Parameters
Name | Type | Description |
---|---|---|
stream | Stream <DictType <StringType , StructType <{ date : DateTimeType } & Values >>> | the stream to map from. |
Returns
MappedProcessBuilder
Remarks
The stream
must be a
date
value, but also any value
property define in the ProcessBuilder
Example
// a stream to map from
const data = new SourceBuilder("receive items data")
.value({
value: new Map([
["one", {
date: new Date(0),
qty: 10
}],
["two", {
date: new Date(0),
qty: 5
}]
])
})
// create the process to map
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapManyFromStream(data.outputStream());
mapManyFromValue
▸ mapManyFromValue(value
): MappedProcessBuilder
Create many instances of a process from a
Value.Parameters
Name | Type | Description |
---|---|---|
value | Map <string , { [K in string | number | symbol]: ValueTypeOf }> | the Map object containing values to map from. |
Returns
MappedProcessBuilder
Remarks
The value
must be
date
value, but also any value
property define in the ProcessBuilder
Example
// create the process to map
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.mapManyFromValue(
new Map([
["one", {
date: new Date(0),
qty: 10
}],
["two", {
date: new Date(0),
qty: 5
}]
]));
match
▸ match(variant
, cases
):
ProcessBuilder
Perform blocks of statements matched on the cases of some variant expression.
Type parameters
Name | Type |
---|---|
V | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
T | extends { [K in string | number | symbol]: Function } |
Parameters
Name | Type | Description |
---|---|---|
variant | V | a function that returns a VariantType expression of properties and resources |
cases | T | an object containing functions that return a block of statements to be executed for the given variant case |
Returns
ProcessBuilder
Example
// sales produes a log message about the buyer
const sales = new ProcessBuilder("sales")
.value("buyer_name", MaybeType(StringType))
.let("message", _ => Const(""))
.match(
(_, resources) => resources.buyer,
{
Some: (block, name) => block.assign("message", _ => StringJoin`Initiating sale to ${name}`),
None: (block) => block.assign("message", _ => StringJoin`Initiating sale to unknown buyer`),
}
)
ml
▸ ml(ml
):
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
, MLs
& { [K in string]: MLDescription<StructType, T> }, Procs
>
Include a ML function that this process can evaluate as a kind of "procedure".
Type parameters
Name | Type |
---|---|
MLName | extends string |
Features | extends Record |
T | extends EastType |
Parameters
Name | Type | Description |
---|---|---|
ml | AbstractMLBuilder | the MLModelBuilder model add to the ProcessBuilder |
Returns
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
, MLs
& { [K in string]: MLDescription<StructType, T> }, Procs
>
Example
// create the ML function of the amount
const amount = new MLModelBuilder("amount")
// the output is amount which is FloatType
.output(FloatType)
// create the sales process and add the ML function
// which is evaluated in each sale
const sales = new ProcessBuilder("sales")
.resource(cash)
// the ML function can be added
.ml(demand)
// the ML can be evaluated similarly to a procedure
// (note, this ML function has no input features)
.let(
"amount",
(_props, _resources, procs) => mls.demand(Struct({}))
)
// ...
overrides
▸ overrides(process
):
ProcessBuilder
<NewName
, NewValues
, { date
: Variable
} & { [K in string | number | symbol]: Variable }, Resources
, Processes
, MLs
, Procs
>
Override an existing process, inheriting its name (for the purposes of simulation) and input
value properties. Any computed properties, imports or other statements are not inherited.The resulting process can then be used to override the process in a scenario that has been constructed with
ScenarioBuilder.copyScenario or ScenarioBuilder.continueScenario using the ScenarioBuilder.overrideProcess method. Given that the name and input value properties match, any execute statements or queued processes throughout the scenario will be executed by the new process.Type parameters
Name | Type |
---|---|
NewName | extends string |
NewValues | extends Record |
Parameters
Name | Type | Description |
---|---|---|
process | AbstractProcessBuilder <NewName , NewValues , Record , Record <string , Variable >, Record , Record > | a process builder to copy the name and input value properties |
Returns
ProcessBuilder
<NewName
, NewValues
, { date
: Variable
} & { [K in string | number | symbol]: Variable }, Resources
, Processes
, MLs
, Procs
>
procedure
▸ procedure(procedure
):
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
, MLs
, Procs
& { [K in string]: ProcedureDescription<ProcName, StructType, T> }>
Include a sub-procedure that this process can evaluate.
Type parameters
Name | Type |
---|---|
ProcName | extends string |
Inputs | extends Record |
T | extends EastType |
Parameters
Name | Type | Description |
---|---|---|
procedure | ProcedureFinalizer | the Procedure model add to the ProcessBuilder |
Returns
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
, MLs
, Procs
& { [K in string]: ProcedureDescription<ProcName, StructType, T> }>
Example
// create a procedure to calculate the tax to apply to a sale
const apply_tax = new Procedure("apply_tax")
// the input parameter
.input("amount", FloatType)
// the output is a FloatType
.output(FloatType)
// the body of the procedure
.body(b => b
// add a 10% tax and return the value
.return(vars => Multiply(vars.amount, 1.1))
)
// create the sales process and add the ml function
// which is evaluated in each sale
const sales = new ProcessBuilder("sales")
.resource(cash)
// import the procedure
.procedure(apply_tax)
// the pre-tax sale amount
.value("amount", FloatType)
// the procedure can be accessed and evaluated inside expressions within the function
.let(
"tax_adjusted_amount",
(vars, _resources, procs) => procs.apply_tax(Struct({ amount: vars.amount }))
)
// ...
process
▸ process(process
):
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
& { [K in string]: ProcessValues }, MLs
, >
Include another process that this process can execute.
Type parameters
Name | Type |
---|---|
ProcessName | extends string |
ProcessValues | extends Record |
Parameters
Name | Type | Description |
---|---|---|
process | AbstractProcessBuilder <ProcessName , ProcessValues , Record , Record , Record , Record > | the ProcessBuilder to add to the ProcessBuilder |
Returns
ProcessBuilder
<Name
, Values
, Properties
, Resources
, Processes
& { [K in string]: ProcessValues }, MLs
, >
Example
// create the process to be added
const receive_items = new ProcessBuilder("receive items")
.value("qty", FloatType)
.log({
message: (props) => StringJoin`Recieved ${props.qty} items`
})
// add a process to another and execute an hour after
const sales = new ProcessBuilder("sales")
.process(receive_items)
.execute("receive items", (props) => Struct({
date: AddDuration(props.date, 1, 'hour'),
qty: Const(5)
}))
.mapFromValue({ date: new Date(0) });
resource
▸ resource(resource
):
ProcessBuilder
<Name
, Values
, Properties
, Resources
& { [K in string]: Variable }, Processes
, MLs
, Procs
>
Include a resource that this process can read or modify.
Type parameters
Name | Type |
---|---|
ResourceName | extends string |
T | extends EastType |
Parameters
Name | Type | Description |
---|---|---|
resource | SimulationResourceBuilder | the ResourceBuilder to add to the ProcessBuilder\ |
Returns
ProcessBuilder
<Name
, Values
, Properties
, Resources
& { [K in string]: Variable }, Processes
, MLs
, Procs
>
Example
// create a resource
const cash = new ResourceBuilder("cash")
.mapFromValue(0)
// create the sale process
const sales = new ProcessBuilder("sales")
.resource(cash)
// set the cash balance
.set("cash", () => Const(5))
// simulate from a date
.mapFromValue({ date: new Date(0) });
set
▸ set(name
, value
):
ProcessBuilder
Set a resource value.
Type parameters
Name | Type |
---|---|
ResourceName | extends string | number | symbol |
V | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
name | ResourceName | the ResourceBuilder name to set |
value | V & Resources [ResourceName ]["type" ] extends ReturnType ["type" ] ? unknown : never | the function defining the value |
Returns
ProcessBuilder
Remarks
this overwrites the existing resource value in it's entirety, even if it is a collection. Use the update
, insert
or delete
methods to modify just part of a resource.
Example
// create a resource
const cash = new ResourceBuilder("cash")
.mapFromValue(0)
// create the sale process
const sales = new ProcessBuilder("sales")
.resource(cash)
// set the cash balance
.set("cash", () => Const(5))
// simulate from a date
.mapFromValue({ date: new Date(0) });
update
▸ update(collection
, key
, value
):
ProcessBuilder
Update an existing value in an array or dictionary. The collection will be mutated in place.
For arrays, you can update any element by integer index (the first element has index 0n
). If the index is out of bounds an error will result.
For dictionaries, you can update any existing key to a new associated value. If the key does not exist an error will result (see also
Type parameters
Name | Type |
---|---|
C | extends (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction |
Parameters
Name | Type | Description |
---|---|---|
collection | C | a function returning the array, set or dictionary to update |
key | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction <ReturnType ["type" ]["value" ]["key" ]> | the key of the item to update |
value | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }, old_value : Variable <ReturnType ["type" ]["value" ]["value" ]>) => { ast_type : AstType ; type : SubType <ReturnType ["type" ]["value" ]["value" ]> } | the value to update to |
Returns
ProcessBuilder
Example
// create a resource
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]))
// subtract the appropriate qty from inventory
const dispatch = new ProcessBuilder("dispatch")
.resource(inventory)
.value("qty", IntegerType)
.value("item", StringType)
.update(
(props, resources) => resources.inventory,
(props) => props.item,
(props, _, _, old_value) => Struct({ qty: Subtract(GetField(old_value, "qty"), props.qty) }),
)
.mapFromValue({
date: new Date(0),
item: "chair",
qty: 2n
});
value
▸ value(name
, type
):
ProcessBuilder
<Name
, Values
& { [K in string]: T }, Properties
& { [K in string]: Variable }, Resources
, Processes
, MLs
, Procs
>
Define an input value property for the process.
Type parameters
Name | Type |
---|---|
PropertyName | extends string |
T | extends EastType |
Parameters
Name | Type | Description |
---|---|---|
name | PropertyName | the name of the property |
type | T | the type of the property |
Returns
ProcessBuilder
<Name
, Values
& { [K in string]: T }, Properties
& { [K in string]: Variable }, Resources
, Processes
, MLs
, Procs
>
Example
// create a simple process with an input value
const sales = new ProcessBuilder("sales")
.value("amount", FloatType)
.mapFromValue({ date: new Date(0), amount: 1 });
warn
▸ warn(message
):
ProcessBuilder
Produce a warning with a message.
Parameters
Name | Type | Description |
---|---|---|
message | string | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | the error message |
Returns
ProcessBuilder
Example
// create the sales process with some properties
const sales = new ProcessBuilder("sales")
// create random qty
.let("qty", () => RandomUniform(0, 1))
.if(
(props) => Less(props.qty, 0.5),
block => block
.warn((props) => StringJoin`value ${props.qty} <= 0.5`)
)
.mapFromValue({ date: new Date(0) });
while
▸ while(predicate
, loop_block
):
ProcessBuilder
Repeatedly perform a block of statements, conditional on some predicate (Boolean) expression.
Parameters
Name | Type | Description |
---|---|---|
predicate | (properties : Properties , resources : Resources , procs : { [K in string | number | symbol]: MLEvaluator } & { [K in string | number | symbol]: ProcedureEvaluator }) => EastFunction | a function that returns a BooleanType expression of properties and resources |
loop_block | (block : ProcessBlockBuilder <Properties , Resources , Processes & { [K in string]: Values }, MLs , Procs >) => ProcessBlockBuilder <Record , Resources , Processes & { [K in string]: Values }, MLs , Procs > | a function that returns a block of statements to be executed repeatedly until the predicate is false . |
Returns
ProcessBuilder
Example
// perform a loop from i = 0 to 10
new ProcessBuilder("my_process")
.let("i", 0n)
.while(
props => LessEqual(props.i, 10n),
block => block
.assign("i", props => Add(props.i, 1n))
)