Commits

Brett Giles  committed 2a8d09a

Forgot to split out parser for SimulateResults

  • Participants
  • Parent commits 4927efe

Comments (0)

Files changed (5)

File GUI/spec/model/simulate_results_model_spec.rb

     it "should throw an error if given invalid input" do
       expect {
         @m.simulate_results = "junk"
-      }.to raise_error  ModelCreateError, /junk/
+      }.to raise_error  ParserError, /junk/
     end
-    it "should set the text or the random value to 'Random Value: rv" do
+    it "should set the text for the random value to 'Random Value: rv" do
       @m.simulate_results =ONEELT
       @m.random_value_text.should == "Random Value: 0.27"
     end
     end
   end
 
-  describe "class method result_values_to_list" do
-    before(:each) do
-      @st = double("StackTranslation", :nil? => false)
-      @st.stub(:reverse_lookup) do |val|
-        case val
-        when "1" then "@p"
-        when "2" then "@q"
-        else val
-        end
-      end
-    end
-    it "should make an empty list with no data" do
-      SimulateResultsModel.result_values_to_list("",@st).should == []
-    end
-    it "should make an empty list with nil data" do
-      SimulateResultsModel.result_values_to_list(nil,@st).should == []
-    end
-    it "should make a singleton list when there is one triple" do
-      SimulateResultsModel.result_values_to_list(make_triple("1","Int",37),@st).should == [["@p","Int","37"]]
-    end
-    it "should make a list of all items" do
-      SimulateResultsModel.result_values_to_list(make_triple("1","Int",37)+make_triple("2","Bool","True")+
-        make_triple("3","qubit",1),@st).should == [["@p","Int","37"],["@q","Bool","True"],["3","qubit","1"]]
-    end
-  end
+ 
 end

File GUI/spec/parsers/simulate_results_parser_spec.rb

+require 'spec/spec_helper'
+require 'spec/specdata/simulate_results_data'
+
+describe SimulateResultsParser do
+  it "should set the text for the random value to 'Random Value: rv" do
+    srp = SimulateResultsParser.new ONEELT
+    srp.random_value.should == "0.27"
+  end
+  it "should set the simulate results to a triple of the stackaddress, type and value for single element" do
+    srp = SimulateResultsParser.new ONEELT
+    srp.simulate_results.should == [["1","Coin","Heads"]]
+  end
+  it "should set the simulate results to two triples of the stackaddress, type and value for two elements" do
+    srp = SimulateResultsParser.new TWOELTS
+    srp.simulate_results.should == [["1","Coin","Heads"],["2","qubit","0"]]
+  end
+end

File GUI/src/dialogs/parsers/simulate_results_parser.rb

+class SimulateResultsParser < AbstractListPatternParser
+  
+  
+  def random_value
+    @md[:random_value]
+  end
+  
+  def simulate_results
+    @parsed_list
+  end
+  
+  def parse_list
+    SimulateResultsParser::values_to_list @md[:triples], SimulateResultsParser::triple_pattern  do |ret, rv|
+      ret << [rv[:stackaddress],rv[:type],rv[:value]]
+    end
+  end
+  
+  def self.embeddable_top_level_regexp
+    Regexp.new "<Simulated><double>(?<random_value>.*)</double><results>(?<triples>(" +
+        SimulateResultsParser::triple_pattern.source +
+        ")*)</results></Simulated>"
+  end
+  
+  def self.triple_pattern
+    Regexp.new "<triple><string>(?<stackaddress>.*?)</string><string>(?<type>.*?)</string><string>(?<value>.*?)</string></triple>"
+  end
+  
+end

File GUI/src/dialogs/simulate_results/simulate_results_model.rb

 
 class SimulateResultsModel
-  include XmlDecode
   attr_accessor :simulate_results
   attr_accessor :simulate_results_text
   attr_accessor :random_value_text
   java_signature "void simulate_results(Object)"
   def simulate_results=(xml_data)
     raise ModelCreateError, "Missing Stack Translation" if @stack_translation.nil?
-    sr = SIMULATE_PATTERN.match xml_data
-
-    raise ModelCreateError, "Invalid Simulate Results: #{xml_data}" if !sr
-    @random_value_text = "Random Value: "+sr[1]
-    @simulate_results = SimulateResultsModel.result_values_to_list(sr[2],@stack_translation)
-  end
-
-  def self.result_values_to_list(rvals,stack_trans)
-    values_to_list rvals, TRIPLE_PATTERN  do |ret, rv|
-      ret << [stack_trans.reverse_lookup(rv[1]),rv[2],rv[3]]
-    end
+    sr = SimulateResultsParser.new xml_data
+    @random_value_text = "Random Value: "+sr.random_value
+    @simulate_results = sr.simulate_results.collect {|srs| [@stack_translation.reverse_lookup(srs[0]),srs[1],srs[2]]}
   end
-  SIMULATE_PATTERN = Regexp.new /<Simulated><double>(.*)<\/double><results>((<triple><string>.*?<\/string><string>.*?<\/string><string>.*?<\/string><\/triple>)*)<\/results><\/Simulated>/
 
-  TRIPLE_PATTERN = Regexp.new /<triple><string>(.*?)<\/string><string>(.*?)<\/string><string>(.*?)<\/string><\/triple>/
 end

File GUI/src/manifest.rb

 end
 
 
-%w{translate_line_ends xml_decode drawing duck_matcher}.each do |f|
+%w{translate_line_ends drawing duck_matcher}.each do |f|
   require "utility/"+f
 end
 
   require "panels/parsers/"+rf+"_parser"
 end
 
+require 'dialogs/parsers/simulate_results_parser'
 require 'painting/canvas_size'
 require 'application_model'