Class: Ruleby::Core::Activation

Inherits:
Object
  • Object
show all
Defined in:
lib/core/engine.rb

Overview

An activation is an action/match pair that is executed if a rule is matched. It also contains metadata that can be used for conflict resolution if two rules are satisfied by the same fact.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, match, counter = 0) ⇒ Activation

Returns a new instance of Activation.



49
50
51
52
53
54
55
56
# File 'lib/core/engine.rb', line 49

def initialize(action, match, counter=0)
  @action = action
  @match = match
  @match.recency.sort!
  @match.recency.reverse!
  @counter = counter
  @used = false
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



46
47
48
# File 'lib/core/engine.rb', line 46

def action
  @action
end

#counterObject

Returns the value of attribute counter.



47
48
49
# File 'lib/core/engine.rb', line 47

def counter
  @counter
end

#matchObject (readonly)

Returns the value of attribute match.



46
47
48
# File 'lib/core/engine.rb', line 46

def match
  @match
end

#usedObject

Returns the value of attribute used.



47
48
49
# File 'lib/core/engine.rb', line 47

def used
  @used
end

Instance Method Details

#<=>(a2) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/core/engine.rb', line 63

def <=>(a2)          
  return @counter <=> a2.counter if @counter != a2.counter
  return @action.priority <=> a2.action.priority if @action.priority != a2.action.priority           

  # NOTE in order for this to work, the array must be reverse sorted
  i = 0; while @match.recency[i] == a2.match.recency[i] && i < @match.recency.size-1 && i < a2.match.recency.size-1 
    i += 1
  end
  return @match.recency[i] <=> a2.match.recency[i]
end

#==(a2) ⇒ Object



74
75
76
# File 'lib/core/engine.rb', line 74

def ==(a2)
  return a2 != nil && @action == a2.action && @match == a2.match
end

#fireObject



58
59
60
61
# File 'lib/core/engine.rb', line 58

def fire()
  @used = true
  @action.fire @match
end

#to_sObject



78
79
80
# File 'lib/core/engine.rb', line 78

def to_s
  return "[#{@action.name}-#{object_id}|#{@counter}|#{@action.priority}|#{@match.recency.join(',')}|#{@match.to_s}] "
end