Conditions & Assignments

Conditions

Rules can be enriched with conditions in order to constrain specific values for variables of the body. A condition is the comparison (>, <, ==, >=, <=, <>) of a variable of the body and an expression. Notice that although the comparison symbols used in conditions partially overlap with the symbols for comparison operators, they have different semantics. While comparison operators calculate Boolean results, comparison symbols in conditions only specify a filter. Each rule can have multiple comma-separated conditions.
contract("Mark",14).
contract("Jeff",22).
rich(X) <- contract(X,Y),Y>=20.
@output("rich").
This individuates contracts for Y>=20 and classifies the respective employee as rich. The expected result:
rich("Jeff").
balanceItem(1, 7, 2, 5).
balanceItem(2, 2, 2, 7).
error(E, I) <- balanceItem(I, X, Y, Z), X <> Y+Z.
@output("error").
This individuates balance items for which X is different from the sum of Y and Z. The expected result:
error(_e, 2).
This next example selects the senior English players:
player(1, "Chelsea").
age(1, 24).
player(2, "Bayern").
age(2, 25).
player(3, "Chelsea").
age(3, 18).
team("Chelsea").
team("Bayern").
seniorEnglish(X) <- player(X, Y), team(Y), age(X, A), Y=="Chelsea", A > 20.
@output("seniorEnglish").
Expected result:
seniorEnglish(1).

Assignment

Rules can be enriched with assignments in order to generate specific values for existentially quantified variables of the head. An assignment is the equation (=) of a variable of the body and an expression. Each rule can have multiple comma-separated assignments.
balanceItem("loans", 23.0).
balanceItem("deposits", 20.0).
operations(Q, Z, A) <- balanceItem(I1,X), balanceItem(I2,Y),
                       I1=="loans", I2=="deposits", 
                       Z=X+Y, 
                       A=(X+Y)/2.
@output("operations").
This generates a fact for operations, summing two balance items. Observe that I1=="loans" and I2=="deposits" are conditions to select the balanceItems (as I1 and I2 appear in the body), whereas Z=X+Y and A=(X+Y)/2 are assignments (as Z and A do not appear in the body). The expected result:
operations(_q, 43, 21.5).