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.



54
55
56
57
58
59
60
61
# File 'lib/core/engine.rb', line 54

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.



51
52
53
# File 'lib/core/engine.rb', line 51

def action
  @action
end

#counterObject

Returns the value of attribute counter.



52
53
54
# File 'lib/core/engine.rb', line 52

def counter
  @counter
end

#matchObject (readonly)

Returns the value of attribute match.



51
52
53
# File 'lib/core/engine.rb', line 51

def match
  @match
end

#usedObject

Returns the value of attribute used.



52
53
54
# File 'lib/core/engine.rb', line 52

def used
  @used
end

Instance Method Details

#<=>(a2) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/core/engine.rb', line 68

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



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

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

#fire(engine = nil) ⇒ Object



63
64
65
66
# File 'lib/core/engine.rb', line 63

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

#modify(match) ⇒ Object



83
84
85
86
# File 'lib/core/engine.rb', line 83

def modify(match)
  @match = match
  # should we update recency, too? 
end

#to_sObject



88
89
90
# File 'lib/core/engine.rb', line 88

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