pub enum LogicNode {
And(Box<LogicNode>, Box<LogicNode>),
Or(Box<LogicNode>, Box<LogicNode>),
Not(Box<LogicNode>),
True,
False,
Variable(String),
}
Expand description
An Enum of all the possible states of a head of a logical formula
Variants§
And(Box<LogicNode>, Box<LogicNode>)
AND operation of left and right
Or(Box<LogicNode>, Box<LogicNode>)
OR operation of left and right
Not(Box<LogicNode>)
NOT operation of child
True
True, always returns true
False
False, always returns false
Variable(String)
Variable, always returns value of variable passed in
Implementations§
source§impl LogicNode
impl LogicNode
sourcepub fn get_value_from_variables(
&self,
variables: &HashMap<&str, bool>
) -> Result<bool, String>
pub fn get_value_from_variables( &self, variables: &HashMap<&str, bool> ) -> Result<bool, String>
Will retrieve the value of a LogicNode given a HashMap of variables
Output
Will output a result type with either the value of the LogicNode given the Hashmap or will give a error with the variable missing from the ‘variables’ argument given.
Examples
X-OR
let xor = parse("([a]|[b])&~([a]&[b])")
.expect("Failed to parse xor gate");
let mut variable_map = HashMap::new();
variable_map.insert("a", true);
variable_map.insert("b", true);
let value_1 = xor.get_value_from_variables(&variable_map).unwrap();
println!("First value: {}!", value_1); // Will print false!
variable_map.insert("a", false);
let value_2 = xor.get_value_from_variables(&variable_map).unwrap();
println!("Second value: {}!", value_1); // Will print true!
variable_map.insert("b", false);
let value_3 = xor.get_value_from_variables(&variable_map).unwrap();
println!("Third value: {}!", value_1); // Will print false!
sourcepub fn get_value(&self) -> Result<bool, String>
pub fn get_value(&self) -> Result<bool, String>
Will retrieve the value of a LogicNode given purely the formula
Output
Will return a result with either the value of the LogicNode or an error value containing the name of the (or one of the) variable contained within the LogicNode.
Examples
Basic usage
let and_of_true_and_false = parse("0&1")
.expect("Unable to parse AND of true and false");
let or_of_true_and_false = parse("0|1")
.expect("Unable to parse OR of true and false");
let value_1 = and_of_true_and_false.get_value().unwrap();
println!("0&1: {}", value_1); // Will return false!
let value_2 = or_of_true_and_false.get_value().unwrap();
println!("0|1: {}", value_2); // Will return true!
sourcepub fn contains_variable(&self) -> bool
pub fn contains_variable(&self) -> bool
Will return whether a given LogicNode contains a variable
Examples
Basic usage
let with_variable = parse("([a]&0)")
.expect("Unable to parse with variable");
let without_variable = parse("(1&0)")
.expect("Unable to parse without variable");
// Will return true!
println!("First contains variable: {}", with_variable.contains_variable());
// Will return false!
println!("Second contains variable: {}", without_variable.contains_variable());
sourcepub fn get_variables(&self) -> Vec<String>
pub fn get_variables(&self) -> Vec<String>
sourcepub fn insert_formula(&self, variable: &str, formula: &LogicNode) -> LogicNode
pub fn insert_formula(&self, variable: &str, formula: &LogicNode) -> LogicNode
Inserts formula in place of variable
Examples
Insert AND in OR
let or = parse("[AND]|[b]").expect("Error parsing or");
let and_in_or = or.insert_formula(
"AND",
&parse("[x]&[y]").expect("Error parsing AND")
);
println!("{}", and_in_or); // Will print ( ( [x] & [y] ) | [b] )
sourcepub fn to_string_using_set(&self, operator_set: &OperatorSet) -> String
pub fn to_string_using_set(&self, operator_set: &OperatorSet) -> String
Convert a Logical node to a String using a certain Operator Set
Example
use rustlogic::operators;
// Create our logical node
let parsed = rustlogic::parse("(~[A]&[B])|[C]").unwrap();
// Create a string using the worded operator set
let worded = parsed.to_string_using_set(&operators::common_sets::worded());
// Will print "((NOT $[A] AND $[B]) OR $[C])"!
println!("{}", worded);
assert_eq!(worded, String::from("((NOT $[A] AND $[B]) OR $[C])"));