SoftwareAG / apama-analytics-builder-block-sdk

Apama Analytics Builder Block SDK
Apache License 2.0
9 stars 5 forks source link

Support block state in $init and $validate #12

Open haraldmeyer opened 3 months ago

haraldmeyer commented 3 months ago

In some cases, it can make sense to populate the state already during the $init or $validation actions if the state can be derived for the parameters and connected inputs and / or will not change afterwards (e.g. converting a human-readable parameter into a format that can be better used in the EPL implementation).

At the moment, one would have to implement this in $process and check if it has already been populated to avoid doing it every time.

gys-c8y commented 3 months ago

I think it may be tricky to support this for partitioned models. In partitioned models, the state is separately maintained for each partition (devices). And the correct state for correct partition is passed to the $process action when evaluating model for that partition. For $init and $validate action, it is unclear which state to pass. Calling $init and $validate action for each partition is unreasonable as number of partition may be unknown at the startup - for example receiving from all inputs. If partition agnostic data is store in the state during the $init and $validate call, then we can probably have a separate state for passing to the $init and $validate action and then clone it to create a partition specific state when first input for a specific partition is received.

haraldmeyer commented 3 months ago

ok, understood. That makes sense. What about having a field in the Activation object that indicates if this is the first time $process has been called for a partition? Or is there another way to determine that?

gys-c8y commented 3 months ago

Adding action on Activation would not work either because the same activation object is used across all blocks when evaluating a model but you need status per-block and not per-model. Besides that, I don't think it would be too much improvement as you would still have to check if init is already done or not.

Maybe we can support $init action on the state event itself which can be called when new state object is created for a partition.

event MyBlock_$State {
  MyBlock_$Parameters $parameters;
  ...
  action $init() { // Called when new instance of this event is created
    ...
  }
}

Also, if you are computing values from parameters which does not change with inputs and across partitions, you do not need to use state object. You can directly store values in a filed on the block event itself.

haraldmeyer commented 3 months ago

Your last comment is spot on: my requirement can actually be fulfilled without storing the derived parameters in a state object. Did not consider this as I needed the state for other purposes anyway. I have a hard time finding other use cases at the moment, so unsure if this feature is really required.

I like your suggestion about $init but not that it would require another field $parameters that you would need to know to add by convention.