Class: ReportModelChanges

Inherits:
OpenStudio::Measure::ModelMeasure
  • Object
show all
Defined in:
lib/measures/ReportModelChanges/measure.rb

Overview

start the measure

Instance Method Summary collapse

Instance Method Details

#arguments(model) ⇒ Object

define the arguments that the user will input



24
25
26
27
28
29
30
31
32
33
# File 'lib/measures/ReportModelChanges/measure.rb', line 24

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

  # make an argument for your name
  compare_model_path = OpenStudio::Measure::OSArgument.makeStringArgument('compare_model_path', true)
  compare_model_path.setDisplayName('Path to model for comparison')
  args << compare_model_path

  return args
end

#nameObject

define the name that a user will see, this method may be deprecated as the display name in PAT comes from the name field in measure.xml



19
20
21
# File 'lib/measures/ReportModelChanges/measure.rb', line 19

def name
  return 'ReportModelChanges'
end

#run(model, runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
66
67
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
101
102
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/measures/ReportModelChanges/measure.rb', line 36

def run(model, runner, user_arguments)
  super(model, runner, user_arguments)

  # use the built-in error checking
  if !runner.validateUserArguments(arguments(model), user_arguments)
    return false
  end

  # assign the user inputs to variables
  compare_model_path = runner.getStringArgumentValue('compare_model_path', user_arguments)

  # load the model
  vt = OpenStudio::OSVersion::VersionTranslator.new
  compare_model = vt.loadModel(compare_model_path)
  if compare_model.empty?
    runner.registerError("Cannot load model from #{compare_model_path} for comparison.")
    return false
  end
  compare_model = compare_model.get

  only_model = []
  only_compare = []
  both = []
  diffs = []
  num_ignored = 0

  # loop through model and find objects in this model only or in both
  model.getModelObjects.each do |object|
    # TODO: compare these some other way
    if !object.iddObject.hasNameField
      num_ignored += 1
      next
    end

    compare_object = compare_model.getObjectByTypeAndName(object.iddObject.type, object.name.to_s)
    if compare_object.empty?
      only_model << object
    else
      both << [object, compare_object.get]
    end
  end

  # loop through model and find objects in comparison model only
  compare_model.getModelObjects.each do |compare_object|
    # TODO: compare these some other way
    if !compare_object.iddObject.hasNameField
      num_ignored += 1
      next
    end

    object = model.getObjectByTypeAndName(compare_object.iddObject.type, compare_object.name.to_s)
    if object.empty?
      only_compare << compare_object
    end
  end

  # loop through and perform the diffs
  both.each do |b|
    object = b[0]
    compare_object = b[1]
    idd_object = object.iddObject

    object_num_fields = object.numFields
    compare_num_fields = compare_object.numFields

    diff = "<table border='1'>\n"
    diff += "<tr style='font-weight:bold'><td>#{object.iddObject.name}</td><td/><td/></tr>\n"
    diff += "<tr style='font-weight:bold'><td>Model Object</td><td>Comparison Object</td><td>Field Name</td></tr>\n"

    # loop over fields skipping handle
    same = true
    (1...[object_num_fields, compare_num_fields].max).each do |i|
      field_name = idd_object.getField(i).get.name

      object_value = ''
      if i < object_num_fields
        object_value = object.getString(i).to_s
      end
      object_value = '-' if object_value.empty?

      compare_value = ''
      if i < compare_num_fields
        compare_value = compare_object.getString(i).to_s
      end
      compare_value = '-' if compare_value.empty?

      row_color = 'green'
      if object_value != compare_value
        same = false
        row_color = 'red'
      end

      diff += "<tr><td style='color:#{row_color}'>#{object_value}</td><td style='color:#{row_color}'>#{compare_value}</td><td>#{field_name}</td></tr>\n"
    end
    diff += "</table><p/><p/>\n"

    if !same
      diffs << diff
    end
  end

  # path for reports
  report_path = Dir.pwd + '/report.html'

  # write the report
  File.open(report_path, 'w') do |file|
    file << "<section>\n<h1>Objects Only In Model</h1>\n"
    file << "<table border='1'>\n"
    only_model.each do |object|
      file << "<tr><td style='white-space:pre'>#{object}</td></tr>\n"
    end
    file << "</table>\n"
    file << "</section>\n"

    file << "<section>\n<h1>Objects Only In Comparison Model</h1>\n"
    file << "<table border='1'>\n"
    only_compare.each do |object|
      file << "<tr><td style='white-space:pre'>#{object}</td></tr>\n"
    end
    file << "</table>\n"
    file << "</section>\n"

    file << "<section>\n<h1>Objects In Both Models With Differences</h1>\n"
    diffs.each do |diff|
      file << diff
    end
    file << "</section>\n"
  end
  runner.registerInfo("Report generated at: <a href='file:///#{report_path}'>#{report_path}</a>")

  runner.registerWarning("#{num_ignored} objects did not have names and were not compared")

  return true
end