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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#![allow(deprecated)]
use crate::bdd_params::{BddParameterEncoder, BddParams};
use crate::{BinaryOp, BooleanNetwork, FnUpdate, VariableId};
use biodivine_lib_bdd::boolean_expression::BooleanExpression;
use biodivine_lib_bdd::{Bdd, BddValuation, BddVariableSet, ValuationsOfClauseIterator};

impl BooleanNetwork {
    /// *Legacy* function for creating a fully instantiated `BooleanNetwork` using parameters.
    ///
    /// In later revisions, this should be part of `BddParameterEncoder`.
    pub fn make_witness(
        &self,
        params: &BddParams,
        encoder: &BddParameterEncoder,
    ) -> BooleanNetwork {
        let valuation = params.as_bdd().sat_witness();
        if valuation.is_none() {
            panic!("Cannot create witness for empty parameter set.");
        }
        self.make_witness_for_valuation(valuation.unwrap(), encoder)
    }

    /// *Legacy* function for creating a fully instantiated `BooleanNetwork` using parameters.
    ///
    /// In later revisions, this should be part of `BddParameterEncoder`.
    pub fn make_witness_for_valuation(
        &self,
        valuation: BddValuation,
        encoder: &BddParameterEncoder,
    ) -> BooleanNetwork {
        // First, make functions for explicit parameters:
        let mut explicit_parameter_expressions = Vec::with_capacity(self.parameters.len());
        for i_p in 0..self.parameters.len() {
            let parameter = &self.parameters[i_p];
            let parameter_input_table = &encoder.explicit_function_tables[i_p];
            let num_inputs = parameter.arity as u16;
            if num_inputs == 0 {
                assert_eq!(parameter_input_table.len(), 1);
                explicit_parameter_expressions.push(BooleanExpression::Const(
                    valuation.value(parameter_input_table[0]),
                ));
            } else {
                let parameter_function_input_vars = BddVariableSet::new_anonymous(num_inputs);

                let variables_and_valuations = parameter_input_table
                    .iter()
                    .zip(ValuationsOfClauseIterator::new_unconstrained(num_inputs));

                // initially, the function is just false
                let mut function_bdd = parameter_function_input_vars.mk_false();
                for (bdd_variable, input_valuation) in variables_and_valuations {
                    if valuation.value(*bdd_variable) {
                        // and for each `1` in the function table, we add the input valuation
                        let input_valuation_bdd = Bdd::from(input_valuation.clone());
                        function_bdd = function_bdd.or(&input_valuation_bdd);
                    }
                }

                let parameter_expression =
                    function_bdd.to_boolean_expression(&parameter_function_input_vars);
                explicit_parameter_expressions.push(parameter_expression);
            }
        }

        let mut result = self.clone();
        for i_var in 0..self.graph.num_vars() {
            if let Some(function) = &self.update_functions[i_var] {
                // Replace parameters in the explicit function with actual functions
                let resolved_function =
                    *Self::replace_parameters(function, &explicit_parameter_expressions);
                result.update_functions[i_var] = Some(resolved_function);
            } else {
                // Build an anonymous update function
                let regulators = &encoder.regulators[i_var];
                let num_inputs = regulators.len() as u16;
                let function_input_table = &encoder.implicit_function_tables[i_var];
                let function_input_vars = BddVariableSet::new_anonymous(num_inputs);

                let variables_and_valuations = function_input_table
                    .iter()
                    .zip(ValuationsOfClauseIterator::new_unconstrained(num_inputs));
                let mut function_bdd = function_input_vars.mk_false();
                for (bdd_variable, input_valuation) in variables_and_valuations {
                    if valuation.value(*bdd_variable) {
                        let input_valuation_bdd = Bdd::from(input_valuation);
                        function_bdd = function_bdd.or(&input_valuation_bdd);
                    }
                }

                let function_expression = function_bdd.to_boolean_expression(&function_input_vars);
                result.update_functions[i_var] = Some(*Self::expression_to_fn_update(
                    &function_expression,
                    regulators,
                ));
            }
        }

        result.parameters.clear();
        result.parameter_to_index.clear();

        result
    }

    fn replace_parameters(
        update_function: &FnUpdate,
        parameter_expressions: &[BooleanExpression],
    ) -> Box<FnUpdate> {
        Box::new(match update_function {
            FnUpdate::Const(value) => FnUpdate::Const(*value),
            FnUpdate::Var(id) => FnUpdate::Var(*id),
            FnUpdate::Not(a) => FnUpdate::Not(Self::replace_parameters(a, parameter_expressions)),
            FnUpdate::Binary(op, a, b) => FnUpdate::Binary(
                *op,
                Self::replace_parameters(a, parameter_expressions),
                Self::replace_parameters(b, parameter_expressions),
            ),
            FnUpdate::Param(id, args) => {
                let parameter_expression = &parameter_expressions[id.0];
                *Self::expression_to_fn_update(parameter_expression, args)
            }
        })
    }

    fn expression_to_fn_update(
        expression: &BooleanExpression,
        args: &[VariableId],
    ) -> Box<FnUpdate> {
        Box::new(match expression {
            BooleanExpression::Const(value) => FnUpdate::Const(*value),
            BooleanExpression::Iff(a, b) => FnUpdate::Binary(
                BinaryOp::Iff,
                Self::expression_to_fn_update(a, args),
                Self::expression_to_fn_update(b, args),
            ),
            BooleanExpression::Imp(a, b) => FnUpdate::Binary(
                BinaryOp::Imp,
                Self::expression_to_fn_update(a, args),
                Self::expression_to_fn_update(b, args),
            ),
            BooleanExpression::Or(a, b) => FnUpdate::Binary(
                BinaryOp::Or,
                Self::expression_to_fn_update(a, args),
                Self::expression_to_fn_update(b, args),
            ),
            BooleanExpression::And(a, b) => FnUpdate::Binary(
                BinaryOp::And,
                Self::expression_to_fn_update(a, args),
                Self::expression_to_fn_update(b, args),
            ),
            BooleanExpression::Xor(a, b) => FnUpdate::Binary(
                BinaryOp::Xor,
                Self::expression_to_fn_update(a, args),
                Self::expression_to_fn_update(b, args),
            ),
            BooleanExpression::Not(a) => FnUpdate::Not(Self::expression_to_fn_update(a, args)),
            BooleanExpression::Variable(name) => {
                // name is x_i, so we can strip x_ away and parse it
                let name = &name[2..];
                let arg_index: usize = name.parse().unwrap();
                FnUpdate::Var(args[arg_index])
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::async_graph::AsyncGraph;
    use crate::BooleanNetwork;
    use std::convert::TryFrom;

    #[test]
    pub fn make_witness_0() {
        let network = BooleanNetwork::try_from(
            "
            a -> b
            a -?? a
            b -|? c
            a ->? c
            c -? a
            c -| d
            $a: a & (p(c) => (c | c))
            $b: p(a) <=> q(a, a)
            $c: q(b, b) => !(b ^ k)
        ",
        )
        .unwrap();
        let graph = AsyncGraph::new(network).unwrap();
        let witness = graph.make_witness(graph.unit_params());
        let witness_string = BooleanNetwork::try_from(
            "
            a -> b
            a -?? a
            b -|? c
            a ->? c
            c -? a
            c -| d
            # validity of the functions has been manually verified
            $a: (a & (true => (c | c)))
            $b: (true <=> (a & a))
            $c: ((b & b) => !(b ^ false))
            $d: !c
        ",
        )
        .unwrap();
        assert_eq!(witness, witness_string);
        // turn the witness into a graph again - there should be exactly one parametrisation:
        let graph = AsyncGraph::new(witness).unwrap();
        assert_eq!(graph.unit_params().cardinality(), 1.0);
    }

    #[test]
    pub fn make_witness_1() {
        let network = BooleanNetwork::try_from(
            "
            SSF -> SWI5

            SSF -> ACE2

            SBF -> SSF
            HCM1 -> SSF

            MBF -> YHP1
            SBF -> YHP1

            MBF -> HCM1
            SBF -> HCM1

            MBF -> YOX1
            SBF -> YOX1

            CLN3 -> SBF
            MBF -> SBF
            YHP1 -| SBF
            YOX1 -| SBF

            CLN3 -> MBF

            ACE2 -> CLN3
            YHP1 -| CLN3
            SWI5 -> CLN3
            YOX1 -| CLN3
        ",
        )
        .unwrap();
        let graph = AsyncGraph::new(network).unwrap();
        let witness = graph.make_witness(graph.unit_params());
        let witness_string = BooleanNetwork::try_from(
            "
            SSF -> SWI5

            SSF -> ACE2

            SBF -> SSF
            HCM1 -> SSF

            MBF -> YHP1
            SBF -> YHP1

            MBF -> HCM1
            SBF -> HCM1

            MBF -> YOX1
            SBF -> YOX1

            CLN3 -> SBF
            MBF -> SBF
            YHP1 -| SBF
            YOX1 -| SBF

            CLN3 -> MBF

            ACE2 -> CLN3
            YHP1 -| CLN3
            SWI5 -> CLN3
            YOX1 -| CLN3
            $ACE2: SSF
            $CLN3: ACE2 | SWI5 | !YHP1 | !YOX1
            $HCM1: MBF | SBF
            $MBF: CLN3
            $SBF: CLN3 | MBF | !YHP1 | !YOX1
            $SSF: HCM1 | SBF
            $SWI5: SSF
            $YHP1: MBF | SBF
            $YOX1: MBF | SBF
        ",
        )
        .unwrap();

        assert_eq!(witness, witness_string);

        let graph = AsyncGraph::new(witness).unwrap();
        assert_eq!(graph.unit_params().cardinality(), 1.0);
    }
}