Class: KBS::Blackboard::Fact

Inherits:
Object
  • Object
show all
Defined in:
lib/kbs/blackboard/fact.rb

Overview

A fact in the blackboard with persistence capabilities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, type, attributes, blackboard = nil) ⇒ Fact

Returns a new instance of Fact.



9
10
11
12
13
14
# File 'lib/kbs/blackboard/fact.rb', line 9

def initialize(uuid, type, attributes, blackboard = nil)
  @uuid = uuid
  @type = type
  @attributes = attributes
  @blackboard = blackboard
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/kbs/blackboard/fact.rb', line 7

def attributes
  @attributes
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/kbs/blackboard/fact.rb', line 7

def type
  @type
end

#uuidObject (readonly)

Returns the value of attribute uuid.



7
8
9
# File 'lib/kbs/blackboard/fact.rb', line 7

def uuid
  @uuid
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
# File 'lib/kbs/blackboard/fact.rb', line 16

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



20
21
22
23
# File 'lib/kbs/blackboard/fact.rb', line 20

def []=(key, value)
  @attributes[key] = value
  @blackboard.update_fact(self, @attributes) if @blackboard
end

#matches?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kbs/blackboard/fact.rb', line 34

def matches?(pattern)
  return false if pattern[:type] && pattern[:type] != @type

  pattern.each do |key, value|
    next if key == :type

    if value.is_a?(Proc)
      return false unless @attributes[key] && value.call(@attributes[key])
    elsif value.is_a?(Symbol) && value.to_s.end_with?('?')
      next
    else
      return false unless @attributes[key] == value
    end
  end

  true
end

#retractObject



30
31
32
# File 'lib/kbs/blackboard/fact.rb', line 30

def retract
  @blackboard.remove_fact(self) if @blackboard
end

#to_hObject



56
57
58
59
60
61
62
# File 'lib/kbs/blackboard/fact.rb', line 56

def to_h
  {
    uuid: @uuid,
    type: @type,
    attributes: @attributes
  }
end

#to_sObject



52
53
54
# File 'lib/kbs/blackboard/fact.rb', line 52

def to_s
  "#{@type}(#{@uuid[0..7]}...: #{@attributes.map { |k, v| "#{k}=#{v}" }.join(', ')})"
end

#update(new_attributes) ⇒ Object



25
26
27
28
# File 'lib/kbs/blackboard/fact.rb', line 25

def update(new_attributes)
  @attributes.merge!(new_attributes)
  @blackboard.update_fact(self, @attributes) if @blackboard
end