Last active
September 11, 2015 17:35
-
-
Save abargnesi/afd1bb7122ca6cd957c8 to your computer and use it in GitHub Desktop.
Datum conversion, XML to JSON, using wongi-engine rule system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
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.
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
The outputted results for the
valuequery does not have two UUID subjects as I was expecting. The rulexml_to_jsononly processes one set of facts in thedo!block.