1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::algorithms::eval_static::encode::*;
use crate::sketchbook::properties::static_props::StatPropertyType;
use crate::sketchbook::Sketch;

use biodivine_lib_param_bn::BooleanNetwork;

/// Processed static property encoded in FOL.
///
/// If we ever need more variants than just FOL prop, make this enum (like
/// the one used for dynamic props).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProcessedStatProp {
    pub id: String,
    pub formula: String,
}

/// Simplified constructors for processed dynamic properties.
impl ProcessedStatProp {
    /// Create FOL `ProcessedStatProp` instance.
    pub fn mk_fol(id: &str, formula: &str) -> ProcessedStatProp {
        ProcessedStatProp {
            id: id.to_string(),
            formula: formula.to_string(),
        }
    }

    /// Get ID of the processed property.
    pub fn id(&self) -> &str {
        &self.id
    }
}

/// Process static properties from a sketch, converting them into one of the supported
/// `ProcessedStatProp` variants. Currently, all properties are encoded into FOL, but we
/// might support some other preprocessing in future.
pub fn process_static_props(
    sketch: &Sketch,
    bn: &BooleanNetwork,
) -> Result<Vec<ProcessedStatProp>, String> {
    let mut static_props = sketch.properties.stat_props().collect::<Vec<_>>();
    // sort properties by IDs for deterministic computation times (and get rid of the IDs)
    static_props.sort_by(|(a_id, _), (b_id, _)| a_id.cmp(b_id));

    let mut processed_props = Vec::new();
    for (id, stat_prop) in static_props {
        // currently, everything is encoded into first-order logic (into a "generic" property)
        let stat_prop_processed = match stat_prop.get_prop_data() {
            StatPropertyType::GenericStatProp(prop) => {
                ProcessedStatProp::mk_fol(id.as_str(), prop.processed_formula.as_str())
            }
            StatPropertyType::RegulationEssential(prop)
            | StatPropertyType::RegulationEssentialContext(prop) => {
                let input_name = prop.input.clone().unwrap();
                let target_name = prop.target.clone().unwrap();
                let mut formula = encode_regulation_essentiality(
                    input_name.as_str(),
                    target_name.as_str(),
                    prop.clone().value,
                    bn,
                );
                if let Some(context_formula) = &prop.context {
                    formula = encode_property_in_context(context_formula, &formula);
                }
                ProcessedStatProp::mk_fol(id.as_str(), &formula)
            }
            StatPropertyType::RegulationMonotonic(prop)
            | StatPropertyType::RegulationMonotonicContext(prop) => {
                let input_name = prop.input.clone().unwrap();
                let target_name = prop.target.clone().unwrap();
                let mut formula = encode_regulation_monotonicity(
                    input_name.as_str(),
                    target_name.as_str(),
                    prop.clone().value,
                    bn,
                );
                if let Some(context_formula) = &prop.context {
                    formula = encode_property_in_context(context_formula, &formula);
                }
                ProcessedStatProp::mk_fol(id.as_str(), &formula)
            }
            StatPropertyType::FnInputEssential(prop)
            | StatPropertyType::FnInputEssentialContext(prop) => {
                let fn_id = prop.target.clone().unwrap();
                let input_idx = prop.input_index.unwrap();
                let number_inputs = sketch.model.get_uninterpreted_fn_arity(&fn_id)?;
                let mut formula = encode_essentiality(
                    number_inputs,
                    input_idx,
                    fn_id.as_str(),
                    prop.clone().value,
                );
                if let Some(context_formula) = &prop.context {
                    formula = encode_property_in_context(context_formula, &formula);
                }
                ProcessedStatProp::mk_fol(id.as_str(), &formula)
            }
            StatPropertyType::FnInputMonotonic(prop)
            | StatPropertyType::FnInputMonotonicContext(prop) => {
                let fn_id = prop.target.clone().unwrap();
                let input_idx = prop.input_index.unwrap();
                let number_inputs = sketch.model.get_uninterpreted_fn_arity(&fn_id)?;
                let mut formula = encode_monotonicity(
                    number_inputs,
                    input_idx,
                    fn_id.as_str(),
                    prop.clone().value,
                );
                if let Some(context_formula) = &prop.context {
                    formula = encode_property_in_context(context_formula, &formula);
                }
                ProcessedStatProp::mk_fol(id.as_str(), &formula)
            }
        };
        processed_props.push(stat_prop_processed)
    }

    Ok(processed_props)
}