Class: Wongi::Engine::StatementGenerator

Inherits:
Action
  • Object
show all
Defined in:
lib/wongi-engine/dsl/actions/statement_generator.rb

Instance Attribute Summary

Attributes inherited from Action

#name, #production, #rete, #rule

Instance Method Summary collapse

Methods included from CoreExt

included

Constructor Details

#initialize(template) ⇒ StatementGenerator

Returns a new instance of StatementGenerator.



4
5
6
# File 'lib/wongi-engine/dsl/actions/statement_generator.rb', line 4

def initialize template
  @template = template
end

Instance Method Details

#execute(token) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wongi-engine/dsl/actions/statement_generator.rb', line 8

def execute token

  subject = if Template.variable?( @template.subject )
    v = token[ @template.subject ]
    raise "Unbound variable #{@template.subject} in token #{token}" if v.nil?
    v
  else
    @template.subject
  end

  predicate = if Template.variable?( @template.predicate )
    v = token[ @template.predicate ]
    raise "Unbound variable #{@template.predicate} in token #{token}" if v.nil?
    v
  else
    @template.predicate
  end

  object = if Template.variable?( @template.object )
    v = token[ @template.object ]
    raise "Unbound variable #{@template.object} in token #{token}" if v.nil?
    v
  else
    @template.object
  end

  wme = WME.new subject, predicate, object

  production.tracer.trace( action: self, wme: wme ) if production.tracer
  if existing = rete.exists?( wme )
    generated = existing.generating_tokens.size
    if generated > 0 && ! token.generated_wmes.include?( existing )
      token.generated_wmes << existing
      existing.generating_tokens << token
    end
  else
    added = rete << wme
    token.generated_wmes << added
    added.generating_tokens << token
  end

end