Skip to content

Context and history-based rules

A rule can make an edge available because a value is true now, because the session has already reached a particular node, or because it arrived through a particular route. Those requirements answer different questions. Choosing the right one lets your application offer the next step for the reason your process actually requires.

This guide applies the distinction introduced in Context, session state and history: context holds the values supplied to the engine, while history records accepted edges and visited nodes. The built-in rules context_value, visited_node and selected_edge let a graph use those two sources of information.

Start with data your application can explain

Section titled “Start with data your application can explain”

Use context for a fact that may change independently of the route. Your application might validate a form, read a current account setting or check whether required attachments exist, then supply the result under a meaningful key. The engine evaluates the value it receives; it does not perform those application checks on your behalf.

For this example, readyToSend is a boolean that your application sets after validating a support request. A browser field claiming that the request is ready is not itself that validation. Establish the value in the part of your application responsible for accepting the data, and keep only the information the rule needs in the session.

The example graph prepares the request. Reaching its final node returns a result your application can display; sending the request remains an application action.

At root node 10, the person chooses guided checks or quick checks. The guided route takes edge 201 to guidance node 15, then edge 206 to checklist node 20. The quick route takes edge 202 directly to the same checklist. Edge 203 returns from the checklist to the options at node 10.

Two routes from options at node 10 reach checklist node 20: guided edges 201 and 206 pass through node 15, while quick edge 202 goes directly. Both return through edge 203. Visiting node 20 records either route; selecting edge 201 identifies the guided route.

Back at the options, edge 204 finishes preparation at terminal 30 when the request is ready and the session has visited the checklist. Edge 205 offers a guidance recap at terminal 40 only if the guided route was used. These are separate endings of this example.

The complete JSON graph includes the routes, localized labels and terminal content. The following extracts explain its rules; you do not need to run the graph to follow the decisions.

Combine a current value with a visited step

Section titled “Combine a current value with a visited step”

These two entries in the graph’s conditions array belong to edge 204:

[
{
"edge_id": 204,
"rule": "context_value",
"params": {
"key": "readyToSend",
"operator": "==",
"value": true
}
},
{
"edge_id": 204,
"rule": "visited_node",
"params": {
"node_id": 20,
"must_be_visited": true
}
}
]

The first entry reads the exact context key readyToSend. Although the operator is written ==, this rule compares values strictly: boolean true matches, but the string "true" and the number 1 do not. If the key is absent, this equality condition also fails. Supply a value with the type your definition expects.

The second entry asks whether node 20 occurs in the session’s node history. It succeeds after either route reaches the checklist, and it remains true after the session returns to node 10. It does not mean “the current node is 20” or “the person completed every item on the checklist.” The latter is a fact your application must establish, represented here by readyToSend.

Conditions attached to the same edge are combined with AND. Edge 204 requires both entries to pass: a ready request alone does not bypass the checklist, and visiting the checklist alone does not make the request ready. The evaluator checks the conditions in order and stops when one blocks the edge.

For several acceptable values of one field, a context_value rule can use membership. For example, a process that accepts the standard or priority service level can use these parameters:

{
"key": "serviceLevel",
"operator": "in",
"value": ["standard", "priority"]
}

With context {"serviceLevel": "priority"}, that rule passes; with {"serviceLevel": "basic"}, it fails. Here value is the list of acceptable values, and the context provides the value being looked up. Membership is also strict about types. Adding two equality conditions for the two service levels would require both equalities, rather than express this alternative.

A visited node does not always identify the choice that led there. In this graph, both routes reach node 20, but only the guided route uses edge 201. Edge 205, which opens the guidance recap, therefore uses this condition:

{
"edge_id": 205,
"rule": "selected_edge",
"params": {
"edge_id": 201,
"must_be_used": true
}
}

The outer edge_id attaches the condition to the choice being evaluated, 205. The edge_id inside params names the earlier choice to find in history, 201. Keeping those roles distinct is especially useful when the rule refers to a different part of a larger graph.

selected_edge means “this edge has already been accepted in this session.” It does not refer to an option highlighted in the interface or to the choice currently being requested. Conditions run before the requested edge and its destination are added to history. Requiring an unused edge to have already selected itself would therefore block its first use.

Both history rules test presence anywhere in the retained history. They do not test how recently something happened, how many times a node appears or the order of two events. The initial root already counts as a visited node.

The boolean flags can also require absence. For example, the quick route 202 is permitted only while the guided route has not been chosen:

{
"edge_id": 202,
"rule": "selected_edge",
"params": {
"edge_id": 201,
"must_be_used": false
}
}

The complete graph applies the converse rule to edge 201, checking that 202 has not been used. This makes guided and quick checks alternatives within one session. Returning to the options does not offer a switch to the other route. The route already taken is also subject to cycle protection, which prevents using the same edge again.

Use integer IDs and real booleans in these parameters. Omitting must_be_visited or must_be_used means true; writing the flag explicitly can make the intended requirement easier to read.

The table shows the two finishing options at node 10. “Available” means that their rules and the other navigation checks pass for the stated inputs.

Situation at node 10 readyToSend used for evaluation Finish preparation, 204 Guidance recap, 205
New session, no route taken false Blocked: request not ready Blocked: no guided route
New session, no route taken true Blocked: checklist not visited Blocked: no guided route
Returned through quick checks, 202, 203 true Available Blocked: 201 was not used
Returned through guided checks, 201, 206, 203 false Blocked: request not ready Available
Same guided history, with validated ready data true Available Available

Your application receives the edges with their availability in the node payload. It can show why preparation is still incomplete, or offer the guidance recap because the earlier route makes it relevant. It should request the selected edge through the engine, which evaluates the choice again. Condition evaluation and edge availability explains that relationship between the displayed options and the actual move.

Suppose the session has returned through the guided route. Its context still contains readyToSend: false, but your application has now validated the request. In this PHP fragment, $engine is the configured core engine, $graph is the loaded JSON graph, $state is that returned session, and $lang is the chosen presentation language:

use ExecKernel\Domain\Session\Context;
$result = $engine->choose(
$graph,
$state,
204,
$lang,
contextUpdates: new Context(['readyToSend' => true]),
);
$state = $result->state;
$payload = $result->payload;

The engine evaluates edge 204 with the proposed value and the existing history. The context condition sees true; the history condition finds node 20. On acceptance, the returned state is at terminal 30, with readyToSend: true and this route:

{
"nodes": [10, 15, 20, 10, 30],
"edges": [201, 206, 203, 204]
}

The payload has type terminal and the “Request ready” content. Your application can display that result and offer its submission action. Keep the returned state for subsequent session handling.

You can also inspect the choices with proposed values before making the move. As explained in Context updates and candidate values, that inspection does not save the proposal: supply the intended values again with choose(). If a choice is rejected, neither the proposed context nor the attempted route becomes an accepted step.

The complete PHP example loads the graph, follows both routes in separate sessions, compares the resulting choices and finishes the guided session. It assumes the installed core autoloader and the JSON file beside the script; it fixes the clock so its observations can be compared.

Use history rules to express requirements about the route itself. Keep authorization, data validation and evidence of real-world work in the application logic that owns them. The built-in rule reference covers the remaining operators and parameter contracts.