Class: WorkflowJSON_Shim

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

Overview

WorkflowJSON_Shim provides a shim interface to the WorkflowJSON class in OpenStudio 2.X when running in OpenStudio 1.X

Instance Method Summary collapse

Constructor Details

#initialize(workflow, osw_dir) ⇒ WorkflowJSON_Shim

Returns a new instance of WorkflowJSON_Shim.



178
179
180
181
182
# File 'lib/openstudio/workflow_json.rb', line 178

def initialize(workflow, osw_dir)
  @workflow = workflow
  @osw_dir = osw_dir
  @current_step_index = 0
end

Instance Method Details

#absoluteFilePathsObject



300
301
302
303
304
305
306
# File 'lib/openstudio/workflow_json.rb', line 300

def absoluteFilePaths
  result = OpenStudio::PathVector.new
  filePaths.each do |file_path|
    result << OpenStudio.toPath(File.absolute_path(file_path.to_s, rootDir.to_s))
  end
  result
end

#absoluteMeasurePathsObject



350
351
352
353
354
355
356
# File 'lib/openstudio/workflow_json.rb', line 350

def absoluteMeasurePaths
  result = OpenStudio::PathVector.new
  measurePaths.each do |measure_path|
    result << OpenStudio.toPath(File.absolute_path(measure_path.to_s, rootDir.to_s))
  end
  result
end

#absoluteOutPathObject



277
278
279
# File 'lib/openstudio/workflow_json.rb', line 277

def absoluteOutPath
  OpenStudio.toPath(File.absolute_path(outPath.to_s, oswDir.to_s))
end

#absoluteRootDirObject



250
251
252
# File 'lib/openstudio/workflow_json.rb', line 250

def absoluteRootDir
  OpenStudio.toPath(File.absolute_path(rootDir.to_s, @osw_dir.to_s))
end

#absoluteRunDirObject



265
266
267
# File 'lib/openstudio/workflow_json.rb', line 265

def absoluteRunDir
  OpenStudio.toPath(File.absolute_path(runDir.to_s, rootDir.to_s))
end

#currentStepObject

Get the current step. boost::optional<WorkflowStep> currentStep() const;



217
218
219
220
221
222
223
224
225
# File 'lib/openstudio/workflow_json.rb', line 217

def currentStep
  steps = @workflow[:steps]

  step = nil
  if @current_step_index < steps.size
    step = WorkflowStep_Shim.new(steps[@current_step_index])
  end
  return Optional_Shim.new(step)
end

#currentStepIndexObject

Get the current step index.



211
212
213
# File 'lib/openstudio/workflow_json.rb', line 211

def currentStepIndex
  @current_step_index
end

#filePathsObject

Returns the paths that will be searched in order for files, default value is ‘./files/’. Evaluated relative to rootDir if not absolute. std::vector<openstudio::path> filePaths() const; std::vector<openstudio::path> absoluteFilePaths() const;



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/openstudio/workflow_json.rb', line 284

def filePaths
  result = OpenStudio::PathVector.new
  if @workflow[:file_paths]
    @workflow[:file_paths].each do |file_path|
      result << OpenStudio.toPath(file_path)
    end
  else
    result << OpenStudio.toPath('./files')
    result << OpenStudio.toPath('./weather')
    result << OpenStudio.toPath('../../files')
    result << OpenStudio.toPath('../../weather')
    result << OpenStudio.toPath('./')
  end
  result
end

#findFile(file) ⇒ Object

Attempts to find a file by name, searches through filePaths in order and returns first match. boost::optional<openstudio::path> findFile(const openstudio::path& file); boost::optional<openstudio::path> findFile(const std::string& fileName);



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/openstudio/workflow_json.rb', line 311

def findFile(file)
  file = file.to_s

  # check if absolute and exists
  if Pathname.new(file).absolute?
    if File.exist?(file)
      return OpenStudio::OptionalPath.new(OpenStudio.toPath(file))
    end

    # absolute path does not exist
    return OpenStudio::OptionalPath.new
  end

  absoluteFilePaths.each do |file_path|
    result = File.join(file_path.to_s, file)
    if File.exist?(result)
      return OpenStudio::OptionalPath.new(OpenStudio.toPath(result))
    end
  end
  OpenStudio::OptionalPath.new
end

#findMeasure(measureDir) ⇒ Object

Attempts to find a measure by name, searches through measurePaths in order and returns first match. */ boost::optional<openstudio::path> findMeasure(const openstudio::path& measureDir); boost::optional<openstudio::path> findMeasure(const std::string& measureDirName);



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/openstudio/workflow_json.rb', line 361

def findMeasure(measureDir)
  measureDir = measureDir.to_s

  # check if absolute and exists
  if Pathname.new(measureDir).absolute?
    if File.exist?(measureDir)
      return OpenStudio::OptionalPath.new(OpenStudio.toPath(measureDir))
    end

    # absolute path does not exist
    return OpenStudio::OptionalPath.new
  end

  absoluteMeasurePaths.each do |measure_path|
    result = File.join(measure_path.to_s, measureDir)
    if File.exist?(result)
      return OpenStudio::OptionalPath.new(OpenStudio.toPath(result))
    end
  end
  OpenStudio::OptionalPath.new
end

#incrementStepObject

Increments current step, returns true if there is another step. bool incrementStep();



229
230
231
232
233
234
235
236
237
# File 'lib/openstudio/workflow_json.rb', line 229

def incrementStep
  @current_step_index += 1

  if @current_step_index < @workflow[:steps].size
    return true
  end

  return false
end

#measurePathsObject

Returns the paths that will be searched in order for measures, default value is ‘./measures/’. Evaluated relative to rootDir if not absolute. std::vector<openstudio::path> measurePaths() const; std::vector<openstudio::path> absoluteMeasurePaths() const;



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/openstudio/workflow_json.rb', line 336

def measurePaths
  result = OpenStudio::PathVector.new
  if @workflow[:measure_paths]
    @workflow[:measure_paths].each do |measure_path|
      result << OpenStudio.toPath(measure_path)
    end
  else
    result << OpenStudio.toPath('./measures')
    result << OpenStudio.toPath('../../measures')
    result << OpenStudio.toPath('./')
  end
  result
end

#oswDirObject

Returns the absolute path to the directory this workflow was loaded from or saved to. Returns current working dir for new WorkflowJSON. openstudio::path oswDir() const;



195
196
197
# File 'lib/openstudio/workflow_json.rb', line 195

def oswDir
  OpenStudio.toPath(@osw_dir)
end

#outPathObject



269
270
271
272
273
274
275
# File 'lib/openstudio/workflow_json.rb', line 269

def outPath
  if @workflow[:out_name]
    OpenStudio.toPath(@workflow[:out_name])
  else
    OpenStudio.toPath('./out.osw')
  end
end

#rootDirObject

Returns the root directory, default value is ‘.’. Evaluated relative to oswDir if not absolute. openstudio::path rootDir() const; openstudio::path absoluteRootDir() const;



242
243
244
245
246
247
248
# File 'lib/openstudio/workflow_json.rb', line 242

def rootDir
  if @workflow[:root_dir]
    OpenStudio.toPath(@workflow[:root_dir])
  else
    OpenStudio.toPath(@osw_dir)
  end
end

#runDirObject

Returns the run directory, default value is ‘./run’. Evaluated relative to rootDir if not absolute. openstudio::path runDir() const; openstudio::path absoluteRunDir() const;



257
258
259
260
261
262
263
# File 'lib/openstudio/workflow_json.rb', line 257

def runDir
  if @workflow[:run_directory]
    OpenStudio.toPath(@workflow[:run_directory])
  else
    OpenStudio.toPath('./run')
  end
end

#runOptionsObject

return empty optional



423
424
425
# File 'lib/openstudio/workflow_json.rb', line 423

def runOptions
  return Optional_Shim.new(nil)
end

#saveAs(path) ⇒ Object



199
200
201
202
203
# File 'lib/openstudio/workflow_json.rb', line 199

def saveAs(path)
  File.open(path.to_s, 'w') do |file|
    file << JSON.pretty_generate(@workflow)
  end
end

#seedFileObject

Returns the seed file path. Evaluated relative to filePaths if not absolute. boost::optional<openstudio::path> seedFile() const;



385
386
387
388
389
390
391
# File 'lib/openstudio/workflow_json.rb', line 385

def seedFile
  result = OpenStudio::OptionalPath.new
  if @workflow[:seed_file]
    result = OpenStudio::OptionalPath.new(OpenStudio.toPath(@workflow[:seed_file]))
  end
  result
end

#setCompletedStatus(status) ⇒ Object



413
414
415
416
# File 'lib/openstudio/workflow_json.rb', line 413

def setCompletedStatus(status)
  @workflow[:completed_status] = status
  @workflow[:completed_at] = timeString
end

#setEplusoutErr(eplusout_err) ⇒ Object



418
419
420
# File 'lib/openstudio/workflow_json.rb', line 418

def setEplusoutErr(eplusout_err)
  @workflow[:eplusout_err] = eplusout_err
end

#startObject

Sets the started at time.



206
207
208
# File 'lib/openstudio/workflow_json.rb', line 206

def start
  @workflow[:started_at] = timeString
end

#stringObject

std::string string(bool includeHash=true) const;



185
186
187
# File 'lib/openstudio/workflow_json.rb', line 185

def string
  JSON.fast_generate(@workflow)
end

#timeStringObject



189
190
191
# File 'lib/openstudio/workflow_json.rb', line 189

def timeString
  ::Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
end

#weatherFileObject

Returns the weather file path. Evaluated relative to filePaths if not absolute. boost::optional<openstudio::path> weatherFile() const;



395
396
397
398
399
400
401
# File 'lib/openstudio/workflow_json.rb', line 395

def weatherFile
  result = OpenStudio::OptionalPath.new
  if @workflow[:weather_file]
    result = OpenStudio::OptionalPath.new(OpenStudio.toPath(@workflow[:weather_file]))
  end
  result
end

#workflowStepsObject

Returns the workflow steps. */ std::vector<WorkflowStep> workflowSteps() const;



405
406
407
408
409
410
411
# File 'lib/openstudio/workflow_json.rb', line 405

def workflowSteps
  result = []
  @workflow[:steps].each do |step|
    result << WorkflowStep_Shim.new(step)
  end
  result
end