Class: Wongi::Engine::DataOverlay

Inherits:
Object
  • Object
show all
Defined in:
lib/wongi-engine/data_overlay.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rete, parent = nil) ⇒ DataOverlay

Returns a new instance of DataOverlay.



7
8
9
10
11
# File 'lib/wongi-engine/data_overlay.rb', line 7

def initialize(rete, parent = nil)
  @rete = rete
  @parent = parent
  rete.add_overlay(self)
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/wongi-engine/data_overlay.rb', line 5

def parent
  @parent
end

#reteObject (readonly)

Returns the value of attribute rete.



4
5
6
# File 'lib/wongi-engine/data_overlay.rb', line 4

def rete
  @rete
end

Instance Method Details

#<<(thing) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/wongi-engine/data_overlay.rb', line 40

def <<(thing)
  case thing
  when Array
    assert WME.new(*thing)
  when WME
    assert(thing)
  else
    raise Error, "overlays can only accept data"
  end
end

#add_token(token, beta) ⇒ Object



121
122
123
124
# File 'lib/wongi-engine/data_overlay.rb', line 121

def add_token(token, beta)
  tokens = raw_tokens(beta)
  tokens << token unless tokens.include?(token)
end

#add_wme(wme, alpha) ⇒ Object



112
113
114
115
# File 'lib/wongi-engine/data_overlay.rb', line 112

def add_wme(wme, alpha)
  wmes = raw_wmes(alpha)
  wmes << wme unless wmes.include?(wme)
end

#ancestor?(other) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/wongi-engine/data_overlay.rb', line 29

def ancestor?(other)
  return false if parent.nil?
  return true if parent == other
  parent.ancestor?(other)
end

#assert(wme) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/wongi-engine/data_overlay.rb', line 51

def assert wme
  @next_cascade ||= []
  @next_cascade << [:assert, wme]
  if @current_cascade.nil?
    @current_cascade = @next_cascade
    @next_cascade = nil
    process_cascade
  end
end

#disposeObject



35
36
37
38
# File 'lib/wongi-engine/data_overlay.rb', line 35

def dispose
  return if self == rete.default_overlay
  rete.remove_overlay(self)
end

#highest(other) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/wongi-engine/data_overlay.rb', line 91

def highest(other)
  return self if self == other
  return self if other.nil?
  return self if ancestor?(other)
  return other if other.ancestor?(self)
  nil # unrelated lineages
end

#new_childObject



13
14
15
# File 'lib/wongi-engine/data_overlay.rb', line 13

def new_child
  DataOverlay.new(rete, self)
end

#process_cascadeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wongi-engine/data_overlay.rb', line 74

def process_cascade
  while @current_cascade
    @current_cascade.each do |(operation, wme, options)|
      case operation
      when :assert
        wme.overlay = self
        rete.real_assert(wme)
      when :retract
        rete.real_retract(wme, options)
        wme.overlay = nil
      end
    end
    @current_cascade = @next_cascade
    @next_cascade = nil
  end
end

#raw_tokens(beta) ⇒ Object



135
136
137
138
# File 'lib/wongi-engine/data_overlay.rb', line 135

def raw_tokens(beta)
  @raw_tokens ||= Hash.new { |h, k| h[k] = [] }
  @raw_tokens[beta.object_id]
end

#raw_wmes(alpha) ⇒ Object



130
131
132
133
# File 'lib/wongi-engine/data_overlay.rb', line 130

def raw_wmes(alpha)
  @raw_wmes ||= Hash.new { |h, k| h[k] = [] }
  @raw_wmes[alpha.object_id]
end

#remove_token(token, beta) ⇒ Object



126
127
128
# File 'lib/wongi-engine/data_overlay.rb', line 126

def remove_token(token, beta)
  raw_tokens(beta).delete(token)
end

#remove_wme(wme, alpha) ⇒ Object



117
118
119
# File 'lib/wongi-engine/data_overlay.rb', line 117

def remove_wme(wme, alpha)
  raw_wmes(alpha).delete(wme)
end

#retract(wme, options = { }) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wongi-engine/data_overlay.rb', line 61

def retract wme, options = { }
  if wme.is_a? Array
    wme = WME.new(*wme)
  end
  @next_cascade ||= []
  @next_cascade << [:retract, wme, options]
  if @current_cascade.nil?
    @current_cascade = @next_cascade
    @next_cascade = nil
    process_cascade
  end
end

#tokens(beta) ⇒ Object



108
109
110
# File 'lib/wongi-engine/data_overlay.rb', line 108

def tokens(beta)
  DeleteSafeEnumerator.new(raw_tokens(beta))
end

#with_childObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wongi-engine/data_overlay.rb', line 17

def with_child
  return unless block_given?
  new_child.tap do |overlay|
    begin
      result = yield overlay
    ensure
      overlay.dispose
    end
    result
  end
end

#wmes(alpha) ⇒ Object

TODO: this is inconsistent. A WME retracted in-flight will be visible in active enumerators but a token will not. But this is how it works.



104
105
106
# File 'lib/wongi-engine/data_overlay.rb', line 104

def wmes(alpha)
  DuplicatingEnumerator.new(raw_wmes(alpha))
end