Skip to content

4. Changing State with Input

Ovan Crone edited this page Dec 19, 2017 · 9 revisions

We will be interacting with Machines to change states when we provide input. To do so effectively, let's recap the code so far.

var schematic = REstateHost.Agent
    .CreateSchematic<string, string>("LoggerMachine")
    .WithState("Created", state => state
        .AsInitialState())
    .WithState("Ready", state => state
        .WithTransitionFrom("Created", "log")
        .WithReentrance("log")
        .WithOnEntry("log info", onEntry => onEntry
            .DescribedAs("Logs the transition as a message.")
            .WithSetting(
                key: "messageFormat", 
                value: "{schematicName}({machineId}) entered {state} on {input}.")))
    .Build();

var stateEngine = REstateHost.Agent
    .GetStateEngine<string, string>();

var machine = await stateEngine.CreateMachineAsync(schematic);

Now that we can look back on the Schematic, we can see that when we created it, it should be in its initial state, "Created". "Created" has a single transition defined from "Created" when it receives the input "log", which we can send now.

var result = await machine.SendAsync("log");

The result of the call will be a Status<TState> which for our Machine is Status<string> since our state type is string. Inspecting the result shows us a few properties:

  • The MachineId of the Machine with which we interacted.
  • The state the machine is in after processing the input.
  • A timestamp of when the transition occurred.
  • A commit tag, a unique and random GUID that identifies a transition in any Machine.
Clone this wiki locally