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.



174
175
176
177
178
# File 'lib/openstudio/workflow_json.rb', line 174

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

Instance Method Details

#absoluteFilePathsObject



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

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



364
365
366
367
368
369
370
# File 'lib/openstudio/workflow_json.rb', line 364

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



280
281
282
# File 'lib/openstudio/workflow_json.rb', line 280

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

#absoluteRootDirObject



253
254
255
# File 'lib/openstudio/workflow_json.rb', line 253

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

#absoluteRunDirObject



268
269
270
# File 'lib/openstudio/workflow_json.rb', line 268

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

#addFilePath(path) ⇒ Object



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

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

#completedStatusObject



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

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;



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

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.



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

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;



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/openstudio/workflow_json.rb', line 287

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



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/openstudio/workflow_json.rb', line 325

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



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/openstudio/workflow_json.rb', line 375

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



231
232
233
234
235
236
237
238
239
240
# File 'lib/openstudio/workflow_json.rb', line 231

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;



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/openstudio/workflow_json.rb', line 350

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;



191
192
193
# File 'lib/openstudio/workflow_json.rb', line 191

def oswDir
  OpenStudio.toPath(@osw_dir)
end

#outPathObject



272
273
274
275
276
277
278
# File 'lib/openstudio/workflow_json.rb', line 272

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

#resetFilePathsObject



318
319
320
# File 'lib/openstudio/workflow_json.rb', line 318

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;



245
246
247
248
249
250
251
# File 'lib/openstudio/workflow_json.rb', line 245

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;



260
261
262
263
264
265
266
# File 'lib/openstudio/workflow_json.rb', line 260

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

#runOptionsObject

return empty optional



445
446
447
# File 'lib/openstudio/workflow_json.rb', line 445

def runOptions
  return Optional_Shim.new(nil)
end

#saveAs(path) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/openstudio/workflow_json.rb', line 195

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;



399
400
401
402
403
404
405
# File 'lib/openstudio/workflow_json.rb', line 399

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



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

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

#setEplusoutErr(eplusout_err) ⇒ Object



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

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

#startObject

Sets the started at time.



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

def start
  @workflow[:started_at] = timeString
end

#stringObject

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



181
182
183
# File 'lib/openstudio/workflow_json.rb', line 181

def string
  JSON.fast_generate(@workflow)
end

#timeStringObject



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

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;



409
410
411
412
413
414
415
# File 'lib/openstudio/workflow_json.rb', line 409

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;



419
420
421
422
423
424
425
# File 'lib/openstudio/workflow_json.rb', line 419

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