我想使用Rust crate 'boolean_expression'来实现一个简单的数字逻辑电路。例如,假设我们有一个简单的电路,该电路由两个2输入逻辑门,总共三个二进制输入信号A,B和C和一个二进制输出信号E组成。
假设第一栅极为具有输入信号A和B以及输出信号D的2输入与门。第二栅极为具有输入C和D以及输出E的2输入或门。
Circuit input signals: A, B, C
Circuit output signal: E
Logic gate equations:
D = A && B
E = C || D
where '&&' is the Boolean 'AND' and '||' is the Boolean 'OR'
让我们进一步假设,我们希望使用简化的有序二进制决策图(BDD)实现该电路的模型。在一个BDD包的常见实现中(例如Python),我将使用某种命令(例如“let A = BDD.var”或类似的命令)将A,B和C定义为 bool 变量。我将上面显示的门方程写为 bool 表达式。我可以对输入A,B,C应用一些固定的 bool 值(“真”,“假”或“可能”),或者我只是问“输入A,B和C的哪些组合定义了输出E?”
现在,我的问题是,使用Rust crate “boolean_expression”的教导,我该怎么做?如何将输入A定义为 bool 变量?如何定义两个门?如何检查输出信号E以确定输入A,B和C的哪些组合定义输出E?简而言之,我要键入什么(具体来说)以使用Rust crate “boolean_expression”的功能?如果您能告诉我要输入的内容-全部-我可以从那里拿走。谢谢你。
而且,当然,我计划实现的实际电路比这个简单示例要复杂得多。
最佳答案
Rust crate “boolean_expression”的作者提供了以下答案作为“main.rs”文件。
// Add this line to dependencies: boolean_expression = "0.3.9"
// for example, add the line under dependencies in Cargo.toml
use boolean_expression::*;
fn main() {
let mut bdd = BDD::new();
// Build the circuit. The arguments passed to 'bdd.terminal()'
// can be any type, as long as they are all the same type (the
// 'BDD' type is polymorphic on its labels); here, we use strings.
let a = bdd.terminal("A");
let b = bdd.terminal("B");
let c = bdd.terminal("C");
let d = bdd.and(a, b);
let e = bdd.or(c, d);
// Can 'e' be true at all? It should be.
assert!(bdd.sat(e));
// Print the expression equivalent of 'e'.
println!("e = {:?}", bdd.to_expr(e));
// Determine a satisfying assignment of [a, b, c] to let `e` be TRUE.
// `sat` is an Option<HashMap<T, bool>>:
// - if `None`, then there is no satisfying assignment.
// (We already checked for this case above.)
// - if `Some(hashmap)`, then the hashmap gives a mapping from
// values of type T (here, the strings we used above) to
// boolean values.
let sat = bdd.sat_one(e);
match sat {
None => panic!("Shouldn't happen!"),
Some(vars) => {
for (var, value) in &vars {
println!("Satisfying assignment: var {} = {}", var, value);
}
}
}
}
关于rust - 如何使用Rust crate 'boolean_expression'来实现简单的逻辑电路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59109453/