urbanopt-reporting 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.rubocop.yml +10 -0
  4. data/CHANGELOG.md +7 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/Gemfile +18 -0
  7. data/Jenkinsfile +10 -0
  8. data/LICENSE.md +27 -0
  9. data/README.md +40 -0
  10. data/Rakefile +45 -0
  11. data/doc_templates/LICENSE.md +27 -0
  12. data/doc_templates/README.md.erb +42 -0
  13. data/doc_templates/copyright_erb.txt +31 -0
  14. data/doc_templates/copyright_js.txt +4 -0
  15. data/doc_templates/copyright_ruby.txt +29 -0
  16. data/lib/measures/.rubocop.yml +5 -0
  17. data/lib/measures/default_feature_reports/LICENSE.md +27 -0
  18. data/lib/measures/default_feature_reports/README.md +26 -0
  19. data/lib/measures/default_feature_reports/README.md.erb +42 -0
  20. data/lib/measures/default_feature_reports/measure.rb +1012 -0
  21. data/lib/measures/default_feature_reports/measure.xml +160 -0
  22. data/lib/urbanopt/reporting.rb +37 -0
  23. data/lib/urbanopt/reporting/default_reports.rb +44 -0
  24. data/lib/urbanopt/reporting/default_reports/construction_cost.rb +169 -0
  25. data/lib/urbanopt/reporting/default_reports/date.rb +97 -0
  26. data/lib/urbanopt/reporting/default_reports/distributed_generation.rb +379 -0
  27. data/lib/urbanopt/reporting/default_reports/end_use.rb +159 -0
  28. data/lib/urbanopt/reporting/default_reports/end_uses.rb +140 -0
  29. data/lib/urbanopt/reporting/default_reports/extension.rb +15 -0
  30. data/lib/urbanopt/reporting/default_reports/feature_report.rb +266 -0
  31. data/lib/urbanopt/reporting/default_reports/generator.rb +92 -0
  32. data/lib/urbanopt/reporting/default_reports/location.rb +99 -0
  33. data/lib/urbanopt/reporting/default_reports/logger.rb +44 -0
  34. data/lib/urbanopt/reporting/default_reports/power_distribution.rb +103 -0
  35. data/lib/urbanopt/reporting/default_reports/program.rb +265 -0
  36. data/lib/urbanopt/reporting/default_reports/reporting_period.rb +300 -0
  37. data/lib/urbanopt/reporting/default_reports/scenario_report.rb +317 -0
  38. data/lib/urbanopt/reporting/default_reports/schema/README.md +33 -0
  39. data/lib/urbanopt/reporting/default_reports/schema/scenario_csv_columns.txt +34 -0
  40. data/lib/urbanopt/reporting/default_reports/schema/scenario_schema.json +857 -0
  41. data/lib/urbanopt/reporting/default_reports/solar_pv.rb +93 -0
  42. data/lib/urbanopt/reporting/default_reports/storage.rb +105 -0
  43. data/lib/urbanopt/reporting/default_reports/timeseries_csv.rb +300 -0
  44. data/lib/urbanopt/reporting/default_reports/validator.rb +112 -0
  45. data/lib/urbanopt/reporting/default_reports/wind.rb +92 -0
  46. data/lib/urbanopt/reporting/derived_extension.rb +63 -0
  47. data/lib/urbanopt/reporting/version.rb +35 -0
  48. data/urbanopt-reporting-gem.gemspec +33 -0
  49. metadata +176 -0
@@ -0,0 +1,160 @@
1
+ <?xml version="1.0"?>
2
+ <measure>
3
+ <schema_version>3.0</schema_version>
4
+ <name>default_feature_reports</name>
5
+ <uid>9ee3135a-8070-4408-bfa1-b75fecf9dd4f</uid>
6
+ <version_id>95706c7a-88ab-433b-b897-00435181a6dd</version_id>
7
+ <version_modified>20200603T001228Z</version_modified>
8
+ <xml_checksum>FB304155</xml_checksum>
9
+ <class_name>DefaultFeatureReports</class_name>
10
+ <display_name>DefaultFeatureReports</display_name>
11
+ <description>Writes default_feature_reports.json and default_feature_reports.csv files used by URBANopt Scenario Default Post Processor</description>
12
+ <modeler_description>This measure only allows for one feature_report per simulation. If multiple features are simulated in a single simulation, a new measure must be written to disaggregate simulation results to multiple features.</modeler_description>
13
+ <arguments>
14
+ <argument>
15
+ <name>feature_id</name>
16
+ <display_name>Feature unique identifier</display_name>
17
+ <description></description>
18
+ <type>String</type>
19
+ <units></units>
20
+ <required>false</required>
21
+ <model_dependent>false</model_dependent>
22
+ <default_value>1</default_value>
23
+ <min_value></min_value>
24
+ <max_value></max_value>
25
+ </argument>
26
+ <argument>
27
+ <name>feature_name</name>
28
+ <display_name>Feature scenario specific name</display_name>
29
+ <description></description>
30
+ <type>String</type>
31
+ <units></units>
32
+ <required>false</required>
33
+ <model_dependent>false</model_dependent>
34
+ <default_value>name</default_value>
35
+ <min_value></min_value>
36
+ <max_value></max_value>
37
+ </argument>
38
+ <argument>
39
+ <name>feature_type</name>
40
+ <display_name>URBANopt Feature Type</display_name>
41
+ <description></description>
42
+ <type>String</type>
43
+ <units></units>
44
+ <required>false</required>
45
+ <model_dependent>false</model_dependent>
46
+ <default_value>Building</default_value>
47
+ <min_value></min_value>
48
+ <max_value></max_value>
49
+ </argument>
50
+ <argument>
51
+ <name>reporting_frequency</name>
52
+ <display_name>Reporting Frequency</display_name>
53
+ <description>The frequency at which to report timeseries output data.</description>
54
+ <type>Choice</type>
55
+ <units></units>
56
+ <required>true</required>
57
+ <model_dependent>false</model_dependent>
58
+ <default_value>Timestep</default_value>
59
+ <choices>
60
+ <choice>
61
+ <value>Detailed</value>
62
+ <display_name>Detailed</display_name>
63
+ </choice>
64
+ <choice>
65
+ <value>Timestep</value>
66
+ <display_name>Timestep</display_name>
67
+ </choice>
68
+ <choice>
69
+ <value>Hourly</value>
70
+ <display_name>Hourly</display_name>
71
+ </choice>
72
+ <choice>
73
+ <value>Daily</value>
74
+ <display_name>Daily</display_name>
75
+ </choice>
76
+ <choice>
77
+ <value>BillingPeriod</value>
78
+ <display_name>BillingPeriod</display_name>
79
+ </choice>
80
+ <choice>
81
+ <value>Monthly</value>
82
+ <display_name>Monthly</display_name>
83
+ </choice>
84
+ <choice>
85
+ <value>Runperiod</value>
86
+ <display_name>Runperiod</display_name>
87
+ </choice>
88
+ </choices>
89
+ <min_value></min_value>
90
+ <max_value></max_value>
91
+ </argument>
92
+ </arguments>
93
+ <outputs />
94
+ <provenances />
95
+ <tags>
96
+ <tag>Reporting.QAQC</tag>
97
+ </tags>
98
+ <attributes>
99
+ <attribute>
100
+ <name>Measure Type</name>
101
+ <value>ReportingMeasure</value>
102
+ <datatype>string</datatype>
103
+ </attribute>
104
+ <attribute>
105
+ <name>Intended Use Case</name>
106
+ <value>Automatic Report Generation</value>
107
+ <datatype>string</datatype>
108
+ </attribute>
109
+ </attributes>
110
+ <files>
111
+ <file>
112
+ <filename>USA_CO_Golden-NREL.724666_TMY3.epw</filename>
113
+ <filetype>epw</filetype>
114
+ <usage_type>test</usage_type>
115
+ <checksum>BDF687C1</checksum>
116
+ </file>
117
+ <file>
118
+ <filename>README.md.erb</filename>
119
+ <filetype>erb</filetype>
120
+ <usage_type>readmeerb</usage_type>
121
+ <checksum>703C9964</checksum>
122
+ </file>
123
+ <file>
124
+ <filename>example_model.osm</filename>
125
+ <filetype>osm</filetype>
126
+ <usage_type>test</usage_type>
127
+ <checksum>CC4BFFAF</checksum>
128
+ </file>
129
+ <file>
130
+ <filename>LICENSE.md</filename>
131
+ <filetype>md</filetype>
132
+ <usage_type>license</usage_type>
133
+ <checksum>EA283B74</checksum>
134
+ </file>
135
+ <file>
136
+ <filename>default_feature_reports_test.rb</filename>
137
+ <filetype>rb</filetype>
138
+ <usage_type>test</usage_type>
139
+ <checksum>56E0804B</checksum>
140
+ </file>
141
+ <file>
142
+ <version>
143
+ <software_program>OpenStudio</software_program>
144
+ <identifier>1.11.0</identifier>
145
+ <min_compatible>1.11.0</min_compatible>
146
+ </version>
147
+ <filename>measure.rb</filename>
148
+ <filetype>rb</filetype>
149
+ <usage_type>script</usage_type>
150
+ <checksum>A00C8077</checksum>
151
+ </file>
152
+ <file>
153
+ <filename>README.md</filename>
154
+ <filetype>md</filetype>
155
+ <usage_type>readme</usage_type>
156
+ <checksum>0B68E96D</checksum>
157
+ </file>
158
+ </files>
159
+ </measure>
160
+ <error>wrong number of arguments (given 1, expected 0)</error>
@@ -0,0 +1,37 @@
1
+ # *********************************************************************************
2
+ # URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other
3
+ # contributors. All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright notice, this list
9
+ # of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above copyright notice, this
12
+ # list of conditions and the following disclaimer in the documentation and/or other
13
+ # materials provided with the distribution.
14
+ #
15
+ # Neither the name of the copyright holder nor the names of its contributors may be
16
+ # used to endorse or promote products derived from this software without specific
17
+ # prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ # OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ # *********************************************************************************
30
+
31
+ ##
32
+ # Retrieve all default_reports classes.
33
+ ##
34
+
35
+ require 'urbanopt/reporting/default_reports'
36
+ require 'urbanopt/reporting/derived_extension'
37
+ require 'urbanopt/reporting/version'
@@ -0,0 +1,44 @@
1
+ # *********************************************************************************
2
+ # URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other
3
+ # contributors. All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright notice, this list
9
+ # of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above copyright notice, this
12
+ # list of conditions and the following disclaimer in the documentation and/or other
13
+ # materials provided with the distribution.
14
+ #
15
+ # Neither the name of the copyright holder nor the names of its contributors may be
16
+ # used to endorse or promote products derived from this software without specific
17
+ # prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ # OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ # *********************************************************************************
30
+
31
+ ##
32
+ # Retrieve all default_reports classes.
33
+ ##
34
+
35
+ require 'urbanopt/reporting/default_reports/scenario_report'
36
+ require 'urbanopt/reporting/default_reports/feature_report'
37
+ require 'urbanopt/reporting/default_reports/construction_cost'
38
+ require 'urbanopt/reporting/default_reports/program'
39
+ require 'urbanopt/reporting/default_reports/reporting_period'
40
+ require 'urbanopt/reporting/default_reports/timeseries_csv'
41
+ require 'urbanopt/reporting/default_reports/distributed_generation'
42
+ require 'urbanopt/reporting/default_reports/power_distribution'
43
+ require 'urbanopt/reporting/default_reports/logger'
44
+ require 'urbanopt/reporting/default_reports/validator'
@@ -0,0 +1,169 @@
1
+ # *********************************************************************************
2
+ # URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other
3
+ # contributors. All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright notice, this list
9
+ # of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above copyright notice, this
12
+ # list of conditions and the following disclaimer in the documentation and/or other
13
+ # materials provided with the distribution.
14
+ #
15
+ # Neither the name of the copyright holder nor the names of its contributors may be
16
+ # used to endorse or promote products derived from this software without specific
17
+ # prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ # OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ # *********************************************************************************
30
+
31
+ require 'json'
32
+ require_relative 'validator'
33
+ require 'json-schema'
34
+
35
+ module URBANopt
36
+ module Reporting
37
+ module DefaultReports
38
+ ##
39
+ # ConstructionCost include construction cost information.
40
+ ##
41
+ class ConstructionCost
42
+ attr_accessor :category, :item_name, :unit_cost, :cost_units, :item_quantity, :total_cost # :nodoc:
43
+ ##
44
+ # ConstructionCost class intialize all construction_cost attributes:
45
+ # +:category+ , +:item_name+ , +:unit_cost+ , +:cost_units+ , +:item_quantity+ , +:total_cost+
46
+ ##
47
+ # [parameters:]
48
+ # +hash+ - _Hash_ - A hash which may contain a deserialized construction_cost.
49
+ ##
50
+ def initialize(hash = {})
51
+ hash.delete_if { |k, v| v.nil? }
52
+ hash = defaults.merge(hash)
53
+
54
+ @category = hash[:category]
55
+ @item_name = hash[:item_name]
56
+ @unit_cost = hash[:unit_cost]
57
+ @cost_units = hash[:cost_units]
58
+ @item_quantity = hash[:item_quantity]
59
+ @total_cost = hash[:total_cost]
60
+
61
+ # initialize class variables @@validator and @@schema
62
+ @@validator ||= Validator.new
63
+ @@schema ||= @@validator.schema
64
+ end
65
+
66
+ ##
67
+ # Assigns default values if attribute values do not exist.
68
+ ##
69
+ def defaults
70
+ hash = {}
71
+ hash[:category] = nil
72
+ hash[:item_name] = nil
73
+ hash[:unit_cost] = nil
74
+ hash[:cost_units] = nil
75
+ hash[:item_quantity] = nil
76
+ hash[:total_cost] = nil
77
+
78
+ return hash
79
+ end
80
+
81
+ ##
82
+ # Converts to a Hash equivalent for JSON serialization.
83
+ ##
84
+ # - Exclude attributes with nil values.
85
+ # - Validate construct_cost hash properties against schema.
86
+ ##
87
+ def to_hash
88
+ result = {}
89
+ result[:category] = @category if @category
90
+ result[:item_name] = @item_name if @item_name
91
+ result[:unit_cost] = @unit_cost if @unit_cost
92
+ result[:cost_units] = @cost_units if @cost_units
93
+ result[:item_quantity] = @item_quantity if @item_quantity
94
+ result[:total_cost] = @total_cost if @total_cost
95
+
96
+ # validate construct_cost properties against schema
97
+ if @@validator.validate(@@schema[:definitions][:ConstructionCost][:properties], result).any?
98
+ raise "construction_cost properties does not match schema: #{@@validator.validate(@@schema[:definitions][:ConstructionCost][:properties], result)}"
99
+ end
100
+
101
+ return result
102
+ end
103
+
104
+ ##
105
+ # Merges an +existing_cost+ with a +new_cost+:
106
+ # - modify the existing_cost by summing the +:total_cost+ and +:item_quantity+ of new_cost and existing_cost.
107
+ # - raise an error if +:category+ , +:cost_units+ and +:unit_cost+ are not identical
108
+ ##
109
+ # [Parameters:]
110
+ # +existing_cost+ - _ConstructionCost_ - An object of ConstructionCost class.
111
+ ##
112
+ # +new_cost+ - _ConstructionCost_ - An object of ConstructionCost class.
113
+ ##
114
+ def self.merge_construction_cost(existing_cost, new_cost)
115
+ # modify the existing_cost by adding the :total_cost and :item_quantity
116
+ existing_cost.total_cost += new_cost.total_cost
117
+ existing_cost.item_quantity += new_cost.item_quantity
118
+
119
+ if existing_cost.category != new_cost.category
120
+ raise "Cannot merge existing cost of category \"#{existing_cost.category}\" with new cost of category \"#{new_cost.category}\"."
121
+ end
122
+
123
+ if existing_cost.cost_units != new_cost.cost_units
124
+ raise "Cannot merge existing cost with cost units \"#{existing_cost.cost_units}\" with new cost with cost units \"#{new_cost.cost_units}\". "
125
+ end
126
+
127
+ if existing_cost.unit_cost != new_cost.unit_cost
128
+ raise "Cannot merge existing cost with unit cost \"#{existing_cost.unit_cost}\" with new cost with unit cost \"#{new_cost.unit_cost}\"; identical items should have identical unit cost."
129
+ end
130
+
131
+ return existing_cost
132
+ end
133
+
134
+ ##
135
+ # Merges muliple construction costs together.
136
+ # - loops over the new_costs and find the index of the cost with identical +:item_name+.
137
+ # - if +item_name+ is identical then modify the existing_cost array by summing the :total_cost and :item_quantity. Else add the new_cost to existing_costs array.
138
+ ##
139
+ # [Parameters:]
140
+ # +existing_costs+ - _Array_ - An array of ConstructionCost objects.
141
+ ##
142
+ # +new_costs+ - _Array_ - An array of ConstructionCost objects.
143
+ def self.merge_construction_costs(existing_costs, new_costs)
144
+ item_name_list = []
145
+ item_name_list = existing_costs.collect(&:item_name)
146
+
147
+ new_costs.each do |x_new|
148
+ if item_name_list.include?(x_new.item_name)
149
+
150
+ # when looping over the new_cost item_names find the index of the item_name_list with the same item name
151
+ id = item_name_list.find_index(x_new.item_name) # the order of the item_name_list is the same as the order of the existing_cost hash-array
152
+
153
+ # modify the existing_cost array by adding the :total_cost and :item_quantity when looping over costs
154
+ existing_costs[id] = merge_construction_cost(existing_costs[id], x_new)
155
+
156
+ else
157
+
158
+ # insert the new hash in to the array
159
+ existing_costs << x_new
160
+
161
+ end
162
+ end
163
+
164
+ return existing_costs
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,97 @@
1
+ # *********************************************************************************
2
+ # URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other
3
+ # contributors. All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without modification,
6
+ # are permitted provided that the following conditions are met:
7
+ #
8
+ # Redistributions of source code must retain the above copyright notice, this list
9
+ # of conditions and the following disclaimer.
10
+ #
11
+ # Redistributions in binary form must reproduce the above copyright notice, this
12
+ # list of conditions and the following disclaimer in the documentation and/or other
13
+ # materials provided with the distribution.
14
+ #
15
+ # Neither the name of the copyright holder nor the names of its contributors may be
16
+ # used to endorse or promote products derived from this software without specific
17
+ # prior written permission.
18
+ #
19
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ # OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ # *********************************************************************************
30
+
31
+ require_relative 'validator'
32
+ require 'json-schema'
33
+ require 'json'
34
+
35
+ module URBANopt
36
+ module Reporting
37
+ module DefaultReports
38
+ ##
39
+ # Date class include information of simulation run date.
40
+ ##
41
+ class Date
42
+ attr_accessor :month, :day_of_month, :year #:nodoc:
43
+ ##
44
+ # Date class intialize all date attributes:
45
+ # +:month+ , +:day_of_month+ , +:year+
46
+ ##
47
+ # [parameters:]
48
+ # +hash+ - _Hash_ - A hash which may contain a deserialized date.
49
+ ##
50
+ def initialize(hash = {})
51
+ hash.delete_if { |k, v| v.nil? }
52
+ hash = defaults.merge(hash)
53
+
54
+ @month = hash[:month].to_i
55
+ @day_of_month = hash[:day_of_month].to_i
56
+ @year = hash[:year].to_i
57
+
58
+ # initialize class variables @@validator and @@schema
59
+ @@validator ||= Validator.new
60
+ @@schema ||= @@validator.schema
61
+ end
62
+
63
+ ##
64
+ # Converts to a hash equivalent for JSON serialization.
65
+ ##
66
+ # - Exclude attributes with nil values.
67
+ # - Validate date properties against schema.
68
+ ##
69
+ def to_hash
70
+ result = {}
71
+ result[:month] = @month if @month
72
+ result[:day_of_month] = @day_of_month if @day_of_month
73
+ result[:year] = @year if @year
74
+
75
+ # validate date hash properties against schema
76
+ if @@validator.validate(@@schema[:definitions][:Date][:properties], result).any?
77
+ raise "end_uses properties does not match schema: #{@@validator.validate(@@schema[:definitions][:Date][:properties], result)}"
78
+ end
79
+
80
+ return result
81
+ end
82
+
83
+ ##
84
+ # Assigns default values if values do not exist.
85
+ ##
86
+ def defaults
87
+ hash = {}
88
+ hash[:month] = nil
89
+ hash[:day_of_month] = nil
90
+ hash[:year] = nil
91
+
92
+ return hash
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end