Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've been playing around with RubyMine for some time now to write and test an EnergyPlus measure. I ran into some problems, hopefully my findings can help some other users.

The MiniTest that came with ruby in Openstudio gave me some errors, I was able to fix by inheriting from MiniTest::Unit::TestCase, i.e.:

class TestMeasure_Test < MiniTest::Unit::TestCase
#class TestMeasure_Test < MiniTest::Test # wrong

Every run needs to have the -I option supplied, I got tired of this and fixed by adding an absolute path in the require:

require 'C:\Program Files (x86)\OpenStudio 1.5.0\Ruby\openstudio'
require 'C:\Program Files (x86)\OpenStudio 1.5.0\Ruby\openstudio\ruleset\ShowRunnerOutput'

In an effort to debug the code, I couldn't find anything working. At least not in combination with MiniTest, and minitest/debugger kept failing in every way. I decided to write plain Ruby code without the minitest to see if my measure works for my EnergyPlus measure. This is all based on samples provided above and in the documentation. I left some lines commented out in case someone wants to convert it back to an OpenStudio measure. Not clean code, but it might help some people out.

require 'C:\Program Files (x86)\OpenStudio 1.5.0\Ruby\openstudio'
require 'C:\Program Files (x86)\OpenStudio 1.5.0\Ruby\openstudio\ruleset\ShowRunnerOutput'
require_relative '../measure.rb'

# Input parameters
osmfile = "file.osm"

# create an instance of the measure
measure = TestMeasure.new

path = OpenStudio::Path.new(File.dirname(__FILE__) + "/" + osmfile)
# load the test model
# translator = OpenStudio::OSVersion::VersionTranslator.new
#model = translator.loadModel(path)
#if model.empty?
#  raise("Model not loaded correctly")
#end
#model = model.get

# load the workspace, for EnergyPlus measure
workspace = OpenStudio::Workspace::load(path,"EnergyPlus".to_IddFileType)
raise "Unable to load OpenStudio Workspace from '" + input_path.to_s + "'." if workspace.empty?
workspace = workspace.get

# Get arguments and set to valid values
arguments = measure.arguments(workspace)
argument_map = OpenStudio::Ruleset.convertOSArgumentVectorToMap(arguments)
# Set valid value
zone_name = arguments[0].clone
zone_name.setValue("New Zone")
argument_map["zone_name"] = zone_name

# create an instance of a runner
runner = OpenStudio::Ruleset::OSRunner.new

# run the measure
# measure.run(model, runner, argument_map)
measure.run(workspace, runner, argument_map)
result = runner.result
show_output(result)
#assert(result.value.valueName == "Success")

#save the model
output_dir = File.expand_path('output', File.dirname(__FILE__))
#FileUtils.mkdir output_dir unless Dir.exist? output_dir
#output_file_path = OpenStudio::Path.new("#{output_dir}/test_imported_idf_model_results.osm")
#model.save(output_file_path,true)
output_file_path = OpenStudio::Path.new("#{output_dir}/output")
workspace.save(output_file_path,true)