Class: OMF::Rete::Store::ObjectStore

Inherits:
Object
  • Object
show all
Includes:
OMF::Rete::Store
Defined in:
lib/omf_rete/store/object_store.rb

Overview

Turns a standard object into a store.

NOTE: This store is NOT tracking changes to the state of the object. It is assumed to stay constant while it is in this store.

Constant Summary

Constants included from OMF::Rete::Store

DEF_TYPE

Instance Method Summary collapse

Methods included from OMF::Rete::Store

#add, #addTuple, create, #createTSet, #on_query, #query, #remove, #removeTuple, #subscribe, #unsubscribe

Constructor Details

#initialize(opts = {}) ⇒ ObjectStore

Returns a new instance of ObjectStore.

Parameters:

  • opts (defaults to: {})

    :include_object Make the fist element the object



25
26
27
28
29
30
# File 'lib/omf_rete/store/object_store.rb', line 25

def initialize(opts = {})
  store_initialize()
  @include_object = opts[:name] || (opts[:include_object] == true ? self : nil)
  @object = nil
  @tsets = {}
end

Instance Method Details

#confirmLength(tuple) ⇒ Object



116
117
118
# File 'lib/omf_rete/store/object_store.rb', line 116

def confirmLength(tuple)
  tuple.is_a?(Array) && tuple.length == (@include_object ? 3 : 2)
end

#find(pattern) ⇒ Object

Return a set of tuples which match pattern. Pattern is a tuples where the first element (include_object == false) or the second element (include_object == true) is a property of this object. If the next element is nil, return the value. if the next one is not nil, only return a tuple if it is set to the same value.

If the property value is an Enumerable, return a separate tuple for every value returned by the enumerable.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/omf_rete/store/object_store.rb', line 75

def find(pattern)
  res = Set.new
  if @include_object
    raise WrongPatternLengthException.new(3, pattern) unless pattern.length == 3
    obj = pattern[0]
    return res if obj && obj != @include_object # not for us
  else
    raise WrongPatternLengthException.new(2, pattern) unless pattern.length == 2
  end
  unless pred = @include_object ? pattern[1] : pattern[0]
    raise OMF::Rete::Store::UnknownPredicateException.new(pred, pattern)
  end
  pred = pred.to_sym
  return res unless @object.respond_to? pred

  val = @object.send(pred)
  if exp_value = @include_object ? pattern[2] : pattern[1]
    # Only return tuple if identical
    a = [pred, exp_value]
    a.insert(0, @include_object) if @include_object
    # need to check if same
    if (val.is_a?(Enumerable) ? val.include?(exp_value) : val == exp_value)
      res << a
    end
    return res
  end

  a = [pred]
  a.insert(0, @include_object) if @include_object
  if val.is_a?(Enumerable)
    res = Set.new(val.map {|v| a.dup << v})
  else
    res << (a << val)
  end
  return res
end

#registerTSet(tset, pattern) ⇒ Object

Register a TSet and add all the object’s state matching the pattern



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/omf_rete/store/object_store.rb', line 52

def registerTSet(tset, pattern)
  pat = pattern.collect do |el|
    (el.is_a?(Symbol) && el.to_s.end_with?('?')) ? nil : el
  end
  @tsets[tset] = pat

  # seed tset which already stored data
  find(pat).each do |t|
    tset.addTuple(t)
  end
  tset
end

#representObject(obj) ⇒ Object

Make this store represent ‘obj’. this will ‘eject’ the currently represented object (if exist)



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/omf_rete/store/object_store.rb', line 35

def representObject(obj)
  @object = obj
  # First clear registered tsets and then seed with state from 'obj'
  @tsets.each do |tset, pat|
    tset.clear()
  end
  @tsets.each do |tset, pat|
    find(pat).each do |t|
      tset.addTuple(t)
    end
  end
  obj
end

#to_sObject



112
113
114
# File 'lib/omf_rete/store/object_store.rb', line 112

def to_s()
  "ObjectStore"
end