Class: Wongi::Engine::StatementGenerator
- 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
- #execute(token) ⇒ Object
-
#initialize(template) ⇒ StatementGenerator
constructor
A new instance of StatementGenerator.
Methods included from CoreExt
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 50 51 52 |
# 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 # link to rete here to ensure proper linking with token wme = WME.new subject, predicate, object, rete production.tracer.trace( action: self, wme: wme ) if production.tracer if existing = rete.exists?( wme ) generated = existing..size if generated > 0 && ! token.generated_wmes.include?( existing ) token.generated_wmes << existing existing. << token end else token.generated_wmes << wme wme. << token # this MUST be done after we link the wme and the token # in order for neg rule invalidation to work rete << wme end end |