# `Macaw.Page.Graph`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/page/graph.ex#L1)

A page's DAG (spec/03 §6): every source on the page as a node, every binding as an edge,
built by pure functions when the page validates (`Macaw.Page.validate/3`) and held in
`%Macaw.Page.Validated{}`'s `graph`.

**Nodes:**
  * `{:feed, name}`, a host source a slot or a derived source reads;
  * `{:derived, name}`, one of the page's derived sources (`derived:`, spec/03 §6.1);
  * `{:state, surface, name}`, a state source a surface holds, its mirrors among them;
  * `{:export, surface, name}`, an export (spec/03 §5).

A **cell**, `{:cell, surface, cell}`, is where bindings end: it reads, and nothing reads
it.

**Edges,** `%{from: node, to: node | cell, via: via, crosses: crosses}`, each a binding:
  * `:export`, a state to the export naming it; `:mirror`, an export to a mirror reading it;
  * `{:input, i}`, a derived source's `i`th input;
  * `{:slot, slot}`, a slot bound to a feed, a derived source or a state;
  * `{:request, slot, param}`, a slot's request param bound to a state;
  * `{:param, param}`, a setting bound to a state;
  * `{:window, slot}` and `:selection`, the standard state a slot or a cell reads by its
    schema (spec/03 §1.2, §1.7).

`crosses` is `nil` for an edge whose two ends one process holds, else `{from, to}`, the
holders of its two ends (spec/03 §6.4): `:page`, `{:region, name}` for a persistent
region, `:visit` for `:session` state and `{:shared, surface}` for `:shared` state.

`order` is every node in rank order, `ranks` each node's rank: a node no node reads into
is 0, any other one more than its greatest input. `engine` is the page's
`Macaw.Data.Derived.Engine` over its derived sources, `nil` with none.

`shared` is each `:page` identity two or more of the page's surfaces declare (spec/03
§2.1), `{:page, name}`, to those surfaces in name order: one value, held by each
surface's own node and judged by its own declaration, so a write on one reaches the
others by this index (`sharing/2`), never by a walk of the page's surfaces.

# `cell`

```elixir
@type cell() :: {:cell, String.t(), String.t()}
```

# `described`

```elixir
@type described() :: %{nodes: [described_node()], edges: [described_edge()]}
```

A page's data flow described as plain nodes and edges (spec/06 §1.4).

# `described_edge`

```elixir
@type described_edge() :: %{
  source: String.t(),
  target: String.t(),
  kind:
    :export
    | :mirror
    | :input
    | :slot
    | :request
    | :param
    | :window
    | :selection,
  label: String.t()
}
```

A described edge: its two ends' ids, the binding's kind and its label.

# `described_node`

```elixir
@type described_node() :: %{
  id: String.t(),
  kind: kind(),
  label: String.t(),
  surface: String.t() | nil,
  rank: non_neg_integer()
}
```

A described node: its id, kind, label, surface (or `nil`) and rank.

# `edge`

```elixir
@type edge() :: %{
  from: node_id(),
  to: node_id() | cell(),
  via: term(),
  crosses: nil | {holder(), holder()}
}
```

# `holder`

```elixir
@type holder() :: :page | :visit | {:region, String.t()} | {:shared, String.t()}
```

# `kind`

```elixir
@type kind() :: :source | :derived | :state | :export | :cell
```

What a described node is (spec/06 §1.4): a feed is a `:source`.

# `node_id`

```elixir
@type node_id() ::
  {:feed, String.t()}
  | {:derived, String.t()}
  | {:state, String.t(), String.t()}
  | {:export, String.t(), String.t()}
```

# `t`

```elixir
@type t() :: %Macaw.Page.Graph{
  crossing: [edge()],
  edges: [edge()],
  engine: Macaw.Data.Derived.Engine.t() | nil,
  homes: %{required(node_id() | cell()) =&gt; holder() | nil},
  inputs: %{required(node_id() | cell()) =&gt; [edge()]},
  order: [node_id()],
  ranks: %{required(node_id()) =&gt; non_neg_integer()},
  readers: %{required(node_id()) =&gt; [edge()]},
  shared: %{required({:page, String.t()}) =&gt; [String.t(), ...]}
}
```

# `crossing`

```elixir
@spec crossing(t()) :: [edge()]
```

Every edge whose two ends different processes hold (spec/03 §6.4), each with its
`crosses`, in the graph's order: the bindings a live runtime carries between processes,
and no other. They are found once, when the graph is built (#417).

# `describe`

```elixir
@spec describe(term()) :: {:ok, described()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The page's data flow as plain nodes and edges (spec/06 §1.4), for a diagram drawn with
no dependency on this library: of a graph, or of a `%Macaw.Page.Validated{}` its graph,
`{:ok, %{nodes, edges}}`, read from the graph's own nodes, ranks and edges.

  * **Nodes,** each `%{id, kind, label, surface, rank}`: every node of the graph and
    every cell a binding ends at, but a standard state (`window`, `selection`) no binding
    reads. `kind` is `:source` (a feed), `:derived`, `:state`, `:export` or `:cell`;
    `id` its kind and name, `"source:<name>"`, `"derived:<name>"`,
    `"state:<surface>.<name>"`, `"export:<surface>.<name>"` or
    `"cell:<surface>.<cell>"`; `label` its name, a surface's own as
    `"<surface>.<name>"`; `surface` its surface's name, `nil` for a source or a derived
    source; `rank` its rank in the graph, a cell's one more than its greatest input's.
    In rank order, then as the graph orders nodes.
  * **Edges,** each `%{source, target, kind, label}`, one per binding, in the graph's
    order: its ends' ids, `kind` the binding's (`:export`, `:mirror`, `:input`, `:slot`,
    `:request`, `:param`, `:window`, `:selection`), and `label` that kind's word and what
    it binds: `"slot rows"`, `"request rows.service"`, `"param sort"`, `"input 0"`,
    `"window rows"`, `"export"`, `"mirror"`, `"selection"`.

Anything else is `{:error, [{[], {:type, :graph}}]}`.

# `new`

```elixir
@spec new(
  [{String.t(), [term(), ...], Macaw.Surface.Validated.t()}],
  Enumerable.t(),
  Macaw.Data.Derived.Engine.t() | nil
) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The graph of a page's validated surfaces (spec/03 §6.2), as `Macaw.Page.validate/3`
builds it: `indexed` each surface the page holds as `{region, at, validated}`, its
region and its path from the page (`[:regions, region, i]`, a pane's under its
container, spec/05 §5.1), `persistent` the names of the persistent regions, and `engine` the page's derived sources' engine, or `nil`.
Answers `{:ok, graph}`, or `{:error, faults}`: each cycle, `{:cycle, nodes}`, its nodes
in order from the first and back to it, at the `from` of the first mirror in it.

# `provenance`

```elixir
@spec provenance(t(), node_id() | cell()) :: {:ok, [edge()]} | :error
```

Why a node or a cell holds what it holds (spec/03 §6.5): `{:ok, edges}`, every edge on a
path into it, each once, by the rank of the node it leaves and then as the graph orders
edges, so the first edges leave the sources everything else is computed from; `:error`
for what the graph doesn't hold.

# `readers`

```elixir
@spec readers(t(), node_id()) :: [edge()]
```

The edges out of a node (spec/03 §6.2): its readers, each edge once, `[]` for a node
nothing reads or the graph doesn't hold.

# `sharing`

```elixir
@spec sharing(t(), node_id()) :: [node_id()]
```

The other surfaces' nodes holding the value a surface's state node holds (spec/03 §2.1,
§6.2): for a state its surface declares at `:page`, each other surface's state of that
name declared at `:page`, in surface name order; `[]` for any other node. A write on one
is put into each of them, read from the graph's `shared` index at a cost independent of
the page's surfaces.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
