class URBANopt::Scenario::ScenarioDatapoint

Public Class Methods

new(scenario, feature_id, feature_name, mapper_class) click to toggle source

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.

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 20
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

Public Instance Methods

clear() click to toggle source

Return the directory that this datapoint will run in.

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 55
def clear
  dir = run_dir
  FileUtils.rm_rf(dir) if File.exist?(dir)
  FileUtils.mkdir_p(dir) if !File.exist?(dir)
end
create_osw() click to toggle source

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.

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 71
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_location() click to toggle source

Gets the type of a feature

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 38
def feature_location
  @feature.feature_location
end
feature_type() click to toggle source

Gets the type of a feature

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 31
def feature_type
  @feature.feature_type
end
out_of_date?() click to toggle source

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.

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 95
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_dir() click to toggle source

Return the directory that this datapoint will run in.

return:

String - Directory that this datapoint will run in.

# File lib/urbanopt/scenario/scenario_datapoint_base.rb, line 46
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