Commits

Brett Giles committed 10784fe

Code refactor for rc1 plus fix jar_path in connection

Refactored list decoding out.

  • Participants
  • Parent commits 8fd9176
  • Tags v0.9.0-rc1-GUI

Comments (0)

Files changed (11)

File src/communications/connection.rb

     res = _make_connection
 
     jar_path= File.expand_path(__FILE__)[Regexp.new /.*?jar!/]
-    jar_path=jar_path[5,jar_path.length - 18] #remove 'file:' from front, lqpl_gui.jar! from back
-    puts jar_path
+    if jar_path
+      jar_path=jar_path[5,jar_path.length - 18] #remove 'file:' from front, lqpl_gui.jar! from back
+    else
+      jar_path = "./"
+    end
+
     if !res
       begin
         @process=ProcessBuilder.new(@connect_to, "").start

File 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
   end
 
   def self.result_values_to_list(rvals,stack_trans)
-    return [] if !rvals or "" == rvals
-    ret = []
-    rv = TRIPLE_PATTERN.match(rvals)
-    return ret if !rv
-    matched_len = rv[0].length
-    ret << [stack_trans.reverse_lookup(rv[1]),rv[2],rv[3]]
-    while rv
-      rv = TRIPLE_PATTERN.match(rvals[matched_len, rvals.length])
-      return ret if !rv
-      matched_len += rv[0].length
+    values_to_list rvals, TRIPLE_PATTERN, do |ret, rv|
       ret << [stack_trans.reverse_lookup(rv[1]),rv[2],rv[3]]
     end
   end

File src/manifest.rb

 require "dialogs/simulate_results/simulate_results_controller"
 require "dialogs/about/about_controller"
 require "exit_handler"
+require "utility/xml_decode"
 
 

File src/panels/classical_stack/classical_stack_model.rb

 require 'exceptions/quantum_stack_model_invalid_create'
 
 class ClassicalStackModel
+  include XmlDecode
   attr_accessor :classical_stack_text
   attr_accessor :classical_stack
 
   def classical_stack=(xml_input)
     cstack_in = CSTACK_PATTERN.match xml_input
     raise QuantumStackModelInvalidCreate, "Invalid Classical Stack: #{xml_input}" if !cstack_in
-    @classical_stack = ClassicalStackModel.classical_values_to_list(cstack_in[1])
+    @classical_stack = ClassicalStackModel::classical_values_to_list(cstack_in[1])
   end
 
   def self.classical_values_to_list(cvals)
-    return [] if !cvals or "" == cvals
-    ret = []
-    cv = CLASSICALVALUES_PATTERN.match(cvals)
-    return ret if !cv
-    matched_len = cv[0].length
-    ret << cv[2].to_i if cv[2]
-    ret << (cv[4] == "True" or cv[4] == "true") if cv[4]
-    while cv
-      cv = CLASSICALVALUES_PATTERN.match(cvals[matched_len, cvals.length])
-      return ret if !cv
-      matched_len += cv[0].length
+    values_to_list cvals, CLASSICALVALUES_PATTERN, do | ret, cv|
       ret << cv[2].to_i if cv[2]
       ret << (cv[4] == "True" or cv[4] == "true") if cv[4]
     end

File src/panels/dump/dump_model.rb

 require "panels/dump/dump_split_model"
 
 class DumpModel
+  include XmlDecode
   attr_accessor :dump
   def dump=(in_xml)
 
   end
 
   def self.dump_values_to_list(dumpvals)
-    return [] if !dumpvals or "" == dumpvals
-    ret = []
-    dv = DUMPVALUES_PATTERN.match(dumpvals)
-    return ret if !dv
-    matched_len = dv[0].length
-    ret << DumpCallModel.new(dv[1]) if dv[1]
-    ret << DumpSplitModel.new(dv[2]) if dv[2]
-    while dv
-      dv = DUMPVALUES_PATTERN.match(dumpvals[matched_len, dumpvals.length])
-      return ret if !dv
-      matched_len += dv[0].length
-    ret << DumpCallModel.new(dv[1]) if dv[1]
-    ret << DumpSplitModel.new(dv[2]) if dv[2]
+    values_to_list dumpvals, DUMPVALUES_PATTERN, do |ret, dv|
+      ret << DumpCallModel.new(dv[1]) if dv[1]
+      ret << DumpSplitModel.new(dv[2]) if dv[2]
     end
   end
   DUMPVALUES_PATTERN = Regexp.new /(<DumpCall>.*?<\/DumpCall>)|(<DumpSplit>.*?<\/DumpSplit>)/

File src/panels/executable_code/executable_code_model.rb

 require 'panels/executable_code/code_pointer'
 
 class ExecutableCodeModel
+  include XmlDecode
   attr_accessor :the_code
   attr_accessor :the_code_pointer
   attr_accessor :the_code_was_updated
   end
 
   def self.instructions_to_list(instructions)
-    return [] if !instructions or instructions == ""
-    ret = []
-    ins = INSTRUCTIONS_PATTERN.match instructions
-    return ret if !ins
     count = 0
-    ret << sprintf("%3d  %s",count,ins[1])
-    matched_len = ins[0].length
-    while ins
-      ins = INSTRUCTIONS_PATTERN.match(instructions[matched_len,instructions.length])
-      return ret if !ins
+    values_to_list instructions, INSTRUCTIONS_PATTERN, do |ret, ins|
+      ret << sprintf("%3d  %s",count,ins[1])
       count += 1
-      ret <<  sprintf("%3d  %s",count,ins[1])
-      matched_len += ins[0].length
     end
   end
 

File src/panels/quantum_stack/descriptor/abstract_descriptor_model.rb

 
 
 class AbstractDescriptorModel
+  include XmlDecode
   attr_accessor :value
   attr_accessor :name
 

File src/panels/quantum_stack/descriptor/classical_descriptor_model.rb

 class ClassicalDescriptorModel< AbstractDescriptorModel
-
   PATTERN=Regexp.new /^<ClassicalStack>(((<cint>(-?\d+)<\/cint>)|(<cbool>(True|False)<\/cbool>))*)<\/ClassicalStack>$/
 
   LIST_PATTERN = Regexp.new /^(<cint>(-?\d+)<\/cint>)|(<cbool>(True|False)<\/cbool>)/
   end
 
   def self.parse_list(sub_string)
-    md = LIST_PATTERN.match(sub_string)
-    raise StackDescriptorModelInvalidCreate, sub_string if !md and sub_string and sub_string.length > 0
-    ret = []
-    return ret if !md
-    len = md[0].length
-    while md
+    r = values_to_list sub_string, LIST_PATTERN, do |ret, md|
       ret << md[2].to_i if md[2]
       ret << (md[4] == 'True') if md[4]
-      md = LIST_PATTERN.match(sub_string[len, sub_string.length])
-      return ret if !md
-      len += md[0].length
     end
+    raise StackDescriptorModelInvalidCreate, sub_string if sub_string and sub_string.length > 0 and r.length == 0
+    r
   end
 
   def length

File src/panels/quantum_stack/descriptor/data_descriptor_model.rb

 
 
   def self.parse_address_list(addresses_string)
-    return [] if addresses_string == ""
-    md = LIST_PATTERN.match addresses_string
-    raise InvalidInput, addresses_string if !md
-    rval=[md[2].to_i]
-    num_found = 1
-    while md
-      md = LIST_PATTERN.match(addresses_string[md[1].length*num_found,addresses_string.length])
-      return rval if !md
+    r = values_to_list addresses_string, LIST_PATTERN, do |rval, md|
       elem = md[2].to_i
       raise InvalidInput, "StackAddress '#{elem}' duplicated for single constructor" if rval.include? elem
       rval << elem
-      num_found += 1
     end
-    rval
+    raise InvalidInput, addresses_string if r.length == 0 and addresses_string and addresses_string.length > 0
+    r
   end
 end

File src/panels/quantum_stack/descriptor/qubit_descriptor_model.rb

 
   def self.parse_list(qubit_string)
     raise InvalidInput, "Must have qubit indicators" if !qubit_string or qubit_string.length == 0
-    md = LIST_PATTERN.match qubit_string
-    raise InvalidInput, qubit_string if !md
-    rval=[[self.translate_qubit(md[2]), self.translate_qubit(md[5])]]
-    [1,2,3].each do |num_found|
-      md = LIST_PATTERN.match(qubit_string[md[1].length*num_found,qubit_string.length])
-      return rval if !md
+    r = values_to_list qubit_string, LIST_PATTERN, do |rval,md|
       elem = [self.translate_qubit(md[2]), self.translate_qubit(md[5])]
       raise InvalidInput, "#{elem} duplicated in qubit" if rval.include? elem
       rval << elem
     end
-    rval
+
+    raise InvalidInput, qubit_string if r.length > 4
+
+    raise InvalidInput, qubit_string if r.length == 0 and qubit_string and qubit_string.length > 0
+    r
   end
 
   def self.translate_qubit(single_qubit)

File src/utility/xml_decode.rb

+module XmlDecode
+  module ClassMethods
+    def values_to_list(input,pattern)
+      return [] if !input or "" == input
+      return_list = []
+      match_vals = pattern.match input
+      return return_list if ! match_vals
+      matched_len = match_vals[0].length
+      yield return_list,match_vals
+      while match_vals
+        match_vals = pattern.match input[matched_len, input.length]
+        return return_list if ! match_vals
+        matched_len += match_vals[0].length
+        yield return_list,match_vals
+      end
+    end
+  end
+
+  def self.included(klass)
+   klass.extend(ClassMethods)
+ end
+
+end