Function rustlogic::custom_parse

source ·
pub fn custom_parse(
    input_string: &str,
    operator_set: &OperatorSet
) -> Result<LogicNode, usize>
Expand description

Parsing of logic formulas with non-default operator symbols Use the OperatorSet Struct

Examples

Using a worded set

use rustlogic::operators;

// Define the set
let operator_set = operators::common_sets::worded();

let parsed = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set);

// -- snipp

Having a customized operator set

use rustlogic::operators;

// This will be our new symbol for the AND logical function
let new_and_operator = "<AND>";

// Then we take the default set and adjust the AND symbol
let custom_operator_set = operators::common_sets::default()
    .adjust_and(new_and_operator);

// Now we can use this set to parse logical strings
let parsed = rustlogic::custom_parse("([A]<AND>[B])|[C]", &custom_operator_set);

// -- snipp

Evaluating a custom logical string

use rustlogic::operators;

// Define the set
let operator_set = operators::common_sets::worded();

let parsed = rustlogic::custom_parse("(NOT FALSE AND TRUE) OR FALSE", &operator_set);

/// Now contains the value of the logical expression
let value = parsed.unwrap().get_value().unwrap();

Evaluating a custom logical string with variables

use rustlogic::operators;
use std::collections::HashMap;

// Define the set
let operator_set = operators::common_sets::worded();

let parsed = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &operator_set);

// We assign the variables to their values
let mut hm = HashMap::new();
hm.insert("A", false);
hm.insert("B", true);
hm.insert("C", false);

// Now contains the value of the logical expression
let value = parsed.unwrap().get_value_from_variables(&hm).unwrap();