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 to to_infix_notation() of Merge:

>>> 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 matches to_str() of Merge:

>>> 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 a SYMB string attribute that specifies the symbol representing the operator. The implementation uses pyparsing.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
__call__(specifier, **init_args)[source]

Call parse on the specifier.

Parameters

specifier (str) –

Return type

Union[str, Merge]

__init__(logic, brackets=('(', ')'), allowed_chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')[source]
Parameters
apply(specifier, annotations, **init_args)[source]

Parse specifier and return its result on annotations if it’s an operation.

Parameters
  • specifier (Union[str, Merge]) – specifier for parse()

  • annotations (Dict[str, Any]) – dictionary to which to apply the parsed operation

  • init_args – further keyword arguments to init the parent operation while parsing

Return type

Dict[str, Any]

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. See apply()

Parameters
Return type

Dict[str, Any]

classmethod functional_is_normal_form(parent_op, logic)[source]

Checks whether the current formula respects the logic operator precedence. See is_normal_form().

Parameters
Return type

bool

classmethod functional_is_pure(op, logic)[source]

Whether the formula op purely consists of operators built from logic. See is_pure().

Parameters
Return type

bool

classmethod functional_parse(specifier, logic, brackets=None, allowed_chars=None, **init_args)[source]

Parse a specifier to a merge operation given a logic. See parse().

Parameters
Return type

Union[str, Merge]

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

Parameters
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.

Parameters

parent_op (Merge) –

Return type

bool

is_pure(op)[source]

Whether the formula op purely consists of operators built from the logic.

Parameters

op (Merge) –

Return type

bool

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 list logic. 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

Union[str, Merge]

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

Parameters
  • specifier (str) –

  • for_filenames (bool) –

allowed_chars: Union[str, List[str]]

The characters allowed within variable names in formulas. Brackets and characters occurring in operation keys are excluded automatically.

brackets: Tuple[str, str]

The symbols to interpret as left and right bracket in strings.

logic: Union[Logic, Sequence[Type[Merge]]]

The list of operators that are allowed and used for parsing ordered by increasing precedence.