Class: KBS::DSL::KnowledgeBase
- Inherits:
-
Object
- Object
- KBS::DSL::KnowledgeBase
- Defined in:
- lib/kbs/dsl/knowledge_base.rb
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #assert(type, attributes = {}) ⇒ Object
- #defrule(name, &block) ⇒ Object
- #fact(type, attributes = {}) ⇒ Object
- #facts ⇒ Object
-
#initialize ⇒ KnowledgeBase
constructor
A new instance of KnowledgeBase.
- #print_facts ⇒ Object
- #print_rule_source(name) ⇒ Object
- #print_rules ⇒ Object
- #query(type, pattern = {}) ⇒ Object
- #reset ⇒ Object
- #retract(fact) ⇒ Object
- #rule(name, &block) ⇒ Object
- #rule_source(name) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ KnowledgeBase
Returns a new instance of KnowledgeBase.
8 9 10 11 12 13 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 8 def initialize @engine = Engine.new @rules = {} @rule_builders = {} @rule_source_locations = {} end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
6 7 8 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 6 def engine @engine end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
6 7 8 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 6 def rules @rules end |
Instance Method Details
#assert(type, attributes = {}) ⇒ Object
34 35 36 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 34 def assert(type, attributes = {}) fact(type, attributes) end |
#defrule(name, &block) ⇒ Object
26 27 28 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 26 def defrule(name, &block) rule(name, &block) end |
#fact(type, attributes = {}) ⇒ Object
30 31 32 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 30 def fact(type, attributes = {}) @engine.add_fact(type, attributes) end |
#facts ⇒ Object
50 51 52 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 50 def facts @engine.working_memory.facts end |
#print_facts ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 61 def print_facts puts "Working Memory Contents:" puts "-" * 40 facts.each_with_index do |fact, i| puts "#{i + 1}. #{fact}" end puts "-" * 40 end |
#print_rule_source(name) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 84 def print_rule_source(name) source = rule_source(name) unless source puts "No source available for rule '#{name}'" return end puts source end |
#print_rules ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 94 def print_rules puts "Knowledge Base Rules:" puts "-" * 40 @rule_builders.each do |name, builder| puts "Rule: #{name}" puts " Description: #{builder.description}" if builder.description puts " Priority: #{builder.priority}" puts " Conditions: #{builder.conditions.size}" builder.conditions.each_with_index do |cond, i| negated = cond.negated ? "NOT " : "" puts " #{i + 1}. #{negated}#{cond.type}(#{cond.pattern})" end puts "" end puts "-" * 40 end |
#query(type, pattern = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 54 def query(type, pattern = {}) @engine.working_memory.facts.select do |fact| next false unless fact.type == type pattern.all? { |key, value| fact.attributes[key] == value } end end |
#reset ⇒ Object
46 47 48 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 46 def reset @engine.reset end |
#retract(fact) ⇒ Object
38 39 40 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 38 def retract(fact) @engine.remove_fact(fact) end |
#rule(name, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 15 def rule(name, &block) @rule_source_locations[name] = block.source_location if block builder = RuleBuilder.new(name) builder.instance_eval(&block) if block_given? @rule_builders[name] = builder rule = builder.build @rules[name] = rule @engine.add_rule(rule) builder end |
#rule_source(name) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 70 def rule_source(name) # Try file-based source first if (location = @rule_source_locations[name]) file, line = location if file && File.exist?(file) source = extract_rule_source(file, line) return source if source end end # Fall back to reconstruction from internal state reconstruct_rule_source(name) end |
#run ⇒ Object
42 43 44 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 42 def run @engine.run end |