Skip to content

Instantly share code, notes, and snippets.

@abargnesi
Last active September 11, 2015 17:35
Show Gist options
  • Select an option

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

Select an option

Save abargnesi/afd1bb7122ca6cd957c8 to your computer and use it in GitHub Desktop.
Datum conversion, XML to JSON, using wongi-engine rule system
#!/usr/bin/env ruby
require 'wongi-engine'
require 'uuid'
engine = Wongi::Engine.create
uuid = UUID.generate.to_sym
engine << [ uuid, :semantic_type, :person ]
engine << [ uuid, :media_type, :'application/xml' ]
engine << [ uuid, :value, '<person id="1"><name>Anthony Bargnesi</name></person>' ]
uuid = UUID.generate.to_sym
engine << [ uuid, :semantic_type, :person ]
engine << [ uuid, :media_type, :'application/xml' ]
engine << [ uuid, :value, '<person id="1"><name>Anthony Bargnesi</name></person>' ]
apply_cmd = open('|components/xml_to_js/apply', 'r+')
engine.rule 'xml_to_json' do
for_all {
has :Datum, :semantic_type, :SemanticType
has :Datum, :media_type, :'application/xml'
has :Datum, :value, :Value
assign :OutValue do |token|
apply_cmd.puts(token[:Value])
apply_cmd.gets
end
}
do! {
uuid = UUID.generate.to_sym
puts "generated json for #{uuid}"
gen uuid, :semantic_type, :SemanticType
gen uuid, :media_type, :'application/json'
gen uuid, :value, :OutValue
}
end
engine.query 'value' do
search_on :Media_type
forall {
has :Datum, :media_type, :Media_type
has :Datum, :_, :_
}
end
engine.execute 'value', { Media_type: :'application/json' }
engine.results['value'].tokens.each do |token|
puts token.wme
end
apply_cmd.close
# Output:
#=> generated json for 59054250-3acd-0133-3b55-082e5f8a66ae
#=> {:"59054250-3acd-0133-3b55-082e5f8a66ae" emantic_type erson}
#=> {:"59054250-3acd-0133-3b55-082e5f8a66ae" :media_type :"application/json"}
#=> {:"59054250-3acd-0133-3b55-082e5f8a66ae" :value "{\"person\":{\"name\":\"Anthony Bargnesi\",\"id\":1}}\n"}
#!/usr/bin/env ruby
require 'wongi-engine'
require 'uuid'
engine = Wongi::Engine.create
engine << [ 'A', :semantic_type, :personA ]
engine << [ 'A', :media_type, :'application/xml' ]
engine << [ 'A', :value, '<person id="1"><name>Anthony Bargnesi</name></person>' ]
engine << [ 'B', :semantic_type, :personB ]
engine << [ 'B', :media_type, :'application/xml' ]
engine << [ 'B', :value, '<person id="1"><name>Anthony Bargnesi</name></person>' ]
apply_cmd = open('|components/xml_to_js/apply', 'r+')
engine.rule 'xml_to_json' do
for_all {
has :Datum, :semantic_type, :SemanticType
has :Datum, :media_type, :'application/xml'
has :Datum, :value, :Value
assign :OutValue do |token|
apply_cmd.puts(token[:Value])
apply_cmd.gets.chomp
end
assign :NewDatum do |token|
UUID.generate
end
}
make {
# NewDatum is different for each matched fact?
gen :NewDatum, :semantic_type, :SemanticType
gen :NewDatum, :media_type, :'application/json'
gen :NewDatum, :value, :OutValue
}
end
engine.query 'value' do
search_on :Media_type
forall {
has :Datum, :media_type, :Media_type
has :Datum, :_, :_
}
end
puts engine.select(:_, :_, :_)
puts "-----"
engine.execute 'value', { Media_type: :'application/json' }
engine.results['value'].tokens.each do |token|
puts token.wme
end
apply_cmd.close
#!/usr/bin/env ruby
require 'wongi-engine'
require 'uuid'
engine = Wongi::Engine.create
id = UUID.generate
engine << [ id, :semantic_type, :person ]
engine << [ id, :media_type, :'application/xml' ]
engine << [ id, :value, '<person id="1"><name>Anthony Bargnesi</name></person>' ]
id = UUID.generate
engine << [ id, :semantic_type, :person ]
engine << [ id, :media_type, :'application/xml' ]
engine << [ id, :value, '<person id="2"><name>Anthony Bargnesi</name></person>' ]
puts "Facts:"
engine.wmes.map { |fact|
puts fact
}
puts "\n"
apply_cmd = open('|components/xml_to_js/apply', 'r+')
engine.rule 'xml_to_json' do
for_all {
has :Datum, :semantic_type, :SemanticType
has :Datum, :media_type, :'application/xml'
has :Datum, :value, :Value
}
make {
action { |token|
# Manually set token assignments for each match of fact set.
# This works (but kludgy) whereas using 'assign' clause within 'for_all' did not.
# The effect there was only one call to make would occur for each matched set.
apply_cmd.puts(token[:Value])
id = UUID.generate
new_value = apply_cmd.gets.chomp
token.assignments[:OutValue] = Proc.new { new_value }
token.assignments[:NewDatum] = Proc.new { id }
}
gen :NewDatum, :semantic_type, :SemanticType
gen :NewDatum, :media_type, :'application/json'
gen :NewDatum, :value, :OutValue
}
end
engine.query 'value' do
search_on :Media_type
forall {
has :Datum, :media_type, :Media_type
has :Datum, :_, :_
}
end
engine.execute 'value', { Media_type: :'application/json' }
puts %Q{Results of 'value' query for [ :Datum :media_type :application/json ]}
engine.results['value'].tokens.each do |token|
puts token.wme
end
apply_cmd.close
@abargnesi
Copy link
Copy Markdown
Author

The outputted results for the value query does not have two UUID subjects as I was expecting. The rule xml_to_json only processes one set of facts in the do! block.

@abargnesi
Copy link
Copy Markdown
Author

The second example makes the new subject an assign variable construct. A NewDatum variable is created for each matched fact whereas I thought I would get one NewDatum variable for the full set of matched facts.

@abargnesi
Copy link
Copy Markdown
Author

The third example seems to work thought its kludgy.

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