|
| 1 | +//! Contains the structure to hold useful data to speed-up the computation. |
| 2 | +
|
| 3 | +use crate::evaluation::mark_duplicate_subform::{ |
| 4 | + mark_duplicates_canonized_multiple, mark_duplicates_canonized_single, |
| 5 | +}; |
| 6 | +use crate::preprocessing::node::HctlTreeNode; |
| 7 | + |
| 8 | +use biodivine_lib_param_bn::symbolic_async_graph::GraphColoredVertices; |
| 9 | + |
| 10 | +use std::collections::HashMap; |
| 11 | + |
| 12 | +/// Struct holding information for efficient caching during the main computation. |
| 13 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 14 | +pub struct EvalContext { |
| 15 | + /// Duplicate sub-formulae and their counter |
| 16 | + pub duplicates: HashMap<String, i32>, |
| 17 | + /// Cached sub-formulae and their result + corresponding mapping of variable renaming |
| 18 | + pub cache: HashMap<String, (GraphColoredVertices, HashMap<String, String>)>, |
| 19 | +} |
| 20 | + |
| 21 | +impl EvalContext { |
| 22 | + /// Instantiate the struct with precomputed duplicates and empty cache. |
| 23 | + pub fn new(duplicates: HashMap<String, i32>) -> EvalContext { |
| 24 | + EvalContext { |
| 25 | + duplicates, |
| 26 | + cache: HashMap::new(), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Instantiate the struct with precomputed duplicates and empty cache. |
| 31 | + pub fn from_single_tree(tree: &HctlTreeNode) -> EvalContext { |
| 32 | + EvalContext { |
| 33 | + duplicates: mark_duplicates_canonized_single(tree), |
| 34 | + cache: HashMap::new(), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Instantiate the struct with precomputed duplicates and empty cache. |
| 39 | + pub fn from_multiple_trees(trees: &Vec<HctlTreeNode>) -> EvalContext { |
| 40 | + EvalContext { |
| 41 | + duplicates: mark_duplicates_canonized_multiple(trees), |
| 42 | + cache: HashMap::new(), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Get the duplicates field containing the sub-formulae and their counter. |
| 47 | + pub fn get_duplicates(&self) -> HashMap<String, i32> { |
| 48 | + self.duplicates.clone() |
| 49 | + } |
| 50 | + |
| 51 | + /// Get the cache field containing the cached sub-formulae, their result and var renaming. |
| 52 | + pub fn get_cache(&self) -> HashMap<String, (GraphColoredVertices, HashMap<String, String>)> { |
| 53 | + self.cache.clone() |
| 54 | + } |
| 55 | + |
| 56 | + /// Extend the standard evaluation context with "pre-computed cache" regarding wild-card nodes. |
| 57 | + pub fn extend_context_with_wild_cards( |
| 58 | + &mut self, |
| 59 | + substitution_context: HashMap<String, GraphColoredVertices>, |
| 60 | + ) { |
| 61 | + // For each `wild-card proposition` in `substitution_context`, increment its duplicate |
| 62 | + // counter. That way, the first occurrence will also be treated as duplicate and taken from |
| 63 | + // cache directly. |
| 64 | + for (prop_name, raw_set) in substitution_context.into_iter() { |
| 65 | + // we dont have to compute canonical sub-formula, because there are no HCTL variables |
| 66 | + let sub_formula = format!("%{}%", prop_name); |
| 67 | + if self.duplicates.contains_key(sub_formula.as_str()) { |
| 68 | + self.duplicates.insert( |
| 69 | + sub_formula.clone(), |
| 70 | + self.duplicates[sub_formula.as_str()] + 1, |
| 71 | + ); |
| 72 | + } else { |
| 73 | + self.duplicates.insert(sub_formula.clone(), 1); |
| 74 | + } |
| 75 | + |
| 76 | + // Add the raw sets directly to the cache to be used during evaluation. |
| 77 | + // The mapping for var renaming is empty, because there are no HCTL vars. |
| 78 | + self.cache.insert(sub_formula, (raw_set, HashMap::new())); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use crate::evaluation::eval_context::EvalContext; |
| 86 | + use crate::mc_utils::get_extended_symbolic_graph; |
| 87 | + use crate::preprocessing::parser::{parse_extended_formula, parse_hctl_formula}; |
| 88 | + |
| 89 | + use biodivine_lib_param_bn::BooleanNetwork; |
| 90 | + |
| 91 | + use std::collections::HashMap; |
| 92 | + |
| 93 | + #[test] |
| 94 | + /// Test equivalent ways to generate EvalContext object. |
| 95 | + fn test_eval_context_creation() { |
| 96 | + let formula = "!{x}: (AX {x} & AX {x})"; |
| 97 | + let syntax_tree = parse_hctl_formula(formula).unwrap(); |
| 98 | + |
| 99 | + let expected_duplicates = HashMap::from([("(Ax {var0})".to_string(), 1)]); |
| 100 | + let eval_info = EvalContext::new(expected_duplicates.clone()); |
| 101 | + |
| 102 | + assert_eq!(eval_info, EvalContext::from_single_tree(&syntax_tree)); |
| 103 | + assert_eq!( |
| 104 | + eval_info, |
| 105 | + EvalContext::from_multiple_trees(&vec![syntax_tree]) |
| 106 | + ); |
| 107 | + assert_eq!(eval_info.get_duplicates(), expected_duplicates); |
| 108 | + |
| 109 | + // check that cache is always initially empty |
| 110 | + assert!(eval_info.get_cache().is_empty()); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + /// Test extension of the EvalContext with "pre-computed cache" regarding wild-card nodes. |
| 115 | + fn test_eval_context_extension() { |
| 116 | + // prepare placeholder BN and STG |
| 117 | + let bn = BooleanNetwork::try_from_bnet("v1, v1").unwrap(); |
| 118 | + let stg = get_extended_symbolic_graph(&bn, 2).unwrap(); |
| 119 | + |
| 120 | + let formula = "!{x}: 3{y}: (@{x}: ~{y} & %subst%) & (@{y}: %subst%)"; |
| 121 | + let syntax_tree = parse_extended_formula(formula).unwrap(); |
| 122 | + let mut eval_info = EvalContext::from_single_tree(&syntax_tree); |
| 123 | + |
| 124 | + assert_eq!( |
| 125 | + eval_info.get_duplicates(), |
| 126 | + HashMap::from([("%subst%".to_string(), 1)]) |
| 127 | + ); |
| 128 | + assert_eq!(eval_info.get_cache(), HashMap::new()); |
| 129 | + |
| 130 | + let raw_set = stg.mk_unit_colored_vertices(); |
| 131 | + let substitution_context = HashMap::from([("subst".to_string(), raw_set.clone())]); |
| 132 | + eval_info.extend_context_with_wild_cards(substitution_context); |
| 133 | + let expected_cache = HashMap::from([("%subst%".to_string(), (raw_set, HashMap::new()))]); |
| 134 | + |
| 135 | + assert_eq!( |
| 136 | + eval_info.get_duplicates(), |
| 137 | + HashMap::from([("%subst%".to_string(), 2)]) |
| 138 | + ); |
| 139 | + assert_eq!(eval_info.get_cache(), expected_cache); |
| 140 | + } |
| 141 | +} |
0 commit comments