OpenStudio 3.2.1 - trouble with object creation in measure
I am writing a measure to mass-assign materials to constructions and constructions to default construction sets. The following bit of code produces a new DefaultSurfaceConstructions with option = true, but not with option = false.
if option then
exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)
exterior_surface_constructions = exterior_surface_constructions.setName(exterior_surface_constructions_name)
surface_constructions = model.getDefaultSurfaceConstructionss()
surface_constructions.each do |surface_construction|
runner.registerInfo("Default Surface Construction = #{surface_construction.name()}")
end
exterior_surface_constructions = surface_constructions[-1]
end
if not option then
exterior_surface_constructions = model.getDefaultSurfaceConstructionsByName(exterior_surface_constructions_name)
runner.registerInfo("ESC after ByName: #{exterior_surface_constructions}")
if exterior_surface_constructions.is_initialized then
exterior_surface_constructions = exterior_surface_constructions.get
runner.registerInfo("ESC after Get: #{exterior_surface_constructions}")
else
exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)
exterior_surface_constructions = exterior_surface_constructions.setName(exterior_surface_constructions_name)
runner.registerInfo("ESC after new and name: #{exterior_surface_constructions}")
end
end
I find that I am having to take a similar detour to generate a new DefaultConstructionSet, also. It seems that the option = false approach would be the more correct one as it first checks if the object already exists, and it works with other objects in OpenStudio 3.2.1, but not with these two?
I don't have an answer, but here's the Ruby Style Guide in case you haven't seen it.
With the option = false, if it does find an object of that type with the correct name, it just gets an existing object and isn’t expected to produce a new object. How are you determine that an object by that name isn’t in the model. It would also be useful to test it with both an expect name of an existing object, and a name that doesn’t exist in the model, that will exercise both paths of the false code.
Well, I figure "exterior_surface_constructions.is_initialized" would evaluate to "true" if "exterior_surface_constructions = model.getDefaultSurfaceConstructionsByName(exterior_surface_constructions_name)" had indeed found an existing object in the model. In that case, I would just "get" it for subsequent use. If there was no such object in the model, I figure "exterior_surface_constructions.is_initialized" would evaluate to "false". In that case, "exterior_surface_constructions = OpenStudio::Model::DefaultSurfaceConstructions.new(model)" simply creates a new one for subsequent use?
Thanks Matthew. I had not seen that style guide yet. I think I am still at the "function over form" stage in my learning, but I am getting better. In fact, briefly reviewing the guide, I don't think I am all that far off?
@mattkoch. So there isn’t an ruby error at a specific line, so the code runs to completion. Based on output of the info statements is it finding a matching object, or is it claiming to make a new object (but you’d an’t find the then object?); or are you looping through this many times, expecting both to happen. What if you do a
puts exterior_surface_constructions
right after you make the new object, what does it return.