Logic

class hybrid_learning.fuzzy_logic.logic_base.logic.Logic(operators=None, **default_overrides)[source]

Bases: MutableSequence

Basic definition of a logic. A logic must have operator builders for some basic connectives (by default AND, OR, NOT, see DEFAULT_CONNECTIVES) and holds a default operator builder precedence for parsing. New operator builders (e.g. for predicates or functions) can be added. The logic allows to iterate over its operators (the operator builders) using usual __getitem__ notation. Operator builders must (cf. Merge sub-classes and MergeBuilder for examples of valid operator builders):

  • be callable with string variable names, e.g. OR("a", "b"), or child operators, e.g. OR(NOT("a"), "b"), returning an operator,

  • return operators that are callable on dicts matching the variable names to instantiations,

  • provide a variadic_ class method that returns a callable which accepts tensors/arrays/floats/Booleans and returns the result of the connective operation upon those inputs,

  • provide a SYMB attribute that specifies the (unique) parsing symbol within this logic.

Operator Access

Obtain primitive variadic instances (i.e. callables on logic values) of the operations via logical_(). The basic connectives specified in DEFAULT_CONNECTIVES or (as overrides) during init can be accessed by their given common name, i.e. the used key. All other operations can be accessed by their parsing SYMB. See also op() for this access.

Sub-classing

To provide your own Logic, either create a Logic instance and override all mandatory (i.e. None valued) DEFAULT_CONNECTIVES values, or define a sub-class that defines its own DEFAULT_CONNECTIVES.

Public Data Attributes:

DEFAULT_CONNECTIVES

Default connectives by common names.

Public Methods:

op(symb)

Get the operator builder for the given symbol.

logical_(symb)

Get a variadic instance of the logical operation specified by symb.

parser()

Return a default parser for this logic.

is_pure(op)

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

insert(precedence, op_builder)

Add an operator builder with precedence precedence.

Inherited from : py: class:MutableSequence

insert(precedence, op_builder)

Add an operator builder with precedence precedence.

append(value)

S.append(value) -- append value to the end of the sequence

clear()

reverse()

S.reverse() -- reverse IN PLACE

extend(values)

S.extend(iterable) -- extend sequence by appending elements from the iterable

pop([index])

Raise IndexError if list is empty or index is out of range.

remove(value)

S.remove(value) -- remove first occurrence of value.

Inherited from : py: class:Sequence

index(value, [start, [stop]])

Raises ValueError if the value is not present.

count(value)

Special Methods:

__init__([operators])

Init.

__setitem__(precedence, op_builder)

__getitem__(i)

__delitem__(i)

__len__()

__add__(other)

__radd__(other)

__repr__()

Return repr(self).

Inherited from : py: class:MutableSequence

__setitem__(precedence, op_builder)

__delitem__(i)

__iadd__(values)

Inherited from : py: class:Sequence

__getitem__(i)

__iter__()

__contains__(value)

__reversed__()

Inherited from : py: class:Reversible

__reversed__()

Inherited from : py: class:Sized

__len__()

Inherited from : py: class:Iterable

__iter__()

Inherited from : py: class:Container

__contains__(value)


Parameters
__add__(other)[source]
Parameters

other (Sequence[Merge]) –

__delitem__(i)[source]
Parameters

i (int) –

Return type

None

__getitem__(i)[source]
Parameters

i (int) –

__init__(operators=None, **default_overrides)[source]

Init.

Parameters
  • operators (Optional[Iterable[Type[Merge]]]) – the list of operators this logic holds; extended by the DEFAULT_CONNECTIVES and the default_overrides; accessible via their symbol

  • default_overrides (Type[Merge]) – overrides and additions in the form common_name=op_builder for the DEFAULT_CONNECTIVES; added to the operators in the order given first by DEFAULT_CONNECTIVES then by additionally added connectives; later also accessible via their common name; set common_name=False to explicitly discard this standard operator from the logic.

Raises

:py:class`TypeError` in case not all mandatory basic connectives (such with None entries in DEFAULT_CONNECTIVES) are overridden

__len__()[source]
__radd__(other)[source]
Parameters

other (Sequence[Merge]) –

__repr__()[source]

Return repr(self).

__setitem__(precedence, op_builder)[source]
Parameters
Return type

None

insert(precedence, op_builder)[source]

Add an operator builder with precedence precedence.

Parameters
Return type

None

is_pure(op)[source]

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

Parameters

op (Merge) –

Return type

bool

logical_(symb)[source]

Get a variadic instance of the logical operation specified by symb.

Parameters

symb (str) –

Return type

Union[Merge, Callable[[Sequence[Union[float, ndarray, Tensor, bool]]], Union[float, ndarray, Tensor]]]

op(symb)[source]

Get the operator builder for the given symbol. The symbol may be the common name, or, in second precedence, the parsing SYMB.

Raises

KeyError in case the symbol is not found or not unique.

Parameters

symb (str) –

Return type

Type[Merge]

parser()[source]

Return a default parser for this logic.

Return type

FormulaParser

DEFAULT_CONNECTIVES: OrderedDict = {'AND': None, 'IMPLIEDBY': False, 'IMPLIES': False, 'NOT': None, 'OR': None}

Default connectives by common names. Set None as default to force users to specify it during init. Set False to just mark the precedence position but not force users to specify an implementation during init. The order also determines the default order of precedence (higher index = higher precedence).

operators: List[Type[Merge]]

Logical operators in their default order of precedence during parsing.