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.



20
21
22
23
24
25
26
# 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

Instance Attribute Details

#featureObject (readonly)

:nodoc:#



9
10
11
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 9

def feature
  @feature
end

#feature_idObject (readonly)

:nodoc:#



9
10
11
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 9

def feature_id
  @feature_id
end

#feature_nameObject (readonly)

:nodoc:#



9
10
11
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 9

def feature_name
  @feature_name
end

#mapper_classObject (readonly)

:nodoc:#



9
10
11
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 9

def mapper_class
  @mapper_class
end

#scenarioObject (readonly)

:nodoc:#



9
10
11
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 9

def scenario
  @scenario
end

Instance Method Details

#clearObject

Return the directory that this datapoint will run in.



55
56
57
58
59
# 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_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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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_locationObject

Gets the type of a feature



38
39
40
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 38

def feature_location
  @feature.feature_location
end

#feature_typeObject

Gets the type of a feature



31
32
33
# File 'lib/urbanopt/scenario/scenario_datapoint_base.rb', line 31

def feature_type
  @feature.feature_type
end

#out_of_date?Boolean

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

return:

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

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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_dirObject

Return the directory that this datapoint will run in.

return:

String - Directory that this datapoint will run in.



46
47
48
49
50
51
# 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