Class: Legion::Extensions::Agentic::Memory::Semantic::Helpers::Concept

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, domain: :general, confidence: nil, properties: {}) ⇒ Concept

Returns a new instance of Concept.



13
14
15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 13

def initialize(name:, domain: :general, confidence: nil, properties: {})
  @id           = SecureRandom.uuid
  @name         = name
  @domain       = domain
  @confidence   = (confidence || Constants::DEFAULT_CONFIDENCE).clamp(0.0, 1.0)
  @properties   = properties.dup
  @relations    = []
  @access_count = 0
  @created_at   = Time.now.utc
end

Instance Attribute Details

#access_countObject

Returns the value of attribute access_count.



11
12
13
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 11

def access_count
  @access_count
end

#confidenceObject

Returns the value of attribute confidence.



11
12
13
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 11

def confidence
  @confidence
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def properties
  @properties
end

#relationsObject (readonly)

Returns the value of attribute relations.



10
11
12
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 10

def relations
  @relations
end

Instance Method Details

#accessObject



55
56
57
58
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 55

def access
  @access_count += 1
  @confidence = [@confidence + Constants::ACCESS_BOOST, 1.0].min
end

#add_relation(type:, target_name:, confidence: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 24

def add_relation(type:, target_name:, confidence: nil)
  return false unless Constants::RELATION_TYPES.include?(type)

  existing = @relations.find { |r| r[:type] == type && r[:target] == target_name }
  if existing
    existing[:confidence] = [existing[:confidence] + Constants::ACCESS_BOOST, 1.0].min
    return existing
  end

  trim_relations if @relations.size >= Constants::MAX_RELATIONS_PER_CONCEPT
  rel = { type: type, target: target_name, confidence: (confidence || Constants::DEFAULT_CONFIDENCE).clamp(0.0, 1.0) }
  @relations << rel
  rel
end

#decayObject



60
61
62
63
64
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 60

def decay
  @confidence = [@confidence - Constants::CONFIDENCE_DECAY, Constants::CONFIDENCE_FLOOR].max
  @relations.each { |r| r[:confidence] = [r[:confidence] - Constants::CONFIDENCE_DECAY, Constants::CONFIDENCE_FLOOR].max }
  @relations.reject! { |r| r[:confidence] <= Constants::CONFIDENCE_FLOOR }
end

#faded?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 66

def faded?
  @confidence <= Constants::CONFIDENCE_FLOOR
end

#get_property(key) ⇒ Object



51
52
53
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 51

def get_property(key)
  @properties[key]
end

#labelObject



70
71
72
73
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 70

def label
  Constants::CONFIDENCE_LABELS.each { |range, lbl| return lbl if range.cover?(@confidence) }
  :uncertain
end


43
44
45
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 43

def related_concepts
  @relations.map { |r| r[:target] }.uniq
end

#relations_of_type(type) ⇒ Object



39
40
41
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 39

def relations_of_type(type)
  @relations.select { |r| r[:type] == type }
end

#set_property(key, value) ⇒ Object



47
48
49
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 47

def set_property(key, value)
  @properties[key] = value
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/concept.rb', line 75

def to_h
  {
    id:           @id,
    name:         @name,
    domain:       @domain,
    confidence:   @confidence,
    properties:   @properties,
    relations:    @relations,
    access_count: @access_count,
    label:        label,
    created_at:   @created_at
  }
end