Class: OMF::Rete::Store::AlphaStore

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

Overview

Class to store tuples for use in MoanaFilter

Direct Known Subclasses

NamedAlphaStore

Constant Summary

Constants included from OMF::Rete::Store

DEF_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OMF::Rete::Store

#add, #confirmLength, create, #on_query, #query, #remove, #subscribe, #unsubscribe

Constructor Details

#initialize(length, opts = {}) ⇒ AlphaStore

Initialize a tuple store for tuples of fixed length length.



18
19
20
21
22
23
24
25
# File 'lib/omf_rete/store/alpha_store.rb', line 18

def initialize(length, opts = {})
  store_initialize()
  @length = length
  @root = Alpha::AlphaInnerElement.new(0, length, self)
  @unregisterHandler = {}
  @index = []
  length.times do @index << {} end
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



13
14
15
# File 'lib/omf_rete/store/alpha_store.rb', line 13

def length
  @length
end

Instance Method Details

#addTuple(tarray) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/omf_rete/store/alpha_store.rb', line 58

def addTuple(tarray)
  @length.times do |i|
    item = tarray[i]
    ia = @index[i][item] ||= Set.new
    unless ia.add?(tarray)
      return  # this is a duplicate
    end
  end
  @root.addTuple(tarray)
end

#createTSet(description, indexPattern) ⇒ Object



52
53
54
55
56
# File 'lib/omf_rete/store/alpha_store.rb', line 52

def createTSet(description, indexPattern)
  tset = Moana::Filter::IndexedTupleSet.new(description, indexPattern)
  registerTSet(tset, description)
  tset
end

#find(pattern) ⇒ Object

Return a set of tuples which match pattern. Pattern is a tuples of the same length this store is configured for where any non-nil element is matched directly and any nil element is considered a wildcard.



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
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/omf_rete/store/alpha_store.rb', line 86

def find(pattern)
  #puts "patern: #{pattern.inspect}"
  seta = []
  allWildcards = true
  @length.times do |i|
    if (item = pattern[i])
      if (item != :_)
        allWildcards = false
        res = @index[i][item] || Set.new
        #puts "res: index #{i}, res: #{res.inspect}"
        seta << res
      end
    end
  end

  if (allWildcards)
    res = Set.new
    @index[0].each_value do |s|
      res.merge(s)
    end
    return res
  end
  # get intersection of all returned sets
  if (seta.empty?)
    return Set.new
  end
  res = nil
  seta.each do |s|
    if res
      res = res.intersection(s)
    else
      res = s
    end
    #puts "merge: in: #{s.inspect}, res: #{res.inspect}"
  end
  return res
end

#onUnregisterTSet(tset, &block) ⇒ Object

Register a block to call whenever a TSet is being unregistered



130
131
132
# File 'lib/omf_rete/store/alpha_store.rb', line 130

def onUnregisterTSet(tset, &block)
  (@unregisterHandler[tset] ||= []) << block
end

#registerTSet(tset, pattern) ⇒ Object

Register a TSet and add all tuples currently and in the future matching pattern



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/omf_rete/store/alpha_store.rb', line 31

def registerTSet(tset, pattern)
  #puts "registerTSet: #{pattern}"
  pat = pattern.collect do |el|
    (el.is_a?(Symbol) && el.to_s.end_with?('?')) ? nil : el
  end
  @root.registerTSet(tset, pat)
  # seed tset which already stored data
  find(pat).each do |t|
    tset.addTuple(t)
  end
  tset
end

#removeTuple(tarray) ⇒ Object

Remove a tuple from the store



71
72
73
74
75
76
77
78
79
# File 'lib/omf_rete/store/alpha_store.rb', line 71

def removeTuple(tarray)
  @length.times do |i|
    item = tarray[i]
    if ia = @index[i][item]
      ia.delete(tarray)
    end
  end
  @root.removeTuple(tarray)
end

#to_sObject



124
125
126
# File 'lib/omf_rete/store/alpha_store.rb', line 124

def to_s()
  "Store"
end

#unregisterTSet(tset) ⇒ Object



44
45
46
47
48
49
# File 'lib/omf_rete/store/alpha_store.rb', line 44

def unregisterTSet(tset)
  (@unregisterHandler[tset] || []).each do |proc|
    proc.call
  end
  @unregisterHandler.delete(tset)
end