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.



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

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

Instance Method Details

#absoluteFilePathsObject



333
334
335
336
337
338
339
# File 'lib/openstudio/workflow_json.rb', line 333

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



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

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



310
311
312
# File 'lib/openstudio/workflow_json.rb', line 310

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

#absoluteRootDirObject



283
284
285
# File 'lib/openstudio/workflow_json.rb', line 283

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

#absoluteRunDirObject



298
299
300
# File 'lib/openstudio/workflow_json.rb', line 298

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

#addFilePath(path) ⇒ Object



341
342
343
344
345
346
# File 'lib/openstudio/workflow_json.rb', line 341

def addFilePath(path)
  if !@workflow[:file_paths]
    @workflow[:file_paths] = []
  end
  @workflow[:file_paths] << path
end

#completedStatusObject



457
458
459
460
461
462
463
# File 'lib/openstudio/workflow_json.rb', line 457

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;



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

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.



243
244
245
# File 'lib/openstudio/workflow_json.rb', line 243

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;



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

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



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/openstudio/workflow_json.rb', line 355

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



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/openstudio/workflow_json.rb', line 405

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



261
262
263
264
265
266
267
268
269
270
# File 'lib/openstudio/workflow_json.rb', line 261

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;



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/openstudio/workflow_json.rb', line 380

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;



221
222
223
# File 'lib/openstudio/workflow_json.rb', line 221

def oswDir
  OpenStudio.toPath(@osw_dir)
end

#outPathObject



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

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

#resetFilePathsObject



348
349
350
# File 'lib/openstudio/workflow_json.rb', line 348

def resetFilePaths
  @workflow[:file_paths] = []
end

#rootDirObject

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



275
276
277
278
279
280
281
# File 'lib/openstudio/workflow_json.rb', line 275

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;



290
291
292
293
294
295
296
# File 'lib/openstudio/workflow_json.rb', line 290

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

#runOptionsObject

return empty optional



475
476
477
# File 'lib/openstudio/workflow_json.rb', line 475

def runOptions
  return Optional_Shim.new(nil)
end

#saveAs(path) ⇒ Object



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

def saveAs(path)
  File.open(path.to_s, 'w') do |f|
    f << JSON.pretty_generate(@workflow)
    # make sure data is written to the disk one way or the other
    begin
      f.fsync
    rescue StandardError
      f.flush
    end
  end
end

#seedFileObject

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



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

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



465
466
467
468
# File 'lib/openstudio/workflow_json.rb', line 465

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

#setEplusoutErr(eplusout_err) ⇒ Object



470
471
472
# File 'lib/openstudio/workflow_json.rb', line 470

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

#startObject

Sets the started at time.



238
239
240
# File 'lib/openstudio/workflow_json.rb', line 238

def start
  @workflow[:started_at] = timeString
end

#stringObject

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



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

def string
  JSON.fast_generate(@workflow)
end

#timeStringObject



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

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;



439
440
441
442
443
444
445
# File 'lib/openstudio/workflow_json.rb', line 439

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;



449
450
451
452
453
454
455
# File 'lib/openstudio/workflow_json.rb', line 449

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