Class: DecisionAgent::Dmn::DecisionTree
- Inherits:
-
Object
- Object
- DecisionAgent::Dmn::DecisionTree
- Defined in:
- lib/decision_agent/dmn/decision_tree.rb
Overview
Evaluates decision trees
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
- .build_node(hash) ⇒ Object
-
.from_hash(hash) ⇒ Object
Build a decision tree from a hash representation.
Instance Method Summary collapse
-
#depth ⇒ Object
Get tree depth.
-
#evaluate(context) ⇒ Object
Evaluate the decision tree with given context.
-
#initialize(id:, name:, root: nil) ⇒ DecisionTree
constructor
A new instance of DecisionTree.
-
#leaf_nodes ⇒ Object
Get all leaf nodes (decision outcomes).
-
#paths ⇒ Object
Get all paths from root to leaves.
-
#to_h ⇒ Object
Convert tree to hash representation.
Constructor Details
#initialize(id:, name:, root: nil) ⇒ DecisionTree
Returns a new instance of DecisionTree.
46 47 48 49 50 51 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 46 def initialize(id:, name:, root: nil) @id = id @name = name @root = root || TreeNode.new(id: "root", label: "Root") @feel_evaluator = Feel::Evaluator.new end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
44 45 46 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 44 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
44 45 46 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 44 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
44 45 46 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 44 def root @root end |
Class Method Details
.build_node(hash) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 140 def self.build_node(hash) node = TreeNode.new( id: hash[:id], label: hash[:label], condition: hash[:condition], decision: hash[:decision] ) hash[:children]&.each do |child_hash| node.add_child(build_node(child_hash)) end node end |
.from_hash(hash) ⇒ Object
Build a decision tree from a hash representation
59 60 61 62 63 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 59 def self.from_hash(hash) tree = new(id: hash[:id], name: hash[:name]) tree.instance_variable_set(:@root, build_node(hash[:root])) tree end |
Instance Method Details
#depth ⇒ Object
Get tree depth
80 81 82 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 80 def depth calculate_depth(@root) end |
#evaluate(context) ⇒ Object
Evaluate the decision tree with given context
54 55 56 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 54 def evaluate(context) traverse(@root, context) end |
#leaf_nodes ⇒ Object
Get all leaf nodes (decision outcomes)
75 76 77 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 75 def leaf_nodes collect_leaf_nodes(@root) end |
#paths ⇒ Object
Get all paths from root to leaves
85 86 87 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 85 def paths collect_paths(@root, []) end |
#to_h ⇒ Object
Convert tree to hash representation
66 67 68 69 70 71 72 |
# File 'lib/decision_agent/dmn/decision_tree.rb', line 66 def to_h { id: @id, name: @name, root: @root.to_h } end |