GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 92.0% 46 / 0 / 50
Functions: 100.0% 1 / 0 / 1
Branches: 75.9% 110 / 0 / 145

src/energyplus/ReverseTranslator/ReverseTranslateEnergyManagementSystemProgram.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "../ReverseTranslator.hpp"
7
8 #include "../../model/EnergyManagementSystemProgram.hpp"
9 #include "../../model/EnergyManagementSystemProgram_Impl.hpp"
10
11 #include "../../utilities/idf/IdfExtensibleGroup.hpp"
12 #include "../../utilities/idf/WorkspaceExtensibleGroup.hpp"
13 #include "../../utilities/core/StringHelpers.hpp"
14
15 #include <utilities/idd/EnergyManagementSystem_Program_FieldEnums.hxx>
16 #include "../../utilities/idd/IddEnums.hpp"
17 #include <utilities/idd/IddEnums.hxx>
18
19 using namespace openstudio::model;
20
21 namespace openstudio {
22
23 namespace energyplus {
24
25 6 OptionalModelObject ReverseTranslator::translateEnergyManagementSystemProgram(const WorkspaceObject& workspaceObject) {
26
4/5
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
6 if (workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_Program) {
27 ✗ LOG(Error, "WorkspaceObject is not IddObjectType: EnergyManagementSystem_Program");
28 ✗ return boost::none;
29 }
30
31
1/1
✓ Branch 1 taken 6 times.
6 OptionalString s = workspaceObject.getString(EnergyManagementSystem_ProgramFields::Name);
32
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!s) {
33 ✗ LOG(Error, "WorkspaceObject EnergyManagementSystem_Program has no Name");
34 ✗ return boost::none;
35 }
36
37 // Make sure all necessary objects are translated first
38 // TODO: JM 2018-08-16: Is this really necessary? Really we should just call the translation of the objects that
39 // **can** be referenced by the EMS program, and these objects should be handling the call to reverseTranslation of the objects
40 // they themselves can reference
41
3/3
✓ Branch 1 taken 6 times.
✓ Branch 8 taken 155 times.
✓ Branch 9 taken 6 times.
161 for (const WorkspaceObject& workspaceObject : m_workspace.objects()) {
42
43 // &&(workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_Program)
44 // && (workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_ProgramCallingManager)
45 // && (workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_MeteredOutputVariable)
46 // && (workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_OutputVariable)
47
48 // These I'm sure we do need.
49
4/6
✓ Branch 1 taken 155 times.
✓ Branch 4 taken 155 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 142 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
310 if ((workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Subroutine)
50
6/9
✓ Branch 1 taken 153 times.
✓ Branch 4 taken 153 times.
✓ Branch 7 taken 153 times.
✓ Branch 10 taken 149 times.
✓ Branch 11 taken 4 times.
✓ Branch 13 taken 155 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
308 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Actuator)
51
7/9
✓ Branch 1 taken 149 times.
✓ Branch 4 taken 149 times.
✓ Branch 7 taken 149 times.
✓ Branch 10 taken 144 times.
✓ Branch 11 taken 5 times.
✓ Branch 13 taken 153 times.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
304 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Sensor)
52
6/9
✓ Branch 1 taken 144 times.
✓ Branch 4 taken 144 times.
✓ Branch 7 taken 144 times.
✓ Branch 10 taken 144 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 149 times.
✓ Branch 14 taken 6 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
299 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_ConstructionIndexVariable)
53
6/9
✓ Branch 1 taken 144 times.
✓ Branch 4 taken 144 times.
✓ Branch 7 taken 144 times.
✓ Branch 10 taken 144 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 144 times.
✓ Branch 14 taken 11 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
299 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_CurveOrTableIndexVariable)
54
7/9
✓ Branch 1 taken 144 times.
✓ Branch 4 taken 144 times.
✓ Branch 7 taken 144 times.
✓ Branch 10 taken 142 times.
✓ Branch 11 taken 2 times.
✓ Branch 13 taken 144 times.
✓ Branch 14 taken 11 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
299 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_GlobalVariable)
55
6/9
✓ Branch 1 taken 142 times.
✓ Branch 4 taken 142 times.
✓ Branch 7 taken 142 times.
✓ Branch 10 taken 142 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 144 times.
✓ Branch 14 taken 11 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
297 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_InternalVariable)
56
11/14
✓ Branch 1 taken 155 times.
✓ Branch 3 taken 153 times.
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 142 times.
✓ Branch 9 taken 142 times.
✓ Branch 12 taken 142 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 142 times.
✓ Branch 17 taken 142 times.
✓ Branch 18 taken 13 times.
✓ Branch 20 taken 142 times.
✓ Branch 21 taken 13 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
463 || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_TrendVariable)) {
57
1/1
✓ Branch 1 taken 13 times.
13 translateAndMapWorkspaceObject(workspaceObject);
58 }
59 6 }
60
61
1/1
✓ Branch 1 taken 6 times.
6 openstudio::model::EnergyManagementSystemProgram emsProgram(m_model);
62
2/2
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
6 emsProgram.setName(*s);
63
64 // Get all model objects that can be referenced int he EMS Program so we can do name / uid substitution
65 const std::vector<IddObjectType> validIddObjectTypes{IddObjectType::OS_EnergyManagementSystem_Subroutine,
66 IddObjectType::OS_EnergyManagementSystem_Actuator,
67 IddObjectType::OS_EnergyManagementSystem_Sensor,
68 IddObjectType::OS_EnergyManagementSystem_ConstructionIndexVariable,
69 IddObjectType::OS_EnergyManagementSystem_CurveOrTableIndexVariable,
70 IddObjectType::OS_EnergyManagementSystem_GlobalVariable,
71 IddObjectType::OS_EnergyManagementSystem_InternalVariable,
72
9/9
✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
✓ Branch 19 taken 6 times.
✓ Branch 22 taken 6 times.
✓ Branch 25 taken 6 times.
12 IddObjectType::OS_EnergyManagementSystem_TrendVariable};
73
74 6 std::vector<model::ModelObject> modelObjects;
75
3/3
✓ Branch 1 taken 6 times.
✓ Branch 8 taken 92 times.
✓ Branch 9 taken 6 times.
98 for (const model::ModelObject& mo : m_model.modelObjects()) {
76
4/4
✓ Branch 2 taken 92 times.
✓ Branch 7 taken 92 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 83 times.
92 if (std::find(validIddObjectTypes.begin(), validIddObjectTypes.end(), mo.iddObjectType()) != validIddObjectTypes.end()) {
77
1/1
✓ Branch 1 taken 9 times.
9 modelObjects.push_back(mo);
78 }
79 6 }
80
81 // Now, we should do the actual name/uid substitution on all lines of the program
82
83 size_t pos;
84 size_t len;
85 6 std::string newline;
86 6 std::string uid;
87
88
1/1
✓ Branch 1 taken 6 times.
6 unsigned n = workspaceObject.numExtensibleGroups();
89 6 OptionalString line;
90
91 // Loop on each line of the program
92
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
28 for (unsigned i = 0; i < n; ++i) {
93 line =
94
3/3
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
22 workspaceObject.getExtensibleGroup(i).cast<WorkspaceExtensibleGroup>().getString(EnergyManagementSystem_ProgramExtensibleFields::ProgramLine);
95
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (line) {
96
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 newline = line.get();
97
98 // Split line to get 'tokens' and look for ModelObject names
99 // splitEMSLineToTokens returns already sanitized tokens (excludes reserved keywords, blank ones, functions, removes parenthesis, etc)
100
2/2
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
22 std::vector<std::string> tokens = splitEMSLineToTokens(newline);
101
102
2/2
✓ Branch 5 taken 46 times.
✓ Branch 6 taken 22 times.
68 for (std::string& token : tokens) {
103
2/2
✓ Branch 5 taken 75 times.
✓ Branch 6 taken 32 times.
107 for (const model::ModelObject& mo : modelObjects) {
104 // Check if program item is the name of a model object
105
1/1
✓ Branch 1 taken 75 times.
75 boost::optional<std::string> _name = mo.name();
106
6/7
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 75 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 61 times.
✓ Branch 8 taken 14 times.
✓ Branch 9 taken 61 times.
75 if (_name && (_name.get() == token)) {
107 // replace model object's name with its handle
108 14 pos = newline.find(token);
109 14 len = token.length();
110
2/2
✓ Branch 1 taken 14 times.
✓ Branch 4 taken 14 times.
14 uid = toString(mo.handle());
111
1/1
✓ Branch 1 taken 14 times.
14 newline.replace(pos, len, uid);
112 // Now that we have done the replacement, no need to keep looping.
113 // Plus, we should break out of the nested loop and go to the next "j"
114 // Otherwise pos could become giberish if there's another object named the same
115 // since it won't be able to find the already-replaced string (this shouldn't happen though)
116 14 break;
117 }
118 75 } // end loop on all modelObjects
119
120 } // end loop on all results in line
121
1/1
✓ Branch 1 taken 22 times.
22 emsProgram.addLine(newline);
122 22 } // end if(line)
123 } // End loop on each line of the program
124
125
1/1
✓ Branch 1 taken 6 times.
6 return emsProgram;
126 6 }
127
128 } // namespace energyplus
129
130 } // namespace openstudio
131