Generate CBD Models from Equations

Transforms equations/textual denotations to CBD models.

class CBD.converters.eq2CBD.eq2CBD[source]

Bases: object

Converts equations (textual denotation) into corresponding CBD models.

After instantiating this class, the parse() method may be called to obtain the CBD model(s).

All equations must be in the form of <output> = <expression>, where <output> identifies the name of an output of the resulting CBD and <expression> defines the corresponding logic. To prevent the creation of an output, := may be used instead of =. Multiple equations can be listed, separated by newlines. Variables used in the expressions will be linked to one another if needs be. When a variable is used without any descriptive reference, it will be used as an input. For instance, the text y = 6 * x will be transformed into a CBD with a single output y and a single input x.

Variable names must match the regex [a-zA-Z_][a-zA-Z0-9_]* and must not be any allowed function name (see below). The variable time is reserved and will be replaced by a TimeBlock.

The following operations are allowed and transformed w.r.t. the standard library’s building blocks (CBD.lib.std). The order of operations applied is: parentheses, function calls, exponents, multiplication/division and addition/subtraction.

  • (A): Places sub-equation A in parentheses, giving precedence on the computation of that equation.

  • -A: Negation of a variable or value. In the case of a constant value, no additional negator will be added to the CBD, unless explicitly requested by placing the value within parentheses: i.e. -(4).

  • 1/A: Inversion of a variable or value. In the case of a constant value, no additional inverter will be added to the CBD, unless explicitly requested by placing the value within parentheses: i.e. 1/(4).

  • ~A or !A or not A: Adds a NotBlock before sub-equation A.

  • A + B + C - D: Sum of two (or more) sub-equations. Whenever a subtraction is encountered, it will be replaced by an addition of the negator and the other terms. In the case of a constant value, the same logic as mentioned above is applied.

  • A * B * C / D: Multiplication of two (or more) sub-equations. Whenever a division is encountered, it will be replaced by the multiplication of the inverted value and other factors. In the case of a constant value, the same logic as mentioned above is applied.

  • A^B: Raises sub-equation A to the power of sub-equation B.

  • A % B or A mod B: Modulo-divides sub-equation A by sub-equation B.

  • A == B: Tests equality between sub-equations A and B.

  • A <= B: Tests inequality between sub-equations A and B. The LessThanOrEqualsBlock will be used here.

  • A < B: Tests inequality between sub-equations A and B. The LessThanBlock will be used here.

  • A >= B: Tests inequality between sub-equations A and B. Behind the scenes, this code will be handled as if it were B <= A.

  • A > B: Tests inequality between sub-equations A and B. Behind the scenes, this code will be handled as if it were B < A.

  • A or B or A || B: Merges both A and B in an OrBlock.

  • A and B or A && B: Merges both A and B in an AndBlock.

  • f(A): executes function f on sub-equation A. Besides all single-argument functions from the math module (see the CBD.lib.std.GenericBlock), the allowed functions (case-insensitive) are:

function

argument/input port count

CBD block

int

1

CBD.lib.std.IntBlock

abs

1

CBD.lib.std.AbsBlock

root

2

CBD.lib.std.RootBlock

sqrt

1

CBD.lib.std.RootBlock with second input fixed to 2

clamp or sat

3

CBD.lib.std.ClampBlock

mux

3 (last argument is the select input)

CBD.lib.std.MultiplexerBlock

d

2 (second argument is the IC)

CBD.lib.std.DelayBlock

der

3 (second argument is the IC, third is the delta_t)

CBD.lib.std.DerivatorBlock

i

3 (second argument is the IC, third is the delta_t)

CBD.lib.std.IntegratorBlock

parse(text, model=None)[source]

Parses the text and constructs a CBD model thereof.

Parameters
  • text (str) – The text to parse.

  • model – An optional CBD model to use for the construction.