Class: URBANopt::Scenario::ScenarioDatapoint

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/scenario/scenario_datapoint_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario, feature_id, feature_name, mapper_class) ⇒ ScenarioDatapoint

ScenarioDatapoint is an agnostic description of the simulation of a Feature in a Scenario A Simulation Mapper will map the

parameters:

scenario - ScenarioBase - Scenario containing this ScenarioDatapoint. feature_id - String - Unique id of the feature for this ScenarioDatapoint. feature_name - String - Human readable name of the feature for this ScenarioDatapoint. mapper_class - String - Name of Ruby class used to translate feature to simulation OSW.



45
46
47
48
49
50
51
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 45

def initialize(scenario, feature_id, feature_name, mapper_class)
  @scenario = scenario
  @feature_id = feature_id
  @feature_name = feature_name
  @feature = scenario.feature_file.get_feature_by_id(feature_id)
  @mapper_class = mapper_class
end

Instance Attribute Details

#featureObject (readonly)

:nodoc:



53
54
55
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 53

def feature
  @feature
end

#feature_idObject (readonly)

:nodoc:#



34
35
36
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 34

def feature_id
  @feature_id
end

#feature_nameObject (readonly)

:nodoc:#



34
35
36
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 34

def feature_name
  @feature_name
end

#mapper_classObject (readonly)

:nodoc:#



34
35
36
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 34

def mapper_class
  @mapper_class
end

#scenarioObject (readonly)

:nodoc:#



34
35
36
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 34

def scenario
  @scenario
end

Instance Method Details

#clearObject

Return the directory that this datapoint will run in.



81
82
83
84
85
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 81

def clear
  dir = run_dir
  FileUtils.rm_rf(dir) if File.exist?(dir)
  FileUtils.mkdir_p(dir) if !File.exist?(dir)
end

#create_oswObject

Create run directory and generate simulation OSW, all previous contents of directory are removed The simulation OSW is created by evaluating the mapper_class’s create_osw method

return:

String - Path to the simulation OSW.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 97

def create_osw
  osw = eval("#{@mapper_class}.new.create_osw(@scenario, @feature_id, @feature_name)")
  dir = run_dir
  FileUtils.rm_rf(dir) if File.exist?(dir)
  FileUtils.mkdir_p(dir) if !File.exist?(dir)
  osw_path = File.join(dir, 'in.osw')
  File.open(osw_path, 'w') do |f|
    f << JSON.pretty_generate(osw)
    # make sure data is written to the disk one way or the other
    begin
      f.fsync
    rescue StandardError
      f.flush
    end
  end
  return osw_path
end

#feature_locationObject

Gets the type of a feature



65
66
67
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 65

def feature_location
  @feature.feature_location
end

#feature_typeObject

Gets the type of a feature



58
59
60
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 58

def feature_type
  @feature.feature_type
end

#out_of_date?Boolean

Return true if the datapoint is out of date, false otherwise. Non-existant files are out of date.

return:

Boolean - True if the datapoint is out of date, false otherwise.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 121

def out_of_date?
  dir = run_dir
  if !File.exist?(dir)
    return true
  end

  out_osw = File.join(dir, 'out.osw')
  if !File.exist?(out_osw)
    return true
  end
  out_osw_time = File.mtime(out_osw)

  # array of files that this datapoint depends on
  dependencies = []

  # depends on the feature file
  dependencies << scenario.feature_file.path

  # depends on the csv file
  dependencies << scenario.csv_file

  # depends on the mapper classes
  Dir.glob(File.join(scenario.mapper_files_dir, '*')).each do |f|
    dependencies << f
  end

  # depends on the root gemfile
  dependencies << File.join(scenario.root_dir, 'Gemfile')
  dependencies << File.join(scenario.root_dir, 'Gemfile.lock')

  # todo, depends on all the measures?

  # check if out of date
  dependencies.each do |f|
    if File.exist?(f)
      if File.mtime(f) > out_osw_time
        puts "File '#{f}' is newer than '#{out_osw}', datapoint out of date"
        return true
      end
    else
      puts "Dependency file '#{f}' does not exist"
    end
  end

  return false
end

#run_dirObject

Return the directory that this datapoint will run in.

return:

String - Directory that this datapoint will run in.



73
74
75
76
77
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 73

def run_dir
  raise 'Feature ID not set' if @feature_id.nil?
  raise 'Scenario run dir not set' if @scenario.run_dir.nil?
  return File.join(@scenario.run_dir, @feature_id + '/')
end