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.



186
187
188
189
190
# File 'lib/openstudio/workflow_json.rb', line 186

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

Instance Method Details

#absoluteFilePathsObject



309
310
311
312
313
314
315
# File 'lib/openstudio/workflow_json.rb', line 309

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



359
360
361
362
363
364
365
# File 'lib/openstudio/workflow_json.rb', line 359

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



286
287
288
# File 'lib/openstudio/workflow_json.rb', line 286

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

#absoluteRootDirObject



259
260
261
# File 'lib/openstudio/workflow_json.rb', line 259

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

#absoluteRunDirObject



274
275
276
# File 'lib/openstudio/workflow_json.rb', line 274

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

#completedStatusObject



422
423
424
425
426
427
428
# File 'lib/openstudio/workflow_json.rb', line 422

def completedStatus
  if @workflow[:completed_status]
    Optional_Shim.new(@workflow[:completed_status])
  else
    Optional_Shim.new(nil)
  end
end

#currentStepObject

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



225
226
227
228
229
230
231
232
233
# File 'lib/openstudio/workflow_json.rb', line 225

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.



219
220
221
# File 'lib/openstudio/workflow_json.rb', line 219

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;



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/openstudio/workflow_json.rb', line 293

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);



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/openstudio/workflow_json.rb', line 320

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);



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/openstudio/workflow_json.rb', line 370

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();



237
238
239
240
241
242
243
244
245
246
# File 'lib/openstudio/workflow_json.rb', line 237

def incrementStep
  @current_step_index += 1
  @workflow[:current_step] = @current_step_index

  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;



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/openstudio/workflow_json.rb', line 345

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;



203
204
205
# File 'lib/openstudio/workflow_json.rb', line 203

def oswDir
  OpenStudio.toPath(@osw_dir)
end

#outPathObject



278
279
280
281
282
283
284
# File 'lib/openstudio/workflow_json.rb', line 278

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;



251
252
253
254
255
256
257
# File 'lib/openstudio/workflow_json.rb', line 251

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;



266
267
268
269
270
271
272
# File 'lib/openstudio/workflow_json.rb', line 266

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

#runOptionsObject

return empty optional



440
441
442
# File 'lib/openstudio/workflow_json.rb', line 440

def runOptions
  return Optional_Shim.new(nil)
end

#saveAs(path) ⇒ Object



207
208
209
210
211
# File 'lib/openstudio/workflow_json.rb', line 207

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;



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

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



430
431
432
433
# File 'lib/openstudio/workflow_json.rb', line 430

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

#setEplusoutErr(eplusout_err) ⇒ Object



435
436
437
# File 'lib/openstudio/workflow_json.rb', line 435

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

#startObject

Sets the started at time.



214
215
216
# File 'lib/openstudio/workflow_json.rb', line 214

def start
  @workflow[:started_at] = timeString
end

#stringObject

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



193
194
195
# File 'lib/openstudio/workflow_json.rb', line 193

def string
  JSON.fast_generate(@workflow)
end

#timeStringObject



197
198
199
# File 'lib/openstudio/workflow_json.rb', line 197

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;



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

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;



414
415
416
417
418
419
420
# File 'lib/openstudio/workflow_json.rb', line 414

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