-
Notifications
You must be signed in to change notification settings - Fork 12
Initial lambda extraction #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lukelowry
wants to merge
3
commits into
develop
Choose a base branch
from
lukel/lambda-dev
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @file ConfigurationChecks.hpp | ||
| * @author Luke Lowery (lukel@tamu.edu) | ||
| * @brief Collects the configuration problems found by a model. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace Model | ||
| { | ||
| /** | ||
| * @brief Collects the configuration problems found by a model. | ||
| * | ||
| * A model's verify() fills one instance with a message for every | ||
| * condition that does not hold and returns it. The caller decides how | ||
| * the messages are reported; passed() is true when there are none. | ||
| */ | ||
| class ConfigurationChecks | ||
| { | ||
| public: | ||
| /// Record a problem. | ||
| void fail(std::string message) | ||
| { | ||
| errors_.push_back(std::move(message)); | ||
| } | ||
|
|
||
| /// Record the message when the condition does not hold. | ||
| void check(bool condition, std::string message) | ||
| { | ||
| if (!condition) | ||
| { | ||
| fail(std::move(message)); | ||
| } | ||
| } | ||
|
|
||
| /// True when no check has failed. | ||
| bool passed() const | ||
| { | ||
| return errors_.empty(); | ||
| } | ||
|
|
||
| /// Every recorded message, in the order the checks ran. | ||
| const std::vector<std::string>& errors() const | ||
| { | ||
| return errors_; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<std::string> errors_; | ||
| }; | ||
| } // namespace Model | ||
| } // namespace GridKit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| /** | ||
| * @file ParameterReader.hpp | ||
| * @author Luke Lowery (lukel@tamu.edu) | ||
| * @brief Typed parameter loading from model data containers. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <map> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <variant> | ||
|
|
||
| #include <magic_enum/magic_enum.hpp> | ||
|
|
||
| #include <GridKit/Model/PhasorDynamics/ModelData.hpp> | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace Model | ||
| { | ||
| /** | ||
| * @brief Reads typed parameters out of a model data container. | ||
| * | ||
| * A provided value with the wrong type or a non-finite value cannot | ||
| * produce a valid model, so every method throws std::invalid_argument | ||
| * at the first such value; the message names the model and the | ||
| * parameter. An omitted optional parameter leaves the target untouched. | ||
| * Real parameters accept integer values. | ||
| * | ||
| * @tparam ModelDataT A model data container satisfying | ||
| * PhasorDynamics::ModelData. | ||
| */ | ||
| template <PhasorDynamics::ModelData ModelDataT> | ||
| class ParameterReader | ||
|
superwhiskers marked this conversation as resolved.
|
||
| { | ||
| public: | ||
| using RealT = typename ModelDataT::RealT; | ||
| using IdxT = typename ModelDataT::IdxT; | ||
| using ParametersT = typename ModelDataT::Parameters; | ||
|
|
||
| /** | ||
| * @param[in] data Model data container to read from. | ||
| * @param[in] model Model name used in rejection messages. | ||
| */ | ||
| ParameterReader(const ModelDataT& data, const char* model) | ||
| : parameters_(data.parameters), | ||
| model_(model) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Load an optional real parameter. Integer values are accepted. | ||
| * | ||
| * @param[in] key Parameter to look up. | ||
| * @param[out] target Stores the finite numeric value when provided. | ||
| * @return true when the parameter was provided. | ||
| */ | ||
| bool loadReal(ParametersT key, RealT& target) const | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be |
||
| { | ||
| if (!parameters_.contains(key)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| const auto& value = parameters_.at(key); | ||
| RealT parsed_value{}; | ||
| if (const auto* real_value = std::get_if<RealT>(&value)) | ||
| { | ||
| parsed_value = *real_value; | ||
| } | ||
| else if (const auto* index_value = std::get_if<IdxT>(&value)) | ||
| { | ||
| parsed_value = static_cast<RealT>(*index_value); | ||
| } | ||
| else | ||
| { | ||
| reject(key, "must be numeric"); | ||
| } | ||
|
|
||
| if (!std::isfinite(parsed_value)) | ||
| { | ||
| reject(key, "must be finite"); | ||
| } | ||
|
|
||
| target = parsed_value; | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Load a real parameter that must be provided. | ||
| * | ||
| * @param[in] key Parameter to look up. | ||
| * @param[out] target Stores the finite numeric value. | ||
| * @return true, since a missing parameter is rejected. | ||
| */ | ||
| bool requireReal(ParametersT key, RealT& target) const | ||
| { | ||
| if (!parameters_.contains(key)) | ||
| { | ||
| reject(key, "is required"); | ||
| } | ||
| return loadReal(key, target); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Load an optional boolean switch parameter. | ||
| * | ||
| * @param[in] key Parameter to look up. | ||
| * @param[out] target Stores the boolean value when provided. | ||
| * @return true when the parameter was provided. | ||
| */ | ||
| bool loadSwitch(ParametersT key, bool& target) const | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likewise, this return value could be |
||
| { | ||
| if (!parameters_.contains(key)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| const auto& value = parameters_.at(key); | ||
| if (const auto* bool_value = std::get_if<bool>(&value)) | ||
| { | ||
| target = *bool_value; | ||
| return true; | ||
| } | ||
|
|
||
| reject(key, "must be boolean"); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Load a switch parameter that must be provided. A boolean or an | ||
| * integer 0/1 value is accepted. | ||
| * | ||
| * @param[in] key Parameter to look up. | ||
| * @param[out] target Stores the switch value. | ||
| * @return true, since a missing parameter is rejected. | ||
| */ | ||
| bool requireSwitch(ParametersT key, bool& target) const | ||
| { | ||
| if (!parameters_.contains(key)) | ||
| { | ||
| reject(key, "is required"); | ||
| } | ||
|
|
||
| const auto& value = parameters_.at(key); | ||
| if (const auto* bool_value = std::get_if<bool>(&value)) | ||
| { | ||
| target = *bool_value; | ||
| return true; | ||
| } | ||
| if (const auto* index_value = std::get_if<IdxT>(&value); | ||
| index_value && (*index_value == 0 || *index_value == 1)) | ||
| { | ||
| target = (*index_value == 1); | ||
| return true; | ||
| } | ||
|
|
||
| reject(key, "must be bool or 0/1"); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Load an optional integer selector parameter. | ||
| * | ||
| * @param[in] key Parameter to look up. | ||
| * @param[out] target Stores the integer value when provided. | ||
| * @return true when the parameter was provided. | ||
| */ | ||
| bool loadSelector(ParametersT key, IdxT& target) const | ||
| { | ||
| if (!parameters_.contains(key)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| const auto& value = parameters_.at(key); | ||
| if (const auto* index_value = std::get_if<IdxT>(&value)) | ||
| { | ||
| target = *index_value; | ||
| return true; | ||
| } | ||
|
|
||
| reject(key, "must be an integer selector"); | ||
| } | ||
|
|
||
| private: | ||
| /// Reject the model data with a message naming the model and parameter. | ||
| [[noreturn]] void reject(ParametersT key, const char* reason) const | ||
| { | ||
| throw std::invalid_argument(std::string(model_) + ": parameter '" | ||
| + std::string(magic_enum::enum_name(key)) | ||
| + "' " + reason); | ||
| } | ||
|
|
||
| const std::map<ParametersT, std::variant<bool, RealT, IdxT>>& parameters_; | ||
| const char* model_; | ||
| }; | ||
| } // namespace Model | ||
| } // namespace GridKit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the end goal should be to report errors using set error codes or structured errors instead of strings, but for now this is fine