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_rules ⇒ Object
- #query(type, pattern = {}) ⇒ Object
- #reset ⇒ Object
- #retract(fact) ⇒ Object
- #rule(name, &block) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ KnowledgeBase
Returns a new instance of KnowledgeBase.
8 9 10 11 12 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 8 def initialize @engine = Engine.new @rules = {} @rule_builders = {} 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
32 33 34 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 32 def assert(type, attributes = {}) fact(type, attributes) end |
#defrule(name, &block) ⇒ Object
24 25 26 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 24 def defrule(name, &block) rule(name, &block) end |
#fact(type, attributes = {}) ⇒ Object
28 29 30 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 28 def fact(type, attributes = {}) @engine.add_fact(type, attributes) end |
#facts ⇒ Object
48 49 50 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 48 def facts @engine.working_memory.facts end |
#print_facts ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 59 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_rules ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 68 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
52 53 54 55 56 57 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 52 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
44 45 46 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 44 def reset @engine.working_memory.facts.clear end |
#retract(fact) ⇒ Object
36 37 38 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 36 def retract(fact) @engine.remove_fact(fact) end |
#rule(name, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 14 def rule(name, &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 |
#run ⇒ Object
40 41 42 |
# File 'lib/kbs/dsl/knowledge_base.rb', line 40 def run @engine.run end |