/
data_dictionary.rb
236 lines (211 loc) · 8.15 KB
/
data_dictionary.rb
1
2
3
4
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require 'json'
# Simple class that creates the data dictionary from the BuildingSync.xsd file
class DataDictionary
def add_spaces(str)
retval = str.dup
regex = /(.*?[^ (])([A-Z][a-z])(.*?)/
retval.gsub!(regex, '\1 \2\3') while retval.match(regex)
regex = /(.*?)([a-z])([A-Z])(.*?)/
retval.gsub!(regex, '\1\2 \3\4') while retval.match(regex)
# Custom fixes
retval.gsub!('e GRID', 'eGRID')
retval.gsub!('EC Ms', 'ECMs')
retval
end
def generate
xsd = File.expand_path("#{File.dirname(__FILE__)}/../BuildingSync.xsd")
@schema = Nokogiri::XML(IO.read(xsd), &:noblanks)
# @schema.remove_namespaces!
# puts @schema.css('element')
# puts @schema.xpath('//')
# elems = @schema.search 'element[name="Audits"]'
# puts elems.length
# elems.each_with_index { |elem, index|
# puts elem if index == 0
# }
workbook = RubyXL::Workbook.new # Create workbook
# #######################
# # Project Data Fields #
# #######################
# project = workbook[0]
# row = col = 0
# project.sheet_name = 'Project Data Fields' # Sheet 1
# project.merge_cells(row, col, row, 4) # Merges A1:F1
# project.add_cell(row, col, 'BuildingSync Data Dictionary, ' + Time.now.strftime('%m/%d/%Y')) # Title
# project[row][col].change_horizontal_alignment('center')
# project.change_row_bold(row, true)
# project.change_row_font_name(row, 'Calibri')
# project.change_row_font_size(row, 24)
# project.change_row_height(row, 42)
# project.change_row_vertical_alignment(row, 'top')
#
# # Headers
# row += 1
# headers = ['Category', 'Field', 'Description', 'Type', 'Units/Pattern']
# widths = [15.14, 37.29, 67.86, 11, 17.86]
# headers.map do |header|
# project.add_cell(row, col, header)
# project[row][col].change_horizontal_alignment('center')
# project.change_column_width(col, widths[col])
# col += 1
# end
# project.change_row_fill(row, '4f81bd')
# project.change_row_font_color(row, 'ffffff')
# project.change_row_bold(row, true)
# project.change_row_font_name(row, 'Calibri')
# project.change_row_font_size(row, 14)
#
# categories = ['Site', 'Customer', 'Facilities']
#
# #######################
# # Systems Data Fields #
# #######################
# systems = workbook.add_worksheet('Systems Data Fields') # Sheet 2
#
# # Headers
# row = col = 0
# headers = ['Category', 'Field', 'Description', 'Type', 'Units/Pattern', 'Comment']
# widths = [16.57, 38.71, 66.29, 14.57, 19.86, 11.29]
# headers.map do |header|
# systems.add_cell(row, col, header)
# systems.sheet_data[row][col].change_horizontal_alignment('center')
# systems.change_column_width(col, widths[col])
# col += 1
# end
# systems.change_row_fill(row, '4f81bd')
# systems.change_row_font_color(row, 'ffffff')
# systems.change_row_bold(row, true)
# systems.change_row_font_name(row, 'Calibri')
# systems.change_row_font_size(row, 14)
#
# ########################
# # Measures Data Fields #
# ########################
# measures = workbook.add_worksheet('Measures Data Fields') # Sheet 3
# children = @schema.xpath('/xs:schema/xs:complexType[@name="MeasureType"]/xs:sequence').children
#
# # Headers
# row = col = 0
# headers = ['Category', 'Field', 'Description', 'Type', 'Units/Pattern']
# widths = [16.14, 46.14, 63.29, 15.14, 19.43]
# headers.map do |header|
# measures.add_cell(row, col, header)
# measures[row][col].change_horizontal_alignment('center')
# measures.change_column_width(col, widths[col])
# col += 1
# end
# measures.change_row_fill(row, '4f81bd')
# measures.change_row_font_color(row, 'ffffff')
# measures.change_row_bold(row, true)
# measures.change_row_font_name(row, 'Calibri')
# measures.change_row_font_size(row, 14)
#
# #########################
# # Reporting Data Fields #
# #########################
# reporting = workbook.add_worksheet('Reporting Data Fields') # Sheet 4
#
# # Headers
# row = col = 0
# headers = ['Category', 'Field', 'Description', 'Type', 'Units/Pattern']
# widths = [18.29, 29.57, 74.71, 13.43, 21.71]
# headers.map do |header|
# reporting.add_cell(row, col, header)
# reporting.sheet_data[row][col].change_horizontal_alignment('center')
# reporting.change_column_width(col, widths[col])
# col += 1
# end
# reporting.change_row_fill(row, '4f81bd')
# reporting.change_row_font_color(row, 'ffffff')
# reporting.change_row_bold(row, true)
# reporting.change_row_font_name(row, 'Calibri')
# reporting.change_row_font_size(row, 14)
######################
# Final Enumerations #
######################
enumerations = workbook.add_worksheet('Final Enumerations') # Sheet 5
enums = {}
new_enums = []
names = []
@schema.xpath('//xs:simpleType/xs:restriction').map do |node|
data = {}
name = node.parent.attribute('name')
name = (name || node.parent.parent.attribute('name')) if name.nil?
name = name.value
data[:name] = name
data[:sub_name] = nil
doc_string = node.parent.parent.xpath('.//xs:annotation/xs:documentation').first
puts name
if name == 'ClimateZone' ||
name == 'LampLabel' ||
name == 'MeasureName' ||
name == 'AssemblyType' # Handle duplicate enum names
# If measure name, then grab the documentation from the parents
if name == 'MeasureName'
doc_string = node.parent.parent.parent.parent.parent.xpath('.//xs:annotation/xs:documentation').first
end
if sub_name = node.parent.parent.parent.parent.parent.attribute('name')
data[:sub_name] = sub_name.value
name += " (#{sub_name.value})"
end
end
data[:documentation] = doc_string ? doc_string.content : nil
next unless node.children.length
names.push(name)
dupes = names.detect { |e| names.count(e) > 1 }
raise 'Error: duplicate enum detected: ' + name unless dupes.nil?
arr = []
node.children.map do |child|
value = child.attribute('value').value
arr.push(value)
dupes = arr.detect { |e| arr.count(e) > 1 }
raise "Error: duplicate enum value detected: '#{value}' in '#{name}'" unless dupes.nil?
end
enums[add_spaces(name)] = arr
data[:enumerations] = arr
data[:enumerations_html] = "<ul><li>#{arr.join("</li><li>")}</li></ul>"
new_enums << data
end
col = 0
enums = enums.sort { |a, b| a[0].downcase <=> b[0].downcase }
enumerations.change_row_fill(0, '4f81bd')
enumerations.change_row_font_color(0, 'ffffff')
enumerations.change_row_bold(0, true)
enumerations.change_row_font_name(0, 'Calibri')
enumerations.change_row_font_size(0, 11)
max_row = 0
enums.map do |enum, values|
row = 0
enumerations.add_cell(row, col, enum)
values.each do |value|
row += 1
enumerations.add_cell(row, col, value)
next unless max_row < row
enumerations.change_row_fill(row, 'dce6f1')
enumerations.change_row_font_name(row, 'Calibri')
enumerations.change_row_font_size(row, 11)
max_row = row
end
col += 1
end
# puts @schema.xpath('/xs:schema').children.length
font = RubyXL::Font.new
font.set_name('Calibri')
font.set_size(11)
FileUtils.mkdir_p 'docs'
workbook.save('docs/DataDictionary.xlsx') # Save
# also save as JSON
# sore the new_data hash
# NOTE If both `name` and `sub_name` attributes are given, then concatenate
# the string literals.
# NOTE Use `Enumerable#sort_by!` instance method for in-place sorting via a
# Schwartzian transform (for improved performance).
new_enums.sort_by! { |hash| [hash[:name], hash[:sub_name]].flatten.join('$') }
pp new_enums
File.open('docs/enumerations.json', 'w') { |f| f << JSON.pretty_generate(new_enums) }
# Copy to website
if Dir.exist? '../website/_data'
File.open('../website/_data/enumerations.json', 'w') { |f| f << JSON.pretty_generate(new_enums) }
end
end
end