BehaviorDsl

io.github.srs.model.entity.dynamicentity.behavior.dsl.BehaviorDsl
object BehaviorDsl

Behavior DSL (pure)

Minimal DSL to build decision logic:

  • PartialBehavior = Kleisli[Option, I, A] – a partial decision (“may produce”).
  • Behavior = Kleisli[Id, I, A] – a total decision (“always produces”).

Partial behaviors compose left-biased (the first Some wins) and are finalized into total behaviors via orElse (or the compatibility alias default).

Attributes

Example
val isEven: Condition[Int] = _ % 2 == 0
val rule: PartialBehavior[Int, String] =
 (isEven ==> "even") | (_ => true) ==> "odd"
val behavior: Behavior[Int, String] =
 rule.orElse("n/a")
behavior.run(2)  // "even"
behavior.run(3)  // "odd"
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Extensions

Extensions

extension [I](c: () => I)
infix def and(d: () => I): () => I

Logical AND of two conditions.

Logical AND of two conditions.

Value parameters

d

condition to combine with

Attributes

Returns

a condition that holds only if both c and d hold

def not: () => I

Logical NOT of a condition.

Logical NOT of a condition.

Attributes

Returns

a condition that holds when c does not hold

infix def or(d: () => I): () => I

Logical OR of two conditions.

Logical OR of two conditions.

Value parameters

d

condition to combine with

Attributes

Returns

a condition that holds if either c or d holds

extension [I, A](cond: () => I)
infix def ==>(act: => A): PartialBehavior[I, A]

Build a partial behavior from a condition.

Build a partial behavior from a condition.

Produces Some(act) when the condition holds; otherwise None. The action is call-by-name and evaluated only if needed.

Value parameters

act

action to produce when the condition holds (lazy)

Attributes

Returns

a PartialBehavior that yields Some(act) if cond(i) is true, else None

extension [I, A](r1: PartialBehavior[I, A])
def default(fallback: => A): Behavior[I, A]

Compatibility alias for orElse.

Compatibility alias for orElse.

Value parameters

fallback

default action to use when no rule fires (lazy)

Attributes

Returns

a total Behavior that always produces an action

def onlyIf(p: () => I): PartialBehavior[I, A]

Gate this partial behavior with an additional predicate.

Gate this partial behavior with an additional predicate.

When p(i) is false, this rule forcibly defers (None).

Value parameters

p

additional predicate on the same input

Attributes

Returns

a PartialBehavior that runs only if p holds

def orElse(fallback: => A): Behavior[I, A]

Finalize a partial behavior into a total behavior by providing a fallback.

Finalize a partial behavior into a total behavior by providing a fallback.

If the composed partial chain yields None, the fallback is used.

Value parameters

fallback

default action to use when no rule fires (lazy)

Attributes

Returns

a total Behavior that always produces an action

infix def |(r2: PartialBehavior[I, A]): PartialBehavior[I, A]

Left-biased composition of two partial behaviors.

Left-biased composition of two partial behaviors.

Evaluates r1(i): if it returns Some(a) the result is kept, otherwise r2(i) is evaluated.

Value parameters

r2

fallback partial behavior to try when r1 defers

Attributes

Returns

a PartialBehavior that prefers r1 over r2