LCOV - code coverage report
Current view: top level - EnergyPlus/InputProcessing - InputProcessor.hh (source / functions) Coverage Total Hit
Test: lcov.output.filtered Lines: 96.3 % 27 26
Test Date: 2025-05-22 16:09:37 Functions: 100.0 % 8 8

            Line data    Source code
       1              : // EnergyPlus, Copyright (c) 1996-2025, The Board of Trustees of the University of Illinois,
       2              : // The Regents of the University of California, through Lawrence Berkeley National Laboratory
       3              : // (subject to receipt of any required approvals from the U.S. Dept. of Energy), Oak Ridge
       4              : // National Laboratory, managed by UT-Battelle, Alliance for Sustainable Energy, LLC, and other
       5              : // contributors. All rights reserved.
       6              : //
       7              : // NOTICE: This Software was developed under funding from the U.S. Department of Energy and the
       8              : // U.S. Government consequently retains certain rights. As such, the U.S. Government has been
       9              : // granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable,
      10              : // worldwide license in the Software to reproduce, distribute copies to the public, prepare
      11              : // derivative works, and perform publicly and display publicly, and to permit others to do so.
      12              : //
      13              : // Redistribution and use in source and binary forms, with or without modification, are permitted
      14              : // provided that the following conditions are met:
      15              : //
      16              : // (1) Redistributions of source code must retain the above copyright notice, this list of
      17              : //     conditions and the following disclaimer.
      18              : //
      19              : // (2) Redistributions in binary form must reproduce the above copyright notice, this list of
      20              : //     conditions and the following disclaimer in the documentation and/or other materials
      21              : //     provided with the distribution.
      22              : //
      23              : // (3) Neither the name of the University of California, Lawrence Berkeley National Laboratory,
      24              : //     the University of Illinois, U.S. Dept. of Energy nor the names of its contributors may be
      25              : //     used to endorse or promote products derived from this software without specific prior
      26              : //     written permission.
      27              : //
      28              : // (4) Use of EnergyPlus(TM) Name. If Licensee (i) distributes the software in stand-alone form
      29              : //     without changes from the version obtained under this License, or (ii) Licensee makes a
      30              : //     reference solely to the software portion of its product, Licensee must refer to the
      31              : //     software as "EnergyPlus version X" software, where "X" is the version number Licensee
      32              : //     obtained under this License and may not use a different name for the software. Except as
      33              : //     specifically required in this Section (4), Licensee shall not use in a company name, a
      34              : //     product name, in advertising, publicity, or other promotional activities any name, trade
      35              : //     name, trademark, logo, or other designation of "EnergyPlus", "E+", "e+" or confusingly
      36              : //     similar designation, without the U.S. Department of Energy's prior written consent.
      37              : //
      38              : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
      39              : // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
      40              : // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
      41              : // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      42              : // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      43              : // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      44              : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
      45              : // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      46              : // POSSIBILITY OF SUCH DAMAGE.
      47              : 
      48              : #ifndef InputProcessor_hh_INCLUDED
      49              : #define InputProcessor_hh_INCLUDED
      50              : 
      51              : // C++ Headers
      52              : #include <map>
      53              : #include <set>
      54              : #include <string>
      55              : #include <unordered_map>
      56              : #include <vector>
      57              : 
      58              : // ObjexxFCL Headers
      59              : #include <ObjexxFCL/Array1D.hh>
      60              : #include <ObjexxFCL/Array1S.fwd.hh>
      61              : #include <ObjexxFCL/Optional.hh>
      62              : 
      63              : #include <nlohmann/json.hpp>
      64              : 
      65              : // EnergyPlus Headers
      66              : #include <EnergyPlus/Data/BaseData.hh>
      67              : #include <EnergyPlus/DataGlobals.hh>
      68              : #include <EnergyPlus/EnergyPlus.hh>
      69              : #include <EnergyPlus/InputProcessing/DataStorage.hh>
      70              : #include <EnergyPlus/InputProcessing/IdfParser.hh>
      71              : #include <EnergyPlus/InputProcessing/InputValidation.hh>
      72              : 
      73              : class IdfParser;
      74              : class Validation;
      75              : struct EnergyPlusData;
      76              : 
      77              : namespace EnergyPlus {
      78              : 
      79              : void cleanEPJSON(nlohmann::json &epjson);
      80              : 
      81              : class InputProcessor
      82              : {
      83              : public:
      84              :     using json = nlohmann::json;
      85              : 
      86              :     json::parser_callback_t callback;
      87              : 
      88              :     InputProcessor();
      89              : 
      90              :     static std::unique_ptr<InputProcessor> factory();
      91              : 
      92              :     template <typename T> T *objectFactory(EnergyPlusData &state, std::string const &objectName)
      93              :     {
      94              :         T *p = data->objectFactory<T>(objectName);
      95              :         if (p != nullptr) return p;
      96              :         auto const &fields = getFields(state, T::canonicalObjectType(), objectName);
      97              :         p = data->addObject<T>(objectName, fields);
      98              :         return p;
      99              :     }
     100              : 
     101              :     template <typename T> T *objectFactory(EnergyPlusData &state)
     102              :     {
     103              :         T *p = data->objectFactory<T>();
     104              :         if (p != nullptr) return p;
     105              :         auto const &fields = getFields(state, T::canonicalObjectType());
     106              :         p = data->addObject<T>(fields);
     107              :         return p;
     108              :     }
     109              : 
     110              :     std::pair<bool, std::string> convertInsensitiveObjectType(std::string const &objectType);
     111              : 
     112              :     void initializeMaps();
     113              : 
     114              :     void markObjectAsUsed(const std::string &objectType, const std::string &objectName);
     115              : 
     116              :     void processInput(EnergyPlusData &state);
     117              : 
     118              :     int getNumSectionsFound(std::string const &SectionWord);
     119              : 
     120              :     int getNumObjectsFound(EnergyPlusData &state, std::string_view const ObjectWord);
     121              : 
     122              :     bool findDefault(std::string &default_value, json const &schema_field_obj);
     123              : 
     124              :     bool findDefault(Real64 &default_value, json const &schema_field_obj);
     125              : 
     126              :     bool getDefaultValue(EnergyPlusData &state, std::string const &objectWord, std::string const &fieldName, Real64 &value);
     127              : 
     128              :     bool getDefaultValue(EnergyPlusData &state, std::string const &objectWord, std::string const &fieldName, std::string &value);
     129              : 
     130              :     std::string getAlphaFieldValue(json const &ep_object, json const &schema_obj_props, std::string const &fieldName, bool uc = true);
     131              : 
     132              :     Real64 getRealFieldValue(json const &ep_object, json const &schema_obj_props, std::string const &fieldName);
     133              : 
     134              :     int getIntFieldValue(json const &ep_object, json const &schema_obj_props, std::string const &fieldName);
     135              : 
     136              :     const json &getObjectSchemaProps(EnergyPlusData &state, std::string const &objectWord);
     137              : 
     138              :     std::pair<std::string, bool> getObjectItemValue(std::string const &field_value, json const &schema_field_obj);
     139              : 
     140              :     const json &getJSONObjectItem(EnergyPlusData &state, std::string_view ObjType, std::string_view ObjName);
     141              : 
     142              :     void getObjectItem(EnergyPlusData &state,
     143              :                        std::string_view Object,
     144              :                        int const Number,
     145              :                        Array1S_string Alphas,
     146              :                        int &NumAlphas,
     147              :                        Array1D<Real64> &Numbers,
     148              :                        int &NumNumbers,
     149              :                        int &Status,
     150              :                        ObjexxFCL::Optional<Array1D_bool> NumBlank = _,
     151              :                        ObjexxFCL::Optional<Array1D_bool> AlphaBlank = _,
     152              :                        ObjexxFCL::Optional<Array1D_string> AlphaFieldNames = _,
     153              :                        ObjexxFCL::Optional<Array1D_string> NumericFieldNames = _);
     154              : 
     155              :     int getIDFObjNum(EnergyPlusData &state, std::string_view Object, int const Number);
     156              : 
     157              :     std::vector<std::string> getIDFOrderedKeys(EnergyPlusData &state, std::string_view Object);
     158              : 
     159              :     int getJSONObjNum(EnergyPlusData &state, std::string const &Object, int const Number);
     160              : 
     161              :     int getObjectItemNum(EnergyPlusData &state,
     162              :                          std::string_view ObjType, // Object Type (ref: IDD Objects)
     163              :                          std::string_view ObjName  // Name of the object type
     164              :     );
     165              : 
     166              :     int getObjectItemNum(EnergyPlusData &state,
     167              :                          std::string_view ObjType,       // Object Type (ref: IDD Objects)
     168              :                          std::string const &NameTypeVal, // Object "name" field type ( used as search key )
     169              :                          std::string const &ObjName      // Name of the object type
     170              :     );
     171              : 
     172              :     void getMaxSchemaArgs(int &NumArgs, int &NumAlpha, int &NumNumeric);
     173              : 
     174              :     void getObjectDefMaxArgs(EnergyPlusData &state,
     175              :                              std::string_view const ObjectWord, // Object for definition
     176              :                              int &NumArgs,                      // How many arguments (max) this Object can have
     177              :                              int &NumAlpha,                     // How many Alpha arguments (max) this Object can have
     178              :                              int &NumNumeric                    // How many Numeric arguments (max) this Object can have
     179              :     );
     180              : 
     181              :     void preProcessorCheck(EnergyPlusData &state, bool &PreP_Fatal); // True if a preprocessor flags a fatal error
     182              : 
     183              :     void preScanReportingVariables(EnergyPlusData &state);
     184              : 
     185              :     void reportIDFRecordsStats(EnergyPlusData &state);
     186              : 
     187              :     void reportOrphanRecordObjects(EnergyPlusData &state);
     188              : 
     189              :     const json &getObjectInstances(std::string const &ObjType);
     190              : 
     191              :     bool checkForUnsupportedObjects(EnergyPlusData &state);
     192              : 
     193              :     //    void clear_state();
     194              : private:
     195              :     friend class EnergyPlusFixture;
     196              :     friend class InputProcessorFixture;
     197              : 
     198              :     struct ObjectInfo
     199              :     {
     200       128435 :         ObjectInfo() = default;
     201              : 
     202        38495 :         ObjectInfo(std::string const &objectType, std::string const &objectName) : objectType(objectType), objectName(objectName)
     203              :         {
     204        38495 :         }
     205              : 
     206              :         ObjectInfo(std::string &&objectType, std::string &&objectName) : objectType(objectType), objectName(objectName)
     207              :         {
     208              :         }
     209              : 
     210       485000 :         bool operator<(const ObjectInfo &rhs) const
     211              :         {
     212       485000 :             int cmp = this->objectType.compare(rhs.objectType);
     213       485000 :             if (cmp == 0) {
     214       210674 :                 return this->objectName < rhs.objectName;
     215              :             }
     216       274326 :             return cmp < 0;
     217              :         }
     218              : 
     219              :         std::string objectType = "";
     220              :         std::string objectName = "";
     221              :     };
     222              : 
     223              :     struct ObjectCache
     224              :     {
     225        16414 :         ObjectCache() = default;
     226              : 
     227              :         ObjectCache(json::const_iterator const &schemaIterator, std::vector<json::const_iterator> const &inputObjectIterators)
     228              :             : schemaIterator(schemaIterator), inputObjectIterators(inputObjectIterators)
     229              :         {
     230              :         }
     231              : 
     232              :         ObjectCache(json::const_iterator &&schemaIterator, std::vector<json::const_iterator> &&inputObjectIterators)
     233              :             : schemaIterator(schemaIterator), inputObjectIterators(inputObjectIterators)
     234              :         {
     235              :         }
     236              : 
     237              :         json::const_iterator schemaIterator;
     238              :         std::vector<json::const_iterator> inputObjectIterators;
     239              :     };
     240              : 
     241              :     struct MaxFields
     242              :     {
     243              :         MaxFields() = default;
     244              :         std::size_t max_fields = 0;
     245              :         std::size_t max_extensible_fields = 0;
     246              :     };
     247              : 
     248              :     MaxFields findMaxFields(
     249              :         EnergyPlusData &state, json const &ep_object, std::string const &extension_key, json const &legacy_idd, std::size_t const min_fields);
     250              : 
     251              :     void setObjectItemValue(EnergyPlusData &state,
     252              :                             json const &ep_object,
     253              :                             json const &ep_schema_object,
     254              :                             std::string const &field,
     255              :                             json const &legacy_field_info,
     256              :                             int &alpha_index,
     257              :                             int &numeric_index,
     258              :                             bool within_max_fields,
     259              :                             Array1S_string Alphas,
     260              :                             int &NumAlphas,
     261              :                             Array1D<Real64> &Numbers,
     262              :                             int &NumNumbers,
     263              :                             ObjexxFCL::Optional<Array1D_bool> NumBlank = _,
     264              :                             ObjexxFCL::Optional<Array1D_bool> AlphaBlank = _,
     265              :                             ObjexxFCL::Optional<Array1D_string> AlphaFieldNames = _,
     266              :                             ObjexxFCL::Optional<Array1D_string> NumericFieldNames = _);
     267              : 
     268              :     void addVariablesForMonthlyReport(EnergyPlusData &state, std::string const &reportName);
     269              : 
     270              :     void addRecordToOutputVariableStructure(EnergyPlusData &state, std::string const &KeyValue, std::string const &VariableName);
     271              : 
     272              :     std::vector<std::string> const &validationErrors();
     273              : 
     274              :     std::vector<std::string> const &validationWarnings();
     275              : 
     276              :     bool checkVersionMatch(EnergyPlusData &state);
     277              : 
     278              :     bool processErrors(EnergyPlusData &state);
     279              : 
     280              :     json const &getFields(EnergyPlusData &state, std::string const &objectType, std::string const &objectName);
     281              : 
     282              :     json const &getFields(EnergyPlusData &state, std::string const &objectType);
     283              : 
     284              :     json const &getPatternProperties(EnergyPlusData &state, json const &schema_obj);
     285              : 
     286      3770951 :     inline std::string convertToUpper(std::string_view s)
     287              :     {
     288      3770951 :         std::string s2;
     289      3770951 :         size_t len = s.size();
     290      3770951 :         s2.resize(len);
     291    118706797 :         for (size_t i = 0; i < len; ++i) {
     292    114935846 :             char c = s[i];
     293    114935846 :             s2[i] = ('a' <= c && c <= 'z') ? c ^ 0x20 : c; // ASCII only
     294              :         }
     295      3770951 :         s2[len] = '\0';
     296      3770951 :         return s2;
     297            0 :     }
     298              : 
     299              :     using UnorderedObjectTypeMap = std::unordered_map<std::string, std::string>;
     300              :     using UnorderedObjectCacheMap = std::unordered_map<std::string, ObjectCache>;
     301              :     using UnusedObjectSet = std::set<ObjectInfo>;
     302              : 
     303              :     std::unique_ptr<IdfParser> idf_parser;
     304              :     std::unique_ptr<Validation> validation;
     305              :     std::unique_ptr<DataStorage> data;
     306              :     static const json &schema();
     307              : 
     308              : public:
     309              :     json epJSON;
     310              : 
     311              : private:
     312              :     // Maps OBJECTTYPE to ObjectType (example entry: {"ZONEHVAC:EQUIPMENTLIST", "ZoneHVAC:EquipmentList"})
     313              :     UnorderedObjectTypeMap caseInsensitiveObjectMap;
     314              :     // Maps ObjectType to ObjectCache (json::const_iterator const &schemaIterator, std::vector<json::const_iterator> const &inputObjectIterators)
     315              :     UnorderedObjectCacheMap objectCacheMap;
     316              : 
     317              :     // ObjectType to vector of ObjectName
     318              :     UnusedObjectSet unusedInputs;
     319              :     char s[129] = {0};
     320              : 
     321              : }; // InputProcessor
     322              : 
     323              : struct DataInputProcessing : BaseGlobalStruct
     324              : {
     325              :     std::unique_ptr<InputProcessor> inputProcessor = InputProcessor::factory();
     326              : 
     327         2126 :     void init_constant_state([[maybe_unused]] EnergyPlusData &state) override
     328              :     {
     329         2126 :     }
     330              : 
     331         1152 :     void init_state([[maybe_unused]] EnergyPlusData &state) override
     332              :     {
     333         1152 :     }
     334              : 
     335         2100 :     void clear_state() override
     336              :     {
     337         2100 :         inputProcessor.reset();
     338         2100 :         inputProcessor = EnergyPlus::InputProcessor::factory();
     339         2100 :     }
     340              : };
     341              : 
     342              : } // namespace EnergyPlus
     343              : 
     344              : #endif
        

Generated by: LCOV version 2.0-1