Commits

Brett Giles committed 05e6f8d

Flog average for lqpl_controller to 8.9, worst is now 18.9 for class method, 18.0 for instance.

Comments (0)

Files changed (16)

GUI/src/application_controller.rb

   # Add content here that you want to be available to all the controllers
   # in your application
 
-  attr_accessor :lqpl_emulator_server_connection
+  attr_reader :lqpl_emulator_server_connection
   @@controllers={}
 
   java_signature "void lqpl_emulator_server_connection(Object)"
-  def lqpl_emulator_server_connection=(sc)
-    @lqpl_emulator_server_connection = sc
-    @lqpl_emulator_server_connection.connect if !@lqpl_emulator_server_connection.connected?
-  end
+  # def lqpl_emulator_server_connection=(sc)
+  #    @lqpl_emulator_server_connection = sc
+  #    @lqpl_emulator_server_connection.connect if !@lqpl_emulator_server_connection.connected?
+  #  end
 
   # doing a suspenders and belt here - will assume default SC if there isn't one.
   java_signature "Object lqpl_emulator_server_connection()"
   def lqpl_emulator_server_connection
     @lqpl_emulator_server_connection = LqplEmulatorServerConnection.instance if !@lqpl_emulator_server_connection
+    @lqpl_emulator_server_connection.connect if !@lqpl_emulator_server_connection.connected?
     @lqpl_emulator_server_connection
   end
 

GUI/src/communications/compiler_server_connection.rb

     @qpo_code
   end
 
+  def compile_and_write_qpo(fname)
+    write_qpo_file if compile(fname)
+  end
+  
   def get_qpo_program
     accum=""
     lineno = 0
     end
   end
 
+  def success_or_fail_message(file_name)
+    "Compile of #{file_name} was #{failed ? 'un' : ''}successful\n"+failure_message
+  end
+  
   def send_included_file(line)
     basef = line[/(<getFirst>)(.*)(<\/getFirst>)/,2]
     f = File.exists?(basef) ? basef : @dir + "/" + basef

GUI/src/dialogs/simulate_results/simulate_results_controller.rb

   end
 
   def set_simulate_results(recursion_depth, stack_translation)
-    set_simulate_data(@lqpl_emulator_server_connection.get_simulate_results(recursion_depth), stack_translation)
+    set_simulate_data(lqpl_emulator_server_connection.get_simulate_results(recursion_depth), stack_translation)
   end
 end

GUI/src/lqpl/lqpl_controller.rb

-java_import javax.swing.JFileChooser
-java_import javax.swing.filechooser.FileNameExtensionFilter
+
 java_import java.awt.event.WindowEvent
 require 'dialogs/about/about_controller'
 require 'exit_handler'
   set_model 'LqplModel'
   set_view 'LqplView'
   set_close_action :close
-
-  {"the_menu.file_compile" => "file_compile", "the_menu.file_load" => "file_load",
-    "the_menu.file_simulate" => "file_simulate","the_menu.view_classical_stack" => "view_sub_panel",
-    "the_menu.view_dump" => "view_sub_panel","the_menu.view_executing_code" => "view_sub_panel",
-    "the_menu.view_stack_translation" => "view_sub_panel"}.each do |k,v|
-      add_listener :type => :action, :components => {k => v}
+  
+  def self.set_file_menu_actions
+    { "the_menu.file_compile" => "file_compile", 
+      "the_menu.file_load" => "file_load",
+      "the_menu.file_simulate" => "file_simulate"}.each do |k,v|
+        add_listener :type => :action, :components => {k => v}
+      end
+  end
+  
+  def self.set_view_menu_actions
+    [ "the_menu.view_classical_stack",
+      "the_menu.view_dump" ,
+      "the_menu.view_executing_code" ,
+      "the_menu.view_stack_translation"].each do |k|
+        add_listener :type => :action, :components => {k => "view_sub_panel"}
+      end
+  end
+  
+  def self.set_up_exit_and_about
+    on_mac do 
+      java_import com.apple.eawt.Application
+      Application.application.about_handler = AboutController.instance
+      Application.application.quit_handler = ExitHandler.instance
+    end
+  
+    not_on_mac do
+      { "the_menu.file_exit" => "file_exit",
+        "the_menu.help_about" => "help_about"}.each do |k,v|
+          add_listener :type => :action, :components => {k => v}
+      end
     end
-
-  case RbConfig::CONFIG["host_os"]
-  when /darwin/i # OSX specific code
-    java_import com.apple.eawt.Application
-    Application.application.about_handler = AboutController.instance
-    Application.application.quit_handler = ExitHandler.instance
-   #when /^win|mswin/i # Windows specific code
-   #when /linux/i # Linux specific code
-  else # Windows and Linux
-    add_listener :type => :action, :components => {"the_menu.file_exit" => "file_exit"}
-    add_listener :type => :action, :components => {"the_menu.help_about" => "help_about"}
   end
+      
+  set_file_menu_actions
+  set_view_menu_actions
+  set_up_exit_and_about
+  
 
   def close
     all_controllers_dispose
     super
   end
   
-
-  
   def load(*args)
-    cmp = CompilerServerConnection.get_instance
-    cmp.connect
-    @lqpl_emulator_server_connection = LqplEmulatorServerConnection.get_instance
-    @lqpl_emulator_server_connection.connect
+
+    @cmp = CompilerServerConnection.get_instance
+    @cmp.connect
+
+    self.lqpl_emulator_server_connection.connect
+    @sub_controllers = [QuantumStackController, ClassicalStackController, DumpController, ExecutableCodeController,
+      StackTranslationController].inject([]) {|memo,controller| memo << controller.instance}
+    @dialogs = [AboutController, SimulateResultsController].inject([]) { |mem, var|  mem << var.instance}
   end
 
   def file_exit_action_performed
   end
 
   def file_compile_action_performed
-    chooser = JFileChooser.new()
-    chooser.set_dialog_title "Open LQPL File for Compiling"
-    qplfiles = FileNameExtensionFilter.new("LQPL source file", ["qpl"].to_java(:string))
-    chooser.set_file_filter(qplfiles)
-    chooser.set_current_directory(java.io.File.new(Dir.getwd))
-    rval = chooser.show_open_dialog(self.my_frame)
-    if rval == JFileChooser::APPROVE_OPTION
-      fname = chooser.get_selected_file.get_absolute_path
-      cmp = CompilerServerConnection.get_instance
-      cmp.compile fname
-      model.messages_text = "Compile of #{chooser.get_selected_file.name} was #{cmp.failed ? 'un' : ''}successful\n"
-      model.messages_text << cmp.failure_message
-      cmp.write_qpo_file if !cmp.failed
+    chooser = JFileChooser.lqpl_source_file_opener
+    if chooser.show_open_dialog(self.my_frame) == JFileChooser::APPROVE_OPTION
+      @cmp.compile_and_write_qpo chooser.get_selected_file.get_absolute_path
+      model.messages_text = @cmp.success_or_fail_message(chooser.get_selected_file.name)
     else
       model.messages_text  = "Compile action cancelled."
     end
   end
 
   def file_load_action_performed
-    chooser = JFileChooser.new()
-    chooser.set_dialog_title "Load LQPO (Assembly) File"
-    qpofiles = FileNameExtensionFilter.new("LQPL assembled file", ["qpo"].to_java(:string))
-    chooser.set_file_filter(qpofiles)
-    chooser.set_current_directory(java.io.File.new(Dir.getwd))
-    rval = chooser.show_open_dialog(nil)
-    if rval == JFileChooser::APPROVE_OPTION
-      fname = chooser.selected_file.absolute_path
-      base_file_name = chooser.selected_file.name
-      @lqpl_emulator_server_connection = LqplEmulatorServerConnection.get_instance
-      @lqpl_emulator_server_connection.send_load_from_file(model.recursion_multiplier_spinner, fname)
-      model.frame_title = "Quantum Emulator - #{base_file_name}"
-      model.go_enabled = true
-      model.step_enabled = true
-      model.spinner_panel_visible = true
-      model.button_panel_visible = true
-      model.messages_text = "#{base_file_name} was loaded."
-      @lqpl_emulator_server_connection.send_set_depth_multiplier(model.recursion_multiplier_spinner)
+    chooser = JFileChooser.lqpl_assembled_file_opener
+    if chooser.show_open_dialog(nil) == JFileChooser::APPROVE_OPTION
+      load_file chooser.selected_file.absolute_path
+      model.set_title_and_enable chooser.selected_file.name
       initialize_sub_controllers
-
     else
       model.messages_text =  "QPO file load cancelled."
     end
     update_view
   end
 
+  def load_file(file_path)
+    self.lqpl_emulator_server_connection.send_load_from_file(model.recursion_multiplier_spinner, file_path)
+    self.lqpl_emulator_server_connection.send_set_depth_multiplier(model.recursion_multiplier_spinner)
+  end
+      
   def all_controllers_dispose
-    AboutController.instance.dispose
-    SimulateResultsController.instance.dispose
-    ClassicalStackController.instance.dispose
-    DumpController.instance.dispose
-    ExecutableCodeController.instance.dispose
-    StackTranslationController.instance.dispose
-    QuantumStackController.instance.dispose
+    @dialogs.each {|d| d.dispose}
+    @sub_controllers.each{|sc| sc.dispose}
   end
   
   def file_simulate_action_performed
 
-    SimulateResultsController.instance.lqpl_emulator_server_connection = LqplEmulatorServerConnection.get_instance
-    SimulateResultsController.instance().set_simulate_results(model.recursion_spinner,StackTranslationController.instance.get_stack_translation)
+    SimulateResultsController.instance.set_simulate_results(model.recursion_spinner,StackTranslationController.instance.get_stack_translation)
     SimulateResultsController.instance.open
   end
 
   end
  
   def initialize_sub_controllers
-    update_sub_controller_scs
     ExecutableCodeController.instance.set_code_and_code_pointer  model.recursion_spinner
 
     update_sub_model_data
     open_sub_panels
-    enable_view_menu_items
-  end
-
-  def enable_view_menu_items
-    model.view_menu_stack_translation_enabled = true
-    model.view_menu_dump_enabled = true
-    model.view_menu_executing_code_enabled = true
-    model.view_menu_classical_stack_enabled = true
-  end
-
-  def update_sub_controller_scs
-    QuantumStackController.instance.lqpl_emulator_server_connection = @lqpl_emulator_server_connection
-    ExecutableCodeController.instance.lqpl_emulator_server_connection = @lqpl_emulator_server_connection
-    ClassicalStackController.instance.lqpl_emulator_server_connection = @lqpl_emulator_server_connection
-    DumpController.instance.lqpl_emulator_server_connection = @lqpl_emulator_server_connection
-    StackTranslationController.instance.lqpl_emulator_server_connection = @lqpl_emulator_server_connection
+    model.enable_view_menu_items
   end
 
   def open_sub_panels
-    QuantumStackController.instance.open
-    ExecutableCodeController.instance.open
-    ClassicalStackController.instance.open
-    DumpController.instance.open
-    StackTranslationController.instance.open
+    @sub_controllers.each {|sc| sc.open}
   end
 
   def update_sub_model_data
-    StackTranslationController.instance.set_stack_translation(model.tree_depth_spinner, model.recursion_spinner)
-    QuantumStackController.instance.set_quantum_stack(model.tree_depth_spinner,model.recursion_spinner,StackTranslationController.instance.get_stack_translation)
-    ExecutableCodeController.instance.set_code_pointer  model.recursion_spinner
-    ClassicalStackController.instance.set_classical_stack(model.tree_depth_spinner, model.recursion_spinner)
-    DumpController.instance.set_dump(model.tree_depth_spinner, model.recursion_spinner)
+    @sub_controllers.each {|sc| sc.set_data_from_lqpl_model(model)}
   end
 
   def step_spinner_state_changed
 
   def recursion_spinner_state_changed
     model.recursion_spinner = java.lang.Integer.new(view_model.recursion_spinner)
-    model.go_enabled = true
-    model.step_enabled = true
-    update_view
-    update_sub_model_data
+    enable_and_update true
   end
 
   def recursion_multiplier_spinner_state_changed
     model.recursion_multiplier_spinner = java.lang.Integer.new("#{view_model.recursion_multiplier_spinner}")
     lqpl_emulator_server_connection.send_set_depth_multiplier(model.recursion_multiplier_spinner)
-    model.go_enabled = true
-    model.step_enabled = true
+    enable_and_update true
+  end
+  
+  def enable_and_update(enabled)
+    model.go_enabled = enabled
+    model.step_enabled = enabled
     update_view
     update_sub_model_data
   end
+  
 
   def tree_depth_spinner_state_changed
     model.tree_depth_spinner = java.lang.Integer.new(view_model.tree_depth_spinner)
   def step_button_action_performed
     sc = LqplEmulatorServerConnection.instance
     res = sc.do_step(model.step_spinner,model.recursion_spinner)
-    update_sub_model_data
-    if res =~ /executed/
-      model.go_enabled = false
-      model.step_enabled = false
-      update_view
-    end
+    enable_and_update !(res =~ /executed/)
   end
 
   def go_button_action_performed
-    sc = LqplEmulatorServerConnection.instance
-    sc.do_run model.recursion_spinner
-    update_sub_model_data
-    model.go_enabled = false
-    model.step_enabled = false
-    update_view
+    self.lqpl_emulator_server_connection.do_run model.recursion_spinner
+    enable_and_update false
   end
 
   def trim_button_action_performed
-    sc = LqplEmulatorServerConnection.instance
-    model.messages_text = sc.do_trim
-    QuantumStackController.instance.set_quantum_stack(model.tree_depth_spinner,model.recursion_spinner,StackTranslationController.instance.get_stack_translation)
-    DumpController.instance.set_dump(model.tree_depth_spinner, model.recursion_spinner)
+    model.messages_text = self.lqpl_emulator_server_connection.do_trim
+    QuantumStackController.instance.set_data_from_lqpl_model(model)
+    DumpController.instance.set_data_from_lqpl_model(model)
     update_view
   end
 end

GUI/src/lqpl/lqpl_menu.rb

-["JMenuBar", "JMenu", "JMenuItem"].each do |nm|
-  java_import  "javax.swing.#{nm}"
+%w{JMenuBar JMenu JMenuItem}.each do |nm|
+  java_import  "javax.swing."+nm
 end
 
 class LqplMenu
     menu_file.add(@file_compile);
     menu_file.add(@file_simulate);
 
-    case RbConfig::CONFIG["host_os"]
-    when /darwin/i # OSX specific code
-    #when /^win|mswin/i # Windows specific code
-    #when /linux/i # Linux specific code
-    else # Windows and Linux
+    not_on_mac do
       @file_exit = JMenuItem.new("Exit")
       menu_file.add(@file_exit)
     end
     mbar.add(menu_view);
 
 
-    case RbConfig::CONFIG["host_os"]
-    when /darwin/i # OSX specific code
-    #when /^win|mswin/i # Windows specific code
-    #when /linux/i # Linux specific code
-    else # Windows and Linux
+    not_on_mac do
       menu_help = JMenu.new("Help")
       @help_about = JMenuItem.new("About")
       menu_help.add(@help_about)

GUI/src/lqpl/lqpl_model.rb

     self.send(LqplModel::symbol_for_view_menu_item(current_command), LqplModel::new_view_command(current_command))
   end
   
+  def enable_view_menu_items
+    self.view_menu_stack_translation_enabled = true
+    self.view_menu_dump_enabled = true
+    self.view_menu_executing_code_enabled = true
+    self.view_menu_classical_stack_enabled = true
+  end
+  
+  def set_title_and_enable(base_file_name)
+    self.frame_title = "Quantum Emulator - #{base_file_name}"
+    self.go_enabled = true
+    self.step_enabled = true
+    self.spinner_panel_visible = true
+    self.button_panel_visible = true
+    self.messages_text = "#{base_file_name} was loaded."
+  end
+  
   def self.toggle_action(current)
     return "Hide" if current == "Show"
     "Show"
 # Platform specific operations, feel free to remove or override any of these
 # that don't work for your platform/application
 
+def on_mac
+ yield if RbConfig::CONFIG["host_os"] =~ /darwin/i
+end
+
+def on_win
+  yield if  RbConfig::CONFIG["host_os"] =~ /^win|mswin/i 
+end
 
+def on_linux
+  yield if  RbConfig::CONFIG["host_os"] =~ /^win|mswin/i 
+end
 
-case RbConfig::CONFIG["host_os"]
-when /darwin/i # OSX specific code
+
+def not_on_mac
+  yield if  !(RbConfig::CONFIG["host_os"] =~ /darwin/i)
+end
+  
+on_mac do
   testing = java.lang.System.get_property("com.drogar.testing.fest")
   if !testing or testing != "true"
     java.lang.System.set_property("apple.laf.useScreenMenuBar", "true")
   end
-when /^win|mswin/i # Windows specific code
-when /linux/i # Linux specific code
 end
 
 # End of platform specific code
 # You will probably want to replace the puts with your application's logger
 def log_the_error(exception, thread=nil)
   rexcep = exception.exception
+  puts exception
   if rexcep.class == ServerProcessNotFound
     show_error_dialog("Server(s) not found",
          "LQPL requires the compiler server and emulator server to be installed on your path.
   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
 
-def show_error_dialog_and_exit(title, message)
+def show_error_dialog(title, message)
   JOptionPane.show_message_dialog(nil, message, title, JOptionPane::DEFAULT_OPTION)
   System.exit(0)
 end
 
 begin
   LqplController.instance.open
-  puts 'Returned from open/close...'
 rescue => e
-  show_error_dialog_and_exit("error",e.to_s)
+  log_the_error(e)
 end

GUI/src/manifest.rb

 
 require "exit_handler"
 
-java_import javax.swing.JOptionPane
+%w{JOptionPane JFileChooser filechooser.FileNameExtensionFilter}.each do |cfile|
+  java_import "javax.swing."+cfile
+end
+
 java_import java.lang.System
 
 java_import java.awt.Point
 
+
 require 'utility/monkey/point'
+require 'utility/monkey/jfile_chooser'
 
 

GUI/src/panels/classical_stack/classical_stack_controller.rb

   set_view 'ClassicalStackView'
   set_close_action :hide
 
+  def set_data_from_lqpl_model(lqpl_model)
+    set_classical_stack(lqpl_model.tree_depth_spinner, lqpl_model.recursion_spinner)
+  end
+  
   def set_classical_stack(tree_depth, recursion_level)
     set_classical_stack_data(lqpl_emulator_server_connection.get_classical_stack(tree_depth,recursion_level))
   end

GUI/src/panels/dump/dump_controller.rb

   set_view 'DumpView'
   set_close_action :hide
 
+  def set_data_from_lqpl_model(lqpl_model)
+    set_dump(lqpl_model.tree_depth_spinner, lqpl_model.recursion_spinner)
+  end
   
   def set_dump(tree_depth, recursion_level)
     set_dump_data(lqpl_emulator_server_connection.get_dump(tree_depth,recursion_level))

GUI/src/panels/executable_code/executable_code_controller.rb

   set_view 'ExecutableCodeView'
   set_close_action :hide
 
+
+  def set_data_from_lqpl_model(lqpl_model)
+    set_code_pointer  lqpl_model.recursion_spinner
+  end
+  
   def set_code_and_code_pointer(recursion_depth)
     model.the_code = lqpl_emulator_server_connection.loaded_code recursion_depth
     model.the_code_pointer = lqpl_emulator_server_connection.code_pointer recursion_depth

GUI/src/panels/quantum_stack/quantum_stack_controller.rb

   set_model 'QuantumStackModel'
   set_view 'QuantumStackView'
 
+  def set_data_from_lqpl_model(lqpl_model)
+    set_quantum_stack(lqpl_model.tree_depth_spinner,lqpl_model.recursion_spinner,StackTranslationController.instance.get_stack_translation)
+  end
+  
   def set_quantum_stack(tree_depth,recursion_depth,stack_trans)
     model.stack_translation = stack_trans
     model.quantum_stack =  lqpl_emulator_server_connection.get_qstack tree_depth,recursion_depth

GUI/src/panels/quantum_stack/quantum_stack_painter.rb

   end
 
   def sub_stack_sizes(g)
-    @sstack_painters.collect {|ret,sstack_painter| sstack_painter.model_paint_size(g)}
+    @sstack_painters.collect {|sstack_painter| sstack_painter.model_paint_size(g)}
   end
 
  

GUI/src/panels/stack_translation/stack_translation_controller.rb

   set_view 'StackTranslationView'
   set_close_action :hide
 
+  def set_data_from_lqpl_model(lqpl_model)
+    set_stack_translation(lqpl_model.tree_depth_spinner, lqpl_model.recursion_spinner)
+  end
+  
   def set_stack_translation(tree_depth, recursion_level)
     set_stack_translation_data(lqpl_emulator_server_connection.get_stack_translation(tree_depth,recursion_level))
   end

GUI/src/utility/monkey/jfile_chooser.rb

+class JFileChooser
+  def self.lqpl_assembled_file_opener
+    chooser = JFileChooser.new()
+    chooser.set_dialog_title "Load LQPO (Assembly) File"
+    qpofiles = FileNameExtensionFilter.new("LQPL assembled file", ["qpo"].to_java(:string))
+    chooser.set_file_filter(qpofiles)
+    chooser.set_current_directory(java.io.File.new(Dir.getwd))
+    chooser
+  end
+  
+  def self.lqpl_source_file_opener
+    chooser = JFileChooser.new()
+    chooser.set_dialog_title "Open LQPL File for Compiling"
+    qplfiles = FileNameExtensionFilter.new("LQPL source file", ["qpl"].to_java(:string))
+    chooser.set_file_filter(qplfiles)
+    chooser.set_current_directory(java.io.File.new(Dir.getwd))
+    chooser
+  end
+end

features/show_simulate_results.feature

     When I select "Load" from the "File" menu
     And I load "coin.reference.qpo" from the project directory "GUI/testdata/qplprograms"
 
+	@wip
   Scenario:
     When I click the button "Go" 1 time on the frame "Quantum Emulator"
     Given I select "Simulate" from the "File" menu