Enum rustlogic::LogicNode

source ·
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

source

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!
source

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!
source

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

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

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

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

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

Trait Implementations§

source§

impl Clone for LogicNode

source§

fn clone(&self) -> LogicNode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LogicNode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for LogicNode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.