Skip to main content

dsntk_workspace/
workspaces.rs

1//! # Container for decision model evaluators
2
3use crate::builder::WorkspaceBuilder;
4use crate::errors::*;
5use antex::ColorMode;
6use dsntk_common::Result;
7use dsntk_feel::context::FeelContext;
8use dsntk_feel::values::Value;
9use dsntk_model_evaluator::ModelEvaluator;
10use std::collections::HashMap;
11use std::path::PathBuf;
12use std::sync::Arc;
13
14/// Container for decision model evaluators.
15pub struct Workspaces {
16  /// Map: invocable path -> (workspace name, model namespace, model name, invocable name)
17  pub(crate) invocables: HashMap<String, (String, String, String, String)>,
18  /// Map: workspace name -> model evaluator
19  pub(crate) evaluators: HashMap<String, Arc<ModelEvaluator>>,
20}
21
22impl Workspaces {
23  /// Creates a new [Workspaces] and loads decision models from specified directory.
24  pub fn new(dirs: &[PathBuf], colors: ColorMode, verbose: bool) -> Self {
25    let builder = WorkspaceBuilder::new(dirs, colors, verbose);
26    Self {
27      invocables: builder.invocables,
28      evaluators: builder.evaluators,
29    }
30  }
31
32  /// Evaluates invocable identified by invocable path.
33  pub fn evaluate(&self, invocable_path: &str, input_data: &FeelContext) -> Result<Value> {
34    if let Some((workspace_name, model_namespace, model_name, invocable_name)) = self.invocables.get(invocable_path)
35      && let Some(evaluator) = self.evaluators.get(workspace_name)
36    {
37      return Ok(evaluator.evaluate_invocable(model_namespace, model_name, invocable_name, input_data));
38    }
39    Err(err_invocable_not_found(invocable_path))
40  }
41}