Class: Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::CausalEdge

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb

Constant Summary

Constants included from Constants

Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::CAUSAL_THRESHOLD, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::CONFIDENCE_LABELS, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::DECAY_RATE, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::DEFAULT_STRENGTH, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::EDGE_TYPES, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::EVIDENCE_THRESHOLD, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::INFERENCE_TYPES, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::MAX_EDGES, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::MAX_HISTORY, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::MAX_VARIABLES, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::REINFORCEMENT_RATE, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::STRENGTH_CEILING, Legion::Extensions::Agentic::Inference::CausalReasoning::Helpers::Constants::STRENGTH_FLOOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cause:, effect:, edge_type:, domain: :general, strength: Constants::DEFAULT_STRENGTH) ⇒ CausalEdge

Returns a new instance of CausalEdge.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 16

def initialize(cause:, effect:, edge_type:, domain: :general, strength: Constants::DEFAULT_STRENGTH)
  @id             = SecureRandom.uuid
  @cause          = cause
  @effect         = effect
  @edge_type      = edge_type
  @domain         = domain
  @strength       = strength.clamp(Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING)
  @evidence_count = 0
  @created_at     = Time.now.utc
  @updated_at     = Time.now.utc
end

Instance Attribute Details

#causeObject (readonly)

Returns the value of attribute cause.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def cause
  @cause
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def domain
  @domain
end

#edge_typeObject (readonly)

Returns the value of attribute edge_type.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def edge_type
  @edge_type
end

#effectObject (readonly)

Returns the value of attribute effect.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def effect
  @effect
end

#evidence_countObject (readonly)

Returns the value of attribute evidence_count.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def evidence_count
  @evidence_count
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def id
  @id
end

#strengthObject (readonly)

Returns the value of attribute strength.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def strength
  @strength
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



14
15
16
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 14

def updated_at
  @updated_at
end

Instance Method Details

#add_evidenceObject



28
29
30
31
32
33
34
35
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 28

def add_evidence
  @evidence_count += 1
  @strength = (@strength + Constants::REINFORCEMENT_RATE).clamp(
    Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING
  )
  @updated_at = Time.now.utc
  self
end

#confidence_labelObject



75
76
77
78
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 75

def confidence_label
  match = Constants::CONFIDENCE_LABELS.find { |range, _| range.cover?(@strength) }
  match ? match[1] : :speculative
end

#confident?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 70

def confident?
  @strength >= Constants::CAUSAL_THRESHOLD &&
    @evidence_count >= Constants::EVIDENCE_THRESHOLD
end

#decayObject



62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 62

def decay
  @strength = (@strength - Constants::DECAY_RATE).clamp(
    Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING
  )
  @updated_at = Time.now.utc
  self
end

#reinforce(amount: Constants::REINFORCEMENT_RATE) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 46

def reinforce(amount: Constants::REINFORCEMENT_RATE)
  @strength = (@strength + amount).clamp(
    Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING
  )
  @updated_at = Time.now.utc
  self
end

#remove_evidenceObject



37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 37

def remove_evidence
  @evidence_count = [@evidence_count - 1, 0].max
  @strength = (@strength - Constants::REINFORCEMENT_RATE).clamp(
    Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING
  )
  @updated_at = Time.now.utc
  self
end

#to_hObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 80

def to_h
  {
    id:             @id,
    cause:          @cause,
    effect:         @effect,
    edge_type:      @edge_type,
    domain:         @domain,
    strength:       @strength,
    evidence_count: @evidence_count,
    confident:      confident?,
    label:          confidence_label,
    created_at:     @created_at,
    updated_at:     @updated_at
  }
end

#weaken(amount: Constants::REINFORCEMENT_RATE) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/helpers/causal_edge.rb', line 54

def weaken(amount: Constants::REINFORCEMENT_RATE)
  @strength = (@strength - amount).clamp(
    Constants::STRENGTH_FLOOR, Constants::STRENGTH_CEILING
  )
  @updated_at = Time.now.utc
  self
end