Sequence class

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

If one of the children nodes returns Status.failure, the Sequence returns Status.failure. If none of composite children Node instances return Status.success, the Sequence will return Status.failure.

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

Example:

void sequenceExample() {
  var x = 0;
  final incrementByOne = makeClosure(action: () => ++x);
  final incrementByTwo = makeClosure(action: () => x += 2);
  final incrementByThree = makeClosure(action: () => x += 3);
  final reset = makeClosure(action: () => x = 0);

  // evaluate nodes depending on the success of the
  // previous node
  final sequence = Sequence([
    // increment by 1 and return Status.success
    incrementByOne,
    // increment by 2 and return Status.success
    incrementByTwo,
    // increment by 3 and return Status.success
    incrementByThree,
    makeClosure(action: () => print('value of x = $x')),
    // reset x back to 0 at the end
    reset
  ], isPartial: true);

  // partial sequence will require multiple calls
  // to the update() method in order to go through
  // composite children nodes
  if (sequence.update() != Status.running) {
    sequence.reset();
  }
  assert(x == 1);

  if (sequence.update() != Status.running) {
    sequence.reset();
  }
  assert(x == 3);

  if (sequence.update() != Status.running) {
    sequence.reset();
  }
  assert(x == 6);

  // executes the last node which prints the value of x
  if (sequence.update() != Status.running) {
    sequence.reset();
  }
}
Inheritance

Constructors

Sequence(List<Node> nodes, {required bool isPartial})
Constructs a Sequence 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