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
use crate::FnUpdate::*;
use crate::{BinaryOp, BooleanNetwork, FnUpdate, ParameterId, VariableId};
use std::collections::HashSet;
impl FnUpdate {
pub fn mk_true() -> FnUpdate {
FnUpdate::Const(true)
}
pub fn mk_false() -> FnUpdate {
FnUpdate::Const(false)
}
pub fn mk_var(id: VariableId) -> FnUpdate {
FnUpdate::Var(id)
}
pub fn mk_param(id: ParameterId, args: &[VariableId]) -> FnUpdate {
FnUpdate::Param(id, args.to_vec())
}
pub fn mk_not(inner: FnUpdate) -> FnUpdate {
FnUpdate::Not(Box::new(inner))
}
pub fn mk_binary(op: BinaryOp, left: FnUpdate, right: FnUpdate) -> FnUpdate {
FnUpdate::Binary(op, Box::new(left), Box::new(right))
}
pub fn negation(self) -> FnUpdate {
FnUpdate::mk_not(self)
}
pub fn and(self, other: FnUpdate) -> FnUpdate {
FnUpdate::mk_binary(BinaryOp::And, self, other)
}
pub fn or(self, other: FnUpdate) -> FnUpdate {
FnUpdate::mk_binary(BinaryOp::Or, self, other)
}
pub fn xor(self, other: FnUpdate) -> FnUpdate {
FnUpdate::mk_binary(BinaryOp::Xor, self, other)
}
pub fn implies(self, other: FnUpdate) -> FnUpdate {
FnUpdate::mk_binary(BinaryOp::Imp, self, other)
}
pub fn iff(self, other: FnUpdate) -> FnUpdate {
FnUpdate::mk_binary(BinaryOp::Iff, self, other)
}
pub fn as_const(&self) -> Option<bool> {
match self {
FnUpdate::Const(value) => Some(*value),
_ => None,
}
}
pub fn as_var(&self) -> Option<VariableId> {
match self {
FnUpdate::Var(value) => Some(*value),
_ => None,
}
}
pub fn as_param(&self) -> Option<(ParameterId, &[VariableId])> {
match self {
FnUpdate::Param(id, args) => Some((*id, args)),
_ => None,
}
}
pub fn as_not(&self) -> Option<&FnUpdate> {
match self {
FnUpdate::Not(inner) => Some(inner),
_ => None,
}
}
pub fn as_binary(&self) -> Option<(&FnUpdate, BinaryOp, &FnUpdate)> {
match self {
FnUpdate::Binary(op, l, r) => Some((l, *op, r)),
_ => None,
}
}
}
impl FnUpdate {
pub fn collect_arguments(&self) -> Vec<VariableId> {
fn r_arguments(function: &FnUpdate, args: &mut HashSet<VariableId>) {
match function {
Const(_) => (),
Var(id) => {
args.insert(*id);
}
Param(_, p_args) => {
for id in p_args {
args.insert(*id);
}
}
Not(inner) => r_arguments(inner, args),
Binary(_, l, r) => {
r_arguments(l, args);
r_arguments(r, args);
}
};
}
let mut args = HashSet::new();
r_arguments(self, &mut args);
let mut result: Vec<VariableId> = args.into_iter().collect();
result.sort();
result
}
pub fn collect_parameters(&self) -> Vec<ParameterId> {
fn r_parameters(function: &FnUpdate, params: &mut HashSet<ParameterId>) {
match function {
Const(_) => (),
Var(_) => (),
Param(id, _) => {
params.insert(*id);
}
Not(inner) => r_parameters(inner, params),
Binary(_, l, r) => {
r_parameters(l, params);
r_parameters(r, params);
}
};
}
let mut params = HashSet::new();
r_parameters(self, &mut params);
let mut result: Vec<ParameterId> = params.into_iter().collect();
result.sort();
result
}
pub fn to_string(&self, context: &BooleanNetwork) -> String {
match self {
FnUpdate::Const(value) => value.to_string(),
FnUpdate::Var(id) => context.get_variable_name(*id).to_string(),
FnUpdate::Not(inner) => format!("!{}", inner.to_string(context)),
FnUpdate::Binary(op, l, r) => {
format!("({} {} {})", l.to_string(context), op, r.to_string(context))
}
FnUpdate::Param(id, args) => {
if args.is_empty() {
context[*id].get_name().to_string()
} else {
let mut arg_string = format!("({}", context.get_variable_name(args[0]));
for arg in args.iter().skip(1) {
arg_string = format!("{}, {}", arg_string, context.get_variable_name(*arg));
}
format!("{}{})", context[*id].get_name(), arg_string)
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::{BinaryOp, BooleanNetwork, FnUpdate};
use std::convert::TryFrom;
#[test]
fn basic_fn_update_test() {
let bn = BooleanNetwork::try_from(
r"
a -> c
b -| c
# Note that this is not really a `valid` function but syntatically it is ok.
$c: a & (a | (a ^ (a => (a <=> !(f(a, b) | (true | false))))))
",
)
.unwrap();
let a = bn.as_graph().find_variable("a").unwrap();
let b = bn.as_graph().find_variable("b").unwrap();
let c = bn.as_graph().find_variable("c").unwrap();
let fun = bn.get_update_function(c).as_ref().unwrap();
let fun_string = fun.to_string(&bn);
assert_eq!(vec![a, b], fun.collect_arguments());
assert_eq!(
vec![bn.find_parameter("f").unwrap()],
fun.collect_parameters()
);
let mut bn = BooleanNetwork::try_from(
r"
a -> c
b -| c
",
)
.unwrap();
let id_f = bn.add_parameter("f", 2).unwrap();
bn.add_string_update_function("c", fun_string.as_str())
.unwrap();
assert_eq!(fun, bn.get_update_function(c).as_ref().unwrap());
let f_a_b = FnUpdate::mk_param(id_f, &vec![a, b]);
let f_a = FnUpdate::mk_var(a);
let mut fun_2 = f_a_b.or(FnUpdate::mk_true().or(FnUpdate::mk_false()));
fun_2 = f_a.clone().iff(fun_2.negation());
fun_2 = f_a.clone().implies(fun_2);
fun_2 = f_a.clone().xor(fun_2);
fun_2 = f_a.clone().or(fun_2);
fun_2 = f_a.clone().and(fun_2);
assert_eq!(fun, &fun_2);
let (_, op, r) = fun_2.as_binary().unwrap();
assert_eq!(BinaryOp::And, op);
let (_, op, r) = r.as_binary().unwrap();
assert_eq!(BinaryOp::Or, op);
let (_, op, r) = r.as_binary().unwrap();
assert_eq!(BinaryOp::Xor, op);
let (_, op, r) = r.as_binary().unwrap();
assert_eq!(BinaryOp::Imp, op);
let (l, op, r) = r.as_binary().unwrap();
assert_eq!(BinaryOp::Iff, op);
assert_eq!(a, l.as_var().unwrap());
let inner = r.as_not().unwrap();
let (l, _, r) = inner.as_binary().unwrap();
assert_eq!((id_f, vec![a, b].as_slice()), l.as_param().unwrap());
let (l, _, r) = r.as_binary().unwrap();
assert!(l.as_const().unwrap());
assert!(!r.as_const().unwrap());
}
}