Class: KBS::DSL::KnowledgeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/kbs/dsl/knowledge_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKnowledgeBase

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

#engineObject (readonly)

Returns the value of attribute engine.



6
7
8
# File 'lib/kbs/dsl/knowledge_base.rb', line 6

def engine
  @engine
end

#rulesObject (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

#factsObject



48
49
50
# File 'lib/kbs/dsl/knowledge_base.rb', line 48

def facts
  @engine.working_memory.facts
end


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


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

#resetObject



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

#runObject



40
41
42
# File 'lib/kbs/dsl/knowledge_base.rb', line 40

def run
  @engine.run
end