Production graph
public RecursiveCollection<UIProductionEntry> Catalogue { get; } =
[
new UIResource("ore") { Title = "Iron ore", Icon = MaterialIcons.Landscape, Category = "Ores", Cost = 1 },
new UIResource("plate") { Title = "Iron plate", Icon = MaterialIcons.Layers },
new UICraft("smelt") { Title = "Smelting", Ingredients = [new("ore", 1)], Products = [new("plate", 1)], Time = TimeSpan.FromSeconds(3.2) }
];
new ProductionGraphComponent("factory")
.BindItems(nameof(Controller.Catalogue))
.SetFormId("factory-layout")
.BindValue(nameof(Controller.Layout), mode: UIBindingMode.OnSubmit)
.SetEditStructure(true)
.OnSave(nameof(Controller.Save));
ProductionGraphComponent draws resources and the crafts between them from one bound collection of UIProductionEntry. A
resource (UIResource: title, icon or picture, colour, category, unit, cost) is drawn as a graph's node — a circle by default,
a card with NodeShape = Card; a circle carries its name under it. A craft (UICraft) is what one run of a recipe takes
(Ingredients), gives (Products, several when a recipe has by-products) and lasts (Time). Every edge says how much of its
resource one run takes, written as a count of the run (×100 L) beside that resource, and the arrow says which way it goes. The
layout, the directions — left to right, right to left, top to bottom, bottom to top — the backward edges of a cycle and the document
of places are the graph's: a plant grown from its own seed stands seed, plant, product in one row, the plant's edge back to its
seed a short dashed arc between the two, clear of the chips on their tops.
A recipe that gives one resource nothing else makes is drawn on its edges rather than as a node of its own: its ingredients run
straight into that resource, each edge saying ×N, and the resource carries the run on itself — ×1 · 3.2 s, what one run gives of
it and how long it lasts. A junction is drawn where one is needed to say which edges belong to one run: a craft with several products,
or a resource several crafts make. While the pointer rests on a resource or a craft, the whole line it stands on keeps its colour —
everything it is made from, all the way up, and everything made from it, all the way down, with the edges between — and the rest of
the sheet steps back; a branch that leaves the line sideways is not on it. HighlightOnHover says whether a canvas does this: a
layered graph does unless told otherwise, and the node canvas does not unless asked.
With EditStructure, Add resource puts a resource where the pointer was; a dot pulled from a resource to another resource makes
a craft of one of each, into a craft adds an ingredient, and out of a craft to a resource adds a product. A recipe drawn as its
edges has no junction to drop on, so a link dropped on the resource it makes asks, in a menu where the pointer let go, which was
meant — Add as an ingredient of that recipe, or New recipe, a second way to make the resource (LinkMenu); dismissed, the link is
let go. The right button on an edge edits its amount or takes it out, and on a craft its Time. A resource's menu has Duplicate
(Ctrl+D), a copy of the resource a step off with none of its recipes, and Disconnect all, which takes every amount naming it
out of its recipes, a recipe left with nothing going with it; both are disabled on a craft, which is its edges — Delete takes it. It all goes into UIProductionDocument.Draft until the save,
which applies it as the graph's does: Layout.Draft.ApplyTo(Catalogue, EditStructure, Mode) and Layout = Layout.WithoutDraft(). ApplyTo
refuses the draft whole while editing is off, under Plan or past its cap (maxEntries, 10 000), and writes only what the canvas
edits — a resource's title and colour, a craft's title, colour and amounts (a new recipe's time too).
A plan over the catalogue
new ProductionGraphComponent("plan")
.BindItems(nameof(Controller.Catalogue))
.SetMode(UIProductionMode.Plan)
.SetFormId("plan").BindValue(nameof(Controller.Plan), mode: UIBindingMode.OnSubmit)
.OnSave(nameof(Controller.Save));
Mode puts the same component to its second use. In UIProductionMode.Plan the viewer names amounts to reach — Add target in the
panel, which opens a picker of the catalogue's resources, pictures and all, or Target on a resource's own menu once the sheet draws it — and the canvas solves, in the
browser and at once, how many runs of which craft reach them. Brought in, on the menu of a resource something makes, takes what
makes it out of the plan: the resource is then counted with the sources, as a part bought rather than built is. The sheet then draws the plan rather than the catalogue: the crafts
that run and the resources they touch, the plan's totals where one run's numbers stood (×20 · 40 s on a resource made once,
×120 · 4 at once on one counted over a period), the targets edged in the accent colour. The sheet says each number once: the period is the panel's
to name, and an edge carries its amount only where the resource it leaves is shared out between several crafts or not all taken —
along a chain the resource's own chip has said it. With no target, or none that can be reached, the sheet is empty — the catalogue drawn whole read as a plan. The catalogue is
not edited in this mode, whatever EditStructure says — on the server too, where ApplyTo refuses a draft under Plan; each use keeps a document of its own, so an application that wants both
binds two — a second component, or a Mode it switches with another document behind it. The panel ends above the zoom bar, so
the bar keeps its corner however long the plan runs.
The panel over the sheet's trailing side is where the plan is asked for and read: the targets and their amounts, what they are
counted over — once, or every minute or hour of a line that keeps running — and what is made least where the catalogue
leaves a choice (two recipes for one resource, a by-product that covers a demand): what is brought in, the time, or the cost
(UIResource.Cost, a source with none counting as one, and so does a cost below zero or not a number, since a source that paid
to be taken would make the least cost fall without end; among equals the second thing decides — the soonest, or the least brought
in). Under them, what is brought in of every source, every made resource's balance (made, taken, left over — a by-product nobody
takes shows here), and every craft's runs, their time and, for a plan counted over a period, how many of the craft keep up
(ceil(runs × time / period)). A row pressed chooses its item on the sheet and brings it into view; a row's name is a button,
which keeps the keyboard when it is pressed, so the tables are read on from where the reader was. The panel's fields are the core's own
(PlanPeriod, PlanObjective and PickerSearch are the component's, for an application to re-title); it folds to its switch, and
Fit keeps clear of its column while it is open, and of its switch folded. Below the md breakpoint — a phone, where it would
stand over the sheet it plans — it starts folded, and its switch opens it; a viewer's own fold or opening is remembered, and wins.
What was asked is part of the document — UIProductionDocument.Plan, a UIProductionPlanRequest of Targets, Period,
Objective and Bought — so a plan is saved as a layout is, WithoutDraft() keeps it, and WithPlan(...) is how an application names the
targets itself — an application that keeps its goals on a page of its own binds the document one way, read-only, and hands it
WithPlan of them each time they change. UIProductionPlanner.Solve(entries, request) is the same solver on the server, for a controller that wants the
numbers: a UIProductionPlan of Crafts (runs, time, workers), Resources (made, taken, surplus, BroughtIn) and the totals,
with Status saying Empty, Solved, Infeasible — a cycle that takes more than it gives has no plan — or Unsettled, when
the solver found no answer it can stand by, a cost that falls without bound or a table that would not settle — or TooLarge, for a
plan past its maxCells (a million, DefaultMaxCells), which it does not try. It is the same
algorithm the browser runs, so the server and the canvas reach the same answer. A plan counted over a period is steady-state — two and a half runs a minute is a rate like any
other — and nothing is queued or scheduled. A plan made once runs every craft a whole number of times: the runs are rounded up,
what that leaves short further up is made up by whole runs until nothing is, and what it leaves over shows as surplus; that is the
least such plan wherever each resource has one recipe, and close to it elsewhere. A sheet whose plan changes which nodes it draws
is laid out and shown whole again.