The Itinera bookJava edition

Part 2: Getting started, Chapter 6

Passing data on

What you want to do

When the bank charges an order, it gives a receipt. You want the charge to give that receipt back, so that the shop has it once the journey ends, and so that the parcel can carry it when the shop ships the order.

The code

The charge now asks for the order's number as well as its price. The bank, still faked, gives a receipt, and the charge hands it on through a contributor, a parameter of run of type Contributor.

Java, from the program ch06_passing_data_on
/** Charges an order's price, and gives back the bank's receipt. */
@Step
final class Charge implements ItineraStep {
  private final String order;
  private final long price;

  Charge(@Input String order, @Input long price) {
    this.order = order;
    this.price = price;
  }

  public Outcome run(Contributor contributor) {
    if (price <= 0) return fail("nothing-to-charge");
    String receipt = Bank.charges(order, price);
    contributor.contribute("receipt", receipt);
    return succeed();
  }
}

Shipping is a second step. It books the carrier, which gives a tracking number at once, and hands that on the same way. The carrier, faked too, weighs the parcel only when it comes to collect it, and takes it only up to 20 kg. The receipt is one of the shipping's inputs, asked for like the order's number.

Java, from the program ch06_passing_data_on
/** Ships an order, with its receipt in the parcel, and gives back its tracking number. */
@Step
final class Ship implements ItineraStep {
  private final String order;
  private final long weight;
  private final String receipt;

  Ship(@Input String order, @Input long weight, @Input String receipt) {
    this.order = order;
    this.weight = weight;
    this.receipt = receipt;
  }

  public Outcome run(Contributor contributor) {
    String tracking = Carrier.books(order);
    contributor.contribute("tracking", tracking);
    if (!Carrier.collects(weight)) return fail("too-heavy");
    System.out.println("the parcel leaves with receipt " + receipt);
    return succeed();
  }
}

The workflow lists the two steps, one @WithStep(...) line each:

Java, from the program ch06_passing_data_on
/** The shop's workflow: it charges, then ships. */
@Workflow(name = "orders")
@WithStep(step = Charge.class, name = "charge")
@WithStep(step = Ship.class, name = "ship")
final class Orders implements ItineraWorkflow {}

Each order now carries its number and its parcel's weight as well as its price, so its initial data is a record of the shop's:

Java, from the program ch06_passing_data_on
/** What the shop knows about an order when its journey starts. */
record NewOrder(String order, long price, long weight) {}

Once a journey has ended, the shop reads back the receipt and the tracking number into another record. A tracking number may be missing, so its component is an Optional<String>.

Java, from the program ch06_passing_data_on
/** What the shop reads back once the journey has ended. */
record Order(String receipt, Optional<String> tracking) {}

Two orders come in: Ann's, whose parcel weighs 2 kg, and Gil's, whose parcel weighs 60 kg.

Java, from the program ch06_passing_data_on
public static void main(String[] args) throws Refusal {
  // The customer, and their order: its number, its price and the parcel's weight in kg.
  var orders =
      List.of(
          Map.entry("Ann", new NewOrder("A-1", 40, 2)),
          Map.entry("Gil", new NewOrder("A-7", 45, 60)));
  for (var entry : orders) {
    System.out.println(entry.getKey() + "'s order:");
    var instance = new Orders().instance().initialData(entry.getValue()).create();
    var result = new LocalExecutor().run(instance);
    var data = result.output(TypeToken.of(Order.class));
    var ended = result.isSucceeded() ? "succeeded" : "failed";
    System.out.println(
        "the journey "
            + ended
            + ", with receipt "
            + data.receipt()
            + " and tracking "
            + data.tracking().orElse("none"));
  }
}

It prints:

What ch06_passing_data_on prints, recorded in ch06_passing_data_on.txt
Ann's order:
the bank charges 40 for order A-1
the carrier books order A-1
the parcel leaves with receipt R-A-1
the journey succeeded, with receipt R-A-1 and tracking T-A-1
Gil's order:
the bank charges 45 for order A-7
the carrier books order A-7
the journey failed, with receipt R-A-7 and tracking none

How it works

  1. A step gives back what it did by contributing it. The contributor, a Contributor from org.itineradev.itinera.journey, has a contribute method that takes a name and a value: here the receipt, under receipt. What a step gives this way is a contribution, and its names need no declaring in advance. A step's contributor belongs to one attempt, one run of the step: Itinera makes one for each attempt, passes it to run, and closes it when the attempt ends.
  2. The journey's data is its data bag. A journey holds its data in its data bag: values under names. It starts as the initial data, and a step's contributions are committed to it when its attempt succeeds. Only values go in: plain data, such as null, booleans, numbers, strings, lists of them and maps from strings to them, and records whose components are values. A lambda, a connection or an ordinary class is not a value.
  3. Initial data can be a record. initialData(entry.getValue()) puts each component of a NewOrder in the initial data under its name, as data(...) did one at a time in chapter 5. Its 40 needs no L, since the component is a long.
  4. A later step reads a contribution as an ordinary input. The shipping asks for receipt with an @Input parameter, just as it asks for the order's number, which came from the initial data. It doesn't know the charge made the receipt, and the charge doesn't know anyone reads it. A step never calls another step: it reads what an earlier one contributed, by name.
  5. The steps run in the order the workflow lists them. The charge's @WithStep line comes first, so the charge runs first, and once it has succeeded, the shipping runs. Nothing reorders them: to read what the shop does with an order, you read the workflow's list, top to bottom.
  6. A failed attempt's contributions are never committed. For Gil's order, the carrier booked the parcel and gave its tracking number, which the shipping contributed. Then the carrier refused to collect a parcel of 60 kg, and the shipping failed with too-heavy. A step's contributions are committed to the data bag only when its attempt succeeds, so Gil's tracking number never reached it, and nothing in the shop can believe his parcel left.
  7. The data bag is the journey's output. When a journey ends, its data bag, as it stands then, is its output, whether the journey succeeded or failed. result.output(TypeToken.of(Order.class)) reads it into Order, each component from the name it is called by; a TypeToken, from org.itineradev.itinera.value, names the type to read. An Optional component is empty when its name is absent, as tracking is after Gil's journey, which orElse("none") prints as none; any other component must be there, or output throws.

So the charge gives back its receipt, and the shipping uses it, without either knowing about the other. But each still reaches the outside world on its own: the charge calls the shop's one bank, and the shipping its one carrier, by name, inside its run. A step written that way can only ever use that bank and that carrier. To charge through another bank, or a pretend one, you would have to change the step.

Outcomes

We have shown how a step gives back what it did, and how a later step and the shop read it.

The usual way With Itinera
A method usually returns what it did to its caller, which hands it on to the next method itself. A step contributes what it did, and a later step or the shop reads it by name, with no step calling another.
When a method fails halfway, what it already wrote often stays written, and undoing it is up to the code. A failed attempt's contributions are never committed, so the data bag holds only what steps that succeeded gave.