Class: DecisionAgent::Dmn::TreeSvgGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/visualizer.rb

Overview

Generates SVG for decision trees

Constant Summary collapse

NODE_WIDTH =
150
NODE_HEIGHT =
60
HORIZONTAL_SPACING =
40
VERTICAL_SPACING =
100

Instance Method Summary collapse

Constructor Details

#initialize(decision_tree) ⇒ TreeSvgGenerator

Returns a new instance of TreeSvgGenerator.



54
55
56
57
# File 'lib/decision_agent/dmn/visualizer.rb', line 54

def initialize(decision_tree)
  @tree = decision_tree
  @positions = {}
end

Instance Method Details

#generateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/decision_agent/dmn/visualizer.rb', line 59

def generate
  calculate_positions(@tree.root, 0, 0)

  width = (@positions.values.map { |p| p[:x] }.max || 0) + NODE_WIDTH + 40
  height = (@positions.values.map { |p| p[:y] }.max || 0) + NODE_HEIGHT + 40

  svg = [
    %(<svg xmlns="http://www.w3.org/2000/svg" width="#{width}" height="#{height}" viewBox="0 0 #{width} #{height}">),
    "<defs>",
    %(  <marker id="arrowhead" markerWidth="10" markerHeight="10" refX="9" refY="3" orient="auto">),
    %(    <polygon points="0 0, 10 3, 0 6" fill="#666" />),
    "  </marker>",
    "</defs>",
    "<g>"
  ]

  # Draw edges first (so they appear behind nodes)
  svg.concat(generate_edges)

  # Draw nodes
  svg.concat(generate_nodes)

  svg << "</g>"
  svg << "</svg>"
  svg.join("\n")
end