src/utilities/sql/SqlFileTimeSeriesQuery.hpp
| 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 | #ifndef UTILITIES_SQL_SQLFILETIMESERIESQUERY_HPP | ||
| 7 | #define UTILITIES_SQL_SQLFILETIMESERIESQUERY_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | #include "SqlFileEnums.hpp" | ||
| 11 | |||
| 12 | #include "../core/Logger.hpp" | ||
| 13 | |||
| 14 | #include <boost/optional.hpp> | ||
| 15 | #include <boost/regex.hpp> | ||
| 16 | |||
| 17 | #include <string> | ||
| 18 | #include <vector> | ||
| 19 | #include <ostream> | ||
| 20 | |||
| 21 | namespace openstudio { | ||
| 22 | |||
| 23 | // forward declarations | ||
| 24 | namespace detail { | ||
| 25 | class SqlFile_Impl; | ||
| 26 | } | ||
| 27 | |||
| 28 | /** Small class to let users identify a SqlFile environment period either by type or by name. | ||
| 29 | * If type is specified, there is a chance the environment name will not be unique. */ | ||
| 30 | class UTILITIES_API EnvironmentIdentifier | ||
| 31 | { | ||
| 32 | public: | ||
| 33 | /** Construct from EnvironmentType enumeration. */ | ||
| 34 | EnvironmentIdentifier(const openstudio::EnvironmentType& type = EnvironmentType()); | ||
| 35 | /** Construct from name. */ | ||
| 36 | EnvironmentIdentifier(const std::string& name); | ||
| 37 | |||
| 38 | /** Return the EnvironmentType, if it is specified. */ | ||
| 39 | boost::optional<openstudio::EnvironmentType> type() const; | ||
| 40 | /** Return the environment period name, if it is specified. */ | ||
| 41 | boost::optional<std::string> name() const; | ||
| 42 | |||
| 43 | private: | ||
| 44 | boost::optional<EnvironmentType> m_type; | ||
| 45 | boost::optional<std::string> m_name; | ||
| 46 | }; | ||
| 47 | |||
| 48 | using OptionalEnvironmentIdentifier = boost::optional<openstudio::EnvironmentIdentifier>; | ||
| 49 | |||
| 50 | /** Small class to let users identify a time series by name, or a set of time | ||
| 51 | * series by regex. */ | ||
| 52 | class UTILITIES_API TimeSeriesIdentifier | ||
| 53 | { | ||
| 54 | public: | ||
| 55 | /** Construct from name. */ | ||
| 56 | TimeSeriesIdentifier(const std::string& name); | ||
| 57 | /** Construct from regex. */ | ||
| 58 | TimeSeriesIdentifier(const boost::regex& re); | ||
| 59 | |||
| 60 | /** Get name, if specified. */ | ||
| 61 | boost::optional<std::string> name() const; | ||
| 62 | /** Get regex, if specified. */ | ||
| 63 | boost::optional<boost::regex> regex() const; | ||
| 64 | |||
| 65 | private: | ||
| 66 | boost::optional<std::string> m_name; | ||
| 67 | boost::optional<boost::regex> m_re; | ||
| 68 | }; | ||
| 69 | |||
| 70 | using OptionalTimeSeriesIdentifier = boost::optional<openstudio::TimeSeriesIdentifier>; | ||
| 71 | |||
| 72 | /** Small class to let users identify one or more key values by name or regex. */ | ||
| 73 | class UTILITIES_API KeyValueIdentifier | ||
| 74 | { | ||
| 75 | public: | ||
| 76 | /** Construct from single name. */ | ||
| 77 | KeyValueIdentifier(const std::string& name); | ||
| 78 | /** Construct from multiple names. Throws if names.size() == 0. */ | ||
| 79 | KeyValueIdentifier(const std::vector<std::string>& names); | ||
| 80 | /** Construct from regex. */ | ||
| 81 | KeyValueIdentifier(const boost::regex& re); | ||
| 82 | |||
| 83 | /** Get name(s). */ | ||
| 84 | std::vector<std::string> names() const; | ||
| 85 | /** Get regex, if it exists. */ | ||
| 86 | boost::optional<boost::regex> regex() const; | ||
| 87 | |||
| 88 | private: | ||
| 89 | std::vector<std::string> m_names; | ||
| 90 | boost::regex m_re; | ||
| 91 | |||
| 92 | − | REGISTER_LOGGER("openstudio.KeyValueIdentifier"); | |
| 93 | }; | ||
| 94 | |||
| 95 | using OptionalKeyValueIdentifier = boost::optional<openstudio::KeyValueIdentifier>; | ||
| 96 | |||
| 97 | /** Class to hold information about one or more SqlFile time series of interest. The query is | ||
| 98 | * limited to one ReportingFrequency so that the resulting TimeSeries.values() Vectors are of the | ||
| 99 | * same length. This class is intended to serve as a way to streamline the SqlFile interface, and | ||
| 100 | * also as data storage for classes like analysis::SqlTimeSeriesVariableAccessor. */ | ||
| 101 | class UTILITIES_API SqlFileTimeSeriesQuery | ||
| 102 | { | ||
| 103 | public: | ||
| 104 | /** @name Constructors */ | ||
| 105 | //@{ | ||
| 106 | |||
| 107 | /** Constructor from optionals. Environment period, reporting frequency, time series, and key | ||
| 108 | * values can all be specified (or not). */ | ||
| 109 | SqlFileTimeSeriesQuery(const boost::optional<openstudio::EnvironmentIdentifier>& oEnvId = boost::none, | ||
| 110 | const boost::optional<openstudio::ReportingFrequency>& oRF = boost::none, | ||
| 111 | const boost::optional<openstudio::TimeSeriesIdentifier>& oTsId = boost::none, | ||
| 112 | const boost::optional<openstudio::KeyValueIdentifier>& oKvId = boost::none); | ||
| 113 | |||
| 114 | /** Constructor from EnvironmentIdentifier. */ | ||
| 115 | SqlFileTimeSeriesQuery(const EnvironmentIdentifier& envId); | ||
| 116 | |||
| 117 | /** Construct from fully specified query. */ | ||
| 118 | SqlFileTimeSeriesQuery(const std::string& environmentPeriod, const openstudio::ReportingFrequency& reportingFrequency, | ||
| 119 | const std::string& timeSeriesName, const std::string& keyValue); | ||
| 120 | |||
| 121 | //@} | ||
| 122 | /** @name Getters */ | ||
| 123 | //@{ | ||
| 124 | |||
| 125 | /** Get the EnvironmentIdentifier. */ | ||
| 126 | boost::optional<openstudio::EnvironmentIdentifier> environment() const; | ||
| 127 | |||
| 128 | /** Get the ReportingFrequency. */ | ||
| 129 | boost::optional<openstudio::ReportingFrequency> reportingFrequency() const; | ||
| 130 | |||
| 131 | /** Get the time series identifier. */ | ||
| 132 | boost::optional<openstudio::TimeSeriesIdentifier> timeSeries() const; | ||
| 133 | |||
| 134 | /** Get the key value identifier. */ | ||
| 135 | boost::optional<openstudio::KeyValueIdentifier> keyValues() const; | ||
| 136 | |||
| 137 | /** Returns true if this query has been vetted (expended) by a SqlFile. */ | ||
| 138 | bool vetted() const; | ||
| 139 | |||
| 140 | //@} | ||
| 141 | /** @name Setters */ | ||
| 142 | //@{ | ||
| 143 | |||
| 144 | /** Set the EnvironmentIdentifier. */ | ||
| 145 | void setEnvironment(const EnvironmentIdentifier& envId); | ||
| 146 | |||
| 147 | /** Set the ReportingFrequency. */ | ||
| 148 | void setReportingFrequency(const openstudio::ReportingFrequency& rf); | ||
| 149 | |||
| 150 | /** Set the TimeSeries NameIdentifier. */ | ||
| 151 | void setTimeSeries(const TimeSeriesIdentifier& tsId); | ||
| 152 | |||
| 153 | /** Set the KeyValues NameIdentifier. */ | ||
| 154 | void setKeyValues(const KeyValueIdentifier& kvId); | ||
| 155 | |||
| 156 | /** Remove any EnvironmentIdentifier. */ | ||
| 157 | void clearEnvironment(); | ||
| 158 | |||
| 159 | /** Remove any ReportingFrequency. */ | ||
| 160 | void clearReportingFrequency(); | ||
| 161 | |||
| 162 | /** Remove any TimeSeries. */ | ||
| 163 | void clearTimeSeries(); | ||
| 164 | |||
| 165 | /** Remove any KeyValues. */ | ||
| 166 | void clearKeyValues(); | ||
| 167 | |||
| 168 | //@} | ||
| 169 | |||
| 170 | protected: | ||
| 171 | // flag that is flipped by setters and SqlFile::expandQuery | ||
| 172 | friend class detail::SqlFile_Impl; | ||
| 173 | bool m_vetted; | ||
| 174 | |||
| 175 | private: | ||
| 176 | boost::optional<EnvironmentIdentifier> m_environment; | ||
| 177 | boost::optional<ReportingFrequency> m_reportingFrequency; | ||
| 178 | boost::optional<TimeSeriesIdentifier> m_timeSeries; | ||
| 179 | boost::optional<KeyValueIdentifier> m_keyValues; | ||
| 180 | }; | ||
| 181 | |||
| 182 | UTILITIES_API std::ostream& operator<<(std::ostream& os, const SqlFileTimeSeriesQuery& query); | ||
| 183 | |||
| 184 | using SqlFileTimeSeriesQueryVector = std::vector<SqlFileTimeSeriesQuery>; | ||
| 185 | |||
| 186 | /** If all queries have been vetted, returns the set of unique environment names. Otherwise, | ||
| 187 | * returns an empty set. */ | ||
| 188 | UTILITIES_API std::set<std::string> environmentPeriods(const std::vector<SqlFileTimeSeriesQuery>& queries); | ||
| 189 | |||
| 190 | /** If all queries have been vetted, returns the set of unique \link ReportingFrequency | ||
| 191 | * ReportingFrequencies \endlink. Otherwise, returns an empty set. */ | ||
| 192 | UTILITIES_API std::set<openstudio::ReportingFrequency> reportingFrequencies(const std::vector<openstudio::SqlFileTimeSeriesQuery>& queries); | ||
| 193 | |||
| 194 | /** If all queries have been vetted, returns the set of unique time series names. Otherwise, | ||
| 195 | * returns an empty set. */ | ||
| 196 | UTILITIES_API std::set<std::string> timeSeriesNames(const std::vector<SqlFileTimeSeriesQuery>& queries); | ||
| 197 | |||
| 198 | } // namespace openstudio | ||
| 199 | |||
| 200 | #endif // UTILITIES_SQL_SQLFILETIMESERIESQUERY_HPP | ||
| 201 |