JFLib

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 NodeKey a cell of a sheet as a sortable key
    • sheet: SheetRef -- the sheet
    • sec: 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) -> NodeKey the key of a cell

  • fn addr(&self) -> CellAddr the cell address

  • enum Effect what 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 run

    • SetFormula(Target, String) -- SETF: give the cell this formula (the engine applies it)
    • OpenTextWindow(SheetRef) -- OPENTEXTWIN(): edit the shape's text
    • OpenShapeSheet(SheetRef) -- OPENSHEETWIN(): show the shape's ShapeSheet
    • OpenGroup(SheetRef) -- OPENGROUPWIN(): open the group in its own window
    • GotoPage(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 commands
    • AddOn(String) -- RUNADDON(name): the app runs its own counterpart when it has one (e.g. the shape data dialog), else reports it
    • Refused(String) -- RUNMACRO, CALLTHIS: foreign code, never run (FX-15)
  • enum Outcome how a cell came out

    • Value(Val) -- computed
    • Kept(Keep) -- not computed: the stored value stays
  • trait TextMetrics the size of text in the app's fonts

    • fn measure(&self, text: &str, font: &str, size: f64, max_width: Option<f64>) -> (f64, f64) (width, height) in inches of text in font at size (inches); lines break at max_width when given, and at line breaks
  • struct Estimate the estimate used without the app's fonts: 0.45 em a character, lines of 1.2 em

  • struct Results the result of a calculation

    • cells: Vec<(SheetRef, CellAddr, Outcome)> -- every computed or kept cell in the order computed
    • cycles: 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 cell

  • fn 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 written

  • struct Recalc what a recalculation did

    • computed: usize -- cells computed
    • written: usize -- cells whose stored value changed
    • kept: Vec<(SheetRef, CellAddr, Keep)> -- cells that kept their stored value, and why
    • cycles: 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 unit
    • Length(f64, Unit) -- a length or angle in a unit (25 mm)
    • Formula(&'a str) -- a formula
  • struct Engine the formula engine of one document

impl Engine

  • fn new() -> Engine an engine with the text estimate

  • fn with_metrics(metrics: Rc<dyn TextMetrics>) -> Engine an engine that measures text with the app's fonts

  • fn with_seed(seed: u64) -> Engine (JD15, FX-06) an engine whose RAND starts from seed (the same numbers every time)

  • fn seed(&mut self, seed: u64) (JD15, FX-06) RAND's generator anew from seed

  • fn reset(&mut self) forget everything (the document was replaced, or its themes changed); RAND's generator goes on

  • fn 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 them

  • fn calculate(&mut self, doc: &Document) -> Results compute every formula of the document (nothing is written; Results::write does); this also records the dependencies later recalculations follow

  • fn evaluate(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr) -> Outcome compute 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 stored

  • fn 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 into tx

  • fn recalc_cells(&mut self, tx: &mut Transaction, cells: &[(SheetRef, CellAddr)]) -> Result<Recalc, EditError> recompute these cells and every cell that depends on them

  • fn 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 anew

  • fn 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 not

  • fn guarded(&mut self, doc: &Document, sheet: SheetRef, addr: &CellAddr) -> bool whether 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) -> bool whether 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 recomputed

  • fn 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 in tx, also guarded ones (then dependents are recomputed); the other effects are for the app

  • fn 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 styles

  • fn all_sheets(doc: &Document) -> Vec<SheetRef> every sheet of the document: the document sheet, styles, pages, masters, the shapes of masters and pages

  • fn formula_cells(doc: &Document) -> Vec<NodeKey> every cell of the document whose value comes from a formula (sheet_formula_cells of all sheets)

Source: docs/API.md in the JFLib repository.