src/energyplus/ReverseTranslator/ReverseTranslateEnergyManagementSystemSubroutine.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/EnergyManagementSystemSubroutine.hpp" | ||
| 9 | #include "../../model/EnergyManagementSystemSubroutine_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_Subroutine_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 | 3 | OptionalModelObject ReverseTranslator::translateEnergyManagementSystemSubroutine(const WorkspaceObject& workspaceObject) { | |
| 26 |
4/5✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 3 times.
|
3 | if (workspaceObject.iddObject().type() != IddObjectType::EnergyManagementSystem_Subroutine) { |
| 27 | ✗ | LOG(Error, "WorkspaceObject is not IddObjectType: EnergyManagementSystem_Subroutine"); | |
| 28 | ✗ | return boost::none; | |
| 29 | } | ||
| 30 | |||
| 31 |
1/1✓ Branch 1 taken 3 times.
|
3 | OptionalString s = workspaceObject.getString(EnergyManagementSystem_SubroutineFields::Name); |
| 32 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (!s) { |
| 33 | ✗ | LOG(Error, "WorkspaceObject EnergyManagementSystem_Subroutine has no Name"); | |
| 34 | ✗ | return boost::none; | |
| 35 | } | ||
| 36 | |||
| 37 | // Make sure we translate the objects that can be referenced here | ||
| 38 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 8 taken 62 times.
✓ Branch 9 taken 3 times.
|
65 | for (const WorkspaceObject& workspaceObject : m_workspace.objects()) { |
| 39 | |||
| 40 | // Note: JM 2018-08-17 | ||
| 41 | // I think an EMS:Subroutine can reference another EMS:Subroutine, we might get problems from that: | ||
| 42 | // The one that is being referenced would need be translated before the one that references before the name/uuid substitution happen. | ||
| 43 | // But it's harder to control that order*, and this is really an edge case though, so not handling it. | ||
| 44 | // * Can't add this condition without getting into a loop: | ||
| 45 | // (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Subroutine) | ||
| 46 | 62 | if ( | |
| 47 | // These I'm sure we do need. | ||
| 48 |
4/6✓ Branch 1 taken 62 times.
✓ Branch 4 taken 62 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 57 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
124 | (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Actuator) |
| 49 |
6/9✓ Branch 1 taken 61 times.
✓ Branch 4 taken 61 times.
✓ Branch 7 taken 61 times.
✓ Branch 10 taken 59 times.
✓ Branch 11 taken 2 times.
✓ Branch 13 taken 62 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
123 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_Sensor) |
| 50 |
6/9✓ Branch 1 taken 59 times.
✓ Branch 4 taken 59 times.
✓ Branch 7 taken 59 times.
✓ Branch 10 taken 59 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 61 times.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
121 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_ConstructionIndexVariable) |
| 51 |
6/9✓ Branch 1 taken 59 times.
✓ Branch 4 taken 59 times.
✓ Branch 7 taken 59 times.
✓ Branch 10 taken 59 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 59 times.
✓ Branch 14 taken 3 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
121 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_CurveOrTableIndexVariable) |
| 52 |
7/9✓ Branch 1 taken 59 times.
✓ Branch 4 taken 59 times.
✓ Branch 7 taken 59 times.
✓ Branch 10 taken 57 times.
✓ Branch 11 taken 2 times.
✓ Branch 13 taken 59 times.
✓ Branch 14 taken 3 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
121 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_GlobalVariable) |
| 53 |
6/9✓ Branch 1 taken 57 times.
✓ Branch 4 taken 57 times.
✓ Branch 7 taken 57 times.
✓ Branch 10 taken 57 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 59 times.
✓ Branch 14 taken 3 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
|
119 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_InternalVariable) |
| 54 |
11/14✓ Branch 1 taken 62 times.
✓ Branch 3 taken 61 times.
✓ Branch 4 taken 1 time.
✓ Branch 6 taken 57 times.
✓ Branch 9 taken 57 times.
✓ Branch 12 taken 57 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 57 times.
✓ Branch 17 taken 57 times.
✓ Branch 18 taken 5 times.
✓ Branch 20 taken 57 times.
✓ Branch 21 taken 5 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
185 | || (workspaceObject.iddObject().type() == IddObjectType::EnergyManagementSystem_TrendVariable)) { |
| 55 |
1/1✓ Branch 1 taken 5 times.
|
5 | translateAndMapWorkspaceObject(workspaceObject); |
| 56 | } | ||
| 57 | 3 | } | |
| 58 |
1/1✓ Branch 1 taken 3 times.
|
3 | openstudio::model::EnergyManagementSystemSubroutine emsProgram(m_model); |
| 59 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | emsProgram.setName(*s); |
| 60 | |||
| 61 | // Get all model objects that can be referenced int he EMS Program so we can do name / uid substitution | ||
| 62 | const std::vector<IddObjectType> validIddObjectTypes{IddObjectType::OS_EnergyManagementSystem_Subroutine, | ||
| 63 | IddObjectType::OS_EnergyManagementSystem_Actuator, | ||
| 64 | IddObjectType::OS_EnergyManagementSystem_Sensor, | ||
| 65 | IddObjectType::OS_EnergyManagementSystem_ConstructionIndexVariable, | ||
| 66 | IddObjectType::OS_EnergyManagementSystem_CurveOrTableIndexVariable, | ||
| 67 | IddObjectType::OS_EnergyManagementSystem_GlobalVariable, | ||
| 68 | IddObjectType::OS_EnergyManagementSystem_InternalVariable, | ||
| 69 |
9/9✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 25 taken 3 times.
|
6 | IddObjectType::OS_EnergyManagementSystem_TrendVariable}; |
| 70 | |||
| 71 | 3 | std::vector<model::ModelObject> modelObjects; | |
| 72 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 3 times.
|
41 | for (const model::ModelObject& mo : m_model.modelObjects()) { |
| 73 |
4/4✓ Branch 2 taken 38 times.
✓ Branch 7 taken 38 times.
✓ Branch 10 taken 7 times.
✓ Branch 11 taken 31 times.
|
38 | if (std::find(validIddObjectTypes.begin(), validIddObjectTypes.end(), mo.iddObjectType()) != validIddObjectTypes.end()) { |
| 74 |
1/1✓ Branch 1 taken 7 times.
|
7 | modelObjects.push_back(mo); |
| 75 | } | ||
| 76 | 3 | } | |
| 77 | |||
| 78 | // Now, we should do the actual name/uid substitution on all lines of the program | ||
| 79 | |||
| 80 | size_t pos; | ||
| 81 | size_t len; | ||
| 82 | 3 | std::string newline; | |
| 83 | 3 | std::string uid; | |
| 84 | |||
| 85 |
1/1✓ Branch 1 taken 3 times.
|
3 | unsigned n = workspaceObject.numExtensibleGroups(); |
| 86 | 3 | OptionalString line; | |
| 87 | |||
| 88 | // Loop on each line of the program | ||
| 89 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (unsigned i = 0; i < n; ++i) { |
| 90 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
12 | line = workspaceObject.getExtensibleGroup(i).cast<WorkspaceExtensibleGroup>().getString( |
| 91 | 6 | EnergyManagementSystem_SubroutineExtensibleFields::ProgramLine); | |
| 92 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (line) { |
| 93 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
6 | newline = line.get(); |
| 94 | |||
| 95 | // Split line to get 'tokens' and look for ModelObject names | ||
| 96 | // splitEMSLineToTokens returns already sanitized tokens (excludes reserved keywords, blank ones, functions, removes parenthesis, etc) | ||
| 97 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
6 | std::vector<std::string> tokens = splitEMSLineToTokens(newline); |
| 98 | |||
| 99 |
2/2✓ Branch 5 taken 16 times.
✓ Branch 6 taken 6 times.
|
22 | for (std::string& token : tokens) { |
| 100 |
2/2✓ Branch 5 taken 35 times.
✓ Branch 6 taken 12 times.
|
47 | for (const model::ModelObject& mo : modelObjects) { |
| 101 | // Check if program item is the name of a model object | ||
| 102 |
1/1✓ Branch 1 taken 35 times.
|
35 | boost::optional<std::string> _name = mo.name(); |
| 103 |
6/7✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 35 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 31 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 31 times.
|
35 | if (_name && (_name.get() == token)) { |
| 104 | // replace model object's name with its handle | ||
| 105 | 4 | pos = newline.find(token); | |
| 106 | 4 | len = token.length(); | |
| 107 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | uid = toString(mo.handle()); |
| 108 |
1/1✓ Branch 1 taken 4 times.
|
4 | newline.replace(pos, len, uid); |
| 109 | // Now that we have done the replacement, no need to keep looping. | ||
| 110 | // Plus, we should break out of the nested loop and go to the next "j" | ||
| 111 | // Otherwise pos could become giberish if there's another object named the same | ||
| 112 | // since it won't be able to find the already-replaced string (this shouldn't happen though) | ||
| 113 | 4 | break; | |
| 114 | } | ||
| 115 | 35 | } // end loop on all modelObjects | |
| 116 | |||
| 117 | } // end loop on all results in line | ||
| 118 |
1/1✓ Branch 1 taken 6 times.
|
6 | emsProgram.addLine(newline); |
| 119 | 6 | } // end if(line) | |
| 120 | } // End loop on each line of the program | ||
| 121 | |||
| 122 |
1/1✓ Branch 1 taken 3 times.
|
3 | return emsProgram; |
| 123 | 3 | } | |
| 124 | |||
| 125 | } // namespace energyplus | ||
| 126 | |||
| 127 | } // namespace openstudio | ||
| 128 |