Class: Ruleby::Core::MatchResult

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

Overview

This class represents a partial match. It contains the variables, values, and some metadata about the match. For the most part, this metadata is used during conflict resolution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = Hash.new, is_match = false, fact_hash = {}, recency = []) ⇒ MatchResult

Returns a new instance of MatchResult.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/utils.rb', line 56

def initialize(variables=Hash.new,is_match=false,fact_hash={},recency=[])
  @variables = variables      

  # a list of recencies of the facts that this matchresult depends on.
  @recency = recency

  # notes where this match result is from a NotPattern or ObjectPattern
  # TODO this isn't really needed anymore.  how can we get rid of it?
  @is_match = is_match 

  # a hash of fact.ids that each tag corresponds to
  @fact_hash = fact_hash 
end

Instance Attribute Details

#fact_hashObject

Returns the value of attribute fact_hash.



53
54
55
# File 'lib/core/utils.rb', line 53

def fact_hash
  @fact_hash
end

#is_matchObject

Returns the value of attribute is_match.



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

def is_match
  @is_match
end

#recencyObject

Returns the value of attribute recency.



54
55
56
# File 'lib/core/utils.rb', line 54

def recency
  @recency
end

#variablesObject

TODO this class needs to be cleaned up so that we don’t have a bunch of properties. Instead, maybe it sould have a list of facts.



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

def variables
  @variables
end

Instance Method Details

#==(match) ⇒ Object



82
83
84
# File 'lib/core/utils.rb', line 82

def ==(match)           
  return match != nil && @variables == match.variables && @is_match == match.is_match && @fact_hash == match.fact_hash
end

#[](sym) ⇒ Object



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

def [](sym)
   return @variables[sym]
end

#[]=(sym, object) ⇒ Object



70
71
72
# File 'lib/core/utils.rb', line 70

def []=(sym, object)
  @variables[sym] = object
end

#clearObject



124
125
126
127
128
# File 'lib/core/utils.rb', line 124

def clear
  @variables = {}
  @fact_hash = {}
  @recency = []
end

#delete(tag) ⇒ Object



130
131
132
133
# File 'lib/core/utils.rb', line 130

def delete(tag)
  @variables.delete(tag)
  @fact_hash.delete(tag)
end

#dupObject



102
103
104
105
106
107
108
109
# File 'lib/core/utils.rb', line 102

def dup     
  dup_mr = MatchResult.new
  dup_mr.recency = @recency.clone
  dup_mr.is_match = @is_match
  dup_mr.variables = @variables.clone
  dup_mr.fact_hash = @fact_hash.clone       
  return dup_mr
end

#fact_idsObject



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

def fact_ids
  return fact_hash.values.uniq
end

#key?(m) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/core/utils.rb', line 86

def key?(m)
  return @variables.key?(m)
end

#keysObject



90
91
92
# File 'lib/core/utils.rb', line 90

def keys
  return @variables.keys
end

#merge(mr) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/core/utils.rb', line 115

def merge(mr)
  new_mr = MatchResult.new
  new_mr.recency = @recency | mr.recency
  new_mr.is_match = mr.is_match
  new_mr.variables = @variables.merge mr.variables 
  new_mr.fact_hash = @fact_hash.merge mr.fact_hash    
  return new_mr
end

#merge!(mr) ⇒ Object



111
112
113
# File 'lib/core/utils.rb', line 111

def merge!(mr)
  return update(mr)
end

#to_sObject



135
136
137
138
139
140
141
142
143
# File 'lib/core/utils.rb', line 135

def to_s
  s = '#MatchResult('
  s = s + 'f)(' unless @is_match
  s = s + object_id.to_s+')('
  @variables.each do |key,value|
    s += "#{key}=#{value}/#{@fact_hash[key]}, "
  end
  return s + ")"
end

#update(mr) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/core/utils.rb', line 94

def update(mr)
  @recency = @recency | mr.recency
  @is_match = mr.is_match
  @variables = @variables.update mr.variables      
  @fact_hash = @fact_hash.update mr.fact_hash      
  return self
end