Skip to content

Examples

valhuber edited this page Apr 21, 2022 · 16 revisions

As shown in the Rules Summary, the list of rules is small. You can learn them in under an hour.

Rules are the "words", and, like writing, their power comes from using them in combination.. This page summarizes the rules, and illustrates combining them to solve several non-trivial examples.

   

Rules Summary

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

https://github.com/valhuber/LogicBank/wiki/Copy-Children

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.count(derive=Customer.OrderCount, as_count_of=Order, where=lambda row: row.ShippedDate is None) Useful for existence checks
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
Copy Children Create multiple types of child rows by copying designated children (deep copy) Clone Order - create a new order, populating OrderDetails from existing order See Rule-Extensibility
See Clone Order

   

Learning Rules

The best way to learn Logic Bank:

  1. Review the list of rules above

  2. Then, review the examples below:

Sample Requirement Illustrates Patterns
Check Credit
Rules are 40X more concise than legacy code
Compute OrderDetail.Amount,
sum OrderDetails into Order & Customer,
verify Balance <= Limit
Constrain a derived result
Chaining
Qualified Sum
Sufficient Raise Raises must exceed 20% Use of old_row
Ship Order When Orders are shipped,
reduce the Customer.Balance, and
Product.UnitsInStock
Illustrates cascade, another form of multi-table logic
Orders for Commissioned Employees Only Ensure that the Order.EmployeeId references an Employee that IsCommissioned Counts as child existence checks
Allocation Allocate Payment to a set of outstanding Orders,
reducing CustomerBalance
illustrates extensibility, providing a reusable pattern:
A provider allocates to a set of recipients, plus rollups
Banking Funds Transfer between accounts A complex transaction using the command pattern

Suggestion: try to imagine the solution, then read the article.