kbs 0.0.1 → 0.2.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 +4 -4
- data/.github/workflows/deploy-github-pages.yml +52 -0
- data/CHANGELOG.md +68 -2
- data/README.md +291 -362
- data/docs/advanced/custom-persistence.md +775 -0
- data/docs/advanced/debugging.md +726 -0
- data/docs/advanced/index.md +8 -0
- data/docs/advanced/performance.md +865 -0
- data/docs/advanced/testing.md +827 -0
- data/docs/api/blackboard.md +1157 -0
- data/docs/api/engine.md +1047 -0
- data/docs/api/facts.md +1212 -0
- data/docs/api/index.md +12 -0
- data/docs/api/rules.md +1104 -0
- data/docs/architecture/blackboard.md +544 -0
- data/docs/architecture/index.md +277 -0
- data/docs/architecture/network-structure.md +343 -0
- data/docs/architecture/rete-algorithm.md +737 -0
- data/docs/assets/css/custom.css +83 -0
- data/docs/assets/images/blackboard-architecture.svg +136 -0
- data/docs/assets/images/compiled-network.svg +101 -0
- data/docs/assets/images/fact-assertion-flow.svg +117 -0
- data/docs/assets/images/fact-rule-relationship.svg +65 -0
- data/docs/assets/images/fact-structure.svg +42 -0
- data/docs/assets/images/inference-cycle.svg +47 -0
- data/docs/assets/images/kb-components.svg +43 -0
- data/docs/assets/images/kbs.jpg +0 -0
- data/docs/assets/images/pattern-matching-trace.svg +136 -0
- data/docs/assets/images/rete-network-layers.svg +96 -0
- data/docs/assets/images/rule-structure.svg +44 -0
- data/docs/assets/images/system-layers.svg +69 -0
- data/docs/assets/images/trading-signal-network.svg +139 -0
- data/docs/assets/js/mathjax.js +17 -0
- data/docs/examples/index.md +223 -0
- data/docs/guides/blackboard-memory.md +589 -0
- data/docs/guides/dsl.md +1321 -0
- data/docs/guides/facts.md +652 -0
- data/docs/guides/getting-started.md +385 -0
- data/docs/guides/index.md +23 -0
- data/docs/guides/negation.md +529 -0
- data/docs/guides/pattern-matching.md +561 -0
- data/docs/guides/persistence.md +451 -0
- data/docs/guides/variable-binding.md +491 -0
- data/docs/guides/writing-rules.md +914 -0
- data/docs/index.md +155 -0
- data/docs/installation.md +156 -0
- data/docs/quick-start.md +221 -0
- data/docs/what-is-a-fact.md +694 -0
- data/docs/what-is-a-knowledge-base.md +350 -0
- data/docs/what-is-a-rule.md +833 -0
- data/examples/.gitignore +1 -0
- data/examples/README.md +2 -2
- data/examples/advanced_example.rb +2 -2
- data/examples/advanced_example_dsl.rb +224 -0
- data/examples/ai_enhanced_kbs.rb +1 -1
- data/examples/ai_enhanced_kbs_dsl.rb +538 -0
- data/examples/blackboard_demo_dsl.rb +50 -0
- data/examples/car_diagnostic.rb +1 -1
- data/examples/car_diagnostic_dsl.rb +54 -0
- data/examples/concurrent_inference_demo.rb +5 -6
- data/examples/concurrent_inference_demo_dsl.rb +362 -0
- data/examples/csv_trading_system.rb +1 -1
- data/examples/csv_trading_system_dsl.rb +525 -0
- data/examples/iot_demo_using_dsl.rb +1 -1
- data/examples/portfolio_rebalancing_system.rb +2 -2
- data/examples/portfolio_rebalancing_system_dsl.rb +613 -0
- data/examples/redis_trading_demo_dsl.rb +177 -0
- data/examples/rule_source_demo.rb +123 -0
- data/examples/run_all.rb +50 -0
- data/examples/run_all_dsl.rb +49 -0
- data/examples/stock_trading_advanced.rb +1 -1
- data/examples/stock_trading_advanced_dsl.rb +404 -0
- data/examples/temp_dsl.txt +9392 -0
- data/examples/timestamped_trading.rb +1 -1
- data/examples/timestamped_trading_dsl.rb +258 -0
- data/examples/trading_demo.rb +1 -1
- data/examples/trading_demo_dsl.rb +322 -0
- data/examples/working_demo.rb +1 -1
- data/examples/working_demo_dsl.rb +160 -0
- data/lib/kbs/blackboard/engine.rb +3 -3
- data/lib/kbs/blackboard/fact.rb +1 -1
- data/lib/kbs/condition.rb +1 -1
- data/lib/kbs/decompiler.rb +204 -0
- data/lib/kbs/dsl/knowledge_base.rb +101 -2
- data/lib/kbs/dsl/variable.rb +1 -1
- data/lib/kbs/dsl.rb +3 -1
- data/lib/kbs/{rete_engine.rb → engine.rb} +42 -1
- data/lib/kbs/fact.rb +1 -1
- data/lib/kbs/version.rb +1 -1
- data/lib/kbs.rb +15 -13
- data/mkdocs.yml +181 -0
- metadata +74 -9
- data/examples/stock_trading_system.rb.bak +0 -563
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Real-world applications demonstrating KBS capabilities. Each example is available in both low-level API and DSL versions.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
### Working Demo
|
|
8
|
+
**Files:** [`working_demo.rb`](https://github.com/madbomber/kbs/blob/main/examples/working_demo.rb) | [`working_demo_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/working_demo_dsl.rb)
|
|
9
|
+
|
|
10
|
+
Simple trading system demonstrating the basics of KBS with momentum signals, volume alerts, and price movement detection. Perfect starting point for learning KBS fundamentals.
|
|
11
|
+
|
|
12
|
+
**Features:**
|
|
13
|
+
|
|
14
|
+
- Basic rule definition and pattern matching
|
|
15
|
+
- Stock momentum detection
|
|
16
|
+
- High volume alerts
|
|
17
|
+
- Price change notifications
|
|
18
|
+
|
|
19
|
+
### Advanced Example
|
|
20
|
+
**Files:** [`advanced_example.rb`](https://github.com/madbomber/kbs/blob/main/examples/advanced_example.rb) | [`advanced_example_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/advanced_example_dsl.rb)
|
|
21
|
+
|
|
22
|
+
More complex patterns including multi-condition rules, variable bindings, and negation patterns. Shows advanced RETE features and rule chaining.
|
|
23
|
+
|
|
24
|
+
**Features:**
|
|
25
|
+
|
|
26
|
+
- Multi-condition pattern matching
|
|
27
|
+
- Variable binding and join tests
|
|
28
|
+
- Negation (NOT conditions)
|
|
29
|
+
- Rule priorities
|
|
30
|
+
|
|
31
|
+
## Stock Trading Systems
|
|
32
|
+
|
|
33
|
+
### Basic Trading Demo
|
|
34
|
+
**Files:** [`trading_demo.rb`](https://github.com/madbomber/kbs/blob/main/examples/trading_demo.rb) | [`trading_demo_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/trading_demo_dsl.rb)
|
|
35
|
+
|
|
36
|
+
Foundational trading signals including momentum detection and volume analysis.
|
|
37
|
+
|
|
38
|
+
**Features:**
|
|
39
|
+
|
|
40
|
+
- Buy/sell signal generation
|
|
41
|
+
- Volume-based alerts
|
|
42
|
+
- Price momentum tracking
|
|
43
|
+
|
|
44
|
+
### Advanced Stock Trading
|
|
45
|
+
**Files:** [`stock_trading_advanced.rb`](https://github.com/madbomber/kbs/blob/main/examples/stock_trading_advanced.rb) | [`stock_trading_advanced_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/stock_trading_advanced_dsl.rb)
|
|
46
|
+
|
|
47
|
+
Sophisticated trading system with technical indicators, portfolio management, and risk controls.
|
|
48
|
+
|
|
49
|
+
**Features:**
|
|
50
|
+
|
|
51
|
+
- Golden cross detection (MA crossover)
|
|
52
|
+
- Momentum breakout signals
|
|
53
|
+
- RSI indicators
|
|
54
|
+
- Volume ratio analysis
|
|
55
|
+
- Risk management rules
|
|
56
|
+
|
|
57
|
+
### CSV Trading System
|
|
58
|
+
**Files:** [`csv_trading_system.rb`](https://github.com/madbomber/kbs/blob/main/examples/csv_trading_system.rb) | [`csv_trading_system_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/csv_trading_system_dsl.rb)
|
|
59
|
+
|
|
60
|
+
Complete trading system that processes historical stock data from CSV files, calculates technical indicators, and generates trading signals.
|
|
61
|
+
|
|
62
|
+
**Features:**
|
|
63
|
+
|
|
64
|
+
- CSV data ingestion
|
|
65
|
+
- Moving average calculations
|
|
66
|
+
- Technical indicator generation
|
|
67
|
+
- Backtesting support
|
|
68
|
+
- Portfolio tracking
|
|
69
|
+
|
|
70
|
+
### Portfolio Rebalancing System
|
|
71
|
+
**Files:** [`portfolio_rebalancing_system.rb`](https://github.com/madbomber/kbs/blob/main/examples/portfolio_rebalancing_system.rb) | [`portfolio_rebalancing_system_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/portfolio_rebalancing_system_dsl.rb)
|
|
72
|
+
|
|
73
|
+
Sector-based portfolio management with automatic rebalancing, drift detection, and underperformer replacement.
|
|
74
|
+
|
|
75
|
+
**Features:**
|
|
76
|
+
|
|
77
|
+
- Target allocation management
|
|
78
|
+
- Sector drift detection
|
|
79
|
+
- Automatic rebalancing rules
|
|
80
|
+
- Underperformer identification
|
|
81
|
+
- Position replacement logic
|
|
82
|
+
|
|
83
|
+
### Timestamped Trading
|
|
84
|
+
**Files:** [`timestamped_trading.rb`](https://github.com/madbomber/kbs/blob/main/examples/timestamped_trading.rb) | [`timestamped_trading_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/timestamped_trading_dsl.rb)
|
|
85
|
+
|
|
86
|
+
Time-aware trading system demonstrating temporal reasoning and time-based rule activation.
|
|
87
|
+
|
|
88
|
+
**Features:**
|
|
89
|
+
|
|
90
|
+
- Time-based rule conditions
|
|
91
|
+
- Stale data detection
|
|
92
|
+
- Market hours awareness
|
|
93
|
+
- Temporal pattern matching
|
|
94
|
+
|
|
95
|
+
### Redis High-Frequency Trading
|
|
96
|
+
**Files:** [`redis_trading_demo.rb`](https://github.com/madbomber/kbs/blob/main/examples/redis_trading_demo.rb) | [`redis_trading_demo_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/redis_trading_demo_dsl.rb)
|
|
97
|
+
|
|
98
|
+
High-performance trading system using Redis for fast in-memory fact storage, ideal for low-latency trading applications.
|
|
99
|
+
|
|
100
|
+
**Features:**
|
|
101
|
+
|
|
102
|
+
- Redis-backed persistence
|
|
103
|
+
- High-frequency market data processing
|
|
104
|
+
- Fast fact lookup and updates
|
|
105
|
+
- Distributed knowledge base support
|
|
106
|
+
|
|
107
|
+
## Expert Systems
|
|
108
|
+
|
|
109
|
+
### Car Diagnostic System
|
|
110
|
+
**Files:** [`car_diagnostic.rb`](https://github.com/madbomber/kbs/blob/main/examples/car_diagnostic.rb) | [`car_diagnostic_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/car_diagnostic_dsl.rb)
|
|
111
|
+
|
|
112
|
+
Expert system for diagnosing car problems based on symptoms. Demonstrates classic expert system pattern with IF-THEN diagnostic rules.
|
|
113
|
+
|
|
114
|
+
**Features:**
|
|
115
|
+
|
|
116
|
+
- Symptom-based diagnosis
|
|
117
|
+
- Multiple diagnostic rules
|
|
118
|
+
- Recommendation generation
|
|
119
|
+
- Negation for ruling out conditions
|
|
120
|
+
|
|
121
|
+
### IoT Monitoring System
|
|
122
|
+
**File:** [`iot_demo_using_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/iot_demo_using_dsl.rb)
|
|
123
|
+
|
|
124
|
+
IoT sensor monitoring system with temperature alerts, inventory management, and customer VIP upgrades. Shows real-world DSL usage patterns.
|
|
125
|
+
|
|
126
|
+
**Features:**
|
|
127
|
+
|
|
128
|
+
- Sensor monitoring
|
|
129
|
+
- Temperature threshold alerts
|
|
130
|
+
- Inventory tracking
|
|
131
|
+
- Multi-domain rules (IoT, inventory, CRM)
|
|
132
|
+
|
|
133
|
+
## AI-Enhanced Systems
|
|
134
|
+
|
|
135
|
+
### AI-Enhanced Knowledge Base
|
|
136
|
+
**Files:** [`ai_enhanced_kbs.rb`](https://github.com/madbomber/kbs/blob/main/examples/ai_enhanced_kbs.rb) | [`ai_enhanced_kbs_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/ai_enhanced_kbs_dsl.rb)
|
|
137
|
+
|
|
138
|
+
Integration with Large Language Models (LLMs) for AI-powered sentiment analysis, market insights, and intelligent trading decisions.
|
|
139
|
+
|
|
140
|
+
**Features:**
|
|
141
|
+
|
|
142
|
+
- LLM integration via `ruby_llm` gem
|
|
143
|
+
- News sentiment analysis
|
|
144
|
+
- AI-powered market insights
|
|
145
|
+
- MCP agent support
|
|
146
|
+
- Hybrid AI + rule-based reasoning
|
|
147
|
+
|
|
148
|
+
**Requirements:**
|
|
149
|
+
|
|
150
|
+
- Ollama running locally (or compatible LLM provider)
|
|
151
|
+
- `ruby_llm` and `ruby_llm-mcp` gems
|
|
152
|
+
|
|
153
|
+
## Advanced Features
|
|
154
|
+
|
|
155
|
+
### Blackboard Memory System
|
|
156
|
+
**Files:** [`blackboard_demo.rb`](https://github.com/madbomber/kbs/blob/main/examples/blackboard_demo.rb) | [`blackboard_demo_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/blackboard_demo_dsl.rb)
|
|
157
|
+
|
|
158
|
+
Demonstrates persistent blackboard architecture with SQLite storage, message queues, audit logs, and fact history tracking.
|
|
159
|
+
|
|
160
|
+
**Features:**
|
|
161
|
+
+
|
|
162
|
+
- SQLite-backed persistence
|
|
163
|
+
- UUID-based fact tracking
|
|
164
|
+
- Message queue (priority-based)
|
|
165
|
+
- Complete audit trail
|
|
166
|
+
- Fact update history
|
|
167
|
+
- Database statistics
|
|
168
|
+
|
|
169
|
+
### Concurrent Inference Patterns
|
|
170
|
+
**Files:** [`concurrent_inference_demo.rb`](https://github.com/madbomber/kbs/blob/main/examples/concurrent_inference_demo.rb) | [`concurrent_inference_demo_dsl.rb`](https://github.com/madbomber/kbs/blob/main/examples/concurrent_inference_demo_dsl.rb)
|
|
171
|
+
|
|
172
|
+
Advanced patterns for multi-threaded knowledge bases including reactive engines, background inference, and event-driven architectures.
|
|
173
|
+
|
|
174
|
+
**Features:**
|
|
175
|
+
|
|
176
|
+
- Auto-inference mode (reactive)
|
|
177
|
+
- Background thread inference
|
|
178
|
+
- Event-driven processing
|
|
179
|
+
- Thread-safe fact addition
|
|
180
|
+
- Continuous reasoning loops
|
|
181
|
+
|
|
182
|
+
## Running Examples
|
|
183
|
+
|
|
184
|
+
### Run Individual Examples
|
|
185
|
+
|
|
186
|
+
Each example is executable from the command line:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
# Run a specific example
|
|
190
|
+
ruby examples/working_demo.rb
|
|
191
|
+
|
|
192
|
+
# Run DSL version
|
|
193
|
+
ruby examples/working_demo_dsl.rb
|
|
194
|
+
|
|
195
|
+
# Run AI-enhanced example (requires Ollama)
|
|
196
|
+
OLLAMA_MODEL=gpt-oss:latest ruby examples/ai_enhanced_kbs.rb
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Run All Examples
|
|
200
|
+
|
|
201
|
+
Run all examples at once:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
# Run all low-level API examples
|
|
205
|
+
ruby examples/run_all.rb
|
|
206
|
+
|
|
207
|
+
# Run all DSL examples
|
|
208
|
+
ruby examples/run_all_dsl.rb
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Example Organization
|
|
212
|
+
|
|
213
|
+
- **Low-level API examples** (`*.rb`) - Direct use of `KBS::Engine`, `KBS::Rule`, `KBS::Condition`
|
|
214
|
+
- **DSL examples** (`*_dsl.rb`) - Using `KBS.knowledge_base` and declarative syntax
|
|
215
|
+
- Both versions demonstrate the same functionality with different APIs
|
|
216
|
+
|
|
217
|
+
## Further Reading
|
|
218
|
+
|
|
219
|
+
- **[Quick Start Guide](../quick-start.md)** - Step-by-step tutorial
|
|
220
|
+
- **[DSL Reference](../guides/dsl.md)** - Complete DSL syntax guide
|
|
221
|
+
- **[Writing Rules](../guides/writing-rules.md)** - Best practices for rule design
|
|
222
|
+
- **[Blackboard Memory](../guides/blackboard-memory.md)** - Persistence guide
|
|
223
|
+
- **[API Reference](../api/index.md)** - Complete API documentation
|