$darkmode
Elektra 0.11.0
Plugin: mathcheck

Compares a key value to a mathematical expression using polish prefix notation defined in the check/math metakey. Operations are + - / * . Operants are keys with names relative to the parent key. How the values are compared is specified at the beginning of the metakey using the conditions <, <=, ==, !=, =>, >, := := is used to set key values. All values are interpreted as double floating point values.

Installation

See installation. The package is called libelektra5-extra.

Keynames

Keynames are all either relative to to-be-tested key (starting with ./ or ../), relative to the parentkey (starting with @/) or absolute (e.g. system:/key).

Examples

check/math = "== + ../testval1 + ../testval2 ../testval3" compares the keyvalue to the sum of testval1-3 and yields an error if the values are not equal. check/math = "\<= - \@/testval1 * \@/testval2 \@/testval3" tests if the keyvalue is less than or equal to testval1 - (testval2 * testval3) and yields an error if not.

Full example:

1 # Backup-and-Restore:user:/tests/mathcheck
2 
3 sudo kdb mount mathcheck.dump user:/tests/mathcheck mathcheck
4 
5 kdb set user:/tests/mathcheck/a 3.1
6 kdb set user:/tests/mathcheck/b 4.5
7 kdb set user:/tests/mathcheck/k 7.6
8 kdb meta-set user:/tests/mathcheck/k check/math "== + ../a ../b"
9 
10 # should fail
11 kdb set user:/tests/mathcheck/k 7.7
12 # RET:5
13 # ERROR:C03200
14 # Set string to "7.7"
15 # Sorry, module mathcheck issued the error C03200:
16 # invalid value: 7.7 != 7.6

To calculate values on-demand you can use:

1 kdb meta-set user:/tests/mathcheck/k check/math ":= + @/a @/b"
2 kdb set user:/tests/mathcheck/a 8.0
3 kdb set user:/tests/mathcheck/b 4.5
4 
5 kdb get user:/tests/mathcheck/k
6 #> 12.5
7 
8 kdb set user:/tests/mathcheck/a 5.5
9 
10 kdb get user:/tests/mathcheck/k
11 #> 10

It also works with constants:

1 kdb meta-set user:/tests/mathcheck/k check/math ":= + ../a '5'"
2 kdb set user:/tests/mathcheck/a 5.5
3 
4 kdb get user:/tests/mathcheck/k
5 #> 10.5
6 
7 kdb set user:/tests/mathcheck/a 8.0
8 
9 kdb get user:/tests/mathcheck/k
10 #> 13
11 
12 #cleanup
13 kdb rm -r user:/tests/mathcheck
14 sudo kdb umount user:/tests/mathcheck