-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from NREL/rjf/query-time-state-features
allow query to overwrite state features
- Loading branch information
Showing
8 changed files
with
166 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod search_app; | ||
pub mod search_app_graph_ops; | ||
pub mod search_app_ops; | ||
pub mod search_app_result; |
This file contains 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 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,57 @@ | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
use itertools::Itertools; | ||
use routee_compass_core::model::{ | ||
access::access_model::AccessModel, | ||
state::{state_error::StateError, state_feature::StateFeature}, | ||
traversal::traversal_model::TraversalModel, | ||
}; | ||
|
||
use crate::app::compass::config::config_json_extension::ConfigJsonExtensions; | ||
|
||
/// collects the state features to use in this search. the features are collected in | ||
/// the following order: | ||
/// 1. from the traversal model | ||
/// 2. from the access model | ||
/// 3. optionally from the query itself | ||
/// using the order above, each new source optionally overwrites any existing feature | ||
/// by name (tuple index 0) as long as they match in StateFeature::get_feature_name and | ||
/// StateFeature::get_feature_unit_name. | ||
pub fn collect_features( | ||
query: &serde_json::Value, | ||
traversal_model: Arc<dyn TraversalModel>, | ||
access_model: Arc<dyn AccessModel>, | ||
) -> Result<Vec<(String, StateFeature)>, StateError> { | ||
// prepare the set of features for this state model | ||
let model_features = traversal_model | ||
.state_features() | ||
.into_iter() | ||
.chain(access_model.state_features()) | ||
.collect::<HashMap<_, _>>(); | ||
// build the state model. inject state features from the traversal and access models | ||
// and then allow the user to optionally override any initial conditions for those | ||
// state features. | ||
let user_features_option: Option<HashMap<String, StateFeature>> = query | ||
.get_config_serde_optional(&"state_features", &"query") | ||
.map_err(|e| StateError::BuildError(e.to_string()))?; | ||
let user_features = user_features_option | ||
.unwrap_or_default() | ||
.into_iter() | ||
.map(|(name, feature)| match model_features.get(&name) { | ||
None => { | ||
let fnames = model_features.keys().join(","); | ||
Err(StateError::UnknownStateVariableName(name, fnames)) | ||
} | ||
Some(existing) if existing.get_feature_type() != feature.get_feature_type() => { | ||
Err(StateError::UnexpectedFeatureType( | ||
existing.get_feature_type(), | ||
feature.get_feature_type(), | ||
)) | ||
} | ||
Some(_) => Ok((name, feature)), | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
let mut added_features: Vec<(String, StateFeature)> = model_features.into_iter().collect_vec(); | ||
added_features.extend(user_features); | ||
Ok(added_features) | ||
} |