Elektra  0.8.19
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.

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:/examples/mathcheck
2 kdb mount mathcheck.dump /examples/mathcheck dump mathcheck
3 kdb set /examples/mathcheck/a 3.1
4 kdb set /examples/mathcheck/b 4.5
5 kdb set /examples/mathcheck/k 7.6
6 kdb setmeta user/examples/mathcheck/k check/math "== + ../a ../b"
7 # should fail
8 kdb set /examples/mathcheck/k 7.7
9 # RET:5
10 # ERRORS:123
11 # Set string to 7.7
12 # The command set failed while accessing the key database with the info:
13 # Error (#123) occurred!
14 # Description: invalid value
15 # Ingroup: plugin
16 # Module: mathcheck
17 # At: /home/thomas/Dev/Elektra/libelektra/src/plugins/mathcheck/mathcheck.c:399
18 # Reason: 7.7 != 7.6
19 # Mountpoint: /examples/mathcheck
20 # Configfile: /home/thomas/.config/mathcheck.dump.25680:1478749409.938013.tmp
21 kdb rm -r /examples/mathcheck
22 kdb umount /examples/mathcheck

To calculate values on-demand you can use:

1 # Backup-and-Restore:/examples/mathcheck
2 kdb mount mathcheck.dump /examples/mathcheck dump mathcheck
3 kdb setmeta user/examples/mathcheck/k check/math ":= + @/a @/b"
4 kdb set /examples/mathcheck/a 8.0
5 kdb set /examples/mathcheck/b 4.5
6 kdb get /examples/mathcheck/k
7 12.5
8 kdb set /examples/mathcheck/a 5.5
9 kdb get /examples/mathcheck/k
10 10
11 kdb rm -r /examples/mathcheck
12 kdb umount /examples/mathcheck

It also works with constants:

1 # Backup-and-Restore:/examples/mathcheck
2 kdb mount mathcheck.dump /examples/mathcheck dump mathcheck
3 kdb setmeta user/examples/mathcheck/k check/math ":= + ../a '5'"
4 kdb set /examples/mathcheck/a 5.5
5 kdb get /examples/mathcheck/k
6 10.5
7 kdb set /examples/mathcheck/a 8.0
8 kdb get /examples/mathcheck/k
9 13
10 kdb rm -r /examples/mathcheck
11 kdb umount /examples/mathcheck