Selector class

A Selector evaluates its composite nodes sequentially expecting one of them to return Status.success, in which case the Selector returns Status.success.

If one of the children nodes returns Status.failure, the Selector evaluates the next child. If none of composite children Node instances return Status.success, the Selector will return Status.failure.

This behavior is similar to an OR statement where the children nodes are the conditions of a boolean expression.

Example:

void selectorExample() {
  // changing the value of this variable will
  // change the behavior of the selector
  var x = 1;

  // checks if x is even:
  // returns Status.success if it is,
  // returns Status.failure if isn't
  final isEven = Closure(() => x % 2 == 0
    ? Status.success
    : Status.failure);

  // increments x by 1
  final incrementByOne = makeClosure(action: () => ++x);

  final selector = Selector(
    [
      // the first selector node is evaluated;
      // it is a sequence node
      Sequence([
        // the first sequence node is evaluated:
        // is x even?
        isEven,
        // if it is, the second sequence node
        // is evaluated: print a statement
        makeClosure(action: () => print('selector node 1: x == $x; even')),
      ], isPartial: false),
      // if x is not even, the second selector node
      // is evaluated because the first one failed,
      // this node simply increments x by one
      // and then prints a statement
      Sequence([
        incrementByOne,
        Print('selector node 2: x == $x; odd'),
      ], isPartial: false),
    ],
    isPartial: false,
  );

  // on first iteration, x is odd, so the selector
  // fails the first node
  // on subsequent iterations, x is even,
  // so only the second node executes
  // this behavior is equivalent to:
  // var x = 1;
  // for (var i = 0; i != 4; ++i) {
  //   if (x % 2 == 0) {
  //     print('selector node 1: x == $x; even');
  //   } else {
  //     ++x;
  //     print('selector node 2: x == $x; odd');
  //   }
  // }
  for (var i = 0; i != 4; ++i) {
    if (selector.update() != Status.running) {
      // reset is necessary to re-evaluate
      // selector nodes from the beginning
      selector.reset();
    }
  }
}
Inheritance

Constructors

Selector(List<Node> nodes, {required bool isPartial})
Constructs a Selector instance.

Properties

hashCode int
The hash code for this object.
no setterinherited
index int
Index of the current Node of the Composite. The value should represent the index of the child Node that is in execution. The index is not guaranteed to be inside the bounds of the List that holds the children Node instances of the Composite.
no setterinherited
length int
Count of the number of Node instances in the Composite.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reset() → void
inherited
toString() String
A string representation of this object.
inherited
update() Status
override

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](int index) Node
Indexes into the List of children Node instances.
inherited