diagram_core/src/formula/engine.rs#
Source: lib/diagram_core/src/formula/engine.rs
engine -- recalculation over the cells of a document (FX-02, FX-04)
let mut eng = Engine::new(); one engine per document
let res = eng.calculate(&doc); every formula, in dependency order; nothing
written (res.write(&mut tx) does)
eng.recalc(&mut tx, &sheets) after an edit in tx: the cells of these sheets
(stale ones included) and all that depend on
them, results written into tx (undo keeps them)
eng.recalc_cells(&mut tx, &cells) the same for single cells
eng.refresh(&doc, &change.sheets()) after undo/redo: dependencies again
eng.user_set(&mut tx, sheet, addr, input) a user's change: GUARD, SETATREF redirection
eng.run_action(&mut tx, sheet, addr) an action or event cell: SETF and effects
eng.evaluate(&doc, sheet, addr) one cell with stored inputs
Engine::with_seed(seed) / eng.seed(seed) (JD15, FX-06) RAND's generator from that seed
(SplitMix64; every engine starts with the same
one, an app may seed it, e.g. from a clock)
Nothing is recalculated on its own: opening a file computes nothing (PLAN.md, "Fallen"). A
formula that cannot be computed here -- an unknown function, a glue helper, a cycle -- keeps
its stored value and is reported (Keep); a value is written only with the formula it
belongs to.
Dependencies are recorded while formulas run: every cell a formula reads (also the ones
THEMEVAL, PAR or TEXTWIDTH read behind the scenes, and the master or style cell an
inherited formula comes from) becomes an edge. The first recalc builds the graph with a
full calculation; later ones follow the edges from the changed sheets. Formulas are pulled
in: a cell that reads another cell not computed yet computes that one first (depth-limited;
a cell met again while it is being computed is a cycle and gives its stored value).
struct NodeKeya cell of a sheet as a sortable keysheet: SheetRef-- the sheetsec: Option<SecKey>-- the section (None: a single cell)row: Option<RowKey>-- the row (None: a single cell or a cell of the section itself)cell: Sym-- the cell name
impl NodeKey
fn new(sheet: SheetRef, addr: &CellAddr) -> NodeKeythe key of a cellfn addr(&self) -> CellAddrthe cell addressenum Effectwhat an action asks for besides changing formulas (SETF is done by the engine itself); the app carries them out -- or not: a RUNADDON the app does not know, a macro, CALLTHIS are reported, never runSetFormula(Target, String)-- SETF: give the cell this formula (the engine applies it)OpenTextWindow(SheetRef)-- OPENTEXTWIN(): edit the shape's textOpenShapeSheet(SheetRef)-- OPENSHEETWIN(): show the shape's ShapeSheetOpenGroup(SheetRef)-- OPENGROUPWIN(): open the group in its own windowGotoPage(String)-- GOTOPAGE(name)Hyperlink(String, String)-- HYPERLINK(address, sub-address)PlaySound(String)-- PLAYSOUND(file or sound name)Command(i64)-- DOCMD(n): one of Visio's command numbers, for the app to map to its own commandsAddOn(String)-- RUNADDON(name): the app runs its own counterpart when it has one (e.g. the shape data dialog), else reports itRefused(String)-- RUNMACRO, CALLTHIS: foreign code, never run (FX-15)
enum Outcomehow a cell came outValue(Val)-- computedKept(Keep)-- not computed: the stored value stays
trait TextMetricsthe size of text in the app's fontsfn measure(&self, text: &str, font: &str, size: f64, max_width: Option<f64>) -> (f64, f64)(width, height) in inches oftextinfontatsize(inches); lines break atmax_widthwhen given, and at line breaks
struct Estimatethe estimate used without the app's fonts: 0.45 em a character, lines of 1.2 emstruct Resultsthe result of a calculationcells: Vec<(SheetRef, CellAddr, Outcome)>-- every computed or kept cell in the order computedcycles: Vec<(SheetRef, CellAddr)>-- cells met while being computed (cycles); they used their stored value
impl Results
fn get(&self, sheet: SheetRef, addr: &CellAddr) -> Option<&Outcome>the outcome of one cellfn write(&self, tx: &mut Transaction) -> Result<usize, EditError>write every result that differs from the stored value into the transaction (cells that inherit their formula get an F="Inh" cell); -> the number of cells writtenstruct Recalcwhat a recalculation didcomputed: usize-- cells computedwritten: usize-- cells whose stored value changedkept: Vec<(SheetRef, CellAddr, Keep)>-- cells that kept their stored value, and whycycles: usize-- cells in cycles
/// the protection cells ([MS-VSDX] Protection section, SS-15)
LOCKS: &[&str] = &[ "LockWidth", "LockHeight", "LockAspect", "LockMoveX", ...
enum Input<'a>a user's change of a cell (Engine::user_set)Value(Value, Option<Unit>)-- a value in internal units, shown in the unitLength(f64, Unit)-- a length or angle in a unit (25 mm)Formula(&'a str)-- a formula
struct Enginethe formula engine of one document
impl Engine
fn new() -> Enginean engine with the text estimatefn with_metrics(metrics: Rc<dyn TextMetrics>) -> Enginean engine that measures text with the app's fontsfn with_seed(seed: u64) -> Engine(JD15, FX-06) an engine whose RAND starts fromseed(the same numbers every time)fn seed(&mut self, seed: u64)(JD15, FX-06) RAND's generator anew fromseedfn reset(&mut self)forget everything (the document was replaced, or its themes changed); RAND's generator goes onfn themes_changed(&mut self)forget the parsed theme parts (JD11: a design was applied or a part replaced); the caller recalculates the sheets that use themfn calculate(&mut self, doc: &Document) -> Resultscompute every formula of the document (nothing is written; Results::write does); this also records the dependencies later recalculations followfn evaluate(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr) -> Outcomecompute one cell from the stored values of its inputs (nothing else is recomputed)fn evaluate_text(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr, text: &str) -> Result<Outcome, ParseError>evaluate a formula text for a cell without storing anything (the formula editor's preview); inputs as storedfn recalc(&mut self, tx: &mut Transaction, sheets: &[SheetRef]) -> Result<Recalc, EditError>recompute after an edit: the formula cells of these sheets (Change::sheets(); stale cells among them) and every cell that depends on them; results go intotxfn recalc_cells(&mut self, tx: &mut Transaction, cells: &[(SheetRef, CellAddr)]) -> Result<Recalc, EditError>recompute these cells and every cell that depends on themfn refresh(&mut self, doc: &Document, sheets: &[SheetRef])after undo or redo (the stored values are right again): record the dependencies of the cells of these sheets anewfn locks(&mut self, doc: &Document, sheet: SheetRef) -> Vec<&'static str>the protection cells of a sheet (SS-15) that are set, with inheritance: "LockWidth", "LockMoveX", "LockThemeColors" ... -- gestures (JD7) and applying a theme (JD11) ask them; a direct change in the ShapeSheet does notfn guarded(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr) -> boolwhether the effective formula of a cell is protected against the user's edits (GUARD around it)fn theme_guarded(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr) -> boolwhether the formula keeps its value when a theme or style is applied (THEMEGUARD around it, or GUARD)fn user_set(&mut self, tx: &mut Transaction, sheet: SheetRef, addr: &CellAddr, input: Input) -> Result<Recalc, EditError>a user's change of a cell (a gesture or a dialog): a guarded formula refuses it (EditError::Guarded); a formula with SETATREF keeps its formula and the change goes to the cells SETATREF names; then everything that depends on it is recomputedfn run_action(&mut self, tx: &mut Transaction, sheet: SheetRef, addr: &CellAddr) -> Result<Vec<Effect>, EditError>run an action or event cell (Actions.Row_1.Action, EventDblClick ...): SETF changes formulas intx, also guarded ones (then dependents are recomputed); the other effects are for the appfn graph_size(&mut self) -> (usize, usize)the number of cells in the dependency graph (for tests and statistics)fn sheet_formula_cells(doc: &Document, s: SheetRef) -> Vec<NodeKey>the cells of one sheet whose value comes from a formula: its own cells with a formula (also F="Inh") and the cells it inherits a formula for from master shapes and stylesfn all_sheets(doc: &Document) -> Vec<SheetRef>every sheet of the document: the document sheet, styles, pages, masters, the shapes of masters and pagesfn formula_cells(doc: &Document) -> Vec<NodeKey>every cell of the document whose value comes from a formula (sheet_formula_cells of all sheets)