Class: Legion::Extensions::Agentic::Inference::Schema::Helpers::WorldModel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorldModel

Returns a new instance of WorldModel.



12
13
14
15
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 12

def initialize
  @relations = {}
  @domains   = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#domainsObject (readonly)

Returns the value of attribute domains.



10
11
12
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 10

def domains
  @domains
end

#relationsObject (readonly)

Returns the value of attribute relations.



10
11
12
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 10

def relations
  @relations
end

Instance Method Details

#add_relation(cause:, effect:, relation_type:, confidence: 0.5) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 17

def add_relation(cause:, effect:, relation_type:, confidence: 0.5)
  return nil unless Constants::RELATION_TYPES.include?(relation_type)

  key = relation_key(cause, effect, relation_type)
  if @relations.key?(key)
    @relations[key].reinforce
  else
    @relations[key] = CausalRelation.new(cause: cause, effect: effect, relation_type: relation_type, confidence: confidence)
    index_domains(cause, effect, key)
    trim_relations
  end
  @relations[key]
end

#contradictionsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 65

def contradictions
  @relations.values.each_with_object([]) do |rel, result|
    next unless rel.relation_type == :causes

    opposite = @relations[relation_key(rel.cause, rel.effect, :prevents)]
    next unless opposite

    result << {
      cause:               rel.cause,
      effect:              rel.effect,
      causes_confidence:   rel.confidence.round(4),
      prevents_confidence: opposite.confidence.round(4)
    }
  end
end

#counterfactual(cause, max_depth: Constants::MAX_COUNTERFACTUAL_DEPTH) ⇒ Object



58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 58

def counterfactual(cause, max_depth: Constants::MAX_COUNTERFACTUAL_DEPTH)
  affected = []
  visited  = Set.new
  propagate_counterfactual(cause, affected, visited, max_depth)
  affected
end

#decay_allObject



81
82
83
84
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 81

def decay_all
  @relations.each_value(&:decay)
  prune_weak
end

#domain_countObject



90
91
92
93
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 90

def domain_count
  all_entities = @relations.values.flat_map { |r| [r.cause, r.effect] }
  all_entities.uniq.size
end

#established_relationsObject



95
96
97
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 95

def established_relations
  @relations.values.select(&:established?)
end

#explain(outcome, max_depth: Constants::MAX_EXPLANATION_CHAIN) ⇒ Object



51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 51

def explain(outcome, max_depth: Constants::MAX_EXPLANATION_CHAIN)
  chain = []
  visited = Set.new
  build_explanation_chain(outcome, chain, visited, max_depth)
  chain
end

#find_causes(effect) ⇒ Object



46
47
48
49
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 46

def find_causes(effect)
  keys = @domains[effect] || []
  keys.filter_map { |k| @relations[k] }.select { |r| r.effect == effect }
end

#find_effects(cause) ⇒ Object



41
42
43
44
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 41

def find_effects(cause)
  keys = @domains[cause] || []
  keys.filter_map { |k| @relations[k] }.select { |r| r.cause == cause }
end

#relation_countObject



86
87
88
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 86

def relation_count
  @relations.size
end

#to_hObject



99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 99

def to_h
  {
    relation_count:      relation_count,
    domain_count:        domain_count,
    established_count:   established_relations.size,
    contradiction_count: contradictions.size
  }
end

#weaken_relation(cause:, effect:, relation_type:) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/inference/schema/helpers/world_model.rb', line 31

def weaken_relation(cause:, effect:, relation_type:)
  key = relation_key(cause, effect, relation_type)
  relation = @relations[key]
  return nil unless relation

  relation.weaken
  prune_if_needed(key)
  relation
end