Class: KBS::Blackboard::Memory
- Inherits:
-
Object
- Object
- KBS::Blackboard::Memory
- Defined in:
- lib/kbs/blackboard/memory.rb
Overview
The Blackboard Memory - central workspace for facts and coordination
Instance Attribute Summary collapse
-
#audit_log ⇒ Object
readonly
Returns the value of attribute audit_log.
-
#message_queue ⇒ Object
readonly
Returns the value of attribute message_queue.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#store ⇒ Object
(also: #db)
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#add_fact(type, attributes = {}) ⇒ Object
Fact Management.
-
#add_observer(observer) ⇒ Object
Observer pattern.
-
#clear_session ⇒ Object
Session management.
- #close ⇒ Object
- #consume_message(topic, consumer) ⇒ Object
-
#facts ⇒ Object
Alias for compatibility with WorkingMemory interface.
- #get_facts(type = nil, pattern = {}) ⇒ Object
- #get_history(fact_uuid = nil, limit: 100) ⇒ Object
- #get_rule_firings(rule_name = nil, limit: 100) ⇒ Object
-
#initialize(db_path: ':memory:', store: nil) ⇒ Memory
constructor
A new instance of Memory.
-
#log_rule_firing(rule_name, fact_uuids, bindings = {}) ⇒ Object
Audit Log delegation.
- #notify_observers(action, fact) ⇒ Object
- #peek_messages(topic, limit: 10) ⇒ Object
-
#post_message(sender, topic, content, priority: 0) ⇒ Object
Message Queue delegation.
- #query_facts(sql_conditions = nil, params = []) ⇒ Object
-
#register_knowledge_source(name, description: nil, topics: []) ⇒ Object
Knowledge Source registry.
- #remove_fact(fact) ⇒ Object
-
#stats ⇒ Object
Statistics.
- #transaction(&block) ⇒ Object
- #update_fact(fact, new_attributes) ⇒ Object
-
#vacuum ⇒ Object
Maintenance.
Constructor Details
#initialize(db_path: ':memory:', store: nil) ⇒ Memory
Returns a new instance of Memory.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kbs/blackboard/memory.rb', line 16 def initialize(db_path: ':memory:', store: nil) @session_id = SecureRandom.uuid @observers = [] # Use provided store or create default SqliteStore @store = store || Persistence::SqliteStore.new( db_path: db_path, session_id: @session_id ) # Initialize composed components based on store type setup_components end |
Instance Attribute Details
#audit_log ⇒ Object (readonly)
Returns the value of attribute audit_log.
14 15 16 |
# File 'lib/kbs/blackboard/memory.rb', line 14 def audit_log @audit_log end |
#message_queue ⇒ Object (readonly)
Returns the value of attribute message_queue.
14 15 16 |
# File 'lib/kbs/blackboard/memory.rb', line 14 def @message_queue end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
14 15 16 |
# File 'lib/kbs/blackboard/memory.rb', line 14 def session_id @session_id end |
#store ⇒ Object (readonly) Also known as: db
Returns the value of attribute store.
14 15 16 |
# File 'lib/kbs/blackboard/memory.rb', line 14 def store @store end |
Instance Method Details
#add_fact(type, attributes = {}) ⇒ Object
Fact Management
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kbs/blackboard/memory.rb', line 57 def add_fact(type, attributes = {}) uuid = SecureRandom.uuid @store.transaction do @store.add_fact(uuid, type, attributes) @audit_log.log_fact_change(uuid, type, attributes, 'ADD') end fact = Fact.new(uuid, type, attributes, self) notify_observers(:add, fact) fact end |
#add_observer(observer) ⇒ Object
Observer pattern
152 153 154 |
# File 'lib/kbs/blackboard/memory.rb', line 152 def add_observer(observer) @observers << observer end |
#clear_session ⇒ Object
Session management
161 162 163 |
# File 'lib/kbs/blackboard/memory.rb', line 161 def clear_session @store.clear_session(@session_id) end |
#close ⇒ Object
183 184 185 |
# File 'lib/kbs/blackboard/memory.rb', line 183 def close @store.close end |
#consume_message(topic, consumer) ⇒ Object
123 124 125 126 127 |
# File 'lib/kbs/blackboard/memory.rb', line 123 def (topic, consumer) @store.transaction do @message_queue.consume(topic, consumer) end end |
#facts ⇒ Object
Alias for compatibility with WorkingMemory interface
106 107 108 |
# File 'lib/kbs/blackboard/memory.rb', line 106 def facts get_facts end |
#get_facts(type = nil, pattern = {}) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/kbs/blackboard/memory.rb', line 97 def get_facts(type = nil, pattern = {}) fact_data = @store.get_facts(type, pattern) fact_data.map do |data| Fact.new(data[:uuid], data[:type], data[:attributes], self) end end |
#get_history(fact_uuid = nil, limit: 100) ⇒ Object
143 144 145 |
# File 'lib/kbs/blackboard/memory.rb', line 143 def get_history(fact_uuid = nil, limit: 100) @audit_log.fact_history(fact_uuid, limit: limit) end |
#get_rule_firings(rule_name = nil, limit: 100) ⇒ Object
147 148 149 |
# File 'lib/kbs/blackboard/memory.rb', line 147 def get_rule_firings(rule_name = nil, limit: 100) @audit_log.rule_firings(rule_name, limit: limit) end |
#log_rule_firing(rule_name, fact_uuids, bindings = {}) ⇒ Object
Audit Log delegation
139 140 141 |
# File 'lib/kbs/blackboard/memory.rb', line 139 def log_rule_firing(rule_name, fact_uuids, bindings = {}) @audit_log.log_rule_firing(rule_name, fact_uuids, bindings) end |
#notify_observers(action, fact) ⇒ Object
156 157 158 |
# File 'lib/kbs/blackboard/memory.rb', line 156 def notify_observers(action, fact) @observers.each { |obs| obs.update(action, fact) } end |
#peek_messages(topic, limit: 10) ⇒ Object
129 130 131 |
# File 'lib/kbs/blackboard/memory.rb', line 129 def (topic, limit: 10) @message_queue.peek(topic, limit: limit) end |
#post_message(sender, topic, content, priority: 0) ⇒ Object
Message Queue delegation
119 120 121 |
# File 'lib/kbs/blackboard/memory.rb', line 119 def (sender, topic, content, priority: 0) @message_queue.post(sender, topic, content, priority: priority) end |
#query_facts(sql_conditions = nil, params = []) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/kbs/blackboard/memory.rb', line 110 def query_facts(sql_conditions = nil, params = []) fact_data = @store.query_facts(sql_conditions, params) fact_data.map do |data| Fact.new(data[:uuid], data[:type], data[:attributes], self) end end |
#register_knowledge_source(name, description: nil, topics: []) ⇒ Object
Knowledge Source registry
134 135 136 |
# File 'lib/kbs/blackboard/memory.rb', line 134 def register_knowledge_source(name, description: nil, topics: []) @store.register_knowledge_source(name, description: description, topics: topics) end |
#remove_fact(fact) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kbs/blackboard/memory.rb', line 70 def remove_fact(fact) uuid = fact.is_a?(Fact) ? fact.uuid : fact @store.transaction do result = @store.remove_fact(uuid) if result @audit_log.log_fact_change(uuid, result[:type], result[:attributes], 'REMOVE') fact_obj = Fact.new(uuid, result[:type], result[:attributes], self) notify_observers(:remove, fact_obj) end end end |
#stats ⇒ Object
Statistics
170 171 172 173 174 175 176 |
# File 'lib/kbs/blackboard/memory.rb', line 170 def stats store_stats = @store.stats = @message_queue.stats audit_stats = @audit_log.stats store_stats.merge().merge(audit_stats) end |
#transaction(&block) ⇒ Object
165 166 167 |
# File 'lib/kbs/blackboard/memory.rb', line 165 def transaction(&block) @store.transaction(&block) end |
#update_fact(fact, new_attributes) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kbs/blackboard/memory.rb', line 85 def update_fact(fact, new_attributes) uuid = fact.is_a?(Fact) ? fact.uuid : fact @store.transaction do fact_type = @store.update_fact(uuid, new_attributes) if fact_type @audit_log.log_fact_change(uuid, fact_type, new_attributes, 'UPDATE') end end end |
#vacuum ⇒ Object
Maintenance
179 180 181 |
# File 'lib/kbs/blackboard/memory.rb', line 179 def vacuum @store.vacuum end |