Commits

Brett Giles  committed 29d14d7

Just doing a flay and flog to reduce code smell - will continue after FEST is done

  • Participants
  • Parent commits e98dc10

Comments (0)

Files changed (13)

   gem "rspec"
   gem "cucumber"
   gem 'simplecov'
+  gem 'flog'
+  gem 'flay'
 end
 

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

   end
 
   def self.result_values_to_list(rvals,stack_trans)
-    values_to_list rvals, TRIPLE_PATTERN, do |ret, rv|
+    values_to_list rvals, TRIPLE_PATTERN  do |ret, rv|
       ret << [stack_trans.reverse_lookup(rv[1]),rv[2],rv[3]]
     end
   end

File GUI/src/main.rb

 
 # Set up global error handling so that it is consistantly logged or outputed
 # You will probably want to replace the puts with your application's logger
-def show_error_dialog_and_exit(exception, thread=nil)
-  puts "Error in application"
-  puts "#{exception.class} - #{exception}"
+def log_the_error(exception, thread=nil)
   rexcep = exception.exception
   if rexcep.class == ServerProcessNotFound
-    t = "Server(s) not found"
-    m = "LQPL requires the compiler server and emulator server to be installed on your path.
+    show_error_dialog("Server(s) not found",
+         "LQPL requires the compiler server and emulator server to be installed on your path.
          Please download or compile these and add them to your path, e.g., in /usr/local/bin.
-         See further details at http://pll.cpsc.ucalgary.ca/lqpl"
-    javax.swing.JOptionPane.show_message_dialog(nil, m, t, javax.swing.JOptionPane::DEFAULT_OPTION)
-    java.lang.System.exit(0)
+         See further details at http://pll.cpsc.ucalgary.ca/lqpl");
   end
   if exception.kind_of? Exception
-    puts exception.backtrace.join("\n")
     File.open("lqplEmulatorError.log", "w") do |f|
       f.puts exception.backtrace.join("\n")
     end
     exception.printStackTrace(java.io.PrintStream.new(output_stream))
     puts output_stream.to_string
   end
+  # add other error handling code goes here
+  show_error_dialog("Application Error","The application has encountered an error and must shut down.")
+  System.exit(0)
+end
 
-  # Your error handling code goes here
-
-  # Show error dialog informing the user that there was an error
-  title = "Application Error"
-  message = "The application has encountered an error and must shut down."
-
-  javax.swing.JOptionPane.show_message_dialog(nil, message, title, javax.swing.JOptionPane::DEFAULT_OPTION)
-  java.lang.System.exit(0)
+def show_error_dialog_and_exit(title, message)
+  JOptionPane.show_message_dialog(nil, message, title, JOptionPane::DEFAULT_OPTION)
+  System.exit(0)
 end
-GlobalErrorHandler.on_error {|exception, thread| show_error_dialog_and_exit(exception, thread) }
+
+GlobalErrorHandler.on_error {|exception, thread| log_the_error(exception, thread) }
 
 begin
   LqplController.instance.open

File GUI/src/manifest.rb

 
 require "exit_handler"
 
+java_import javax.swing.JOptionPane
+java_import java.lang.System
 
 

File GUI/src/panels/classical_stack/classical_stack_model.rb

   end
 
   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 = check_and_return_value(PATTERN,in_string,ClassicalStackModel::classical_values_to_list)
   end
 
   def self.classical_values_to_list(cvals)
-    values_to_list cvals, CLASSICALVALUES_PATTERN, do | ret, cv|
+    values_to_list cvals[1], 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 GUI/src/panels/dump/dump_model.rb

 class DumpModel
   include XmlDecode
   attr_accessor :dump
+  
   def dump=(in_xml)
-
-    m = DUMP_PATTERN.match in_xml
-    raise QuantumStackModelInvalidCreate, "Invalid dump: #{in_xml}" if !m
-    @dump = DumpModel.dump_values_to_list m[1]
+    @dump = check_and_return_value(DUMP_PATTERN,in_xml,DumpModel.dump_values_to_list m[1]
   end
 
   def text=(whatev)
   end
 
   def self.dump_values_to_list(dumpvals)
-    values_to_list dumpvals, DUMPVALUES_PATTERN, do |ret, dv|
+    values_to_list dumpvals[1], DUMPVALUES_PATTERN  do |ret, dv|
       ret << DumpCallModel.new(dv[1]) if dv[1]
       ret << DumpSplitModel.new(dv[2]) if dv[2]
     end

File GUI/src/panels/executable_code/executable_code_model.rb

 
   def self.instructions_to_list(instructions)
     count = 0
-    values_to_list instructions, INSTRUCTIONS_PATTERN, do |ret, ins|
+    values_to_list instructions, INSTRUCTIONS_PATTERN  do |ret, ins|
       ret << sprintf("%3d  %s",count,ins[1])
       count += 1
     end

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

     raise StackDescriptorModelInvalidCreate
   end
 
+  def check_and_return_value(pattern,in_string,data_parser)
+    matched = pattern.match in_string
+    raise StackDescriptorModelInvalidCreate, "Invalid input for #{self.class}: #{in_string}" if ! matched
+    data_parser matched[1]
+  end
+  
   def length
     return @value.length
   end

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

   LIST_PATTERN = Regexp.new /^(<cint>(-?\d+)<\/cint>)|(<cbool>(True|False)<\/cbool>)/
 
   def initialize(in_string)
-    matc = PATTERN.match in_string
-    raise StackDescriptorModelInvalidCreate, in_string if ! matc
-    @value = ClassicalDescriptorModel::parse_list matc[1]
+    @value = check_and_return_value(PATTERN,in_string,ClassicalDescriptorModel::parse_list)
   end
 
   def self.parse_list(sub_string)
-    r = values_to_list sub_string, LIST_PATTERN, do |ret, 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]
     end

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

   # match 2
 
   def initialize(in_string)
-    matc = PATTERN.match in_string
-    raise StackDescriptorModelInvalidCreate, in_string if ! matc
-    @value = DataDescriptorModel::parse_pairs matc[1]
+    @value = check_and_return_value(PATTERN,in_string,DataDescriptorModel::parse_pairs)
   end
 
 
 
 
   def self.parse_address_list(addresses_string)
-    r = values_to_list addresses_string, LIST_PATTERN, do |rval, 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

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

   # match 2 and 5
 
   def initialize(in_string)
-    matc = PATTERN.match in_string
-    raise StackDescriptorModelInvalidCreate, in_string if !matc
-    @value = QubitDescriptorModel::parse_list matc[1]
+    @value = check_and_return_value(PATTERN,in_string,QubitDescriptorModel::parse_list)
   end
 
   def length
 
   def self.parse_list(qubit_string)
     raise InvalidInput, "Must have qubit indicators" if !qubit_string or qubit_string.length == 0
-    r = values_to_list qubit_string, LIST_PATTERN, do |rval,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

File GUI/src/panels/quantum_stack/quantum_stack_painter.rb

   attr :model_element
 
   def initialize(model_element)
-    @model_element = model_element
-
-    @descriptor_painter = DescriptorPainterFactory.make_painter(model_element.descriptor)
-    @sstack_painters = @model_element.substacks.collect {|s| QuantumStackPainter.new(s)}
+    self.model_element=model_element
   end
 
   def model_element=(model)
   # end of painter interface
 
   def model_paint_size(g)
+    #todo - refactor to make "size" a class
     return @preferred_size if @preferred_size
     if model_element.bottom?
       dim= get_string_size(g,"...")

File GUI/src/utility/drawing.rb

   end
 
   def draw_text_centered_between(g,text, point1, point2)
-      point = Point.new((point1.x + point2.x) / 2, (point1.y + point2.y) / 2)
-      draw_centered_text(g,text,point)
+      draw_centered_text(g,text,mid_point(point1, point2))
+  end
+  
+  def mid_point(point1, point2)
+    Point.new((point1.x + point2.x) / 2, (point1.y + point2.y) / 2)
   end
 
   def draw_sized_text(g,size,text, point1, point2,reference)
-    point = Point.new((point1.x + point2.x) / 2, (point1.y + point2.y) / 2)
+    point = mid_point(point1, point2)
     atext = AttributedString.new(text)
     atext.add_attribute(TextAttribute::SIZE, size, 0, text.length)
     case reference
     when :left then draw_text_to_left_of_point(g,atext.iterator,Point.new(point.x,point.y))
     when :right then draw_text_to_right_of_point(g,atext.iterator,point)
-    else draw_text_centered_between(g,atext.iterator,point1,point2)
+    else draw_text_centered_at_point(g,atext.iterator,point)
     end
   end