lex-cognitive-weather 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +13 -0
- data/LICENSE +21 -0
- data/README.md +77 -0
- data/lex-cognitive-weather.gemspec +30 -0
- data/lib/legion/extensions/cognitive_weather/client.rb +25 -0
- data/lib/legion/extensions/cognitive_weather/helpers/constants.rb +45 -0
- data/lib/legion/extensions/cognitive_weather/helpers/front.rb +65 -0
- data/lib/legion/extensions/cognitive_weather/helpers/storm.rb +82 -0
- data/lib/legion/extensions/cognitive_weather/helpers/weather_engine.rb +127 -0
- data/lib/legion/extensions/cognitive_weather/runners/cognitive_weather.rb +115 -0
- data/lib/legion/extensions/cognitive_weather/version.rb +9 -0
- data/lib/legion/extensions/cognitive_weather.rb +18 -0
- data/spec/legion/extensions/cognitive_weather/client_spec.rb +76 -0
- data/spec/legion/extensions/cognitive_weather/helpers/constants_spec.rb +127 -0
- data/spec/legion/extensions/cognitive_weather/helpers/front_spec.rb +160 -0
- data/spec/legion/extensions/cognitive_weather/helpers/storm_spec.rb +184 -0
- data/spec/legion/extensions/cognitive_weather/helpers/weather_engine_spec.rb +206 -0
- data/spec/legion/extensions/cognitive_weather/runners/cognitive_weather_spec.rb +175 -0
- data/spec/spec_helper.rb +27 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 893b7ba8190aef887d3f947faef5ebe04762243ea85726e15457bc38836e6cfb
|
|
4
|
+
data.tar.gz: 846c2cc18aa47669c40ae0b58b7d7a1c7e43628d15591f96e2f663488ebf1bf7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 38e14187ed93e249dc32507203e46d25d6c44105d4a28e3534cd0d4434ef5218692d12a17d81f243dd6148403bf45e8bb3114557dd3a279d65bfc1e93a62dcd0
|
|
7
|
+
data.tar.gz: ef942645b2188f675dbcd486db4a374980116cd1b38a93ff82a2485f8483daecfa138fcf790cefeb7d413818c29117626cea0076258ac9ae1fc839c0b6b30dbb
|
data/Gemfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
source 'https://rubygems.org'
|
|
4
|
+
|
|
5
|
+
gemspec
|
|
6
|
+
|
|
7
|
+
group :test do
|
|
8
|
+
gem 'rspec', '~> 3.13'
|
|
9
|
+
gem 'rubocop', '~> 1.75', require: false
|
|
10
|
+
gem 'rubocop-rspec', require: false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
gem 'legion-gaia', path: '../../legion-gaia'
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Esity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# lex-cognitive-weather
|
|
2
|
+
|
|
3
|
+
A LegionIO cognitive architecture extension that models internal cognitive conditions as atmospheric weather patterns. Fronts represent cognitive pressure systems across domains, and storms model active episodes of confusion, conflict, or sudden insight.
|
|
4
|
+
|
|
5
|
+
## What It Does
|
|
6
|
+
|
|
7
|
+
Tracks **fronts** (atmospheric pressure systems) and **storms** (active weather episodes) in cognitive domains.
|
|
8
|
+
|
|
9
|
+
Each front has:
|
|
10
|
+
- A type (`:warm`, `:cold`, `:occluded`, `:stationary`)
|
|
11
|
+
- Pressure, temperature, and humidity values
|
|
12
|
+
- A severity label based on pressure
|
|
13
|
+
|
|
14
|
+
Each storm has:
|
|
15
|
+
- A condition type (`:clear`, `:cloudy`, `:foggy`, `:stormy`, `:lightning`, `:blizzard`)
|
|
16
|
+
- Intensity and coverage values
|
|
17
|
+
- An insight log populated by `lightning_strike!` events
|
|
18
|
+
- A clarity label inverse to intensity
|
|
19
|
+
|
|
20
|
+
Fronts can be intensified or retreated. Storms can be intensified or dissipated. The engine surfaces current conditions via a weather forecast and a full atmospheric report.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
require 'lex-cognitive-weather'
|
|
26
|
+
|
|
27
|
+
client = Legion::Extensions::CognitiveWeather::Client.new
|
|
28
|
+
|
|
29
|
+
# Create an atmospheric front
|
|
30
|
+
result = client.create_front(
|
|
31
|
+
front_type: :cold,
|
|
32
|
+
domain: 'reasoning',
|
|
33
|
+
pressure: 0.6,
|
|
34
|
+
temperature: 0.4,
|
|
35
|
+
humidity: 0.5
|
|
36
|
+
)
|
|
37
|
+
# => { success: true, front: { id: "uuid...", front_type: :cold, pressure: 0.6, severity_label: "moderate", ... } }
|
|
38
|
+
|
|
39
|
+
front_id = result[:front][:id]
|
|
40
|
+
|
|
41
|
+
# Brew a storm from the front
|
|
42
|
+
storm = client.brew_storm(
|
|
43
|
+
front_id: front_id,
|
|
44
|
+
condition: :foggy,
|
|
45
|
+
intensity: 0.5,
|
|
46
|
+
coverage: 0.4
|
|
47
|
+
)
|
|
48
|
+
# => { success: true, storm: { id: "uuid...", condition: :foggy, intensity: 0.5, clarity: "hazy", raging: false, ... } }
|
|
49
|
+
|
|
50
|
+
# Intensify the storm
|
|
51
|
+
client.intensify(storm_id: storm[:storm][:id])
|
|
52
|
+
# => { success: true, storm: { intensity: 0.55, ... } }
|
|
53
|
+
|
|
54
|
+
# Get current forecast
|
|
55
|
+
client.forecast
|
|
56
|
+
# => { success: true, condition: :foggy, avg_intensity: 0.55, active_storms: 1, clarity_label: "hazy", ... }
|
|
57
|
+
|
|
58
|
+
# List all fronts
|
|
59
|
+
client.list_fronts
|
|
60
|
+
# => { success: true, fronts: [...], count: 1 }
|
|
61
|
+
|
|
62
|
+
# Full atmospheric report
|
|
63
|
+
client.weather_status
|
|
64
|
+
# => { success: true, front_count: 1, storm_count: 1, fronts: [...], storms: [...], forecast: {...}, ... }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Development
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
bundle install
|
|
71
|
+
bundle exec rspec
|
|
72
|
+
bundle exec rubocop
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/legion/extensions/cognitive_weather/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'lex-cognitive-weather'
|
|
7
|
+
spec.version = Legion::Extensions::CognitiveWeather::VERSION
|
|
8
|
+
spec.authors = ['Esity']
|
|
9
|
+
spec.email = ['matthewdiverson@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'LEX CognitiveWeather'
|
|
12
|
+
spec.description = 'Internal cognitive weather systems for brain-modeled agentic AI — ' \
|
|
13
|
+
'atmospheric fronts, storms of confusion, fog of uncertainty, and lightning strikes of insight'
|
|
14
|
+
spec.homepage = 'https://github.com/LegionIO/lex-cognitive-weather'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
spec.required_ruby_version = '>= 3.4'
|
|
17
|
+
|
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/LegionIO/lex-cognitive-weather'
|
|
20
|
+
spec.metadata['documentation_uri'] = 'https://github.com/LegionIO/lex-cognitive-weather'
|
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/LegionIO/lex-cognitive-weather'
|
|
22
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/LegionIO/lex-cognitive-weather/issues'
|
|
23
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
24
|
+
|
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
26
|
+
Dir.glob('{lib,spec}/**/*') + %w[lex-cognitive-weather.gemspec Gemfile LICENSE README.md]
|
|
27
|
+
end
|
|
28
|
+
spec.require_paths = ['lib']
|
|
29
|
+
spec.add_development_dependency 'legion-gaia'
|
|
30
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/cognitive_weather/helpers/constants'
|
|
4
|
+
require 'legion/extensions/cognitive_weather/helpers/front'
|
|
5
|
+
require 'legion/extensions/cognitive_weather/helpers/storm'
|
|
6
|
+
require 'legion/extensions/cognitive_weather/helpers/weather_engine'
|
|
7
|
+
require 'legion/extensions/cognitive_weather/runners/cognitive_weather'
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Extensions
|
|
11
|
+
module CognitiveWeather
|
|
12
|
+
class Client
|
|
13
|
+
include Runners::CognitiveWeather
|
|
14
|
+
|
|
15
|
+
def initialize(**)
|
|
16
|
+
@weather_engine = Helpers::WeatherEngine.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
attr_reader :weather_engine
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveWeather
|
|
6
|
+
module Helpers
|
|
7
|
+
module Constants
|
|
8
|
+
FRONT_TYPES = %i[warm cold occluded stationary].freeze
|
|
9
|
+
CONDITION_TYPES = %i[clear cloudy foggy stormy lightning blizzard].freeze
|
|
10
|
+
|
|
11
|
+
MAX_FRONTS = 100
|
|
12
|
+
MAX_STORMS = 50
|
|
13
|
+
|
|
14
|
+
PRESSURE_CHANGE_RATE = 0.05
|
|
15
|
+
DISSIPATION_RATE = 0.03
|
|
16
|
+
|
|
17
|
+
# Range-based severity label lookup — ordered from highest to lowest
|
|
18
|
+
SEVERITY_LABELS = [
|
|
19
|
+
{ range: (0.9..1.0), label: 'catastrophic' },
|
|
20
|
+
{ range: (0.75..0.9), label: 'severe' },
|
|
21
|
+
{ range: (0.55..0.75), label: 'moderate' },
|
|
22
|
+
{ range: (0.35..0.55), label: 'mild' },
|
|
23
|
+
{ range: (0.15..0.35), label: 'light' },
|
|
24
|
+
{ range: (0.0..0.15), label: 'calm' }
|
|
25
|
+
].freeze
|
|
26
|
+
|
|
27
|
+
# Range-based clarity label lookup — ordered from highest to lowest
|
|
28
|
+
CLARITY_LABELS = [
|
|
29
|
+
{ range: (0.85..1.0), label: 'crystal' },
|
|
30
|
+
{ range: (0.65..0.85), label: 'clear' },
|
|
31
|
+
{ range: (0.45..0.65), label: 'hazy' },
|
|
32
|
+
{ range: (0.25..0.45), label: 'murky' },
|
|
33
|
+
{ range: (0.0..0.25), label: 'opaque' }
|
|
34
|
+
].freeze
|
|
35
|
+
|
|
36
|
+
def self.label_for(table, value)
|
|
37
|
+
clamped = value.clamp(0.0, 1.0)
|
|
38
|
+
entry = table.find { |e| e[:range].cover?(clamped) }
|
|
39
|
+
entry ? entry[:label] : table.last[:label]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module CognitiveWeather
|
|
8
|
+
module Helpers
|
|
9
|
+
class Front
|
|
10
|
+
attr_reader :id, :front_type, :domain, :created_at
|
|
11
|
+
attr_accessor :pressure, :temperature, :humidity
|
|
12
|
+
|
|
13
|
+
def initialize(front_type:, domain:, pressure: 0.5, temperature: 0.5, humidity: 0.5)
|
|
14
|
+
raise ArgumentError, "unknown front_type: #{front_type}" unless Constants::FRONT_TYPES.include?(front_type.to_sym)
|
|
15
|
+
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@front_type = front_type.to_sym
|
|
18
|
+
@domain = domain.to_s
|
|
19
|
+
@pressure = pressure.clamp(0.0, 1.0)
|
|
20
|
+
@temperature = temperature.clamp(0.0, 1.0)
|
|
21
|
+
@humidity = humidity.clamp(0.0, 1.0)
|
|
22
|
+
@created_at = Time.now.utc
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Advance: pressure rises, front strengthens
|
|
26
|
+
def advance!(rate = Constants::PRESSURE_CHANGE_RATE)
|
|
27
|
+
@pressure = (@pressure + rate.clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Retreat: pressure falls, front weakens
|
|
31
|
+
def retreat!(rate = Constants::PRESSURE_CHANGE_RATE)
|
|
32
|
+
@pressure = (@pressure - rate.clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def high_pressure?
|
|
36
|
+
@pressure >= 0.65
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def low_pressure?
|
|
40
|
+
@pressure <= 0.35
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def severity_label
|
|
44
|
+
Constants.label_for(Constants::SEVERITY_LABELS, @pressure)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_h
|
|
48
|
+
{
|
|
49
|
+
id: @id,
|
|
50
|
+
front_type: @front_type,
|
|
51
|
+
domain: @domain,
|
|
52
|
+
pressure: @pressure.round(10),
|
|
53
|
+
temperature: @temperature.round(10),
|
|
54
|
+
humidity: @humidity.round(10),
|
|
55
|
+
high_pressure: high_pressure?,
|
|
56
|
+
low_pressure: low_pressure?,
|
|
57
|
+
severity: severity_label,
|
|
58
|
+
created_at: @created_at.iso8601
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module CognitiveWeather
|
|
8
|
+
module Helpers
|
|
9
|
+
class Storm
|
|
10
|
+
attr_reader :id, :condition, :front_ids, :created_at, :insight_log
|
|
11
|
+
attr_accessor :intensity, :coverage
|
|
12
|
+
|
|
13
|
+
def initialize(condition:, front_ids: [], intensity: 0.5, coverage: 0.5)
|
|
14
|
+
raise ArgumentError, "unknown condition: #{condition}" unless Constants::CONDITION_TYPES.include?(condition.to_sym)
|
|
15
|
+
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@condition = condition.to_sym
|
|
18
|
+
@front_ids = Array(front_ids).dup
|
|
19
|
+
@intensity = intensity.clamp(0.0, 1.0)
|
|
20
|
+
@coverage = coverage.clamp(0.0, 1.0)
|
|
21
|
+
@created_at = Time.now.utc
|
|
22
|
+
@insight_log = []
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Intensify: storm grows stronger
|
|
26
|
+
def intensify!(rate = Constants::PRESSURE_CHANGE_RATE)
|
|
27
|
+
@intensity = (@intensity + rate.clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
28
|
+
@coverage = (@coverage + (rate * 0.5).clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Dissipate: storm weakens and clears
|
|
32
|
+
def dissipate!(rate = Constants::DISSIPATION_RATE)
|
|
33
|
+
@intensity = (@intensity - rate.clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
34
|
+
@coverage = (@coverage - (rate * 0.5).clamp(0.0, 1.0)).clamp(0.0, 1.0)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A lightning strike of sudden insight — random intensity, logged for audit
|
|
38
|
+
def lightning_strike!(domain: nil)
|
|
39
|
+
insight_id = SecureRandom.uuid
|
|
40
|
+
insight_intensity = rand.round(10)
|
|
41
|
+
entry = {
|
|
42
|
+
id: insight_id,
|
|
43
|
+
domain: domain,
|
|
44
|
+
intensity: insight_intensity,
|
|
45
|
+
struck_at: Time.now.utc.iso8601
|
|
46
|
+
}
|
|
47
|
+
@insight_log << entry
|
|
48
|
+
entry
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def raging?
|
|
52
|
+
@intensity >= 0.75
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def clearing?
|
|
56
|
+
@intensity <= 0.25
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def clarity_label
|
|
60
|
+
# Clarity is inverse of intensity: high intensity = low clarity
|
|
61
|
+
Constants.label_for(Constants::CLARITY_LABELS, 1.0 - @intensity)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def to_h
|
|
65
|
+
{
|
|
66
|
+
id: @id,
|
|
67
|
+
condition: @condition,
|
|
68
|
+
front_ids: @front_ids,
|
|
69
|
+
intensity: @intensity.round(10),
|
|
70
|
+
coverage: @coverage.round(10),
|
|
71
|
+
raging: raging?,
|
|
72
|
+
clearing: clearing?,
|
|
73
|
+
clarity: clarity_label,
|
|
74
|
+
insight_count: @insight_log.size,
|
|
75
|
+
created_at: @created_at.iso8601
|
|
76
|
+
}
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveWeather
|
|
6
|
+
module Helpers
|
|
7
|
+
class WeatherEngine
|
|
8
|
+
attr_reader :fronts, :storms
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@fronts = []
|
|
12
|
+
@storms = []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Create a new atmospheric front; respects MAX_FRONTS
|
|
16
|
+
def create_front(front_type:, domain:, pressure: 0.5, temperature: 0.5, humidity: 0.5)
|
|
17
|
+
return nil if @fronts.size >= Constants::MAX_FRONTS
|
|
18
|
+
|
|
19
|
+
front = Front.new(
|
|
20
|
+
front_type: front_type,
|
|
21
|
+
domain: domain,
|
|
22
|
+
pressure: pressure,
|
|
23
|
+
temperature: temperature,
|
|
24
|
+
humidity: humidity
|
|
25
|
+
)
|
|
26
|
+
@fronts << front
|
|
27
|
+
front
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Brew a storm anchored to a front; respects MAX_STORMS
|
|
31
|
+
def brew_storm(front_id:, condition: :stormy, intensity: 0.5, coverage: 0.5)
|
|
32
|
+
return nil if @storms.size >= Constants::MAX_STORMS
|
|
33
|
+
return nil unless find_front(front_id)
|
|
34
|
+
|
|
35
|
+
storm = Storm.new(
|
|
36
|
+
condition: condition,
|
|
37
|
+
front_ids: [front_id],
|
|
38
|
+
intensity: intensity,
|
|
39
|
+
coverage: coverage
|
|
40
|
+
)
|
|
41
|
+
@storms << storm
|
|
42
|
+
storm
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Intensify a storm by id
|
|
46
|
+
def intensify_storm(storm_id:, rate: Constants::PRESSURE_CHANGE_RATE)
|
|
47
|
+
storm = find_storm(storm_id)
|
|
48
|
+
return nil unless storm
|
|
49
|
+
|
|
50
|
+
storm.intensify!(rate)
|
|
51
|
+
storm
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Dissipate all storms by one cycle
|
|
55
|
+
def dissipate_all!(rate = Constants::DISSIPATION_RATE)
|
|
56
|
+
@storms.each { |s| s.dissipate!(rate) }
|
|
57
|
+
@storms.size
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Current conditions summary: dominant condition, average intensity, active storm count
|
|
61
|
+
def weather_forecast
|
|
62
|
+
if @storms.empty?
|
|
63
|
+
return {
|
|
64
|
+
condition: :clear,
|
|
65
|
+
avg_intensity: 0.0,
|
|
66
|
+
active_storms: 0,
|
|
67
|
+
dominant_front: nil,
|
|
68
|
+
clarity_label: 'crystal',
|
|
69
|
+
severity_label: 'calm'
|
|
70
|
+
}
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
avg_intensity = @storms.sum(&:intensity) / @storms.size.to_f
|
|
74
|
+
dominant_storm = @storms.max_by(&:intensity)
|
|
75
|
+
dominant_front = most_severe
|
|
76
|
+
|
|
77
|
+
{
|
|
78
|
+
condition: dominant_storm.condition,
|
|
79
|
+
avg_intensity: avg_intensity.round(10),
|
|
80
|
+
active_storms: @storms.size,
|
|
81
|
+
dominant_front: dominant_front&.to_h,
|
|
82
|
+
clarity_label: dominant_storm.clarity_label,
|
|
83
|
+
severity_label: dominant_front ? dominant_front.severity_label : 'calm'
|
|
84
|
+
}
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Front with highest pressure
|
|
88
|
+
def most_severe
|
|
89
|
+
return nil if @fronts.empty?
|
|
90
|
+
|
|
91
|
+
@fronts.max_by(&:pressure)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Front with lowest pressure
|
|
95
|
+
def calmest
|
|
96
|
+
return nil if @fronts.empty?
|
|
97
|
+
|
|
98
|
+
@fronts.min_by(&:pressure)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Full atmospheric report
|
|
102
|
+
def atmospheric_report
|
|
103
|
+
{
|
|
104
|
+
front_count: @fronts.size,
|
|
105
|
+
storm_count: @storms.size,
|
|
106
|
+
fronts: @fronts.map(&:to_h),
|
|
107
|
+
storms: @storms.map(&:to_h),
|
|
108
|
+
forecast: weather_forecast,
|
|
109
|
+
most_severe: most_severe&.to_h,
|
|
110
|
+
calmest: calmest&.to_h
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def find_front(front_id)
|
|
117
|
+
@fronts.find { |f| f.id == front_id.to_s }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def find_storm(storm_id)
|
|
121
|
+
@storms.find { |s| s.id == storm_id.to_s }
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveWeather
|
|
6
|
+
module Runners
|
|
7
|
+
module CognitiveWeather
|
|
8
|
+
extend self
|
|
9
|
+
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def create_front(front_type: :warm, domain: 'default', pressure: 0.5,
|
|
14
|
+
temperature: 0.5, humidity: 0.5, engine: nil, **)
|
|
15
|
+
eng = engine || weather_engine
|
|
16
|
+
front = eng.create_front(
|
|
17
|
+
front_type: front_type.to_sym,
|
|
18
|
+
domain: domain,
|
|
19
|
+
pressure: pressure.to_f,
|
|
20
|
+
temperature: temperature.to_f,
|
|
21
|
+
humidity: humidity.to_f
|
|
22
|
+
)
|
|
23
|
+
if front
|
|
24
|
+
Legion::Logging.debug "[cognitive_weather] front created: type=#{front_type} " \
|
|
25
|
+
"domain=#{domain} pressure=#{pressure}"
|
|
26
|
+
{ success: true, front: front.to_h }
|
|
27
|
+
else
|
|
28
|
+
Legion::Logging.warn '[cognitive_weather] create_front: MAX_FRONTS reached'
|
|
29
|
+
{ success: false, reason: :max_fronts_reached }
|
|
30
|
+
end
|
|
31
|
+
rescue ArgumentError => e
|
|
32
|
+
Legion::Logging.error "[cognitive_weather] create_front failed: #{e.message}"
|
|
33
|
+
{ success: false, error: e.message }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def brew_storm(front_id:, condition: :stormy, intensity: 0.5, coverage: 0.5, engine: nil, **)
|
|
37
|
+
eng = engine || weather_engine
|
|
38
|
+
storm = eng.brew_storm(
|
|
39
|
+
front_id: front_id.to_s,
|
|
40
|
+
condition: condition.to_sym,
|
|
41
|
+
intensity: intensity.to_f,
|
|
42
|
+
coverage: coverage.to_f
|
|
43
|
+
)
|
|
44
|
+
if storm
|
|
45
|
+
Legion::Logging.debug "[cognitive_weather] storm brewed: condition=#{condition} " \
|
|
46
|
+
"intensity=#{intensity} front=#{front_id}"
|
|
47
|
+
{ success: true, storm: storm.to_h }
|
|
48
|
+
else
|
|
49
|
+
Legion::Logging.warn '[cognitive_weather] brew_storm: front not found or MAX_STORMS reached'
|
|
50
|
+
{ success: false, reason: :brew_failed }
|
|
51
|
+
end
|
|
52
|
+
rescue ArgumentError => e
|
|
53
|
+
Legion::Logging.error "[cognitive_weather] brew_storm failed: #{e.message}"
|
|
54
|
+
{ success: false, error: e.message }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def intensify(storm_id:, rate: Helpers::Constants::PRESSURE_CHANGE_RATE, engine: nil, **)
|
|
58
|
+
eng = engine || weather_engine
|
|
59
|
+
storm = eng.intensify_storm(storm_id: storm_id.to_s, rate: rate.to_f)
|
|
60
|
+
if storm
|
|
61
|
+
Legion::Logging.debug "[cognitive_weather] storm intensified: id=#{storm_id} " \
|
|
62
|
+
"intensity=#{storm.intensity.round(3)} raging=#{storm.raging?}"
|
|
63
|
+
{ success: true, storm: storm.to_h }
|
|
64
|
+
else
|
|
65
|
+
Legion::Logging.warn "[cognitive_weather] intensify: storm #{storm_id} not found"
|
|
66
|
+
{ success: false, reason: :storm_not_found }
|
|
67
|
+
end
|
|
68
|
+
rescue ArgumentError => e
|
|
69
|
+
Legion::Logging.error "[cognitive_weather] intensify failed: #{e.message}"
|
|
70
|
+
{ success: false, error: e.message }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def forecast(engine: nil, **)
|
|
74
|
+
eng = engine || weather_engine
|
|
75
|
+
forecast = eng.weather_forecast
|
|
76
|
+
Legion::Logging.debug "[cognitive_weather] forecast: condition=#{forecast[:condition]} " \
|
|
77
|
+
"avg_intensity=#{forecast[:avg_intensity]&.round(3)} " \
|
|
78
|
+
"storms=#{forecast[:active_storms]}"
|
|
79
|
+
forecast.merge(success: true)
|
|
80
|
+
rescue ArgumentError => e
|
|
81
|
+
Legion::Logging.error "[cognitive_weather] forecast failed: #{e.message}"
|
|
82
|
+
{ success: false, error: e.message }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def list_fronts(engine: nil, **)
|
|
86
|
+
eng = engine || weather_engine
|
|
87
|
+
fronts = eng.fronts.map(&:to_h)
|
|
88
|
+
Legion::Logging.debug "[cognitive_weather] list_fronts: count=#{fronts.size}"
|
|
89
|
+
{ success: true, fronts: fronts, count: fronts.size }
|
|
90
|
+
rescue ArgumentError => e
|
|
91
|
+
Legion::Logging.error "[cognitive_weather] list_fronts failed: #{e.message}"
|
|
92
|
+
{ success: false, error: e.message }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def weather_status(engine: nil, **)
|
|
96
|
+
eng = engine || weather_engine
|
|
97
|
+
report = eng.atmospheric_report
|
|
98
|
+
Legion::Logging.debug "[cognitive_weather] weather_status: fronts=#{report[:front_count]} " \
|
|
99
|
+
"storms=#{report[:storm_count]}"
|
|
100
|
+
report.merge(success: true)
|
|
101
|
+
rescue ArgumentError => e
|
|
102
|
+
Legion::Logging.error "[cognitive_weather] weather_status failed: #{e.message}"
|
|
103
|
+
{ success: false, error: e.message }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def weather_engine
|
|
109
|
+
@weather_engine ||= Helpers::WeatherEngine.new
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
require 'legion/extensions/cognitive_weather/version'
|
|
5
|
+
require 'legion/extensions/cognitive_weather/helpers/constants'
|
|
6
|
+
require 'legion/extensions/cognitive_weather/helpers/front'
|
|
7
|
+
require 'legion/extensions/cognitive_weather/helpers/storm'
|
|
8
|
+
require 'legion/extensions/cognitive_weather/helpers/weather_engine'
|
|
9
|
+
require 'legion/extensions/cognitive_weather/runners/cognitive_weather'
|
|
10
|
+
require 'legion/extensions/cognitive_weather/client'
|
|
11
|
+
|
|
12
|
+
module Legion
|
|
13
|
+
module Extensions
|
|
14
|
+
module CognitiveWeather
|
|
15
|
+
extend Legion::Extensions::Core if Legion::Extensions.const_defined? :Core
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|