Skip to content

Instantly share code, notes, and snippets.

@abargnesi
Last active September 12, 2015 13:00
Show Gist options
  • Select an option

  • Save abargnesi/5ca65987b955e44ae3de to your computer and use it in GitHub Desktop.

Select an option

Save abargnesi/5ca65987b955e44ae3de to your computer and use it in GitHub Desktop.
Assign called for each reference in gen.
#!/usr/bin/env ruby
require 'wongi-engine'
require 'uuid'
engine = Wongi::Engine.create
uuid = UUID.generate.to_sym
engine << [ uuid, :value, nil ]
engine << [ uuid, :type, "Date" ]
uuid = UUID.generate.to_sym
engine << [ uuid, :value, nil ]
engine << [ uuid, :type, "Date" ]
puts 'Facts:'
engine.wmes.map { |fact|
puts fact
}
puts "\n"
engine.rule 'date rule' do
for_all {
has :Datum, :value, nil
has :Datum, :type, "Date"
assign :OutValue do |token|
puts 'assign OutValue'
'2015-09-12'
end
assign :NewDatum do |token|
puts 'assign NewDatum'
UUID.generate.to_sym
end
}
do! {
gen :NewDatum, :value, :OutValue
gen :NewDatum, :name, "Date"
}
end
puts 'All Facts:'
puts engine.select(:_, :_, :_)
#!/usr/bin/env ruby
require 'wongi-engine'
require 'uuid'
engine = Wongi::Engine.create
uuid = UUID.generate.to_sym
engine << [ uuid, :value, nil ]
engine << [ uuid, :type, "Date" ]
uuid = UUID.generate.to_sym
engine << [ uuid, :value, nil ]
engine << [ uuid, :type, "Date" ]
puts 'Facts:'
engine.wmes.map { |fact|
puts fact
}
puts "\n"
value_hash = {}
engine.rule 'date rule' do
for_all {
has :Datum, :value, nil
has :Datum, :type, "Date"
assign :OutValue do |token|
puts 'assign OutValue'
'2015-09-12'
end
assign :NewDatum do |token|
# We save the mapping of Datum to NewDatum so we use a single
# value in the 'gen' calls below.
value_hash[token[:Datum]] ||= UUID.generate.to_sym
end
}
do! {
gen :NewDatum, :value, :OutValue
gen :NewDatum, :name, "Date"
}
end
puts 'All Facts:'
puts engine.select(:_, :_, :_)
@abargnesi
Copy link
Copy Markdown
Author

Output:

Facts:
{:"4aef04c0-3b79-0133-3bad-082e5f8a66ae" :value nil}
{:"4aef04c0-3b79-0133-3bad-082e5f8a66ae" :type "Date"}
{:"4aef0a70-3b79-0133-3bad-082e5f8a66ae" :value nil}
{:"4aef0a70-3b79-0133-3bad-082e5f8a66ae" :type "Date"}

assign NewDatum
assign OutValue
assign NewDatum
assign NewDatum
assign OutValue
assign NewDatum
All Facts:
{:"4aef04c0-3b79-0133-3bad-082e5f8a66ae" :value nil}
{:"4aef04c0-3b79-0133-3bad-082e5f8a66ae" :type "Date"}
{:"4aef0a70-3b79-0133-3bad-082e5f8a66ae" :value nil}
{:"4aef0a70-3b79-0133-3bad-082e5f8a66ae" :type "Date"}
{:"4aef2990-3b79-0133-3bad-082e5f8a66ae" :value "2015-09-12"}
{:"4aef2de0-3b79-0133-3bad-082e5f8a66ae" :name "Date"}
{:"4aef3250-3b79-0133-3bad-082e5f8a66ae" :value "2015-09-12"}
{:"4aef3610-3b79-0133-3bad-082e5f8a66ae" :name "Date"}

@abargnesi
Copy link
Copy Markdown
Author

The NewDatum assign is called four times because it is referenced twice and matches against 2 sets of facts. I would like NewDatum to be the same value for each set of generated facts.

@abargnesi
Copy link
Copy Markdown
Author

The data_machine_2.rb example is successful because a new UUID is mapped to the token[:Datum].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment