Parent

Robot

Author

Manuela Ruiz (mruiz@lcc.uma.es)

This class represents a robot, that is, a set of projects that are executed in some specified ordering, so as to automatically produce a set of designs

Attributes

project_paths[RW]
execution_modes[RW]

Public Class Methods

new() click to toggle source

Initializing the robot

      # File lib/main-structures.rb, line 1792
1792:         def initialize

1793:                 @project_paths = Array.new

1794:                 @execution_modes = Array.new

1795:         end

Public Instance Methods

execute(number_of_designs, design_directory) click to toggle source
number_of_designs

number of designs to be generated

design_directory

directory to save the generated designs in

 Executes the robot 
      # File lib/main-structures.rb, line 1850
1850:         def execute(number_of_designs, design_directory)

1851:                 goals_command_directory = "#{File.dirname(__FILE__)}/commands/random-goal-execute-command.rb"

1852:                 goals_command_directory.gsub!("/", "\\")

1853:                 

1854:                 current_shape_directory = "#{File.dirname(__FILE__)}/commands/temp/current_shape.txt"

1855:                 current_shape_directory.gsub!("/", "\\")

1856:                 

1857:                 project_directory = "#{File.dirname(__FILE__)}/commands/temp/project.txt"

1858:                 project_directory.gsub!("/", "\\")

1859:                 

1860:                 n_command_directory = "#{File.dirname(__FILE__)}/commands/n-execute-random-command.rb"

1861:                 n_command_directory.gsub!("/", "\\")

1862:                 

1863:                 for i in 1..number_of_designs.to_i

1864:                         design_path = "#{design_directory}\\design#{i}.txt"

1865:                         

1866:                         first = true

1867:                         

1868:                         t1 = Time.now

1869:                         @project_paths.each_with_index { |project_path, p|

1870:                                 

1871:                                 Shade.project.load(project_path)

1872:                                 Shade.project.execution.reset

1873:                                 

1874:                                 if !first

1875:                                         new_axiom = LabelledShape.new(Array.new, Array.new)

1876:                                         new_axiom.load(current_shape_directory)

1877:                                         Shade.project.execution.file_axiom = true

1878:                                         Shade.project.execution.grammar.axiom = new_axiom

1879:                                         Shade.project.execution.reset

1880:                                         

1881:                                         Shade.project.execution.current_shape.refresh

1882:                                         Shade.project.execution.current_shape.create_pi

1883:                                 end

1884:                                 

1885:                                 mode = @execution_modes[p]

1886:                                 

1887:                                 #Save project into fix .txt file

1888:                                 Shade.project.save(project_directory, true)

1889:                                 

1890:                                 #Save current shape into fix .txt file

1891:                                 Shade.project.execution.current_shape.save(current_shape_directory)

1892:                                 

1893:                                 #Save execution history

1894:                                 history_directory = "#{File.dirname(__FILE__)}/commands/temp"

1895:                                 history_directory.gsub!("/", "\\")

1896:                                 Shade.project.execution.save_execution_history(history_directory)

1897:                                 

1898:                                 if mode == "Goals"

1899:                                         output = system("ruby \"#{goals_command_directory}\" 0 1000")

1900:                                 else

1901:                                         output = system("ruby \"#{n_command_directory}\" #{mode.to_i}")

1902:                                 end

1903:                                 

1904:                                 #Catch return from external command                         

1905:                                 if !($? == 0)

1906:                                         UI.messagebox("The loaded constraints/goals of #{project_path} use some functions of SketchUp API. The execution will be performed inside the SketchUp environment, and it may take more time.")

1907:                                         

1908:                                         if mode == "Goals"

1909:                                                 success = Shade.project.execution.apply_goal_rules_random(false, 0, 1000)

1910:                                         else

1911:                                                 success = Shade.project.execution.apply_n_rules_random(mode.to_i, false)

1912:                                         end

1913:                                         if !success

1914:                                                 UI.messagebox("Failure. Impossible to satisfy goals in #{project_path}.")

1915:                                         else

1916:                                                 #Save current shape into fix .txt file

1917:                                                 Shade.project.execution.current_shape.save(current_shape_directory)

1918:                                         end

1919:                                 else

1920:                                         log_directory = "#{File.dirname(__FILE__)}/commands/temp/result.log"

1921:                                         log_directory.gsub!("/", "\\")

1922:                                         File.open(log_directory, 'r') do |f|

1923:                                                 line = f.gets.strip

1924:                                                 if (line == "false")

1925:                                                         UI.messagebox("Failure. Impossible to satisfy goals in #{project_path}.")

1926:                                                 end

1927:                                                         

1928:                                         end        

1929:                                 end

1930: 

1931:                                 first = false

1932:                         }

1933:                         Shade.project.execution.current_shape.load(current_shape_directory)

1934:                         Shade.project.execution.current_shape.refresh

1935:                         Shade.project.execution.current_shape.create_pi

1936:                         Shade.project.execution.current_shape.save(design_path)

1937:                         t2 = Time.now

1938:                         puts "Elapsed time for design #{i}: #{t2 - t1} seconds"

1939:                         #delete files of temporal directory

1940:                         dir = Dir.new("#{File.dirname(__FILE__)}/commands/temp")

1941:                         dir.each { |file_name|

1942:                                 if file_name == '.' or file_name == '..' then next

1943:                                 else File.delete("#{File.dirname(__FILE__)}/commands/temp/#{file_name}")

1944:                                 end

1945:                         }

1946:                         

1947:                 end

1948:                 

1949:         end
load(path) click to toggle source
path

the path to save the robot from

 Loads the robot from the specified path
      # File lib/main-structures.rb, line 1831
1831:         def load(path)

1832:                 @project_paths = Array.new

1833:                 @execution_modes = Array.new

1834:                 File.open(path.strip, 'r') do |f|

1835:                         while line = f.gets

1836:                                 line_a = line.split(':')

1837: 

1838:                                 project_path = "#{File.dirname(path)}\\#{line_a[0]}.prj"

1839:                                 execution_mode = line_a[1].strip

1840:                                 @project_paths.push project_path

1841:                                 @execution_modes.push execution_mode        

1842:                         end

1843:                 end

1844:         end
save(path) click to toggle source
path

the path to save the robot in

 Saves the robot in the specified path
      # File lib/main-structures.rb, line 1800
1800:         def save(path)

1801:                 #the path is a .txt file

1802:                 #we need the directory

1803:                 directory = ShadeUtils.get_directory_from_path(path)

1804:                 title = ShadeUtils.get_title_from_path(path)

1805:                 

1806:                 old_project_path = Shade.project.path

1807:                 

1808:                 File.open(path.strip, 'w') do |f|

1809:                         @project_paths.each_with_index { |project, i|

1810:                                 project_title = ShadeUtils.get_title_from_path(project)

1811:                                 f.write("#{project_title}:#{@execution_modes[i]}\n")

1812: 

1813:                                 Shade.project.load(project)

1814:                                 Shade.project.save("#{directory}\\#{project_title}.prj", true)

1815:                         }

1816:                 end   

1817:                 

1818:                 if old_project_path

1819:                         Shade.project.load(old_project_path)

1820:                         

1821:                         Shade.project.execution.reset

1822:                         Shade.project.refresh(true)

1823:                 else

1824:                         ShadeUtils.create_default_project

1825:                 end

1826:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.