Render, visual, and export methods for decision trees. Extracted from
ObjDecisionSwitch to separate core tree evaluation logic from
presentation and serialisation concerns.
ObjData.ObjData
|
v
ObjDecisionSwitch (factory.core)
|
v
ObjDecisionSwitchVisual (factory.core/extend.visual)
ObjDecisionSwitchVisual inherits all core functionality from
ObjDecisionSwitch (read, evaluate, compute_decision, simulate,
verify_outcomes, etc.) and adds visualisation and import/export
capabilities.
| Method |
Description |
diagram(direction) |
Generate a Mermaid flowchart diagram and store it in the database |
diagram_terminal() |
Render an interactive tree diagram in the terminal using Rich |
diagram_sankey(decision_name, guid) |
Show outcome distribution as a bar chart table |
| Method |
Description |
pmml_operator(op) |
Map a decision tree operator to its PMML equivalent |
from_pmml_operator(op) |
Map a PMML operator back to a decision tree operator |
save_pmml(file_path) |
Export the loaded tree to PMML 4.4 XML |
load_pmml(file_path) |
Import a tree from PMML XML and persist to database |
| Method |
Description |
save_csv(file_path) |
Export tree nodes to CSV |
load_csv(file_path, decision_name, three_words="", changed_by="", change_notes="", strict=True, show_diff=False) |
Import tree nodes from CSV and persist as a new version |
_print_node_diff(decision_name, package, base_version) |
Read-only diff helper used when load_csv(show_diff=True) |
get_three_words(decision_name) |
Generate a short version label via the GuidName service |
- New version number =
latest_version + 1. The new tree row is cloned from the previous version (preserving DataTable, simlimit, etc.) and the def_decision_treenodes rows are inserted with the new version stamped on each row.
- A row is also written to
def_decision_tree_history as ChangeType='IMPORT'.
- After a successful import the CSV is moved to
data.documents/done/<basename>_v<N>_<ts>.csv.
strict=True (default) aborts the import and returns 0 if the def_decision_tree row INSERT/UPDATE silently fails (e.g. missing column). Pass strict=False to fall back to the legacy "log and continue" behaviour.
show_diff=True prints added / removed / changed rules vs the prior version (identity = (Guid, rank, target_node), body = the remaining columns) before the inserts run. Reuses the shared read_decision_tree_nodes_version query so the SQL stays in ObjDecisionSwitch.yaml.
| Method |
Description |
save_excel(file_path) |
Export tree as a styled Excel workbook (Overview, Nodes, Flow sheets) |
| Method |
Description |
map_operator_to_sql(op) |
Map a decision tree operator to its SQL equivalent |
generate_sql_statement(decision_name, input_table) |
Generate a SELECT with nested CASE expressions |
generate_and_store_sql(decision_name, input_table) |
Generate SQL and store in def_decision_tree.operationsql |
compare_simulation_with_sql(decision_name, input_table) |
Run simulation and SQL, then compare outcomes |
| Method |
Description |
_quote_sql_value(value) |
Quote a value for safe SQL embedding |
_build_sql_case(node_id, default_outcome) |
Recursively build nested CASE expression for a node |
¶ CLI Commands
Run via python factory.core/extend.visual/ObjDecisionSwitchVisual.py <command>.
| Command |
Description |
diagram <name> |
Generate Mermaid diagram and save to file |
diagram-terminal <name> |
Display tree in terminal |
generate-sql <name> |
Print generated SQL statement |
store-sql <name> |
Generate SQL and store in database |
compare <name> <table> |
Compare simulation vs SQL results |
distribution <name> |
Show outcome distribution chart |
export <name> <format> |
Export tree (csv, excel, yaml, pmml) |
import-tree <name> <format> |
Import tree (csv, pmml) |
from ObjDecisionSwitchVisual import ObjDecisionSwitchVisual
tree = ObjDecisionSwitchVisual()
tree.read("CreditScoring")
# Generate Mermaid diagram
mermaid_text = tree.diagram()
# Export to Excel
tree.save_excel("CreditScoring.xlsx")
# Generate SQL
sql = tree.generate_sql_statement(
"CreditScoring", "data_applicants"
)