Skip to content

Rule

Antecedent

Expression tree that represents and evaluates the antecedent of a rule.

structure

The structure of a rule is:
if (antecedent) then (consequent)

The structure of the antecedent of a rule is:
if variable is [hedge]* term [(and|or) variable is [hedge]* term]*


*-marked elements may appear zero or more times,
elements in brackets are optional, and
elements in parentheses are compulsory.

related

Attributes

expression instance-attribute

expression: Expression | None = None

text instance-attribute

text = text

Functions

__init__

__init__(text: str = '') -> None

Constructor.

Parameters:

Name Type Description Default
text str

antecedent as text.

''

__repr__

__repr__() -> str

Return the code to construct the antecedent in Python.

Returns:

Type Description
str

code to construct the antecedent in Python.

__str__

__str__() -> str

Return the antecedent as text.

Returns:

Type Description
str

antecedent as text

activation_degree

activation_degree(
    conjunction: TNorm | None = None, disjunction: SNorm | None = None, node: Expression | None = None
) -> Scalar

Compute the activation degree of the antecedent on the expression tree from the given node.

Parameters:

Name Type Description Default
conjunction TNorm | None

conjunction operator from the rule block

None
disjunction SNorm | None

disjunction operator from the rule block

None
node Expression | None

node in the expression tree of the antecedent

None

Returns:

Type Description
Scalar

activation degree of the antecedent.

infix

infix(node: Expression | None = None) -> str

Return the infix notation of the node.

Parameters:

Name Type Description Default
node Expression | None

node in the expression tree of the antecedent

None

Returns:

Type Description
str

infix notation of the node.

is_loaded

is_loaded() -> bool

Return whether the antecedent is loaded.

Returns:

Type Description
bool

antecedent is loaded.

load

load(engine: Engine) -> None

Load the antecedent using the engine to identify and get references to the input and output variables.

Parameters:

Name Type Description Default
engine Engine

engine to get references in the antecedent.

required

postfix

postfix(node: Expression | None = None) -> str

Return the postfix notation of the node.

Parameters:

Name Type Description Default
node Expression | None

node in the expression tree of the antecedent

None

Returns:

Type Description
str

postfix notation of the node.

prefix

prefix(node: Expression | None = None) -> str

Return the prefix notation of the node.

Parameters:

Name Type Description Default
node Expression | None

node in the expression tree of the antecedent

None

Returns:

Type Description
str

prefix notation of the node.

unload

unload() -> None

Unload the antecedent.

Consequent

Proposition set that represents and evaluates the consequent of a rule.

structure

The structure of a rule is:
if (antecedent) then (consequent)

The structure of the consequent of a rule is:
then variable is [hedge]* term [and variable is [hedge]* term]* [with w]?


*-marked elements may appear zero or more times,
elements in brackets are optional,
elements in parentheses are compulsory, and
?-marked elements may appear once or not at all.

related

Attributes

conclusions instance-attribute

conclusions: list[Proposition] = []

text instance-attribute

text: str = text

Functions

__init__

__init__(text: str = '') -> None

Constructor.

text: consequent as text.

__repr__

__repr__() -> str

Return the code to construct the consequent in Python.

Returns:

Type Description
str

code to construct the consequent in Python.

__str__

__str__() -> str

Return the consequent as text.

Returns:

Type Description
str

consequent as text

is_loaded

is_loaded() -> bool

Return whether the consequent is loaded.

Returns:

Type Description
bool

consequent is loaded.

load

load(engine: Engine) -> None

Load the consequent using the engine to identify and get references to the input and output variables.

Parameters:

Name Type Description Default
engine Engine

engine to get references in the consequent.

required

modify

modify(activation_degree: Scalar, implication: TNorm | None) -> None

Modify the consequent with the activation degree and the implication operator.

Parameters:

Name Type Description Default
activation_degree Scalar

activation degree computed in the antecedent of the rule

required
implication TNorm | None

implication operator configured in the rule block.

required

unload

unload() -> None

Unload the consequent.

Expression

Bases: ABC

Base class to build an expression tree.

related

Functions

__init__ abstractmethod

__init__() -> None

Constructor.

Operator

Bases: Expression

Expression that represents a non-terminal node in the expression tree as a binary operator (i.e., and or or) on two Expression nodes.

related

Attributes

left instance-attribute

left = left

name instance-attribute

name = name

right instance-attribute

right = right

Functions

__init__

__init__(name: str = '', right: Expression | None = None, left: Expression | None = None) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the operator

''
left Expression | None

left expression in the binary tree

None
right Expression | None

right expression in the binary tree.

None

__str__

__str__() -> str

Return the name of the operator.

Returns:

Type Description
str

name of the operator.

Proposition

Bases: Expression

Expression that represents a terminal node in the expression tree as variable is [hedge]* term.

related

Attributes

hedges instance-attribute

hedges: list[Hedge] = []

term instance-attribute

term = term

variable instance-attribute

variable = variable

Functions

__init__

__init__(variable: Variable | None = None, hedges: Iterable[Hedge] | None = None, term: Term | None = None) -> None

Constructor.

Parameters:

Name Type Description Default
variable Variable | None

variable in the proposition

None
hedges Iterable[Hedge] | None

list of hedges that apply to the term of the variable

None
term Term | None

term in the proposition.

None

__str__

__str__() -> str

Return proposition as text.

Returns:

Type Description
str

proposition as text.

Rule

Conditional statement that contributes to the control of an Engine.

A rule consists of an Antecedent and a Consequent, each with propositions in the form variable is term.

The propositions in the Antecedent are connected by the conjunctive and or the disjunctive or, which are fuzzy logic operators represented as TNorm and SNorm (respectively).

The propositions in the Consequent are independent and separated by a symbolic and.

The term in any proposition can be preceded by a hedge that modifies its membership function value of the term.

The contribution of a rule to the control of the engine can be determined by its weight \(w \in [0.0, 1.0]\), which is equal to 1.0 if omitted.

structure

The structure of a rule is:
if (antecedent) then (consequent) [with weight]

The structure of the antecedent is:
if variable is [hedge]* term [(and|or) variable is [hedge]* term]*

The structure of the consequent is:
then variable is [hedge]* term [and variable is [hedge]* term]* [with w]?


where elements in brackets are optional, elements in parentheses are compulsory, *-marked elements may appear zero or more times, and ?-marked elements may appear once or not at all.

related

Attributes

AND class-attribute instance-attribute

AND = 'and'

IF class-attribute instance-attribute

IF = 'if'

IS class-attribute instance-attribute

IS = 'is'

OR class-attribute instance-attribute

OR = 'or'

THEN class-attribute instance-attribute

THEN = 'then'

WITH class-attribute instance-attribute

WITH = 'with'

activation_degree instance-attribute

activation_degree = scalar(0.0)

antecedent instance-attribute

antecedent = antecedent or Antecedent()

consequent instance-attribute

consequent = consequent or Consequent()

enabled instance-attribute

enabled = enabled

text property writable

text: str

Get/Set the rule as text.

Getter

Returns:

Type Description
str

rule as text

Setter

Parameters:

Name Type Description Default
text str

rule as text

required

triggered instance-attribute

triggered = array(False)

weight instance-attribute

weight = weight

Functions

__init__

__init__(
    enabled: bool = True,
    weight: float = 1.0,
    antecedent: Antecedent | None = None,
    consequent: Consequent | None = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
enabled bool

enable the rule

True
weight float

weight of the rule

1.0
antecedent Antecedent | None

antecedent of the rule

None
consequent Consequent | None

consequent of the rule

None

__repr__

__repr__() -> str

Return the code to construct the rule in Python.

Returns:

Type Description
str

code to construct the rule in Python.

__str__

__str__() -> str

Return the code to construct the rule in the FuzzyLite Language.

Returns:

Type Description
str

code to construct the rule in the FuzzyLite Language.

activate_with

activate_with(conjunction: TNorm | None, disjunction: SNorm | None) -> Scalar

Compute and set activation degree of the rule with the conjunction and disjunction operators.

Parameters:

Name Type Description Default
conjunction TNorm | None

conjunction operator

required
disjunction SNorm | None

disjunction operator

required

Returns:

Type Description
Scalar

activation degree of the rule.

create staticmethod

create(text: str, engine: Engine | None = None) -> Rule

Create rule from the text.

Parameters:

Name Type Description Default
text str

rule as text

required
engine Engine | None

engine that the rule (partially) controls

None

deactivate

deactivate() -> None

Deactivate the rule.

is_loaded

is_loaded() -> bool

Return whether the rule is loaded.

Returns:

Type Description
bool

rule is loaded.

load

load(engine: Engine) -> None

Load the rule using the engine to identify and get references to the input and output variables.

Parameters:

Name Type Description Default
engine Engine

engine that the rule (partially) controls

required

parse

parse(text: str) -> None

Parse and load the rule based on the text.

Parameters:

Name Type Description Default
text str

rule as text.

required

trigger

trigger(implication: TNorm | None) -> None

Trigger the rule using the implication operator and the previously computed activation degree.

Parameters:

Name Type Description Default
implication TNorm | None

implication operator.

required

Raises:

Type Description
RuntimeError

when the rule is not loaded

unload

unload() -> None

Unload the rule.

RuleBlock

Block of rules and fuzzy logic operators required to control an engine.

related

Attributes

activation instance-attribute

activation = activation

conjunction instance-attribute

conjunction = conjunction

description instance-attribute

description = description

disjunction instance-attribute

disjunction = disjunction

enabled instance-attribute

enabled = enabled

implication instance-attribute

implication = implication

name instance-attribute

name = name

rules instance-attribute

rules = list(rules or [])

Functions

__getitem__

__getitem__(item: int) -> Rule
__getitem__(item: slice) -> list[Rule]
__getitem__(item: int | slice) -> Rule | list[Rule]

Allow indexing rules in rule block (eg, rule_block[0]).

Parameters:

Name Type Description Default
item int | slice

rule index or slice

required

Returns:

Type Description
Rule | list[Rule]

rule at index or slice of rules

__init__

__init__(
    name: str = "",
    description: str = "",
    enabled: bool = True,
    conjunction: TNorm | None = None,
    disjunction: SNorm | None = None,
    implication: TNorm | None = None,
    activation: Activation | None = None,
    rules: Iterable[Rule] | None = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
name str

name of the rule block

''
description str

description of the rule block

''
enabled bool

enable the rule block

True
conjunction TNorm | None

conjunction operator

None
disjunction SNorm | None

disjunction operator

None
implication TNorm | None

implication operator

None
activation Activation | None

activation method

None
rules Iterable[Rule] | None

list of rules.

None

__iter__

__iter__() -> Iterator[Rule]

Return the iterator of the rules.

Returns:

Type Description
Iterator[Rule]

iterator of the rules

__len__

__len__() -> int

Return the number of rules.

Returns:

Type Description
int

number of rules

__repr__

__repr__() -> str

Return the code to construct the rule block in Python.

Returns:

Type Description
str

code to construct the rule block in Python.

__str__

__str__() -> str

Return the code to construct the rule block in the FuzzyLite Language.

Returns:

Type Description
str

code to construct the rule block in the FuzzyLite Language.

activate

activate() -> None

Activate the rule block.

load_rules

load_rules(engine: Engine) -> None

Load all the rules in the rule block.

Parameters:

Name Type Description Default
engine Engine

engine where this rule block is registered.

required

reload_rules

reload_rules(engine: Engine) -> None

Reload all the rules in the rule block.

Parameters:

Name Type Description Default
engine Engine

engine where this rule block is registered.

required

rule

rule(index: int) -> Rule

Get the rule at the index.

Parameters:

Name Type Description Default
index int

index of the rule.

required

Returns:

Type Description
Rule

rule at the index

unload_rules

unload_rules() -> None

Unload all the rules in the rule block.