-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathepJSONTranslator.cpp
More file actions
507 lines (411 loc) · 19.9 KB
/
epJSONTranslator.cpp
File metadata and controls
507 lines (411 loc) · 19.9 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
* See also https://openstudio.net/license
***********************************************************************************************************************/
#include "epJSONTranslator.hpp"
#include "../utilities/core/Assert.hpp"
#include "../utilities/core/Logger.hpp"
#include "../utilities/idd/IddEnums.hpp"
#include "../utilities/idf/IdfFile.hpp"
#include "../utilities/idf/Workspace.hpp"
#include "../utilities/idf/WorkspaceObject.hpp"
#include "../utilities/idf/IdfExtensibleGroup.hpp"
#include "../utilities/idf/WorkspaceExtensibleGroup.hpp"
#include "../utilities/core/ApplicationPathHelpers.hpp"
#include <utilities/idd/IddEnums.hxx>
#include <json/json.h>
#include <fmt/format.h>
#include <vector>
#include <string_view>
namespace openstudio::epJSON {
template <typename... KeyType>
const Json::Value& safeLookupValue(const Json::Value& base, const KeyType&... keys) {
const Json::Value* obj = &base;
const Json::Value* last_found = &base;
const Json::Value* last_null = nullptr;
const auto lookupKey = [&obj, &last_found, &last_null](const auto& key) -> void {
if (!obj->isNull() && obj->isObject()) {
const Json::Value& next_obj = (*obj)[key];
if (!next_obj.isNull()) {
last_found = &next_obj;
} else {
last_null = &next_obj;
}
obj = &next_obj;
}
};
(lookupKey(keys), ...);
if (last_null != nullptr) {
return *last_null;
}
OS_ASSERT(last_found);
return *last_found;
}
enum class JSONValueType
{
Number,
Integer,
String,
Array,
Object,
NumberOrString
};
const std::string& toJSONFieldName(std::map<std::string, std::string>& fieldNames, const std::string& fieldNameInput) {
if (const auto& cached_value = fieldNames.find(fieldNameInput); cached_value != fieldNames.end()) {
return cached_value->second;
}
auto fieldName = boost::to_lower_copy(fieldNameInput);
boost::replace_all(fieldName, " ", "_");
boost::replace_all(fieldName, ".", "_");
boost::replace_all(fieldName, "%", "");
boost::replace_all(fieldName, "*", "_");
boost::replace_all(fieldName, "**", "_");
boost::replace_all(fieldName, "-", "_");
boost::replace_all(fieldName, "/", "_");
boost::replace_all(fieldName, "_", "_");
boost::replace_all(fieldName, "(", "");
boost::replace_all(fieldName, ")", "_");
boost::replace_all(fieldName, "__", "_");
boost::replace_all(fieldName, ":", "_");
const auto cache_value = [&](const auto& input, const auto& value) -> const auto& { return fieldNames.emplace(input, value).first->second; };
using namespace std::literals::string_view_literals;
static constexpr std::array<std::pair<std::string_view, std::string_view>, 13> manualFixPairs{{
{"poissons_ratio"sv, "poisson_s_ratio"sv},
{"youngs_modulus"sv, "young_s_modulus"sv},
{"g_function_lnt_ts_value"sv, "g_function_ln_t_ts_value"sv},
// E+ 22.2.0 introduced fields in coils like `2017 Rated Evaporator Fan Power per volume Flow Rate`
// We CANNOT leave a field starting with `<number> xxxx` in the IDD, because an enum (or a variable) in C++ cannot start with a number
// so a manual fix here is needed
{"rated_evaporator_fan_power_per_volume_flow_rate_2017", "2017_rated_evaporator_fan_power_per_volume_flow_rate"},
{"rated_evaporator_fan_power_per_volume_flow_rate_2023", "2023_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_1_rated_evaporator_fan_power_per_volume_flow_rate_2017", "2017_speed_1_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_2_rated_evaporator_fan_power_per_volume_flow_rate_2017", "2017_speed_2_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_3_rated_evaporator_fan_power_per_volume_flow_rate_2017", "2017_speed_3_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_4_rated_evaporator_fan_power_per_volume_flow_rate_2017", "2017_speed_4_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_1_rated_evaporator_fan_power_per_volume_flow_rate_2023", "2023_speed_1_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_2_rated_evaporator_fan_power_per_volume_flow_rate_2023", "2023_speed_2_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_3_rated_evaporator_fan_power_per_volume_flow_rate_2023", "2023_speed_3_rated_evaporator_fan_power_per_volume_flow_rate"},
{"speed_4_rated_evaporator_fan_power_per_volume_flow_rate_2023", "2023_speed_4_rated_evaporator_fan_power_per_volume_flow_rate"},
}};
if (auto it = std::find_if(manualFixPairs.cbegin(), manualFixPairs.cend(), [&fieldName](const auto& p) { return p.first == fieldName; });
it != manualFixPairs.cend()) {
return cache_value(fieldNameInput, it->second);
}
return cache_value(fieldNameInput, fieldName);
}
/** Locate the properties for a given object:
* schema root > properties > [type_description] (ObjectName) > patternProperties > "^.*\\S.*$" > properties */
const Json::Value& getSchemaObjectProperties(const Json::Value& schema, const std::string& type_description) {
const auto& patternProperties = safeLookupValue(schema, "properties", type_description, "patternProperties");
if (!patternProperties.isObject()) {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "Unable to find epJSON schema object for patternProperties for " << type_description);
return patternProperties;
}
return safeLookupValue(patternProperties, patternProperties.getMemberNames()[0], "properties");
}
const Json::Value& getSchemaFieldNames(const Json::Value& schema, const std::string& type_description) {
return safeLookupValue(schema, "properties", type_description, "legacy_idd", "fields");
}
JSONValueType schemaPropertyTypeDecode(const Json::Value& type) {
if (type.isNull() || !type.isString()) {
// LOG_FREE(LogLevel::Warn, "epJSONTranslator", "Unknown value passed to schemaPropertyTypeDecode, returning generic 'NumberOrString' Option");
return JSONValueType::NumberOrString;
}
if (type.asString() == "string") {
return JSONValueType::String;
} else if (type.asString() == "number") {
return JSONValueType::Number;
} else if (type.asString() == "integer") {
return JSONValueType::Integer;
} else if (type.asString() == "object") {
return JSONValueType::Object;
} else if (type.asString() == "array") {
return JSONValueType::Array;
}
return JSONValueType::NumberOrString;
}
/** Locate the properties for a given field of an object.
* - group_name can be empty, or it's the name of the extensible group.
* - field_name is the name of the field
* - field_property_name is something like 'type', 'anyOf', or 'enum'
*
* if group_name is empty:
* getSchemaObjectProperties > [field_name] > [field_property_name]
* else:
* getSchemaObjectProperties > [group_name] > items > properties > [field_name] > [field_property_name]
*/
const Json::Value& getSchemaObjectFieldProperty(const Json::Value& schema, const std::string& type_description, const std::string& group_name,
const std::string& field_name, const std::string& field_property_name) {
const auto& objectProperties = getSchemaObjectProperties(schema, type_description);
if (group_name.empty()) {
return safeLookupValue(objectProperties, field_name, field_property_name);
} else {
return safeLookupValue(objectProperties, group_name, "items", "properties", field_name, field_property_name);
}
}
/** Find the 'type' property of a field and convert that to enum.*/
JSONValueType getSchemaObjectFieldPropertyType(const Json::Value& schema, const std::string& type_description, const std::string& group_name,
const std::string& field_name) {
JSONValueType type = schemaPropertyTypeDecode(getSchemaObjectFieldProperty(schema, type_description, group_name, field_name, "type"));
if (type == JSONValueType::NumberOrString) {
LOG_FREE(LogLevel::Warn, "epJSONTranslator",
"Unknown value passed to schemaPropertyTypeDecode, returning generic 'NumberOrString' Option. "
<< "Occurred for type_description= " << type_description << ", group_name=" << group_name << ", field_name=" << field_name);
}
return type;
}
/** epJSON (unlike IDF) is case sensitive, so this routine find the correct 'enum' choice casing
* It applies to fieldType = 'ChoiceType' or 'RealType' (since RealType can also be `anyOf` with values like 'Autosize' 'Autocalculate'))
* eg: if given value='autosize', will convert it to 'Autosize' so that EnergyPlus' InputParser does recognize it */
std::string fixupEnumerationValue(const Json::Value& schema, const std::string& value, const std::string& type_description,
const std::string& group_name, const std::string& field_name, const openstudio::IddFieldType fieldType) {
if (fieldType == openstudio::IddFieldType::ChoiceType) {
const auto& fieldProperty = getSchemaObjectFieldProperty(schema, type_description, group_name, field_name, "enum");
const auto lower = boost::to_lower_copy(value);
if (fieldProperty.isNull()) {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "Unable to find enum value for " << value << " in " << group_name << "::" << field_name)
return value;
}
for (const auto& enumOption : fieldProperty) {
if (enumOption.isString()) {
const auto& enumStr = enumOption.asString();
if (boost::to_lower_copy(enumStr) == lower) {
return enumStr;
}
}
}
// value wasn't found, so return passed-in value
return value;
}
if (fieldType == openstudio::IddFieldType::RealType) {
const auto& fieldProperty = getSchemaObjectFieldProperty(schema, type_description, group_name, field_name, "anyOf");
if (fieldProperty.isArray()) {
const auto lower = boost::to_lower_copy(value);
for (const auto& possibleValues : fieldProperty) {
const auto& enumOptions = possibleValues["enum"];
if (enumOptions.isArray()) {
for (const auto& enumOption : enumOptions) {
if (enumOption.isString()) {
const auto& enumStr = enumOption.asString();
const auto lowerEnumStr = boost::to_lower_copy(enumStr);
if (lowerEnumStr == lower) {
return enumStr;
}
if (lowerEnumStr.find("auto") == 0 && lower.find("auto") == 0) {
// it's the "auto" option, return it
return enumStr;
}
}
}
}
}
}
// value wasn't found, so return passed-in value
return value;
}
// Not a real or enumeration type, return value
return value;
}
/* Find name of an extensible group and cache it */
auto getGroupName(std::map<std::pair<std::string, std::string>, std::pair<std::string, bool>>& group_names,
std::map<std::string, std::string>& /*field_names*/, const Json::Value& schema, const std::string& type_description,
const std::string& group_name) -> const auto& {
const auto key = std::pair{type_description, group_name};
if (const auto cached = group_names.find(key); cached != group_names.end()) {
return cached->second;
}
const auto cache_result = [&](const auto& name, const bool is_array) -> const auto& {
return group_names.emplace(key, std::pair{name, is_array}).first->second;
};
const auto& objectProperties = getSchemaObjectProperties(schema, type_description);
for (const auto& propertyName : objectProperties.getMemberNames()) {
const auto& type = safeLookupValue(objectProperties, propertyName, "type");
if (type.isString()) {
if (type.asString() == "array") {
return cache_result(propertyName, true);
}
}
}
// group name is irrelevant if it's not an array group
return cache_result("", false);
}
openstudio::path defaultSchemaPath(openstudio::IddFileType filetype) {
openstudio::path schemaPath;
if (filetype == openstudio::IddFileType::EnergyPlus) {
schemaPath = openstudio::getEnergyPlusDirectory() / openstudio::toPath("Energy+.schema.epJSON");
} else {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "At the moment, only IddFileType::EnergyPlus is supported");
// Later: use OpenStudio.epJSON
}
return schemaPath;
}
Json::Value loadJSON(const openstudio::path& path) {
Json::Value root;
std::ifstream ifs;
ifs.open(openstudio::toString(path));
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
[[maybe_unused]] bool success = Json::parseFromStream(builder, ifs, &root, &errs);
// todo handle errors here
return root;
}
std::string getFieldName(const bool is_array, const IddObject& iddObject, const Json::Value& schema, const std::string& type_description,
const std::size_t group_number, const std::size_t field_number, std::string_view field_name) {
if (is_array) {
return std::string{field_name};
}
// Legacy IDD field names
const auto& fieldNames = getSchemaFieldNames(schema, type_description);
// use the index of the field inside of the IddObject to look up what its name should be
// inside of the epJSON schema
//
// This is (partially) necessary because OpenStudio treats all groups as extensible.
const auto& lookedUpFieldName =
fieldNames[static_cast<int>((group_number - 1) * iddObject.extensibleGroup().size() + field_number + iddObject.nonextensibleFields().size())];
LOG_FREE(LogLevel::Error, "epJSONTranslator", "Unable to look up field name for input field" << field_name)
OS_ASSERT(lookedUpFieldName.isString());
return lookedUpFieldName.asString();
}
Json::Value toJSON(const openstudio::IdfFile& idf, const openstudio::path& schemaPath) {
openstudio::path schemaToLoad = schemaPath;
if (schemaToLoad.empty()) {
schemaToLoad = defaultSchemaPath(idf.iddFileType());
if (schemaToLoad.empty()) {
return Json::Value::null;
}
}
std::map<std::string, int> type_counts;
Json::Value result;
std::map<std::pair<std::string, std::string>, std::pair<std::string, bool>> group_names;
std::map<std::string, std::string> field_names;
Json::Value schema = loadJSON(schemaToLoad);
if (schema.isNull()) {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "Schema is invalid at path=" << schemaToLoad);
return Json::Value::null;
}
result["Version"]["Version 1"]["version_identifier"] = fmt::format("{}.{}", idf.version().major(), idf.version().minor());
for (const auto& obj : idf.objects()) {
if (obj.iddObject().type().value() == openstudio::IddObjectType::CommentOnly) {
// we aren't translating comments it seems
continue;
}
const auto& type_description = obj.iddObject().type().valueDescription();
const auto& name = obj.name();
const auto& defaultedName = obj.nameString(true);
auto& json_group = result[type_description];
const bool is_fluid_properties_name = type_description.find("FluidProperties:Name") != std::string::npos;
const bool is_lcc_use_price_escalation = type_description.find("LifeCycleCost:UsePriceEscalation") != std::string::npos;
const auto& usable_json_object_name = [&]() {
if (name && !is_fluid_properties_name) {
return *name;
} else {
if (!defaultedName.empty() && !is_fluid_properties_name) {
return defaultedName;
} else {
return fmt::format("{} {}", type_description, ++type_counts[type_description]);
}
}
}();
auto& json_object = json_group[usable_json_object_name];
json_object = Json::Value(Json::objectValue);
if (name) {
if (is_fluid_properties_name) {
json_object["fluid_name"] = *name;
} else if (is_lcc_use_price_escalation) {
json_object["lcc_price_escalation_name"] = *name;
}
}
const auto visitField = [&schema, &type_description](auto&& visitor, const openstudio::IddField& iddField, const std::string& group_name,
const auto& fieldName, const auto& field, const auto idx) -> bool {
const auto jsonFieldType = getSchemaObjectFieldPropertyType(schema, type_description, group_name, fieldName);
switch (jsonFieldType) {
case JSONValueType::String: {
const auto fieldString = field.getString(idx);
if (fieldString && !fieldString->empty()) {
visitor(fixupEnumerationValue(schema, *fieldString, type_description, group_name, fieldName, iddField.properties().type));
return true;
}
}
case JSONValueType::Integer: {
const auto fieldInt = field.getInt(idx);
if (fieldInt) {
visitor(*fieldInt);
return true;
}
}
case JSONValueType::Number:
case JSONValueType::NumberOrString: {
const auto fieldDouble = field.getDouble(idx);
if (fieldDouble) {
const auto fieldInt = field.getInt(idx);
if (fieldInt && static_cast<double>(*fieldInt) == *fieldDouble) {
if (iddField.name().find("Number") != std::string::npos) {
visitor(*fieldInt);
return true;
}
}
visitor(*fieldDouble);
return true;
}
}
case JSONValueType::Array:
case JSONValueType::Object:
break;
}
{
const auto fieldString = field.getString(idx);
if (fieldString && !fieldString->empty()) {
visitor(fixupEnumerationValue(schema, *fieldString, type_description, group_name, fieldName, iddField.properties().type));
return true;
}
}
return false;
};
std::size_t cur_group_number = 0;
for (const auto& g : obj.extensibleGroups()) {
++cur_group_number;
// get first field and try to make a group name out of it
const auto& [group_name, is_array_group] =
openstudio::epJSON::getGroupName(group_names, field_names, schema, type_description, obj.iddObject().extensibleGroup()[0].name());
auto& containing_json = [&json_object, &group_name = group_name, is_array_group = is_array_group]() -> auto& {
if (is_array_group) {
auto& array_obj = json_object[group_name];
return array_obj.append(Json::Value{Json::objectValue});
} else {
return json_object;
}
}();
for (unsigned int idx = 0; idx < g.numFields(); ++idx) {
const auto& iddField = obj.iddObject().extensibleGroup()[idx];
const auto fieldName = getFieldName(is_array_group, obj.iddObject(), schema, type_description, cur_group_number, idx,
toJSONFieldName(field_names, iddField.name()));
[[maybe_unused]] const auto fieldAdded = visitField([&containing_json, &fieldName](const auto& value) { containing_json[fieldName] = value; },
iddField, group_name, fieldName, g, idx);
}
}
for (unsigned int idx = 0; idx < obj.numFields(); ++idx) {
const auto& iddField = obj.iddObject().getField(idx);
const auto& fieldName = toJSONFieldName(field_names, iddField->name());
if (iddField->isNameField()) {
// skip name, we already got that
continue;
}
if (obj.iddObject().isExtensibleField(idx)) {
// skip extensible field, we already dealt with that
continue;
}
visitField([&json_object, &fieldName](const auto& value) { json_object[fieldName] = value; }, iddField.get(), "", fieldName, obj, idx);
}
}
return result;
}
Json::Value toJSON(const openstudio::Workspace& workspace, const openstudio::path& schemaPath) {
return toJSON(workspace.toIdfFile(), schemaPath);
}
std::string toJSONString(const openstudio::IdfFile& inputFile, const openstudio::path& schemaPath) {
return toJSON(inputFile, schemaPath).toStyledString();
}
std::string toJSONString(const openstudio::Workspace& workspace, const openstudio::path& schemaPath) {
return toJSON(workspace, schemaPath).toStyledString();
}
} // namespace openstudio::epJSON