FormulaParser
- class hybrid_learning.fuzzy_logic.logic_base.parsing.FormulaParser(logic, brackets=('(', ')'), allowed_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')[source]
Bases:
object
Base class to parse string formula specifiers in infix notation to merge operations. Furthermore, it assists in validating logical formulas and producing nice string representations (see e.g.
to_str()
). This basic parser is an inverse toto_infix_notation()
ofMerge
:>>> from hybrid_learning.fuzzy_logic import FormulaParser, boolean >>> logic = boolean.Logic() >>> parser = FormulaParser(logic) >>> formula_obj = parser("a || ~(b && c)") >>> formula_obj OR('a', NOT(AND('b', 'c'))) >>> parser(formula_obj.to_str()) == formula_obj True
Also, the
to_str()
method matchesto_str()
ofMerge
:>>> f = "c || ~(b && a)" >>> parser.to_str(f) 'c || ~(a && b)' >>> parser.to_str(f) == parser(f).to_str(precedence=logic, use_whitespace=True) True
Parsing
Calling a parser (same as calling
parse()
) will parse a string to a logical operation. Parsing assumes infix notation of operators, and prefix for unary operators, with brackets allowed. A list of operator builders, e.g.Merge
sub-classes, defines the precedence of the operators (list must be ordered by increasing precedence). The operator builders each must provide aSYMB
string attribute that specifies the symbol representing the operator. The implementation usespyparsing.infixNotation()
.Functional Interface
The
functional_*
class methods provide a functional interface to parsing operations that will accept the logic (a list of allowed operators by precedence) as argument.Public Methods:
parse
(specifier, **init_args)Parse a specifier to a merge operation.
to_str
(specifier[, for_filenames])Parse the specifier and return an easy-to-read string representation.
is_pure
(op)Whether the formula
op
purely consists of operators built from the logic.is_normal_form
(parent_op)Checks whether the current formula respects the operator precedence.
apply
(specifier, annotations, **init_args)Parse
specifier
and return its result on annotations if it's an operation.Special Methods:
__init__
(logic[, brackets, allowed_chars])__call__
(specifier, **init_args)Call parse on the specifier.
- Parameters
- __init__(logic, brackets=('(', ')'), allowed_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')[source]
- apply(specifier, annotations, **init_args)[source]
Parse
specifier
and return its result on annotations if it’s an operation.
- classmethod functional_apply(specifier, annotations, logic, brackets=None, allowed_chars=None, **init_args)[source]
Parse
specifier
and return its result on annotations if it’s an operation. Seeapply()
- classmethod functional_is_normal_form(parent_op, logic)[source]
Checks whether the current formula respects the logic operator precedence. See
is_normal_form()
.
- classmethod functional_is_pure(op, logic)[source]
Whether the formula
op
purely consists of operators built fromlogic
. Seeis_pure()
.
- classmethod functional_parse(specifier, logic, brackets=None, allowed_chars=None, **init_args)[source]
Parse a specifier to a merge operation given a
logic
. Seeparse()
.
- classmethod functional_to_str(specifier, logic, brackets=None, allowed_chars=None, for_filenames=False, **kwargs)[source]
Parse the specifier and return a unique string representation. See
to_str()
.
- is_normal_form(parent_op)[source]
Checks whether the current formula respects the operator precedence. Uses introspection and assumes that all operators are objects of operator builders that are classes.
- parse(specifier, **init_args)[source]
Parse a specifier to a merge operation. Will return the original string if it does not contain operation specifiers. Specifiers are any of the
SYMB
symbols of the classes listed in the operator builder listlogic
. The sorting of the list specifies the operator precedence.- Parameters
specifier (str) – the string specifier to parse
init_args – any keyword arguments for init of the generated operations (parent and all children);
out_key
is only applied to the parent operation
- Returns
the parsing outcome, a string (for a single variable) or a Merge operation
- Raises
pyparsing.ParseException
in case parsing fails (e.g. if a non-allowed char is used for variables)- Return type
- to_str(specifier, for_filenames=False, **kwargs)[source]
Parse the specifier and return an easy-to-read string representation. If
for_filenames
is given, it the output string is suitable for filenames (no whitespace, pretty symbols).