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
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

#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



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

#factsObject



50
51
52
# File 'lib/kbs/dsl/knowledge_base.rb', line 50

def facts
  @engine.working_memory.facts
end


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


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


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

#resetObject



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

#runObject



42
43
44
# File 'lib/kbs/dsl/knowledge_base.rb', line 42

def run
  @engine.run
end