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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openstudio/analysis/translator/workflow.rb', line 23

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] : []
  @file_paths << '../lib'
  @measure_paths = options[:measure_paths] ? options[:measure_paths] : []

  # Initialize static inputs from the OSA
  !@osa[:seed].nil? ? @seed_file = File.basename(@osa[:seed][:path]) : @seed_file = ''
  if @options[:seed]
    @seed_file = @options[:seed]
  end
  !@osa[:weather_file].nil? ? @weather_file = File.basename(@osa[:weather_file][:path]) : @weather_file = ''
  @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
    @osa[:problem][:workflow][i][:arguments]&.each do |arg|
      next if arg[:value].nil?
      step_hash[:arguments][arg[:name].to_sym] = arg[:value]
    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'
    @steps << step_hash
  end
end

Instance Attribute Details

#analysisObject (readonly)

Returns the value of attribute analysis.



12
13
14
# File 'lib/openstudio/analysis/translator/workflow.rb', line 12

def analysis
  @analysis
end

#file_pathsObject (readonly)

Returns the value of attribute file_paths.



16
17
18
# File 'lib/openstudio/analysis/translator/workflow.rb', line 16

def file_paths
  @file_paths
end

#measure_pathsObject (readonly)

Returns the value of attribute measure_paths.



17
18
19
# File 'lib/openstudio/analysis/translator/workflow.rb', line 17

def measure_paths
  @measure_paths
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/openstudio/analysis/translator/workflow.rb', line 15

def options
  @options
end

#osaObject (readonly)

Returns the value of attribute osa.



13
14
15
# File 'lib/openstudio/analysis/translator/workflow.rb', line 13

def osa
  @osa
end

#osa_filenameObject (readonly)

Returns the value of attribute osa_filename.



10
11
12
# File 'lib/openstudio/analysis/translator/workflow.rb', line 10

def osa_filename
  @osa_filename
end

#osa_idObject (readonly)

Returns the value of attribute osa_id.



20
21
22
# File 'lib/openstudio/analysis/translator/workflow.rb', line 20

def osa_id
  @osa_id
end

#osw_versionObject (readonly)

Returns the value of attribute osw_version.



14
15
16
# File 'lib/openstudio/analysis/translator/workflow.rb', line 14

def osw_version
  @osw_version
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



11
12
13
# File 'lib/openstudio/analysis/translator/workflow.rb', line 11

def root_path
  @root_path
end

#seed_fileObject (readonly)

Returns the value of attribute seed_file.



18
19
20
# File 'lib/openstudio/analysis/translator/workflow.rb', line 18

def seed_file
  @seed_file
end

#stepsObject (readonly)

Returns the value of attribute steps.



21
22
23
# File 'lib/openstudio/analysis/translator/workflow.rb', line 21

def steps
  @steps
end

#weather_fileObject (readonly)

Returns the value of attribute weather_file.



19
20
21
# File 'lib/openstudio/analysis/translator/workflow.rb', line 19

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



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/openstudio/analysis/translator/workflow.rb', line 76

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



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/openstudio/analysis/translator/workflow.rb', line 129

def process_datapoints(osd_filename_array)
  r = []
  osd_filename_array.each do |osd_file|
    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

  r
end