Class: KBS::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, conditions: [], action: nil, priority: 0) {|_self| ... } ⇒ Rule

Returns a new instance of Rule.

Yields:

  • (_self)

Yield Parameters:

  • _self (KBS::Rule)

    the object that the method was called on



8
9
10
11
12
13
14
15
16
# File 'lib/kbs/rule.rb', line 8

def initialize(name, conditions: [], action: nil, priority: 0, &block)
  @name = name
  @conditions = conditions
  @action = action
  @priority = priority
  @fired_count = 0

  yield self if block_given?
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



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

def action
  @action
end

#conditionsObject

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/kbs/rule.rb', line 5

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority.



5
6
7
# File 'lib/kbs/rule.rb', line 5

def priority
  @priority
end

Instance Method Details

#fire(facts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kbs/rule.rb', line 18

def fire(facts)
  @fired_count += 1
  return unless @action

  bindings = extract_bindings(facts)

  # Support both 1-parameter and 2-parameter actions
  if @action.arity == 1 || @action.arity == -1
    @action.call(facts)
  else
    @action.call(facts, bindings)
  end
end