[][src]Enum rustlogic::LogicNode

pub enum LogicNode {
    And(Box<LogicNode>, Box<LogicNode>),
    Or(Box<LogicNode>, Box<LogicNode>),
    Not(Box<LogicNode>),
    True,
    False,
    Variable(String),
}

An Enum of all the possible states of a head of a logical formula

Variants

AND operation of left and right

OR operation of left and right

NOT operation of child

True

True, always returns true

False

False, always returns false

Variable(String)

Variable, always returns value of variable passed in

Methods

impl LogicNode[src]

pub fn get_value_from_variables(
    &self,
    variables: &HashMap<&str, bool>
) -> Result<bool, String>
[src]

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!

pub fn get_value(&self) -> Result<bool, String>[src]

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!

pub fn contains_variable(&self) -> bool[src]

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());

pub fn get_variables(&self) -> Vec<String>[src]

Returns a sorted vector of all Variables used in a LogicNode

Examples

NOR

let nor = parse("~([a]|[b])")
    .expect("Unable to parse with variable");

// Will return ["a", "b"]
println!("Variables in nor: {:?}", nor.get_variables());

pub fn insert_formula(&self, variable: &str, formula: &LogicNode) -> LogicNode[src]

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])

Trait Implementations

impl Clone for LogicNode[src]

impl Debug for LogicNode[src]

impl Display for LogicNode[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.