Class: OpenStudio::Analysis::Translator::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/analysis/translator/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(osa_filename, options = {}) ⇒ Workflow

Returns a new instance of Workflow.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openstudio/analysis/translator/workflow.rb', line 53

def initialize(osa_filename, options = {})
  @osa_filename = osa_filename
  @root_path = File.expand_path(File.dirname(@osa_filename))

  # try to read the osa json file
  if File.exist?(@osa_filename)
    @osa = ::JSON.parse(File.read(@osa_filename), symbolize_names: true)[:analysis]
  else
    raise "File #{@osa_filename} does not exist"
  end

  # Initialize some other instance variables
  @osw_version = '0.0.1'
  @options = options
  @file_paths = options[:file_paths] ? options[:file_paths] : []
  @measure_paths = options[:measure_paths] ? options[:measure_paths] : []

  # Initialize static inputs from the OSA
  @seed_file = File.basename(@osa[:seed][:path])
  if @options[:seed]
    @seed_file = @options[:seed]
  end
  @weather_file = File.basename(@osa[:weather_file][:path])
  @osa_id = @osa[:_id]
  @steps = []
  @osa[:problem][:workflow].each_with_index do |step, i|
    step_hash = {}
    step_hash[:measure_dir_name] = File.basename(step[:measure_definition_directory])
    step_hash[:arguments] = {}
    # Measures can have no arguments -- make sure to catch it
    if @osa[:problem][:workflow][i][:arguments]
      @osa[:problem][:workflow][i][:arguments].each do |arg|
        next if arg[:value].nil?
        step_hash[:arguments][arg[:name].to_sym] = arg[:value]
      end
    end
    step_hash[:name] = step[:name] if step[:name]
    step_hash[:description] = step[:description] if step[:description]
    if @options[:da_descriptions]
      step_hash[:name] = @options[:da_descriptions][i][:name]
      step_hash[:description] = @options[:da_descriptions][i][:description]
    end
    # DLM: the following fields are deprecated and should be removed once EDAPT reports no longer rely on them, they are moved to step.results
    step_hash[:measure_id] = step[:measure_definition_uuid] if step[:measure_definition_uuid]
    step_hash[:version_id] = step[:measure_definition_version_uuid] if step[:measure_definition_version_uuid]
    step_hash[:modeler_description] = step[:modeler_description] if step[:modeler_description]
    step_hash[:taxonomy] = step[:taxonomy] if step[:taxonomy]
    step_hash[:measure_type] = step[:measure_type]
    step_hash[:measure_type] = 'ModelMeasure' if step_hash[:measure_type] == 'RubyMeasure'
    @steps << step_hash
  end
end

Instance Attribute Details

#analysisObject (readonly)

Returns the value of attribute analysis.



42
43
44
# File 'lib/openstudio/analysis/translator/workflow.rb', line 42

def analysis
  @analysis
end

#file_pathsObject (readonly)

Returns the value of attribute file_paths.



46
47
48
# File 'lib/openstudio/analysis/translator/workflow.rb', line 46

def file_paths
  @file_paths
end

#measure_pathsObject (readonly)

Returns the value of attribute measure_paths.



47
48
49
# File 'lib/openstudio/analysis/translator/workflow.rb', line 47

def measure_paths
  @measure_paths
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/openstudio/analysis/translator/workflow.rb', line 45

def options
  @options
end

#osaObject (readonly)

Returns the value of attribute osa.



43
44
45
# File 'lib/openstudio/analysis/translator/workflow.rb', line 43

def osa
  @osa
end

#osa_filenameObject (readonly)

Returns the value of attribute osa_filename.



40
41
42
# File 'lib/openstudio/analysis/translator/workflow.rb', line 40

def osa_filename
  @osa_filename
end

#osa_idObject (readonly)

Returns the value of attribute osa_id.



50
51
52
# File 'lib/openstudio/analysis/translator/workflow.rb', line 50

def osa_id
  @osa_id
end

#osw_versionObject (readonly)

Returns the value of attribute osw_version.



44
45
46
# File 'lib/openstudio/analysis/translator/workflow.rb', line 44

def osw_version
  @osw_version
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



41
42
43
# File 'lib/openstudio/analysis/translator/workflow.rb', line 41

def root_path
  @root_path
end

#seed_fileObject (readonly)

Returns the value of attribute seed_file.



48
49
50
# File 'lib/openstudio/analysis/translator/workflow.rb', line 48

def seed_file
  @seed_file
end

#stepsObject (readonly)

Returns the value of attribute steps.



51
52
53
# File 'lib/openstudio/analysis/translator/workflow.rb', line 51

def steps
  @steps
end

#weather_fileObject (readonly)

Returns the value of attribute weather_file.



49
50
51
# File 'lib/openstudio/analysis/translator/workflow.rb', line 49

def weather_file
  @weather_file
end

Instance Method Details

#process_datapoint(osd_filename) ⇒ Object

Convert a file in the form of an OSD into an OSW



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/openstudio/analysis/translator/workflow.rb', line 107

def process_datapoint(osd_filename)
  # Try to read the osd json file
  osd = nil
  if File.exist?(osd_filename)
    osd = ::JSON.parse(File.read(osd_filename), symbolize_names: true)[:data_point]
  else
    raise "File #{osd_filename} does not exist"
  end

  # Parse the osd hash based off of the osa hash. First check that the analysis id matches
  raise "File #{osd_filename} does not reference #{@osa_id}." unless @osa_id == osd[:analysis_id]
  osw_steps_instance = @steps
  osw_steps_instance.each_with_index do |step, i|
    next unless @osa[:problem][:workflow][i][:variables]
    @osa[:problem][:workflow][i][:variables].each do |var|
      var_name = var[:argument][:name]
      var_value_uuid = var[:uuid]
      var_value = osd[:set_variable_values][var_value_uuid.to_sym]
      step[:arguments][var_name.to_sym] = var_value
    end
  end

  # Overwrite the seed and weather files if they are present in the datapoint.json
  if (osd[:weather_file] != '') && !osd[:weather_file].nil?
    weather_file = osd[:weather_file]
  else
    weather_file = @weather_file
  end
  if (osd[:seed] != '') && !osd[:seed].nil?
    seed_file = osd[:seed]
  else
    seed_file = @seed_file
  end

  # Save the OSW hash
  osw = {}
  created_at = ::Time.now
  osw[:seed_file] = seed_file
  osw[:weather_file] = weather_file
  osw[:file_format_version] = @osw_version
  osw[:osa_id] = @osa_id
  osw[:osd_id] = osd[:_id]
  osw[:created_at] = created_at
  osw[:measure_paths] = @measure_paths
  osw[:file_paths] = @file_paths
  osw[:run_directory] = './run'
  osw[:steps] = osw_steps_instance
  osw[:name] = osd[:name] if osd[:name]
  osw[:description] = osd[:description] if osd[:description]
  osw
end

#process_datapoints(osd_filename_array) ⇒ Object

Runs an array of OSD files



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/openstudio/analysis/translator/workflow.rb', line 160

def process_datapoints(osd_filename_array)
  r = []
  osd_filename_array.each do |osd_file|
    begin
      r << process_datapoint(osd_file)
    rescue StandardError => e
      r << nil
      puts "Warning: Failed to process datapoint #{osd_file} with error #{e.message} in #{e.backtrace.join('\n')}"
    end
  end

  r
end