Class: Legion::Extensions::Agentic::Homeostasis::Temporal::Helpers::TemporalPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain:, event:) ⇒ TemporalPattern

Returns a new instance of TemporalPattern.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 13

def initialize(domain:, event:)
  @domain = domain
  @event = event
  @intervals = []
  @pattern_type = :random
  @mean_interval = nil
  @observation_count = 0
  @last_predicted = nil
  @accuracy_count = 0
  @total_predictions = 0
end

Instance Attribute Details

#accuracy_countObject (readonly)

Returns the value of attribute accuracy_count.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def accuracy_count
  @accuracy_count
end

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def domain
  @domain
end

#eventObject (readonly)

Returns the value of attribute event.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def event
  @event
end

#last_predictedObject (readonly)

Returns the value of attribute last_predicted.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def last_predicted
  @last_predicted
end

#mean_intervalObject (readonly)

Returns the value of attribute mean_interval.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def mean_interval
  @mean_interval
end

#observation_countObject (readonly)

Returns the value of attribute observation_count.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def observation_count
  @observation_count
end

#pattern_typeObject (readonly)

Returns the value of attribute pattern_type.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def pattern_type
  @pattern_type
end

#total_predictionsObject (readonly)

Returns the value of attribute total_predictions.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 10

def total_predictions
  @total_predictions
end

Instance Method Details

#add_observation(timestamps) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 25

def add_observation(timestamps)
  return if timestamps.size < 2

  @intervals = compute_intervals(timestamps)
  @observation_count = timestamps.size
  @mean_interval = @intervals.sum / @intervals.size.to_f
  @pattern_type = classify_pattern
end

#bursty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 61

def bursty?
  @pattern_type == :bursty
end

#periodic?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 57

def periodic?
  @pattern_type == :periodic
end

#predict_next(from: Time.now.utc) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 34

def predict_next(from: Time.now.utc)
  return nil unless @mean_interval && @observation_count >= Constants::MIN_PATTERN_OBSERVATIONS

  predicted = from + @mean_interval
  @last_predicted = predicted
  @total_predictions += 1
  { predicted_at: predicted, confidence: prediction_confidence, pattern: @pattern_type }
end

#prediction_accuracyObject



51
52
53
54
55
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 51

def prediction_accuracy
  return 0.0 if @total_predictions.zero?

  @accuracy_count.to_f / @total_predictions
end

#record_actual(actual_time) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 43

def record_actual(actual_time)
  return unless @last_predicted

  error = (actual_time - @last_predicted).abs
  tolerance = (@mean_interval || 60) * 0.3
  @accuracy_count += 1 if error <= tolerance
end

#to_hObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/homeostasis/temporal/helpers/temporal_pattern.rb', line 65

def to_h
  {
    domain:            @domain,
    event:             @event,
    pattern_type:      @pattern_type,
    mean_interval:     @mean_interval,
    observation_count: @observation_count,
    accuracy:          prediction_accuracy
  }
end