Schematex

Decision tree diagram

About decision trees

A decision tree is a branching diagram that represents a sequence of choices and their consequences as a rooted tree: each internal node is a question or decision, each edge is an answer or action, and each leaf is an outcome. The format appears across three quite different practices — troubleshooting flowcharts and clinical decision rules (taxonomy trees), risk and investment analysis using expected value (decision analysis), and machine learning model inspection (classifier trees). Despite surface differences all three share the same tree structure, which is why Schematex encodes them under one keyword with a mode selector.

Schematex decision trees cover: (1) taxonomy mode — the yes/no question flows used in medical triage (Turing 1937 lineage; now standard in clinical decision support), (2) decision analysis mode — the expected-value rollback method developed in management science (Raiffa & Schlaifer, 1961), and (3) ML mode — the CART split/leaf format used to visualize scikit-learn and similar trained classifiers (Breiman et al., 1984).

decisiontree·§
↘ preview
100%
Line 5: Orphan line (bad indent): yes: question "Outage confirmed on status page?"
UTF-8 · LF · 12 lines · 533 chars✗ parse error

1. Your first decision tree

The smallest useful decision tree: a root question with two branches.

decisiontree·§
↘ preview
100%
Line 4: Orphan line (bad indent): yes: answer "Check display — connect external monitor"
UTF-8 · LF · 7 lines · 257 chars✗ parse error

Four rules cover 80% of usage:

  1. Start with decisiontree, optionally with :mode and a quoted title.
  2. Each question node uses question "text" (or shorthand q "text").
  3. Each answer/leaf uses answer "text" (or a "text" or leaf "text").
  4. Branch labels — yes:, no:, or a custom label "X": — prefix the child node on the same line.

Indentation controls nesting: each level adds 2 spaces. The parser computes parent-child relationships from indent depth.

Comments must start with # or // on their own line.


2. Modes

The mode is set in the header line:

HeaderModeUsed for
decisiontreetaxonomyYes/no question flows, troubleshooting guides, clinical decision support
decisiontree:decision (or decisiontree:da)decision analysisInvestment decisions, risk analysis, expected value calculation
decisiontree:mlmachine learningVisualizing trained CART classifiers (scikit-learn, XGBoost, etc.)

Default direction is top-down for taxonomy and ML, left-right for decision analysis.


3. Taxonomy mode

Best for: troubleshooting guides, FAQs, clinical protocols, product recommendation flows.

Node keywords

KeywordAliasesMeaning
question "…"q "…"Internal node — a question with children
answer "…"a "…", leaf "…"Leaf node — a terminal outcome

Branch labels

SyntaxMeaning
yes: question "…"Branch labeled "yes"
no: answer "…"Branch labeled "no"
label "Custom text": answer "…"Branch with any custom label

Custom labels let you go beyond yes/no for multi-way decisions from one question.

decisiontree·§
↘ preview
100%
Line 4: Orphan line (bad indent): yes: q "ECG changes present?"
UTF-8 · LF · 11 lines · 391 chars✗ parse error
decisiontree·§
↘ preview
100%
Line 4: Orphan line (bad indent): label "Severe (8-10)": answer "Emergency — send to ER immediately"
UTF-8 · LF · 7 lines · 322 chars✗ parse error

4. Decision analysis mode

Best for: investment decisions, build-vs-buy analysis, risk-weighted strategy evaluation.

Node keywords

KeywordAliasesMeaning
decision "…"Decision node — the actor chooses a branch
chance "…"Chance node — an uncertain outcome
end "…"outcome "…"Terminal node — final payoff

Branch keywords

KeywordMeaning
choice "label"Names the incoming branch from a decision node
prob NSets the probability (0–1) on the incoming branch from a chance node

Payoff attribute

payoff=N on any node sets the payoff value. On end / outcome nodes it defines the terminal value. The parser runs expected-value rollback automatically: each chance node's EV is the probability-weighted sum of its children's EVs; each decision node's EV is the maximum child EV, and the optimal branch is flagged.

Constraint: probabilities on all direct children of a chance node must sum to 1.0 (±0.01). The parser throws a DTreeParseError if they do not.

decisiontree:decision·§
↘ preview
100%
Line 4: Orphan line (bad indent): choice "Build in-house"
UTF-8 · LF · 13 lines · 459 chars✗ parse error

5. Machine learning mode

Best for: explaining trained CART classifiers, model transparency reports, feature importance analysis.

Node keywords

KeywordMeaning
split "…"Internal split node — contains a feature test
leaf "…"Leaf node — class or regression value

Branch prefixes

true and false prefix child nodes to mark which branch each child represents.

Properties (key=value, no colon, no quotes around values)

PropertyApplies toMeaning
feature=namesplitFeature name used at the split
op="<="splitComparison operator (quote if contains special chars)
threshold=5.9splitSplit threshold value
samples=150split, leafSample count at this node
gini=0.5split, leafGini impurity
entropy=0.5split, leafEntropy impurity
mse=0.3split, leafMean squared error (regression)
gain=0.2split, leafInformation gain
class=nameleafPredicted class name
value=50leafSample count; use value=[50,30,20] for class distribution
decisiontree:ml·§
↘ preview
100%
Line 6: Orphan line (bad indent): true leaf "Setosa" class=Iris-setosa value=50 gini=0.0
UTF-8 · LF · 9 lines · 456 chars✗ parse error

6. Config options

Config lines appear between the header and the first node. Each is key: value (colon, no config keyword).

Shared config (all modes)

KeyValuesDefaultEffect
direction:top-down, left-righttop-down (taxonomy/ML), left-right (decision)Layout direction
edgeStyle: (or edge-style:)diagonal, orthogonal, bracketmode-dependentEdge drawing style

Taxonomy config

KeyValuesDefaultEffect
branchLabels: (or branch-labels:)boolean, relationbooleanBranch label style

Decision analysis config

KeyValuesDefaultEffect
branchLength: (or branch-length:)probabilityoffScale branch length proportional to probability

ML config

KeyValuesDefaultEffect
impurity:gini, entropy, mse, gainginiImpurity metric shown on nodes
classes:comma-separated listClass label names for display
decisiontree:ml "Loan classifier"
direction: top-down
impurity: gini
classes: Approved, Denied, Review

7. Labels & comments

  • Diagram title: decisiontree "Title" — the quoted string after the header keyword.
  • Node label: the quoted string immediately after the node keyword — question "Is the fee waived?".
  • Branch label: yes:, no:, or label "Custom": before the child node — on the same line as the child.
  • Payoff: payoff=250000 at the end of a decision/end node line.
  • ML properties: key=value tokens after the node's label string (no […] brackets, no colons).
  • Comments: # or // at the start of a line (after optional leading whitespace). Only full-line comments are supported — inline trailing comments are not.

8. Reserved words & escaping

Reserved node keywords: decision, chance, end, outcome, choice, prob, split, leaf, question, q, answer, a.

Reserved branch prefixes: yes:, no:, true, false, label.

Reserved header forms: decisiontree, decisiontree:decision, decisiontree:da, decisiontree:ml.

Strings with spaces must be double-quoted: question "Annual revenue > $1M?". Node labels, branch labels from label "…": syntax, and the diagram title all require double quotes.

Reserved tokenContextNotes
yes: / no:Line start in taxonomyCannot be used as a label — use label "yes": if you need literal text "yes"
true / falseLine start in ML modeCannot be a node label
choiceLine start in decision modeActs as branch wrapper, not a node
probLine start in decision modeMust be followed by a number

9. Common mistakes

You wroteParser saysFix
yes: "Approve" (no node keyword)DTreeParseError: Missing taxonomy node kindyes: answer "Approve"
Probabilities on chance children summing to 0.8DTreeParseError: probabilities do not sum to 1.0Adjust so all prob values sum to exactly 1.0 (±0.01)
question "text" with a child at the same indent levelChild not parsed as a child — becomes a siblingIndent children by 2 more spaces than the parent
config direction = top-down (using config keyword)config is a fishbone keyword — not recognized hereUse direction: top-down (no config prefix)
feature = petal_length (spaces around =)Parsed as separate tokens; property not recognizedNo spaces: feature=petal_length
[payoff: 500000] bracket syntaxNot recognized — parser ignores brackets for payoffUse payoff=500000 (no brackets, no spaces around =)
decisiontree:taxonomyDTreeParseError: Invalid headerUse decisiontree (no mode suffix for taxonomy)

10. Grammar (EBNF)

document       = header ( config-line )* node

header         = "decisiontree" ( ":" mode )? ( WS quoted-string )? NEWLINE
mode           = "decision" | "da" | "ml"
                 // omitted → taxonomy

config-line    = config-key ":" WS config-value NEWLINE
config-key     = "direction" | "edgeStyle" | "edge-style"
               | "branchLabels" | "branch-labels"
               | "branchLength" | "branch-length"
               | "impurity" | "classes"

// ── Taxonomy mode ──────────────────────────────
node           = ( branch-prefix WS )? tax-node ( WS "[" tax-attrs "]" )? NEWLINE
                   INDENT child-node*
tax-node       = ( "question" | "q" ) WS quoted-string
               | ( "answer" | "a" | "leaf" ) WS quoted-string
branch-prefix  = "yes:" | "no:" | "label" WS quoted-string ":"

// ── Decision-analysis mode ─────────────────────
da-node        = "decision" WS quoted-string NEWLINE INDENT da-child+
               | "chance" WS quoted-string NEWLINE INDENT da-prob-child+
               | ( "end" | "outcome" ) WS quoted-string ( WS "payoff=" number )? NEWLINE
da-child       = "choice" WS quoted-string NEWLINE INDENT da-node
da-prob-child  = "prob" WS number WS da-node    // prob, value, and child all on one line

// ── ML mode ───────────────────────────────────
ml-node        = ( "true" | "false" )? ml-kind WS quoted-string ml-prop* NEWLINE
                   INDENT ml-child*
                 // "true"/"false" and ml-kind must be on the same line
ml-kind        = "split" | "leaf"
ml-prop        = WS key "=" value     // no spaces around "="

comment        = ( "#" | "//" ) any NEWLINE
quoted-string  = '"' any-char-but-quote* '"'

Authoritative source: src/diagrams/decisiontree/parser.ts. If this diverges from the parser, the parser wins — please open an issue.


11. Standard compliance

Schematex decision trees cover three established conventions:

Taxonomy mode follows the question/answer flowchart conventions common in clinical decision support systems (Arden Syntax lineage) and ISO 9001 troubleshooting procedures.

Decision analysis mode follows the expected-value rollback method from management science: decision nodes (square, actor chooses), chance nodes (circle, probabilistic), terminal nodes with payoffs. EV is computed automatically via backward induction (Raiffa & Schlaifer, 1961).

ML mode follows the CART (Classification and Regression Trees) split/leaf notation (Breiman et al., 1984) compatible with scikit-learn's export_text and plot_tree output.

What is implemented today:

  • ✅ All three modes: taxonomy, decision analysis, ML
  • ✅ Taxonomy: question/q, answer/a/leaf, yes:, no:, label "X": branch labels
  • ✅ Decision: decision, chance, end/outcome, choice, prob, payoff=N, automatic EV rollback
  • ✅ ML: split, leaf, true/false branch prefixes, feature=, threshold=, gini=, entropy=, mse=, class=, value=
  • ✅ Config: direction:, edgeStyle:, impurity:, classes:, branchLabels:, branchLength:
  • ⏳ Probability-weighted branch length rendering (branchLength: probability parsed but not yet applied visually)
  • ⏳ Sensitivity analysis overlays (tornado chart-style annotations on decision trees)
  • ⏳ Forest / ensemble view — multiple trees side-by-side

References:

  • Raiffa, H. & Schlaifer, R. (1961). Applied Statistical Decision Theory. Harvard Business School.
  • Breiman, L. et al. (1984). Classification and Regression Trees. Wadsworth.
  • Wikipedia — Decision tree · Decision tree learning

12. Roadmap

Planned — not yet parseable. Do not use these in generated DSL today; the parser will reject or ignore them.

  • branchLength: probability visual rendering — the config key parses but the layout engine does not yet scale branch lengths by probability.
  • Sensitivity / tornado analysissensitivity: block annotating which prob values most affect the final EV.
  • Export annotations — structured table output (decision matrix) alongside the diagram SVG.
  • Forest viewforest keyword wrapping multiple decisiontree blocks in a side-by-side comparison layout.
  • Collapse/expand interactive markerscollapsed: true on a node to render a triangle placeholder for deep subtrees.

Track in the GitHub issues if you need any of these sooner.