kbs 0.2.1 → 0.4.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.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -1
  3. data/README.md +66 -25
  4. data/lib/kbs/agenda.rb +301 -0
  5. data/lib/kbs/alpha_memory.rb +80 -9
  6. data/lib/kbs/alpha_network.rb +95 -0
  7. data/lib/kbs/beta_memory.rb +54 -19
  8. data/lib/kbs/blackboard/audit_log.rb +26 -25
  9. data/lib/kbs/blackboard/engine.rb +49 -40
  10. data/lib/kbs/blackboard/fact.rb +17 -21
  11. data/lib/kbs/blackboard/memory.rb +116 -40
  12. data/lib/kbs/blackboard/message_queue.rb +39 -28
  13. data/lib/kbs/blackboard/persistence/hybrid_store.rb +20 -9
  14. data/lib/kbs/blackboard/persistence/redis_store.rb +59 -51
  15. data/lib/kbs/blackboard/persistence/sqlite_store.rb +29 -32
  16. data/lib/kbs/blackboard/persistence/store.rb +11 -1
  17. data/lib/kbs/blackboard/redis_message_queue.rb +16 -7
  18. data/lib/kbs/condition.rb +42 -2
  19. data/lib/kbs/decompiler.rb +300 -124
  20. data/lib/kbs/dsl/condition_helpers.rb +8 -6
  21. data/lib/kbs/dsl/knowledge_base.rb +73 -93
  22. data/lib/kbs/dsl/pattern_evaluator.rb +38 -15
  23. data/lib/kbs/dsl/rule_builder.rb +118 -40
  24. data/lib/kbs/dsl/variable.rb +51 -6
  25. data/lib/kbs/dsl.rb +10 -3
  26. data/lib/kbs/engine.rb +395 -77
  27. data/lib/kbs/errors.rb +38 -0
  28. data/lib/kbs/exists_node.rb +12 -0
  29. data/lib/kbs/fact.rb +5 -19
  30. data/lib/kbs/fact_registry.rb +51 -0
  31. data/lib/kbs/introspection.rb +93 -0
  32. data/lib/kbs/join_indexing.rb +106 -0
  33. data/lib/kbs/join_node.rb +93 -57
  34. data/lib/kbs/join_test_builder.rb +80 -0
  35. data/lib/kbs/join_tests.rb +66 -0
  36. data/lib/kbs/memory_index.rb +104 -0
  37. data/lib/kbs/negation_node.rb +6 -82
  38. data/lib/kbs/network_node.rb +31 -0
  39. data/lib/kbs/pattern_matching.rb +52 -0
  40. data/lib/kbs/production_node.rb +50 -6
  41. data/lib/kbs/quantifier_node.rb +140 -0
  42. data/lib/kbs/rule.rb +26 -13
  43. data/lib/kbs/rule_source.rb +74 -0
  44. data/lib/kbs/rule_step.rb +27 -0
  45. data/lib/kbs/token.rb +76 -10
  46. data/lib/kbs/tracer.rb +80 -0
  47. data/lib/kbs/truth_maintenance.rb +82 -0
  48. data/lib/kbs/version.rb +1 -1
  49. data/lib/kbs/working_memory.rb +25 -3
  50. data/lib/kbs.rb +17 -4
  51. metadata +22 -129
  52. data/.envrc +0 -3
  53. data/.github/workflows/deploy-github-pages.yml +0 -52
  54. data/COMMITS.md +0 -196
  55. data/Rakefile +0 -8
  56. data/docs/advanced/custom-persistence.md +0 -775
  57. data/docs/advanced/debugging.md +0 -726
  58. data/docs/advanced/index.md +0 -8
  59. data/docs/advanced/performance.md +0 -865
  60. data/docs/advanced/testing.md +0 -827
  61. data/docs/api/blackboard.md +0 -1157
  62. data/docs/api/engine.md +0 -1047
  63. data/docs/api/facts.md +0 -1212
  64. data/docs/api/index.md +0 -12
  65. data/docs/api/rules.md +0 -1104
  66. data/docs/architecture/blackboard.md +0 -544
  67. data/docs/architecture/index.md +0 -277
  68. data/docs/architecture/network-structure.md +0 -343
  69. data/docs/architecture/rete-algorithm.md +0 -737
  70. data/docs/assets/css/custom.css +0 -83
  71. data/docs/assets/images/blackboard-architecture.svg +0 -136
  72. data/docs/assets/images/compiled-network.svg +0 -101
  73. data/docs/assets/images/fact-assertion-flow.svg +0 -117
  74. data/docs/assets/images/fact-rule-relationship.svg +0 -65
  75. data/docs/assets/images/fact-structure.svg +0 -42
  76. data/docs/assets/images/inference-cycle.svg +0 -47
  77. data/docs/assets/images/kb-components.svg +0 -43
  78. data/docs/assets/images/kbs.jpg +0 -0
  79. data/docs/assets/images/pattern-matching-trace.svg +0 -136
  80. data/docs/assets/images/rete-network-layers.svg +0 -96
  81. data/docs/assets/images/rule-structure.svg +0 -44
  82. data/docs/assets/images/system-layers.svg +0 -69
  83. data/docs/assets/images/trading-signal-network.svg +0 -139
  84. data/docs/assets/js/mathjax.js +0 -17
  85. data/docs/examples/index.md +0 -223
  86. data/docs/guides/blackboard-memory.md +0 -589
  87. data/docs/guides/dsl.md +0 -1321
  88. data/docs/guides/facts.md +0 -652
  89. data/docs/guides/getting-started.md +0 -385
  90. data/docs/guides/index.md +0 -23
  91. data/docs/guides/negation.md +0 -529
  92. data/docs/guides/pattern-matching.md +0 -561
  93. data/docs/guides/persistence.md +0 -451
  94. data/docs/guides/variable-binding.md +0 -491
  95. data/docs/guides/writing-rules.md +0 -914
  96. data/docs/index.md +0 -155
  97. data/docs/installation.md +0 -156
  98. data/docs/quick-start.md +0 -221
  99. data/docs/what-is-a-fact.md +0 -694
  100. data/docs/what-is-a-knowledge-base.md +0 -350
  101. data/docs/what-is-a-rule.md +0 -833
  102. data/examples/.gitignore +0 -1
  103. data/examples/README.md +0 -531
  104. data/examples/advanced_example.rb +0 -270
  105. data/examples/advanced_example_dsl.rb +0 -224
  106. data/examples/ai_enhanced_kbs.rb +0 -523
  107. data/examples/ai_enhanced_kbs_dsl.rb +0 -538
  108. data/examples/blackboard_demo.rb +0 -50
  109. data/examples/blackboard_demo_dsl.rb +0 -50
  110. data/examples/car_diagnostic.rb +0 -64
  111. data/examples/car_diagnostic_dsl.rb +0 -54
  112. data/examples/concurrent_inference_demo.rb +0 -362
  113. data/examples/concurrent_inference_demo_dsl.rb +0 -362
  114. data/examples/csv_trading_system.rb +0 -559
  115. data/examples/csv_trading_system_dsl.rb +0 -525
  116. data/examples/iot_demo_using_dsl.rb +0 -83
  117. data/examples/portfolio_rebalancing_system.rb +0 -651
  118. data/examples/portfolio_rebalancing_system_dsl.rb +0 -613
  119. data/examples/redis_trading_demo.rb +0 -177
  120. data/examples/redis_trading_demo_dsl.rb +0 -177
  121. data/examples/rule_source_demo.rb +0 -123
  122. data/examples/run_all.rb +0 -50
  123. data/examples/run_all_dsl.rb +0 -49
  124. data/examples/sample_stock_data.csv +0 -46
  125. data/examples/stock_trading_advanced.rb +0 -469
  126. data/examples/stock_trading_advanced_dsl.rb +0 -404
  127. data/examples/temp_dsl.txt +0 -9392
  128. data/examples/timestamped_trading.rb +0 -286
  129. data/examples/timestamped_trading_dsl.rb +0 -258
  130. data/examples/trading_demo.rb +0 -334
  131. data/examples/trading_demo_dsl.rb +0 -322
  132. data/examples/working_demo.rb +0 -176
  133. data/examples/working_demo_dsl.rb +0 -160
  134. data/mkdocs.yml +0 -181
@@ -1,538 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'ruby_llm'
4
- require 'ruby_llm/mcp'
5
- require_relative '../lib/kbs'
6
- require 'json'
7
- require 'date'
8
-
9
- # Configure RubyLLM for Ollama
10
- RubyLLM.configure do |config|
11
- config.ollama_api_base = ENV['OLLAMA_API_BASE'] || 'http://localhost:11434'
12
- end
13
-
14
- module AIEnhancedKBS
15
- class AIKnowledgeSystem
16
- include KBS::DSL::ConditionHelpers
17
-
18
- attr_reader :kb
19
-
20
- def initialize
21
- @kb = nil
22
- @ai_client = setup_ai_client
23
- @mcp_agent = setup_mcp_agent
24
- @sentiment_cache = {}
25
- @explanation_cache = {}
26
- setup_ai_enhanced_rules
27
- end
28
-
29
- def setup_ai_client
30
- model = ENV['OLLAMA_MODEL'] || 'gpt-oss:latest'
31
- puts "🤖 Initializing RubyLLM::Chat with Ollama model: #{model}"
32
- RubyLLM::Chat.new(provider: :ollama, model: model)
33
- end
34
-
35
- def setup_mcp_agent
36
- if defined?(RubyLLM::MCP::Agent)
37
- RubyLLM::MCP::Agent.new(
38
- name: "market_analyst",
39
- description: "AI agent for market analysis and trading insights"
40
- )
41
- else
42
- nil
43
- end
44
- end
45
-
46
- def setup_ai_enhanced_rules
47
- ai_sys = self # Capture self for use in perform blocks
48
- @kb = KBS.knowledge_base do
49
- rule "ai_sentiment_analysis" do
50
- priority 20
51
- on :news_data,
52
- symbol: satisfies { |s| s && s.length > 0 },
53
- headline: satisfies { |h| h && h.length > 10 },
54
- content: satisfies { |c| c && c.length > 50 }
55
-
56
- perform do |facts|
57
- news = facts.find { |f| f.type == :news_data }
58
- symbol = news[:symbol]
59
-
60
- # AI-powered sentiment analysis
61
- sentiment = ai_sys.analyze_sentiment_with_ai(news[:headline], news[:content])
62
-
63
- puts "🤖 AI SENTIMENT ANALYSIS: #{symbol}"
64
- puts " Headline: #{news[:headline][0..80]}..."
65
- puts " AI Sentiment: #{sentiment[:sentiment]} (#{sentiment[:confidence]}%)"
66
- puts " Key Themes: #{sentiment[:themes].join(', ')}"
67
- puts " Market Impact: #{sentiment[:market_impact]}"
68
-
69
- # Add sentiment fact to working memory
70
- ai_sys.kb.fact :ai_sentiment, {
71
- symbol: symbol,
72
- sentiment_score: sentiment[:score],
73
- confidence: sentiment[:confidence],
74
- themes: sentiment[:themes],
75
- market_impact: sentiment[:market_impact],
76
- timestamp: Time.now
77
- }
78
- end
79
- end
80
-
81
- rule "ai_strategy_generation" do
82
- priority 15
83
- on :market_conditions,
84
- volatility: satisfies { |v| v && v > 25 },
85
- trend: satisfies { |t| t && t.length > 0 }
86
- on :portfolio_state, cash_ratio: satisfies { |c| c && c > 0.2 }
87
-
88
- perform do |facts|
89
- market = facts.find { |f| f.type == :market_conditions }
90
- portfolio = facts.find { |f| f.type == :portfolio_state }
91
-
92
- # Generate AI strategy
93
- strategy = ai_sys.generate_ai_trading_strategy(market, portfolio)
94
-
95
- puts "🧠 AI TRADING STRATEGY"
96
- puts " Market Context: #{market[:trend]} trend, #{market[:volatility]}% volatility"
97
- puts " Strategy: #{strategy[:name]}"
98
- puts " Rationale: #{strategy[:rationale]}"
99
- puts " Actions: #{strategy[:actions].join(', ')}"
100
- puts " Risk Level: #{strategy[:risk_level]}"
101
- end
102
- end
103
-
104
- rule "dynamic_rule_creation" do
105
- priority 12
106
- on :pattern_anomaly,
107
- pattern_type: satisfies { |p| p && p.length > 0 },
108
- confidence: satisfies { |c| c && c > 0.8 },
109
- occurrences: satisfies { |o| o && o > 5 }
110
-
111
- perform do |facts|
112
- anomaly = facts.find { |f| f.type == :pattern_anomaly }
113
-
114
- # AI generates new trading rule
115
- new_rule_spec = generate_rule_with_ai(anomaly)
116
-
117
- puts "🎯 AI RULE GENERATION"
118
- puts " Pattern: #{anomaly[:pattern_type]}"
119
- puts " New Rule: #{new_rule_spec[:name]}"
120
- puts " Logic: #{new_rule_spec[:description]}"
121
-
122
- # Dynamically add new rule to engine
123
- if new_rule_spec[:valid]
124
- dynamic_rule = create_rule_from_spec(new_rule_spec)
125
- @kb.engine.add_rule(dynamic_rule)
126
- puts " ✅ Rule added to knowledge base"
127
- end
128
- end
129
- end
130
-
131
- rule "ai_risk_assessment" do
132
- priority 18
133
- on :position, unrealized_pnl: satisfies { |pnl| pnl && pnl.abs > 1000 }
134
- on :market_data, symbol: satisfies { |s| s && s.length > 0 }
135
-
136
- perform do |facts|
137
- position = facts.find { |f| f.type == :position }
138
- market_data = facts.find { |f| f.type == :market_data }
139
-
140
- # AI-powered risk analysis
141
- risk_analysis = ai_sys.analyze_position_risk_with_ai(position, market_data)
142
-
143
- puts "⚠️ AI RISK ASSESSMENT: #{position[:symbol]}"
144
- puts " Current P&L: $#{position[:unrealized_pnl]}"
145
- puts " Risk Level: #{risk_analysis[:risk_level]}"
146
- puts " Key Risks: #{risk_analysis[:risks].join(', ')}"
147
- puts " Recommendation: #{risk_analysis[:recommendation]}"
148
- puts " Confidence: #{risk_analysis[:confidence]}%"
149
-
150
- # Act on high-risk situations
151
- if risk_analysis[:risk_level] == "HIGH" && risk_analysis[:confidence] > 80
152
- puts " 🚨 HIGH RISK DETECTED - Consider position adjustment"
153
- end
154
- end
155
- end
156
-
157
- rule "ai_explanation_generator" do
158
- priority 5
159
- on :trade_recommendation,
160
- action: satisfies { |a| ["BUY", "SELL", "HOLD"].include?(a) },
161
- symbol: satisfies { |s| s && s.length > 0 }
162
-
163
- perform do |facts|
164
- recommendation = facts.find { |f| f.type == :trade_recommendation }
165
-
166
- # Generate natural language explanation
167
- explanation = ai_sys.generate_trade_explanation(recommendation, facts)
168
-
169
- puts "💬 AI EXPLANATION: #{recommendation[:symbol]} #{recommendation[:action]}"
170
- puts " Reasoning: #{explanation[:reasoning]}"
171
- puts " Context: #{explanation[:context]}"
172
- puts " Confidence: #{explanation[:confidence]}%"
173
- puts " Alternative View: #{explanation[:alternative]}"
174
- end
175
- end
176
-
177
- rule "ai_pattern_recognition" do
178
- priority 10
179
- on :price_history,
180
- symbol: satisfies { |s| s && s.length > 0 },
181
- data_points: satisfies { |d| d && d.length >= 30 }
182
-
183
- perform do |facts|
184
- price_data = facts.find { |f| f.type == :price_history }
185
-
186
- # AI identifies patterns
187
- patterns = ai_sys.identify_patterns_with_ai(price_data[:data_points])
188
-
189
- if patterns.any?
190
- puts "📊 AI PATTERN RECOGNITION: #{price_data[:symbol]}"
191
- patterns.each do |pattern|
192
- puts " Pattern: #{pattern[:name]} (#{pattern[:confidence]}%)"
193
- puts " Prediction: #{pattern[:prediction]}"
194
- puts " Time Horizon: #{pattern[:time_horizon]}"
195
- end
196
- end
197
- end
198
- end
199
- end
200
- end
201
-
202
- def analyze_sentiment_with_ai(headline, content)
203
- cache_key = "#{headline[0..50]}_#{content[0..100]}".hash
204
- return @sentiment_cache[cache_key] if @sentiment_cache[cache_key]
205
-
206
- prompt = build_sentiment_prompt(headline, content)
207
- puts "\n🔗 Calling RubyLLM for sentiment analysis..."
208
-
209
- message = @ai_client.ask(prompt)
210
- response_text = message.content.to_s
211
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
212
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
213
-
214
- result = parse_sentiment_response(response_text)
215
-
216
- @sentiment_cache[cache_key] = result
217
- result
218
- end
219
-
220
- def generate_ai_trading_strategy(market_conditions, portfolio_state)
221
- prompt = build_strategy_prompt(market_conditions, portfolio_state)
222
- puts "\n🔗 Calling RubyLLM for trading strategy..."
223
-
224
- message = @ai_client.ask(prompt)
225
- response_text = message.content.to_s
226
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
227
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
228
-
229
- parse_strategy_response(response_text)
230
- end
231
-
232
- def generate_rule_with_ai(anomaly_data)
233
- prompt = build_rule_generation_prompt(anomaly_data)
234
- puts "\n🔗 Calling RubyLLM for rule generation..."
235
-
236
- message = @ai_client.ask(prompt)
237
- response_text = message.content.to_s
238
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
239
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
240
-
241
- parse_rule_specification(response_text)
242
- end
243
-
244
- def analyze_position_risk_with_ai(position, market_data)
245
- prompt = build_risk_analysis_prompt(position, market_data)
246
- puts "\n🔗 Calling RubyLLM for risk analysis..."
247
-
248
- message = @ai_client.ask(prompt)
249
- response_text = message.content.to_s
250
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
251
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
252
-
253
- parse_risk_analysis(response_text)
254
- end
255
-
256
- def generate_trade_explanation(recommendation, context_facts)
257
- cache_key = "#{recommendation[:symbol]}_#{recommendation[:action]}_#{context_facts.length}".hash
258
- return @explanation_cache[cache_key] if @explanation_cache[cache_key]
259
-
260
- prompt = build_explanation_prompt(recommendation, context_facts)
261
- puts "\n🔗 Calling RubyLLM for trade explanation..."
262
-
263
- message = @ai_client.ask(prompt)
264
- response_text = message.content.to_s
265
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
266
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
267
-
268
- result = parse_explanation_response(response_text)
269
-
270
- @explanation_cache[cache_key] = result
271
- result
272
- end
273
-
274
- def identify_patterns_with_ai(price_data)
275
- prompt = build_pattern_recognition_prompt(price_data)
276
- puts "\n🔗 Calling RubyLLM for pattern recognition..."
277
-
278
- message = @ai_client.ask(prompt)
279
- response_text = message.content.to_s
280
- puts "✅ Got response from Ollama (#{response_text.length} chars)"
281
- puts "📝 Response: #{response_text[0..200]}..." if response_text.length > 200
282
-
283
- parse_pattern_response(response_text)
284
- end
285
-
286
- # Prompt builders
287
- def build_sentiment_prompt(headline, content)
288
- <<~PROMPT
289
- Analyze the sentiment of this financial news for trading implications:
290
-
291
- Headline: #{headline}
292
- Content: #{content[0..500]}...
293
-
294
- Provide a JSON response with:
295
- {
296
- "sentiment": "positive|negative|neutral",
297
- "score": -1.0 to 1.0,
298
- "confidence": 0-100,
299
- "themes": ["theme1", "theme2"],
300
- "market_impact": "bullish|bearish|neutral"
301
- }
302
- PROMPT
303
- end
304
-
305
- def build_strategy_prompt(market_conditions, portfolio_state)
306
- <<~PROMPT
307
- Generate a trading strategy for these conditions:
308
-
309
- Market: #{market_conditions[:trend]} trend, #{market_conditions[:volatility]}% volatility
310
- Portfolio: #{(portfolio_state[:cash_ratio] * 100).round(1)}% cash
311
-
312
- Provide a JSON strategy with:
313
- {
314
- "name": "strategy_name",
315
- "rationale": "why this strategy fits",
316
- "actions": ["action1", "action2"],
317
- "risk_level": "LOW|MEDIUM|HIGH"
318
- }
319
- PROMPT
320
- end
321
-
322
- def build_risk_analysis_prompt(position, market_data)
323
- <<~PROMPT
324
- Analyze the risk of this trading position:
325
-
326
- Position: #{position[:symbol]}, P&L: $#{position[:unrealized_pnl]}
327
- Market Data: #{market_data.to_json}
328
-
329
- Provide risk assessment as JSON:
330
- {
331
- "risk_level": "LOW|MEDIUM|HIGH",
332
- "risks": ["risk1", "risk2"],
333
- "recommendation": "hold|reduce|exit",
334
- "confidence": 0-100
335
- }
336
- PROMPT
337
- end
338
-
339
- def build_explanation_prompt(recommendation, context_facts)
340
- <<~PROMPT
341
- Explain this trading recommendation in simple terms:
342
-
343
- Recommendation: #{recommendation[:action]} #{recommendation[:symbol]}
344
- Context: #{context_facts.length} supporting facts
345
-
346
- Provide explanation as JSON:
347
- {
348
- "explanation": "clear explanation",
349
- "reasoning": "why this makes sense",
350
- "risks": ["risk1", "risk2"]
351
- }
352
- PROMPT
353
- end
354
-
355
- def build_pattern_recognition_prompt(price_data)
356
- <<~PROMPT
357
- Identify trading patterns in this price data:
358
-
359
- Data: #{price_data.to_json}
360
-
361
- Return JSON array of patterns:
362
- [
363
- {
364
- "pattern": "pattern_name",
365
- "confidence": 0-100,
366
- "description": "what this means"
367
- }
368
- ]
369
- PROMPT
370
- end
371
-
372
- def build_rule_generation_prompt(anomaly_data)
373
- <<~PROMPT
374
- Generate a trading rule specification for this anomaly pattern:
375
-
376
- Pattern Type: #{anomaly_data[:pattern_type]}
377
- Confidence: #{anomaly_data[:confidence]}
378
- Occurrences: #{anomaly_data[:occurrences]}
379
-
380
- Return JSON with:
381
- {
382
- "name": "rule_name",
383
- "description": "what this rule does",
384
- "valid": true|false
385
- }
386
- PROMPT
387
- end
388
-
389
- def create_rule_from_spec(spec)
390
- # Simplified rule creation - in production would parse spec more thoroughly
391
- KBS::Rule.new(
392
- spec[:name],
393
- conditions: [],
394
- action: lambda { |facts, bindings| puts "Dynamic rule fired: #{spec[:name]}" }
395
- )
396
- end
397
-
398
- # Response parsers
399
- def extract_json(response)
400
- # Strip markdown code fences if present
401
- json_text = response.strip
402
- json_text = json_text.gsub(/^```json\s*/, '').gsub(/```\s*$/, '').strip
403
- json_text
404
- end
405
-
406
- def parse_sentiment_response(response)
407
- JSON.parse(extract_json(response), symbolize_names: true)
408
- end
409
-
410
- def parse_strategy_response(response)
411
- JSON.parse(extract_json(response), symbolize_names: true)
412
- end
413
-
414
- def parse_explanation_response(response)
415
- result = JSON.parse(extract_json(response), symbolize_names: true)
416
- # Ensure all expected keys exist
417
- {
418
- reasoning: result[:reasoning] || result[:explanation] || "No reasoning provided",
419
- context: result[:context] || "No context provided",
420
- confidence: result[:confidence] || 50,
421
- alternative: result[:alternative] || result[:risks]&.join(", ") || "No alternatives provided"
422
- }
423
- end
424
-
425
- def parse_pattern_response(response)
426
- patterns = JSON.parse(extract_json(response), symbolize_names: true)
427
- # Ensure array format and normalize keys
428
- patterns = [patterns] unless patterns.is_a?(Array)
429
- patterns.map do |p|
430
- {
431
- name: p[:pattern] || p[:name] || "Unknown Pattern",
432
- confidence: p[:confidence] || 50,
433
- prediction: p[:prediction] || p[:description] || "No prediction",
434
- time_horizon: p[:time_horizon] || "Unknown"
435
- }
436
- end
437
- end
438
-
439
- def parse_risk_analysis(response)
440
- JSON.parse(extract_json(response), symbolize_names: true)
441
- end
442
-
443
- def parse_rule_specification(response)
444
- JSON.parse(extract_json(response), symbolize_names: true)
445
- end
446
-
447
- def demonstrate_ai_enhancements
448
- puts "🤖 AI-ENHANCED KNOWLEDGE-BASED SYSTEM"
449
- puts "=" * 70
450
- puts "Integrating #{@ai_client.class.name} and #{@mcp_agent.class.name if @mcp_agent}"
451
- puts "=" * 70
452
-
453
- # Scenario 1: AI Sentiment Analysis
454
- puts "\n📰 SCENARIO 1: AI-Powered News Sentiment"
455
- puts "-" * 50
456
- @kb.reset
457
-
458
- @kb.fact :news_data, {
459
- symbol: "AAPL",
460
- headline: "Apple Reports Record Q4 Earnings, Beats Expectations by 15%",
461
- content: "Apple Inc. announced exceptional fourth quarter results today, with revenue growing 12% year-over-year to $94.9 billion. iPhone sales exceeded analysts' expectations, driven by strong demand for the iPhone 15 Pro models. The company also announced a new $90 billion share buyback program and increased its dividend by 4%. CEO Tim Cook expressed optimism about the AI integration roadmap and services growth trajectory.",
462
- published_at: Time.now
463
- }
464
- @kb.run
465
-
466
- # Scenario 2: AI Strategy Generation
467
- puts "\n🧠 SCENARIO 2: AI Trading Strategy Generation"
468
- puts "-" * 50
469
- @kb.reset
470
-
471
- @kb.fact :market_conditions, {
472
- volatility: 28.5,
473
- trend: "sideways",
474
- sector_rotation: "technology_to_healthcare"
475
- }
476
-
477
- @kb.fact :portfolio_state, {
478
- cash_ratio: 0.25,
479
- largest_position: "AAPL",
480
- sector_concentration: 0.45
481
- }
482
- @kb.run
483
-
484
- # Scenario 3: AI Risk Assessment
485
- puts "\n⚠️ SCENARIO 3: AI Risk Assessment"
486
- puts "-" * 50
487
- @kb.reset
488
-
489
- @kb.fact :position, {
490
- symbol: "TSLA",
491
- shares: 100,
492
- entry_price: 250.00,
493
- current_price: 235.00,
494
- unrealized_pnl: -1500
495
- }
496
-
497
- @kb.fact :market_data, {
498
- symbol: "TSLA",
499
- volatility: 45.2,
500
- beta: 2.1,
501
- sector: "Consumer Discretionary"
502
- }
503
- @kb.run
504
-
505
- # Scenario 4: Trade Explanation
506
- puts "\n💬 SCENARIO 4: AI Trade Explanation"
507
- puts "-" * 50
508
- @kb.reset
509
-
510
- @kb.fact :trade_recommendation, {
511
- symbol: "GOOGL",
512
- action: "BUY",
513
- quantity: 50,
514
- confidence: 85
515
- }
516
-
517
- @kb.fact :technical_analysis, {
518
- symbol: "GOOGL",
519
- rsi: 35,
520
- moving_average_signal: "golden_cross",
521
- volume_trend: "increasing"
522
- }
523
- @kb.run
524
-
525
- puts "\n" + "=" * 70
526
- puts "AI ENHANCEMENT DEMONSTRATION COMPLETE"
527
- puts "🎯 The system now combines rule-based logic with AI insights"
528
- puts "🧠 Dynamic pattern recognition and strategy generation"
529
- puts "💬 Natural language explanations for all decisions"
530
- puts "⚡ Real-time sentiment analysis and risk assessment"
531
- end
532
- end
533
- end
534
-
535
- if __FILE__ == $0
536
- system = AIEnhancedKBS::AIKnowledgeSystem.new
537
- system.demonstrate_ai_enhancements
538
- end
@@ -1,50 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative '../lib/kbs/blackboard'
4
-
5
- puts "Blackboard Memory System Demonstration"
6
- puts "=" * 70
7
-
8
- engine = KBS::Blackboard::Engine.new(db_path: 'knowledge_base.db')
9
-
10
- puts "\nAdding persistent facts..."
11
- sensor1 = engine.add_fact(:sensor, { location: "room_1", type: "temperature", value: 22 })
12
- sensor2 = engine.add_fact(:sensor, { location: "room_2", type: "humidity", value: 65 })
13
- alert = engine.add_fact(:alert, { level: "warning", message: "Check sensors" })
14
-
15
- puts "Facts added with UUIDs:"
16
- puts " Sensor 1: #{sensor1.uuid}"
17
- puts " Sensor 2: #{sensor2.uuid}"
18
- puts " Alert: #{alert.uuid}"
19
-
20
- puts "\nPosting messages to blackboard..."
21
- engine.post_message("TemperatureMonitor", "sensor_data", { reading: 25, timestamp: Time.now }, priority: 5)
22
- engine.post_message("HumidityMonitor", "sensor_data", { reading: 70, timestamp: Time.now }, priority: 3)
23
- engine.post_message("SystemController", "commands", { action: "calibrate", target: "all_sensors" }, priority: 10)
24
-
25
- puts "\nConsuming high-priority message..."
26
- message = engine.consume_message("commands", "MainController")
27
- puts " Received: #{message[:content]}" if message
28
-
29
- puts "\nUpdating sensor value..."
30
- sensor1[:value] = 28
31
-
32
- puts "\nDatabase Statistics:"
33
- stats = engine.stats
34
- stats.each do |key, value|
35
- puts " #{key.to_s.gsub('_', ' ').capitalize}: #{value}"
36
- end
37
-
38
- puts "\nFact History (last 5 entries):"
39
- history = engine.blackboard.get_history(limit: 5)
40
- history.each do |entry|
41
- puts " [#{entry[:timestamp].strftime('%H:%M:%S')}] #{entry[:action]}: #{entry[:fact_type]}(#{entry[:attributes]})"
42
- end
43
-
44
- puts "\nQuerying facts by type..."
45
- sensors = engine.blackboard.get_facts(:sensor)
46
- puts " Found #{sensors.size} sensor(s)"
47
- sensors.each { |s| puts " - #{s}" }
48
-
49
- puts "\n" + "=" * 70
50
- puts "Blackboard persisted to: knowledge_base.db"
@@ -1,50 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative '../lib/kbs/blackboard'
4
-
5
- puts "Blackboard Memory System Demonstration"
6
- puts "=" * 70
7
-
8
- engine = KBS::Blackboard::Engine.new(db_path: 'knowledge_base.db')
9
-
10
- puts "\nAdding persistent facts..."
11
- sensor1 = engine.add_fact(:sensor, { location: "room_1", type: "temperature", value: 22 })
12
- sensor2 = engine.add_fact(:sensor, { location: "room_2", type: "humidity", value: 65 })
13
- alert = engine.add_fact(:alert, { level: "warning", message: "Check sensors" })
14
-
15
- puts "Facts added with UUIDs:"
16
- puts " Sensor 1: #{sensor1.uuid}"
17
- puts " Sensor 2: #{sensor2.uuid}"
18
- puts " Alert: #{alert.uuid}"
19
-
20
- puts "\nPosting messages to blackboard..."
21
- engine.post_message("TemperatureMonitor", "sensor_data", { reading: 25, timestamp: Time.now }, priority: 5)
22
- engine.post_message("HumidityMonitor", "sensor_data", { reading: 70, timestamp: Time.now }, priority: 3)
23
- engine.post_message("SystemController", "commands", { action: "calibrate", target: "all_sensors" }, priority: 10)
24
-
25
- puts "\nConsuming high-priority message..."
26
- message = engine.consume_message("commands", "MainController")
27
- puts " Received: #{message[:content]}" if message
28
-
29
- puts "\nUpdating sensor value..."
30
- sensor1[:value] = 28
31
-
32
- puts "\nDatabase Statistics:"
33
- stats = engine.stats
34
- stats.each do |key, value|
35
- puts " #{key.to_s.gsub('_', ' ').capitalize}: #{value}"
36
- end
37
-
38
- puts "\nFact History (last 5 entries):"
39
- history = engine.blackboard.get_history(limit: 5)
40
- history.each do |entry|
41
- puts " [#{entry[:timestamp].strftime('%H:%M:%S')}] #{entry[:action]}: #{entry[:fact_type]}(#{entry[:attributes]})"
42
- end
43
-
44
- puts "\nQuerying facts by type..."
45
- sensors = engine.blackboard.get_facts(:sensor)
46
- puts " Found #{sensors.size} sensor(s)"
47
- sensors.each { |s| puts " - #{s}" }
48
-
49
- puts "\n" + "=" * 70
50
- puts "Blackboard persisted to: knowledge_base.db"
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative '../lib/kbs'
4
-
5
- engine = KBS::Engine.new
6
-
7
- puts "Creating a simple expert system for diagnosing car problems..."
8
- puts "-" * 60
9
-
10
- rule1 = KBS::Rule.new(
11
- "dead_battery",
12
- conditions: [
13
- KBS::Condition.new(:symptom, { problem: "won't start" }),
14
- KBS::Condition.new(:symptom, { problem: "no lights" })
15
- ],
16
- action: lambda do |facts, bindings|
17
- puts "DIAGNOSIS: Dead battery - The car won't start and has no lights"
18
- puts "RECOMMENDATION: Jump start the battery or replace it"
19
- end
20
- )
21
-
22
- rule2 = KBS::Rule.new(
23
- "flat_tire",
24
- conditions: [
25
- KBS::Condition.new(:symptom, { problem: "pulling to side" }),
26
- KBS::Condition.new(:symptom, { problem: "low tire pressure" })
27
- ],
28
- action: lambda do |facts, bindings|
29
- puts "DIAGNOSIS: Flat or low tire"
30
- puts "RECOMMENDATION: Check tire pressure and inflate or replace tire"
31
- end
32
- )
33
-
34
- rule3 = KBS::Rule.new(
35
- "overheating",
36
- conditions: [
37
- KBS::Condition.new(:symptom, { problem: "high temperature" }),
38
- KBS::Condition.new(:symptom, { problem: "steam from hood" }, negated: false),
39
- KBS::Condition.new(:symptom, { problem: "coolant leak" }, negated: true)
40
- ],
41
- action: lambda do |facts, bindings|
42
- puts "DIAGNOSIS: Engine overheating (no coolant leak detected)"
43
- puts "RECOMMENDATION: Check radiator and cooling system"
44
- end
45
- )
46
-
47
- engine.add_rule(rule1)
48
- engine.add_rule(rule2)
49
- engine.add_rule(rule3)
50
-
51
- puts "\nAdding symptoms..."
52
- engine.add_fact(:symptom, { problem: "won't start", severity: "high" })
53
- engine.add_fact(:symptom, { problem: "no lights", severity: "high" })
54
-
55
- puts "\nRunning inference engine..."
56
- engine.run
57
-
58
- puts "\n" + "-" * 60
59
- puts "\nAdding more symptoms..."
60
- engine.add_fact(:symptom, { problem: "high temperature", severity: "critical" })
61
- engine.add_fact(:symptom, { problem: "steam from hood", severity: "high" })
62
-
63
- puts "\nRunning inference engine again..."
64
- engine.run