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

OS Measure - Building Story

asked 2015-10-15 10:32:53 -0600

updated 2015-10-15 10:37:48 -0600

All; Thanks in advance for your assistance. I am a novice when it comes writing both with Ruby and OS Measures, but I am running head-long into it. So, this is really a two part question. First, I am working/playing with writing a measure to rename spaces based on the Building Story, but am having issues. The segment of code that is giving me fits is this: #get model objects spaces=model.getSpaces story=model.getBuidlingStorys

#reporting initial condition of model
runner.registerInitialCondition("The building has #{spaces.size} spaces" )

spaces.each do |space|
  #story name, space name
  story_name = space.getBuildingStory
  base_name = "#{story_name} - #{space_name}"
end #end of spaces.each do

Namely, OS does not like the " story_name = space.getBuildingStory" line. I have tried a few different iterations to get the story name based on the assigned space, but am coming up short. Any ideas?

This leads to the second part of my question. I have read/reviewed the 'Writing Measures' Guide and looked at the OpenStudio SDK, specifically the 'model' part of this documentation. It appears that this is all written with respect to C++/C# [guessing here?]. So, what is a good/best/better path to try to learn how to create Ruby scripts, but using this documentation reference? Is there some 'Rosetta Stone' that will help us novice mortals learn to use these scripts or the syntax? Reviewing Measures from the BCL is a path, and I have found it fairly straightforward to 'read' these files and follow what I believe is happening, but reading others scripts and drafting scripts are two different levels of competency. Thanks in advance for any help anyone can provide to either/both questions above.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
5

answered 2015-10-15 10:40:43 -0600

updated 2015-10-15 13:59:54 -0600

The method that you want is space.buildingStory, you can find that in the documentation for the Space object. You will want to do something like this:

#reporting initial condition of model
runner.registerInitialCondition("The building has #{spaces.size} spaces" )

spaces.each do |space|
  #story name, space name
  building_story= space.buildingStory # returns an optional story, might not be available so need to check below

  base_name = "" # needed so you can access this variable outside of the following if statement
  if !building_story.empty?
    building_story = building_story.get # dereference the OptionalBuildingStory to get the BuildingStory
    base_name = "#{building_story.name} - #{space.name}"
  end
end #end of spaces.each do
edit flag offensive delete link more

Comments

Follow on question, because there are still errors. It does not like the: base_name = "#{building_story.name} - #{space.name}" Resulting in an error of 'undefined method 'name'. If I remove this, it runs - but no space names change. I think there are 2 errors here. 1-something with getting the name of the building_story 2- I believe I need something like space.setName(base_name) to get the new name assigned to the spaces - correct? Thanks in advance.

dradair's avatar dradair  ( 2015-10-15 13:08:35 -0600 )edit
2

You need to add building_story = space.buildingStory.get after if !building_story.empty?. Without it, the 'building_story' object is an OpenStudio::Model::OptionalBuildingStory object that doesn't contain a method for name.

ericringold's avatar ericringold  ( 2015-10-15 13:17:05 -0600 )edit

Eric - Yes! I needed that as well as to add: space.setName(base_name) within the do loop to reassigned the new name to the spaces.

dradair's avatar dradair  ( 2015-10-15 13:33:51 -0600 )edit

Thanks Eric, that's what happens when I post code without running it first :-) Thanks for cleaning up my mistake (I'll update the code in the answer)

macumber's avatar macumber  ( 2015-10-15 13:58:42 -0600 )edit
4

answered 2015-10-15 12:12:18 -0600

For the second part of your question - I've found that learning to read and navigate the SDK documentation is really key for a non-programmer to get comfortable with reading and writing measure. I recommend first re-reading the 'Run' section of the Measure Writing guide, especially the sub-sections 'What Methods are Available?' and 'Understanding the Methods' (and probably reading them again after that). Some other tips:

  • From a given class reference page, say Space Class, clicking 'Inheritance diagram for openstudio::model::Space' will show all the classes whose methods the Space class inherits: image description

  • Similarly, clicking 'List of all members' at the top of the page will show a list of all the methods, inherited or native, that are available for a particular class. In that list, the method name is shown on the left, the method input type immediately follow in parentheses, and the method output type follows that. On the right side is the class that the method originates from. image description

    This opens up a lot of possibilities for navigating around and manipulating model objects.

  • If you need refreshing on the basics of programming or the syntax of Ruby, there are lots of good tutorials and resources online. I found the books 'Computer Science Programming Basics in Ruby' (basic stuff, specific to Ruby) and 'Programming Ruby' (more detailed) to be of great help. And as always, when in doubt, searching for "ruby [whatever I want to do]" on Google usually turns up a helpful StackExchange or other answer.

  • Finally, read and write lots of measures. Edit them, break 'em and fix 'em, find snippets that might be useful and do something different with them. Search the many OpenStudio-related Github repositories for code (Github search lets you choose by language, so you can filter to just see Ruby results) to see how a method or class is used, in addition to measures on the BCL. If you come across something you don't know, use the above resources to figure it out. It takes a little time to get comfortable, but it's time well spent when you can harness the power that measures give you.

edit flag offensive delete link more

Comments

Eric; Thanks for the information and guidance. I have done some programming, but I am an engineer by training & trade, so this is whole new world to figure out. We'll see how this all goes... EDIT - Question - do you use an IDE for writing scripts or....what is the IDE us use?

dradair's avatar dradair  ( 2015-10-15 13:28:03 -0600 )edit
1

I'm in the same boat, and yeah it can seem daunting, but I really think the benefits make it worth it. I don't use an IDE, just a text editor. Notepad++ is pretty good, but I've been using Atom more recently. I think the OS dev team use RubyMine; see David Goldwasser's great answer for more about that.

ericringold's avatar ericringold  ( 2015-10-15 14:01:57 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2015-10-15 10:32:53 -0600

Seen: 397 times

Last updated: Oct 15 '15