Skip to main content

Scenario - ResourceBuilder

Scenario.ResourceBuilder

A builder to define a resource 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

NameType
Nameextends string

Resource

constructor

new ResourceBuilder(name, module?):

ResourceBuilder

Construct a resource with a given name.

Type parameters

NameType
Nameextends string

Parameters

NameTypeDescription
nameNamethe name of the ResourceBuilder.
module?ModulePath | ModuleBuilder-

Returns

ResourceBuilder

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);

mapFromPipeline

mapFromPipeline(pipeline): SimulationResourceBuilder<Name, ReturnType<ReturnType["outputStream"]>["type"]>

Assign the resource's initial state to the value contained output from a pipeline.

Type parameters

NameType
Pextends (builder: PipelineBuilder) => TabularPipelineBuilder<DictType, Record> | GenericPipelineBuilder<EastType, Record>

Parameters

NameType
pipelineP

Returns

SimulationResourceBuilder<Name, ReturnType<ReturnType["outputStream"]>["type"]>

Remarks

The pipeline can output any EastType stream

Example

 // a stream to map from
const data = new SourceBuilder("inventory data")
.value({
value: new Map([
["one", {
date: new Date(0),
qty: 10
}],
["two", {
date: new Date(0),
qty: 5
}]
])
})

const inventory = new ResourceBuilder("inventory")
.mapFromPipeline(builder => builder
.from(data.outputStream())
)

mapFromStream

mapFromStream(stream): SimulationResourceBuilder

Assign the resource's initial state to the value contained in a datastream.

Type parameters

NameType
Textends EastType

Parameters

NameTypeDescription
streamStreamthe stream to map from.

Returns

SimulationResourceBuilder

Remarks

The stream can be any EastType stream

Example

 // create a stream with some inventory
const data = new SourceBuilder("inventory data")
.value({
value: new Map([
["one", {
date: new Date(0),
qty: 10
}],
["two", {
date: new Date(0),
qty: 5
}]
])
})

// map some inventory from the stream
const inventory = new ResourceBuilder("inventory")
.mapFromStream(data.outputStream())

mapFromValue

mapFromValue(value): SimulationResourceBuilder<Name, EastTypeOf>

Assign the resource's initial state to the provided value.

Type parameters

NameType
Textends Value

Parameters

NameTypeDescription
valueTthe value to map from.

Returns

SimulationResourceBuilder<Name, EastTypeOf>

Remarks

The type value may be a value of any valid EastType type, such as

PrimitiveValue, ArrayValue, SetValue, DictValue, VariantValue, StructValue, BlobValue.

Example

 // map some inventory from data
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]))

mapFromValue(value, type): SimulationResourceBuilder

Assign the resource's initial state to the provided value and type.

Type parameters

NameType
Textends EastType

Parameters

NameTypeDescription
valueValueTypeOfthe value to map from.
typeTthe type to use for the resource value.

Returns

SimulationResourceBuilder

Remarks

The type may be any valid EastType type, such as

PrimitiveValue, ArrayValue, SetValue, DictValue, VariantValue, StructValue, BlobValue.

Example

 // map some inventory from data
const inventory = new ResourceBuilder("inventory")
.mapFromValue(new Map([
["socks", { qty: 1n }],
["chairs", { qty: 3n }]
]), DictType(StringType, StructType({ qty: IntegerType})))