Class: OpenStudio::Analysis::Workflow

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Create an instance of the OpenStudio::Analysis::Workflow



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

def initialize
  @items = []
end

Instance Attribute Details

#itemsObject (readonly) Also known as: measures

Returns the value of attribute items.



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

def items
  @items
end

Class Method Details

.from_file(filename) ⇒ Object

Read the Workflow description from a persisted file. The format at the moment is the current analysis.json

Returns:

  • (Object)

    Return an instance of the workflow object



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/openstudio/analysis/workflow.rb', line 296

def self.from_file(filename)
  o = nil
  if File.exist? filename
    j = JSON.parse(File.read(filename), symbolize_names: true)
    o = OpenStudio::Analysis::Workflow.load(j)
  else
    raise "Could not find workflow file #{filename}"
  end

  o
end

.load(h) ⇒ Object

Load from a from a hash

Parameters:

  • h (Hash or String)

    Path to file or hash



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/openstudio/analysis/workflow.rb', line 258

def self.load(h)
  # get the version of the file
  file_format_version = h[:file_format_version] ? h[:file_format_version] : 1
  puts "Parsing file version #{file_format_version}"

  o = OpenStudio::Analysis::Workflow.new

  if h[:workflow]
    h[:workflow].each do |wf|
      puts "Adding measure #{wf[:name]}"

      # Go though the defined measure paths and try and find the local measure
      local_measure_dir = nil
      if wf[:measure_definition_directory_local] && Dir.exist?(wf[:measure_definition_directory_local])
        local_measure_dir = wf[:measure_definition_directory_local]
      else
        # search in the measure paths for the measure
        OpenStudio::Analysis.measure_paths.each do |p|
          test_dir = File.join(p, File.basename(wf[:measure_definition_directory]))
          if Dir.exist?(test_dir)
            local_measure_dir = test_dir
            break
          end
        end
      end

      raise "Could not find local measure when loading workflow for #{wf[:measure_definition_class_name]} in #{File.basename(wf[:measure_definition_directory])}. You may have to add measure paths to OpenStudio::Analysis.measure_paths" unless local_measure_dir

      o.add_measure_from_analysis_hash(wf[:name], wf[:display_name], local_measure_dir, wf)
    end
  end
  o
end

Instance Method Details

#add_measure(instance_name, instance_display_name, local_path_to_measure, measure_metadata) ⇒ Object

Add a measure from the custom hash format without reading the measure.rb or measure.xml file

Parameters:

  • local_path_to_measure (String)

    This is the local path to the measure directory, relative or absolute. It is used when zipping up all the measures.

  • measure_metadata (Hash)

    Format of the measure.xml in JSON format

Returns:

  • (Object)

    Returns the measure that was added as an OpenStudio::AnalysisWorkflowStep object



93
94
95
96
97
# File 'lib/openstudio/analysis/workflow.rb', line 93

def add_measure(instance_name, instance_display_name, local_path_to_measure, )
  @items << OpenStudio::Analysis::WorkflowStep.from_measure_hash(instance_name, instance_display_name, local_path_to_measure, )

  @items.last
end

#add_measure_from_analysis_hash(instance_name, instance_display_name, local_path_to_measure, measure_metadata) ⇒ Object

Add a measure from the analysis hash format

Parameters:

  • local_path_to_measure (String)

    This is the local path to the measure directory, relative or absolute. It is used when zipping up all the measures.

  • measure_metadata (Hash)

    Format of the measure.xml in JSON format

Returns:

  • (Object)

    Returns the measure that was added as an OpenStudio::AnalysisWorkflowStep object



106
107
108
109
110
# File 'lib/openstudio/analysis/workflow.rb', line 106

def add_measure_from_analysis_hash(instance_name, instance_display_name, local_path_to_measure, )
  @items << OpenStudio::Analysis::WorkflowStep.from_analysis_hash(instance_name, instance_display_name, local_path_to_measure, )

  @items.last
end

#add_measure_from_csv(measure) ⇒ Object

Add a measure from the format that CSV parses into. This is a helper method to map the csv data to the new programmatic interface format

Returns:

  • (Object)

    Returns the measure that was added as an OpenStudio::AnalysisWorkflowStep object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/openstudio/analysis/workflow.rb', line 174

def add_measure_from_csv(measure)
  hash = {}
  hash[:classname] = measure[:measure_data][:classname]
  hash[:name] = measure[:measure_data][:name]
  hash[:display_name] = measure[:measure_data][:display_name]
  hash[:measure_type] = measure[:measure_data][:measure_type]
  hash[:uid] = measure[:measure_data][:uid] ? measure[:measure_data][:uid] : SecureRandom.uuid
  hash[:version_id] = measure[:measure_data][:version_id] ? measure[:measure_data][:version_id] : SecureRandom.uuid

  # map the arguments - this can be a variable or argument, add them all as arguments first,
  # the make_variable will remove from arg and place into variables
  hash[:arguments] = measure[:args]

  m = add_measure(measure[:measure_data][:name], measure[:measure_data][:display_name], measure[:measure_data][:local_path_to_measure], hash)

  measure[:vars].each do |variable|
    next unless ['variable', 'pivot'].include? variable[:variable_type]

    dist = variable[:distribution]
    opt = {
      variable_type: variable[:variable_type],
      variable_display_name_short: variable[:display_name_short],
      static_value: variable[:distribution][:mode]
    }

    m.make_variable(variable[:name], variable[:display_name], dist, opt)
  end
end

#add_measure_from_excel(measure) ⇒ Object

Add a measure from the format that Excel parses into. This is a helper method to map the excel data to the new programmatic interface format

Returns:

  • (Object)

    Returns the measure that was added as an OpenStudio::AnalysisWorkflowStep object



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
158
159
160
161
162
163
164
165
166
167
# File 'lib/openstudio/analysis/workflow.rb', line 117

def add_measure_from_excel(measure)
  hash = {}
  hash[:classname] = measure['measure_file_name']
  hash[:name] = measure['name']
  hash[:display_name] = measure['display_name']
  hash[:measure_type] = measure['measure_type']
  hash[:uid] = measure['uid'] ? measure['uid'] : SecureRandom.uuid
  hash[:version_id] = measure['version_id'] ? measure['version_id'] : SecureRandom.uuid

  # map the arguments - this can be a variable or argument, add them all as arguments first,
  # the make_variable will remove from arg and place into variables
  args = []

  measure['variables'].each do |variable|
    args << {
      local_variable: variable['name'],
      variable_type: variable['type'],
      name: variable['name'],
      display_name: variable['display_name'],
      display_name_short: variable['display_name_short'],
      units: variable['units'],
      default_value: variable['distribution']['static_value'],
      value: variable['distribution']['static_value']
    }
  end
  hash[:arguments] = args

  m = add_measure(measure['name'], measure['display_name'], measure['local_path_to_measure'], hash)

  measure['variables'].each do |variable|
    next unless ['variable', 'pivot'].include? variable['variable_type']

    dist = {
      type: variable['distribution']['type'],
      minimum: variable['distribution']['min'],
      maximum: variable['distribution']['max'],
      mean: variable['distribution']['mean'],
      standard_deviation: variable['distribution']['stddev'],
      values: variable['distribution']['discrete_values'],
      weights: variable['distribution']['discrete_weights'],
      step_size: variable['distribution']['delta_x']
    }
    opt = {
      variable_type: variable['variable_type'],
      variable_display_name_short: variable['display_name_short'],
      static_value: variable['distribution']['static_value']
    }

    m.make_variable(variable['name'], variable['display_name'], dist, opt)
  end
end

#add_measure_from_path(instance_name, instance_display_name, local_path_to_measure) ⇒ Object

Add a measure to the workflow from a path. This will parse the measure.xml which must exist.

Parameters:

  • local_path_to_measure (String)

    This is the local path to the measure directory, relative or absolute. It is used when zipping up all the measures.

Returns:

  • (Object)

    Returns the measure that was added as an OpenStudio::AnalysisWorkflowStep object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openstudio/analysis/workflow.rb', line 62

def add_measure_from_path(instance_name, instance_display_name, local_path_to_measure)
  measure_filename = 'measure.rb'

  if File.exist?(local_path_to_measure) && File.file?(local_path_to_measure)
    measure_filename = File.basename(local_path_to_measure)
    local_path_to_measure = File.dirname(local_path_to_measure)
  end

  if Dir.exist?(local_path_to_measure) && File.directory?(local_path_to_measure)
    measure_hash = nil
    if File.exist?(File.join(local_path_to_measure, 'measure.xml'))
      measure_hash = parse_measure_xml(File.join(local_path_to_measure, 'measure.xml'))
    else
      raise 'Could not find measure.xml'
    end

    add_measure(instance_name, instance_display_name, local_path_to_measure, measure_hash)
  else
    raise "could not find measure to add to workflow #{local_path_to_measure}"
  end

  @items.last
end

#all_variablesArray

Return all the variables in the analysis as an array. The list that is returned is read only.

Returns:

  • (Array)

    All variables in the workflow



220
221
222
# File 'lib/openstudio/analysis/workflow.rb', line 220

def all_variables
  @items.map(&:variables).flatten
end

#clearObject

Remove all the items in the workflow



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

def clear
  @items.clear
end

#eachObject

Iterate over all the WorkflowItems



204
205
206
# File 'lib/openstudio/analysis/workflow.rb', line 204

def each
  @items.each { |i| yield i }
end

#find_measure(instance_name) ⇒ Object Also known as: find_workflow_step

Find the measure by its instance name

Returns:

  • (Object)

    The WorkflowStep with the instance_name



212
213
214
# File 'lib/openstudio/analysis/workflow.rb', line 212

def find_measure(instance_name)
  @items.find { |i| i.name == instance_name }
end

#to_hash(version = 1) ⇒ Object

Save the workflow to a hash object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/openstudio/analysis/workflow.rb', line 225

def to_hash(version = 1)
  h = nil
  if version == 1
    arr = []
    @items.each_with_index do |item, index|
      temp_h = item.to_hash(version)
      temp_h[:workflow_index] = index

      arr << temp_h
    end

    h = arr
  else
    raise "Version #{version} not yet implemented for to_hash"
  end

  h
end

#to_json(version = 1) ⇒ String

Save the workflow to a JSON string

Returns:

  • (String)

    JSON formatted string



247
248
249
250
251
252
253
# File 'lib/openstudio/analysis/workflow.rb', line 247

def to_json(version = 1)
  if version == 1
    JSON.pretty_generate(to_hash(version))
  else
    raise "Version #{version} not yet implemented for to_json"
  end
end