Skip to content

Rule Summary

Val Huber edited this page Mar 29, 2022 · 12 revisions

This table briefly summarizes the rules.

Defining Rules

Rules are declared with function calls - see logic#declare_logic(), which looks like this:

Rule.constraint(validate=Customer,
                as_condition=lambda row: row.Balance <= row.CreditLimit,
                error_msg="balance ({row.Balance}) exceeds credit ({row.CreditLimit})")
Rule.sum(derive=Customer.Balance, as_sum_of=Order.AmountTotal,
         where=lambda row: row.ShippedDate is None)

Rule.sum(derive=Order.AmountTotal, as_sum_of=OrderDetail.Amount)

Rule.formula(derive=OrderDetail.Amount, as_expression=lambda row: row.UnitPrice * row.Quantity)
Rule.copy(derive=OrderDetail.UnitPrice, from_parent=Product.UnitPrice)

Rule.formula(derive=OrderDetail.ShippedDate, as_expression=lambda row: row.OrderHeader.ShippedDate)

Activating Rules

To activate the rules (declare_logic is the function shown above):

LogicBank.activate(session=session, activator=`declare_logic`)

Rules Summary

The table shows excerpts only; see the nw sample for full syntax.

Rule Summary Example Notes
Constraint Boolean function must be True
else transaction rolled back
row.Balance <= row.CreditLimit
row.Salary >= Decimal('1.20') * old_row.Salary
Multi-field
old_row
Formula Function computes column value row.UnitPrice * row.Quantity
row.OrderHeader.ShippedDate
lambda, or function
Parent (OrderHeader) references
Sum Derive parent-attribute as sum of designated child attribute; optional child qualification Rule.sum(derive=Customer.Balance, as_sum_of=Order.AmountTotal,where=lambda row: row.ShippedDate is None) Parent attribute can be hybrid (virtual)
scalable: pruning, adjustment
Count Derive parent-attribute as count of child rows; optional child qualification Rule.sum(derive=Customer.Balance, as_sum_of=Order.AmountTotal,where=lambda row: row.ShippedDate is None) Parent attribute can be hybrid (virtual)
scalable: pruning, adjustment
Copy Child value set from Parent OrderDetail.ProductPrice = copy(Product.Price) Unlike formula references, parent changes are not propagated
e.g, Order totals for Monday are not affected by a Tuesday price increase
Event Python Function on insert, call congratulate_sales_rep See Extensibility for a information on early, row and commit events
Parent Check Ensure Parent row exists Orders must have a Customer See Referential Integrity
Allocation Allocate a provider amount to recipients allocate a payment to outstanding orders See Allocation for an example
Copy Row Create child row by copying parent audit Employee Salary changes to EmployeeAudit See Rule Extensibility

Rule Patterns

Pattern Notes Example
Chain Up parent sums and counts mean that child row changes can adjust parents Derive Balance
Chain Down child copy and parent references mean that parent row changes can cascade to children Ship Order
Constrain a Derived Result constraints may require derived values Balance < creditLimit
Auditing Note the Copy Row rule Salary Audit
old_row useful for state transition logic Meaningful Raise