Skip to content

Graph loaders and repositories

A graph loader turns a definition from an array, a file or a database into the Graph object that ExecKernel uses to execute a process. Your application chooses the source when it configures the loader. Once the graph has been loaded, the engine receives the same kind of object, so the code that starts a session and presents its options can remain the same.

A repository handles the reading of stored data. The GraphRepositoryInterface::fetch() contract returns an array containing one complete definition. A loader then builds the nodes, edges and associated data, checks their consistency, and returns a Graph. In the SQL implementation, SqlGraphRepository reads the rows and SqlGraphLoader passes that definition through ArrayGraphLoader.

Choose according to where your application keeps its definitions and what it already has in memory.

Implementation Input A suitable use
ArrayGraphLoader One normalized definition array, supplied to its constructor; load() receives the expected identity. Your PHP code already has the definition as data.
JsonGraphLoader A JSON string or the path to a JSON file, passed to load(). Import one known JSON document.
DslGraphLoader FlowDSL text or the path to a .dsl / .flowdsl file. Load a definition written in FlowDSL.
LocalGraphLoader A configured directory or a map of definition arrays; load() selects a graph by code. It also accepts a file path directly. Your application selects named definitions distributed with its files or kept in memory.
SqlGraphLoader with SqlGraphRepository A repository constructed with a PDO connection; load() selects a stored graph. Your application keeps its definitions in the supported SQL tables.

For a directory of versioned JSON files, LocalGraphLoader gives the rest of your application a stable selection such as load('learning-path'). A database source becomes useful when your application already manages graph data in tables. The SQL repository reads those tables; your application supplies the connection, schema and records.

The array expected by ArrayGraphLoader uses fields such as graph_code, root_node_id and is_terminal. A representation must reach the loader that understands it: JsonGraphLoader parses JSON, while DslGraphLoader parses FlowDSL. YAML goes through the parser and import path explained in JSON and YAML graph definitions.

In LocalGraphLoader, the constructor parameter named graphRepository is a PHP map of definition arrays. It is separate from the GraphRepositoryInterface object accepted by SqlGraphLoader. If you configure both a directory and an in-memory map, matching files take precedence.

The following comparison reuses learning-path, introduced in Anatomy of a graph. At node 10, a person can choose Read the guide through edge 101, or Practise in the browser through edge 102. The two choices lead to terminals 20 and 30. A condition on 102 requires practiceEnabled to be true.

The examples assume ExecKernel’s Composer autoloader is already loaded. The source definition is available as learning-path.json; the SQL version is prepared in SQL-backed graphs.

Suppose your application’s graphs/ directory contains learning-path.json. Configure that directory as the source:

use ExecKernel\Infrastructure\Loader\LocalGraphLoader;
$loader = new LocalGraphLoader(
basePath: __DIR__ . '/graphs',
);

The loader can now resolve a graph code within that directory. Your application chooses the graph by its name; it does not need to assemble a new file path at each call.

For the database source, $pdo is a connection to the prepared graph database, in exception mode and with no transaction open. The SQL guide shows how to create that connection and populate the tables.

use ExecKernel\Infrastructure\Loader\SqlGraphLoader;
use ExecKernel\Infrastructure\Repository\SqlGraphRepository;
$repository = new SqlGraphRepository($pdo);
$loader = new SqlGraphLoader($repository);

The repository knows how to reconstruct the stored definition. The loader uses that definition to build the graph. Configuring these objects does not read a graph yet; the read happens when your application calls load().

With either configuration, the selection is identical:

$graph = $loader->load('learning-path');

For the directory source, this selects learning-path.json and checks that its internal code is learning-path. For SQL, it selects the row whose code is learning-path and reads the associated collections. The returned object has root node 10, three nodes, two edges, and their conditions, translated text and terminal outputs.

That object can go straight to the engine. This common block starts the graph in English with practice enabled:

use ExecKernel\Domain\Session\Context;
use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
$engine = (new ExecKernelFactory())->build();
$result = $engine->start(
$graph,
'en',
new Context(['practiceEnabled' => true]),
);
$title = $result->payload['title'];
$options = $result->payload['edges'];

Your application receives the title How would you like to learn?. The two entries in $options contain the same information whichever source loaded the graph:

Edge ID labels[0].text availability
101 Read the guide available
102 Practise in the browser available

Your application can use the labels for its buttons and retain each edge ID for the subsequent choice. The condition still belongs to the graph: if the application starts with practiceEnabled: false, edge 102 is reported as blocked with enabled: false. Changing the data source does not change that decision.

For the surrounding integration, including the facade that combines graph loading with engine calls, see Embedding the engine · Factory and Facade.

Distinguish a graph code from a source path

Section titled “Distinguish a graph code from a source path”

A code identifies the definition, while a path identifies a file containing it. This matters when you replace the loader.

With LocalGraphLoader, load('learning-path') is a named lookup and verifies the definition’s code. A direct file path, including a filename resolved under basePath, requests that file without requiring its filename to match the internal code.

JsonGraphLoader and DslGraphLoader receive the source text or path directly. Their optional expectedIdentity argument adds an identity check when your application expects a particular definition:

use ExecKernel\Infrastructure\Loader\JsonGraphLoader;
$graph = (new JsonGraphLoader())->load(
__DIR__ . '/graphs/learning-path.json',
expectedIdentity: 'learning-path',
);

Use the canonical string code for application-level selection. Some adapters also accept an integer graph ID; the SQL guide explains how that ID relates to the code.

If the source cannot be read, the identity does not match, or the definition contains invalid references, loading raises an exception instead of giving your application an executable graph. The appropriate correction belongs to the source or definition; Graph validation and diagnostics explains how to interpret those failures.

Loading a definition and retaining a session

Section titled “Loading a definition and retaining a session”

A loaded graph describes the process shared by its sessions. It tells the engine which nodes and edges exist, what their conditions are, and which text and outputs accompany them.

A session records a particular execution: its current node, context and history. Reading the graph again does not restore those values. Your application must retain session state separately if it needs to continue a person’s earlier execution. The choice of a JSON or SQL graph source is therefore independent of the session store and persistence mode.

If your definitions come from another source, the extension point is described in Custom validators and loaders. The PHP interface reference provides the exact public contracts.