Module: BTAPMeasureHelper

Included in:
BTAPModelMeasure
Defined in:
lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb

Instance Method Summary collapse

Instance Method Details

#arguments(model) ⇒ Object

define the arguments that the user will input



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb', line 5

def arguments(model)
  args = OpenStudio::Measure::OSArgumentVector.new

  if @use_json_package == true
    # Set up package version of input.
    json_default = {}
    @measure_interface_detailed.each do |argument|
      json_default[argument['name']] = argument['default_value']
    end
    default = JSON.pretty_generate(json_default)
    arg = OpenStudio::Ruleset::OSArgument.makeStringArgument('json_input', true)
    arg.setDisplayName('Contains a json version of the input as a single package.')
    arg.setDefaultValue(default)
    args << arg
  else
    # Conductances for all surfaces and subsurfaces.
    @measure_interface_detailed.each do |argument|
      arg = nil
      statement = nil
      case argument['type']
        when 'String'
          arg = OpenStudio::Ruleset::OSArgument.makeStringArgument(argument['name'], argument['is_required'])
          arg.setDisplayName(argument['display_name'])
          arg.setDefaultValue(argument['default_value'].to_s)

        when 'Double'
          arg = OpenStudio::Ruleset::OSArgument.makeDoubleArgument(argument['name'], argument['is_required'])
          arg.setDisplayName((argument['display_name']).to_s)
          arg.setDefaultValue((argument['default_value']).to_s.to_f)

        when 'Integer'
          arg = OpenStudio::Ruleset::OSArgument.makeIntegerArgument(argument['name'], argument['is_required'])
          arg.setDisplayName((argument['display_name']).to_s)
          arg.setDefaultValue((argument['default_value']).to_s.to_i)

        when 'Choice'
          arg = OpenStudio::Measure::OSArgument.makeChoiceArgument(argument['name'], argument['choices'], argument['is_required'])
          arg.setDisplayName(argument['display_name'])
          arg.setDefaultValue(argument['default_value'].to_s)
          puts arg.defaultValueAsString

        when 'Bool'
          arg = OpenStudio::Measure::OSArgument.makeBoolArgument(argument['name'], argument['is_required'])
          arg.setDisplayName(argument['display_name'])
          arg.setDefaultValue(argument['default_value'])

        when 'StringDouble'
          if @use_string_double == false
            arg = OpenStudio::Ruleset::OSArgument.makeDoubleArgument(argument['name'], argument['is_required'])
            arg.setDefaultValue(argument['default_value'].to_f)
          else
            arg = OpenStudio::Ruleset::OSArgument.makeStringArgument(argument['name'], argument['is_required'])
            arg.setDefaultValue(argument['default_value'].to_s)
          end
          arg.setDisplayName(argument['display_name'])
      end
      args << arg
    end
  end
  return args
end

#get_hash_of_arguments(user_arguments, runner) ⇒ Object

returns a hash of the user inputs for you to use in your measure.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb', line 68

def get_hash_of_arguments(user_arguments, runner)
  values = {}
  if @use_json_package
    return JSON.parse(runner.getStringArgumentValue('json_input', user_arguments))
  else

    @measure_interface_detailed.each do |argument|
      case argument['type']
        when 'String', 'Choice'
          values[argument['name']] = runner.getStringArgumentValue(argument['name'], user_arguments)
        when 'Double'
          values[argument['name']] = runner.getDoubleArgumentValue(argument['name'], user_arguments)
        when 'Integer'
          values[argument['name']] = runner.getIntegerArgumentValue(argument['name'], user_arguments)
        when 'Bool'
          values[argument['name']] = runner.getBoolArgumentValue(argument['name'], user_arguments)
        when 'StringDouble'
          value = nil
          if @use_string_double == false
            value = runner.getDoubleArgumentValue(argument['name'], user_arguments).to_f
          else
            value = runner.getStringArgumentValue(argument['name'], user_arguments)
            if valid_float?(value)
              value = value.to_f
            end
          end
          values[argument['name']] = value
      end
    end
  end

  return values
end

#valid_float?(str) ⇒ Boolean

Helper method to see if str is a valid float.

Returns:

  • (Boolean)


153
154
155
156
157
# File 'lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb', line 153

def valid_float?(str)
  !!Float(str)
rescue StandardError
  false
end

#validate_and_get_arguments_in_hash(model, runner, user_arguments) ⇒ Object

boilerplate that validated ranges of inputs.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/openstudio-standards/utilities/template_measure/resources/BTAPMeasureHelper.rb', line 103

def validate_and_get_arguments_in_hash(model, runner, user_arguments)
  return_value = true
  values = get_hash_of_arguments(user_arguments, runner)
  # use the built-in error checking
  if !runner.validateUserArguments(arguments(model), user_arguments)
    runner_register(runner, 'Error', 'validateUserArguments failed... Check the argument definition for errors.')
    return_value = false
  end

  # Validate arguments
  errors = ''
  @measure_interface_detailed.each do |argument|
    case argument['type']
      when 'Double'
        value = values[argument['name']]
        if (!argument['max_double_value'].nil? && (value.to_f > argument['max_double_value'].to_f)) ||
           (!argument['min_double_value'].nil? && (value.to_f < argument['min_double_value'].to_f))
          error = "#{argument['name']} must be between #{argument['min_double_value']} and #{argument['max_double_value']}. You entered #{value.to_f} for this #{argument['name']}.\n Please enter a value withing the expected range.\n"
          errors << error
        end

      when 'Integer'
        value = values[argument['name']]
        if (!argument['max_integer_value'].nil? && (value.to_i > argument['max_integer_value'].to_i)) ||
           (!argument['min_integer_value'].nil? && (value.to_i < argument['min_integer_value'].to_i))
          error = "#{argument['name']} must be between #{argument['min_integer_value']} and #{argument['max_integer_value']}. You entered #{value.to_i} for this #{argument['name']}.\n Please enter a value withing the expected range.\n"
          errors << error
        end

      when 'StringDouble'
        value = values[argument['name']]
        if !argument['valid_strings'].include?(value) && !valid_float?(value)
          error = "#{argument['name']} must be a string that can be converted to a float, or one of these #{argument['valid_strings']}. You have entered #{value}\n"
          errors << error
        elsif (!argument['max_double_value'].nil? && (value.to_f > argument['max_double_value'])) ||
              (!argument['min_double_value'].nil? && (value.to_f < argument['min_double_value']))
          error = "#{argument['name']} must be between #{argument['min_double_value']} and #{argument['max_double_value']}. You entered #{value} for #{argument['name']}. Please enter a stringdouble value in the expected range.\n"
          errors << error
        end
    end
  end
  # If any errors return false, else return the hash of argument values for user to use in measure.
  if errors != ''
    runner.registerError(errors)
    return false
  end
  return values
end