Class: Legion::Extensions::Agentic::Inference::BeliefRevision::Helpers::Belief

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

Constant Summary

Constants included from Constants

Constants::BELIEF_STATES, Constants::CONTRADICTION_THRESHOLD, Constants::CREDENCE_CEILING, Constants::CREDENCE_FLOOR, Constants::CREDENCE_LABELS, Constants::DECAY_RATE, Constants::DEFAULT_CREDENCE, Constants::ENTRENCHMENT_ALPHA, Constants::EVIDENCE_TYPES, Constants::EVIDENCE_WEIGHT, Constants::LINK_TYPES, Constants::MAX_BELIEFS, Constants::MAX_EVIDENCE_PER_BELIEF, Constants::MAX_HISTORY, Constants::MAX_LINKS, Constants::STATE_THRESHOLDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, proposition:, domain: :general, credence: DEFAULT_CREDENCE) ⇒ Belief

Returns a new instance of Belief.



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

def initialize(id:, proposition:, domain: :general, credence: DEFAULT_CREDENCE)
  @id               = id
  @proposition      = proposition
  @domain           = domain
  @credence         = credence.to_f.clamp(CREDENCE_FLOOR, CREDENCE_CEILING)
  @evidence_for     = []
  @evidence_against = []
  @entrenchment     = 0.0
  @revision_count   = 0
  @created_at       = Time.now.utc
  @updated_at       = @created_at
  @protected        = false
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def created_at
  @created_at
end

#credenceObject (readonly)

Returns the value of attribute credence.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def credence
  @credence
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def domain
  @domain
end

#entrenchmentObject (readonly)

Returns the value of attribute entrenchment.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def entrenchment
  @entrenchment
end

#evidence_againstObject (readonly)

Returns the value of attribute evidence_against.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def evidence_against
  @evidence_against
end

#evidence_forObject (readonly)

Returns the value of attribute evidence_for.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def evidence_for
  @evidence_for
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def id
  @id
end

#propositionObject (readonly)

Returns the value of attribute proposition.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def proposition
  @proposition
end

#revision_countObject (readonly)

Returns the value of attribute revision_count.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def revision_count
  @revision_count
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



12
13
14
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 12

def updated_at
  @updated_at
end

Instance Method Details

#add_opposing_evidence(evidence) ⇒ Object



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

def add_opposing_evidence(evidence)
  return nil if @evidence_against.size >= MAX_EVIDENCE_PER_BELIEF

  @evidence_against << evidence
  update_credence_from_evidence(evidence, :oppose)
  evidence
end

#add_supporting_evidence(evidence) ⇒ Object



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

def add_supporting_evidence(evidence)
  return nil if @evidence_for.size >= MAX_EVIDENCE_PER_BELIEF

  @evidence_for << evidence
  update_credence_from_evidence(evidence, :support)
  evidence
end

#believed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 77

def believed?
  @credence >= 0.5
end

#contradicts?(other) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 85

def contradicts?(other)
  (believed? && other.disbelieved?) || (disbelieved? && other.believed?)
end

#credence_labelObject



72
73
74
75
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 72

def credence_label
  CREDENCE_LABELS.each { |range, lbl| return lbl if range.cover?(@credence) }
  :disbelieved
end

#decayObject



100
101
102
103
104
105
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 100

def decay
  return if @protected
  return if @entrenchment >= STATE_THRESHOLDS[:entrenched]

  @credence = approach_default(@credence)
end

#disbelieved?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 81

def disbelieved?
  @credence < 0.3
end

#evidence_ratioObject



89
90
91
92
93
94
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 89

def evidence_ratio
  total = @evidence_for.size + @evidence_against.size
  return 0.5 if total.zero?

  @evidence_for.size.to_f / total
end

#protect!Object



52
53
54
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 52

def protect!
  @protected = true
end

#protected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 60

def protected?
  @protected
end

#revise(new_credence:) ⇒ Object



45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 45

def revise(new_credence:)
  @credence = new_credence.to_f.clamp(CREDENCE_FLOOR, CREDENCE_CEILING)
  @revision_count += 1
  @updated_at = Time.now.utc
  deepen_entrenchment
end

#stateObject



64
65
66
67
68
69
70
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 64

def state
  return :protected if @protected
  return :entrenched if @entrenchment >= STATE_THRESHOLDS[:entrenched]
  return :held if @credence >= STATE_THRESHOLDS[:held]

  :tentative
end

#to_hObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 107

def to_h
  {
    id:               @id,
    proposition:      @proposition,
    domain:           @domain,
    credence:         @credence.round(4),
    credence_label:   credence_label,
    state:            state,
    evidence_for:     @evidence_for.size,
    evidence_against: @evidence_against.size,
    entrenchment:     @entrenchment.round(4),
    revision_count:   @revision_count,
    protected:        @protected
  }
end

#total_evidence_countObject



96
97
98
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 96

def total_evidence_count
  @evidence_for.size + @evidence_against.size
end

#unprotect!Object



56
57
58
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief.rb', line 56

def unprotect!
  @protected = false
end