src/energyplus/ForwardTranslator/ForwardTranslatePythonPluginInstance.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 "../ForwardTranslator.hpp" | ||
| 7 | |||
| 8 | #include "../../model/Model.hpp" | ||
| 9 | #include "../../model/PythonPluginInstance.hpp" | ||
| 10 | #include "../../model/ExternalFile.hpp" | ||
| 11 | |||
| 12 | #include "../../utilities/idf/IdfExtensibleGroup.hpp" | ||
| 13 | |||
| 14 | #include <utilities/idd/PythonPlugin_Instance_FieldEnums.hxx> | ||
| 15 | #include <utilities/idd/PythonPlugin_SearchPaths_FieldEnums.hxx> | ||
| 16 | |||
| 17 | #include "../../utilities/idd/IddEnums.hpp" | ||
| 18 | #include <utilities/idd/IddEnums.hxx> | ||
| 19 | #include <utilities/idd/IddFactory.hxx> | ||
| 20 | |||
| 21 | #include <algorithm> | ||
| 22 | |||
| 23 | constexpr static auto pythonSearchPathsName = "Python Plugin Search Paths"; | ||
| 24 | |||
| 25 | using namespace openstudio::model; | ||
| 26 | |||
| 27 | namespace openstudio { | ||
| 28 | |||
| 29 | namespace energyplus { | ||
| 30 | |||
| 31 | 6 | boost::optional<IdfObject> ForwardTranslator::translatePythonPluginInstance(PythonPluginInstance& modelObject) { | |
| 32 | |||
| 33 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
6 | path filePath = modelObject.externalFile().filePath(); |
| 34 |
2/3✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!openstudio::filesystem::exists(filePath)) { |
| 35 | ✗ | LOG(Warn, modelObject.briefDescription() << " will not be translated, cannot find the referenced file '" << filePath << "'"); | |
| 36 | ✗ | return boost::none; | |
| 37 | } | ||
| 38 | |||
| 39 | // make the path correct for this system | ||
| 40 |
1/1✓ Branch 1 taken 6 times.
|
6 | filePath = openstudio::filesystem::system_complete(filePath); |
| 41 | |||
| 42 | // If the plugin class name can't be found in the referenced external file, then it shouldn't be translated | ||
| 43 | // We do it here and inside model, because between the time the user created the model object and now, the python file may have been modified | ||
| 44 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::string pluginClassName = modelObject.pluginClassName(); |
| 45 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 5 times.
|
6 | if (!modelObject.findPluginClassNameInFile(pluginClassName)) { |
| 46 |
11/11✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 26 taken 1 time.
✓ Branch 29 taken 1 time.
✓ Branch 32 taken 1 time.
|
1 | LOG(Warn, "PythonPluginInstance " << modelObject.name().get() << " does not have plugin class name '" << pluginClassName |
| 47 | << "' contained in referenced external file"); | ||
| 48 | 1 | return boost::none; | |
| 49 | } | ||
| 50 | |||
| 51 | // Name | ||
| 52 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | IdfObject idfObject = createRegisterAndNameIdfObject(openstudio::IddObjectType::PythonPlugin_Instance, modelObject); |
| 53 | |||
| 54 | // Run During Warmup Days | ||
| 55 |
3/3✓ Branch 1 taken 5 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 4 times.
|
5 | if (modelObject.runDuringWarmupDays()) { |
| 56 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | idfObject.setString(openstudio::PythonPlugin_InstanceFields::RunDuringWarmupDays, "Yes"); |
| 57 | } else { | ||
| 58 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
8 | idfObject.setString(openstudio::PythonPlugin_InstanceFields::RunDuringWarmupDays, "No"); |
| 59 | } | ||
| 60 | |||
| 61 | // Python Module Name | ||
| 62 |
1/1✓ Branch 1 taken 5 times.
|
5 | std::string pythonModuleName = toString(filePath.stem()); |
| 63 |
1/1✓ Branch 1 taken 5 times.
|
5 | idfObject.setString(openstudio::PythonPlugin_InstanceFields::PythonModuleName, pythonModuleName); |
| 64 | |||
| 65 | // Python Class Name | ||
| 66 |
1/1✓ Branch 1 taken 5 times.
|
5 | idfObject.setString(openstudio::PythonPlugin_InstanceFields::PluginClassName, pluginClassName); |
| 67 | |||
| 68 | // PythonPlugin:SearchPaths | ||
| 69 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | std::string searchPath = toString(filePath.parent_path()); |
| 70 | |||
| 71 |
3/3✓ Branch 1 taken 572 times.
✓ Branch 4 taken 572 times.
✓ Branch 11 taken 5 times.
|
577 | auto it = std::find_if(m_idfObjects.begin(), m_idfObjects.end(), [](auto& idfObject) { return idfObject.nameString() == pythonSearchPathsName; }); |
| 72 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
|
5 | if (it == m_idfObjects.end()) { |
| 73 | // First time: create it | ||
| 74 |
1/1✓ Branch 1 taken 2 times.
|
2 | auto& pythonPluginSearchPaths = m_idfObjects.emplace_back(IddObjectType::PythonPlugin_SearchPaths); |
| 75 | |||
| 76 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | pythonPluginSearchPaths.setName(pythonSearchPathsName); |
| 77 | // These are IDD defaults | ||
| 78 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | pythonPluginSearchPaths.setString(PythonPlugin_SearchPathsFields::AddCurrentWorkingDirectorytoSearchPath, "Yes"); |
| 79 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | pythonPluginSearchPaths.setString(PythonPlugin_SearchPathsFields::AddInputFileDirectorytoSearchPath, "Yes"); |
| 80 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | pythonPluginSearchPaths.setString(PythonPlugin_SearchPathsFields::AddepinEnvironmentVariabletoSearchPath, "Yes"); |
| 81 | |||
| 82 | // Just push the searchPath to it | ||
| 83 |
4/6✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
6 | pythonPluginSearchPaths.pushExtensibleGroup({searchPath}); |
| 84 | } else { | ||
| 85 | |||
| 86 | // Try to locate it before adding it | ||
| 87 | 3 | bool newSearchPath = true; | |
| 88 |
3/3✓ Branch 2 taken 3 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 1 time.
|
5 | for (const IdfExtensibleGroup& eg : it->extensibleGroups()) { |
| 89 |
4/4✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2 times.
|
4 | if (searchPath == eg.getString(0)) { |
| 90 | 2 | newSearchPath = false; | |
| 91 | 2 | break; | |
| 92 | } | ||
| 93 | 3 | } | |
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
|
3 | if (newSearchPath) { |
| 96 |
4/6✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 1 time.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
4 | it->pushExtensibleGroup({searchPath}); |
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 |
1/1✓ Branch 1 taken 5 times.
|
5 | return idfObject; |
| 101 | 9 | } | |
| 102 | |||
| 103 | } // namespace energyplus | ||
| 104 | |||
| 105 | } // namespace openstudio | ||
| 106 |