Numbers and Math
To deal with numbers and math in Blawx code, you need to use number values, numerical constraints, and mathematical operations.
The first thing to do when dealing with numbers is to create attributes that can hold the numerical values by using the Number datatype.
Whenever you need to specify a number (as opposed to calculate a number), you can use the number value block. The number value block will accept positive and negative integers, and zero, and numbers with a decimal point.
To perform calculations on numbers in Blawx you use a numerical constraint block with the "equals" operator ("="), and then put math operator blocks inside the right side of the numerical constraint block.
The numerical constraint block with the equals operator selected looks like this:
The math operator block looks like this:
If you had a rule that calculated the net amount of an invoice using a numerical constraint block, you would draft that rule like this:
Here, we have used the numerical constraint block to constrain the variable Total to the result of applying the multiplication operator to the values of "amount" and "discount".
If you are familiar with other kinds of programming, this might seem unfamiliar. In most programming languages, mathemtical operations are implemented imperatively, so the phrase "total = amount X discount" would be interpreted by the computer to mean "take the amount, and multiply it by the discount, and save the result in a variable called total". In Blawx, mathematical operations are implemented as logical constraints on variables. So the phrase "total = amount X discount" included in the conditions of a rule means "only return answers where the formula total = amount X discount" holds. And the values that can be returned for any of the variables in that statement can be numbers, or they can be constrained variables. For example, if the total is known to be between 8 and 9, and the amount is known to be 10, then Blawx can answer that the discount is between 0.1 and 0.2.
A fact scenario and question based on the above rule might look like this:
Here you can see we are using the number value block to specify a total and a discount rate.
Nesting Math Operators
If you need to do more complicated math, you can nest math operator blocks inside one another. For example, to subtract 2 from the sum of 7 and the product of 4 and 5, you could encode this:
Nesting math operators in this way can cause your code to become very wide, from left to right, and more difficult to read. To avoid this, you can use multiple calculations, and then reuse the variables, like this:
Using either set of blocks should result in the Answer variable being bound to the value 25.
Note that the numerical constraint block is also used to impose disequality constraints, and comparisons, which are described in the numerical constraints section of the documentation.