Docs‎ > ‎

Reactive Logic Tutorial

  • Key element in the Training Ramp for Reactive Logic.
  • Recommended Pre-Requisites
  • Skim the advanced examples to scan the logic.  The logic is complex,  but a quick scan will give you a good sense of how Reactive Logic can address complex problems.

Using a Case Study approach of a familiar but complex database, this Tutorial explains the approach for using API Creator.  It progresses from simple (and typical) examples, to very complex examples.

The Tutorial is designed to illustrate the key design patterns for Reactive Logic.  These are called out explicitly in the sections below.

Process Overview

The sub-sections below provide a summary of the steps to use Business Logic.  For further background, see the Overview and the Architecture.  

Create an API

You begin by using the New API in API Creator - accessed via the Browser - to create an API (project) and register a database.

REST Resources

The next typical step is to define some REST Resources to enable client development to begin.  API Creator enables parallel development of client applications and business logic.

Business Logic

Use API Creator to declare your business logic.

The complete logic for the Sample is shown here, and described in the following sections.  Observe this is both...
  • Database Bound - your logic is bound to database tables and columns, so that it can be re-used over all transactions that affect the data.

  • Unordered - while this listing is organized by table, you can enter the logic in any order.  Execution will be ordered by the system based on dependencies.

  • Filterable - select one of the Topic links (colored rectangles to the right of the rules) to see logic for that Topic


Sample Database

Click the image at right to view the structure of the Sample Database in a new window.  It is a small and familiar, but with sufficiently complex structures to enable us to explore several interesting Use Cases below.


Loading this Project

You can load this project into your installation as described here.


Key Concept: Think Spreadsheet

Like many systems, API Creator provides an Event model, wherein you can write JavaScript event handlers to handle table updates.  Experienced programmers will find this model to be quite familiar.

The key to getting the value out of API Creator is to use events in conjunction with spreadsheet-like Reactive Logic.  We call it Think Spreadsheet, and a good way to understand it is via the examples below.

Going a bit further, these examples illustrate several of the Key Logic Patterns.  We recommend you review these concepts, so you have them at your disposal when addressing complex logic.  You'll find the rules to be deceptively simple - they are quite powerful.

You might want to keep a list handy of the rules.


Basic Examples

The following examples are "typically" complex, and are a great place to start learning about Business Logic.  The next section examines some more complex examples.

How you review these examples might depend on your objectives:

  • I am evaluating - I want to see some code, and understand solutions to a range of Use Cases
You can simply review the problems / solutions  (Note: these examples are provided in the download, although we have removed some comments here so things fit in a screen shot).
  • I am about to do a project - I need to see how to solve problems, and document my work
We suggest you have a window / sheet of paper for the data model and for the rules;
then try to solve each problem, and compare your solution to the sample

Quick note: many basic examples are built upon the Demo database.  The following examples are built upon the Sample (similar but larger).  


Validation (Place New Order)

Details

Learn more here.
Validation during the "Place Order" business transaction has a number of different requirements that illustrate the most common business logic patterns.  The actual validation takes place on a parent table ("sample:customers") but the triggering events are changes in the orders and lineitems (adding new items, changing quantities, paying an order). 


Make Order Ready

Make Ready describes how we Placed Orders which typically found in a "Shopping Cart" state (order.is_ready == false), where they do not affect Customer balance and Product quantities until a subsequent Make Order Ready transaction is executed.  So, we need to devise logic that, when an Order is "made ready" .


Advanced Examples

The patterns above illustrate the most common patterns we see in logic design. The sub-sections below describe the more complicated cases we have seen through extensive experience.

You will see that the solutions are remarkably short. So the coding to deliver complex problems is small. The solutions, on the other hand, are dense. This illustrates the power of logic to address complexity, and also that the real job centers on the design and approach instead of low level coding. Exactly where it should be.

Explore Allocation (Add Payment - allocate to orders)

Details

Learn more here.
The Allocation example is a powerful illustration of business logic.  This is a classic example of the providing an allocation re-usable service via Business Logic Extensibility.  Here we add a Payment to a Customer, and its amount is allocated to that Customers' outstanding unpaid orders. 

Extensible Libraries - this example includes using custom Java and {JS} code to process and allocate payments.

Group By Rollup

Details

Learn more here
Total sales by month and salesrep - These examples are found on here.

It is common to sub-total object by grouping them.  For example, we might to group orders by year, month and salesrep, to monitor sales activity.  These totals are maintained in the table 
empsales

Bill Of Materials Price Rollup - These examples are found on here.

Consider when a Product price is changed, and that product is a component of a kit.  The Requirement must be that each of the kits reflect the price change (e.g., if a bolt increases in price, so do all the kits that use bolts: wings, fuselages, etc). 

Budget Rollup

Here is how to roll a budget up an org chart (database structure here).


Bill of Materials Kit Explosion

Details

Learn more here.
This example is found here.  While described here as a separate topic, this is a requirement within Place Order.  When ordering a Product that is a kit (i.e., composed of other Parts, such as a plane with Wings etc), we want to reduce inventory for each of the components of the kit.


Audit Transaction (Salary Change)

Details

Learn more here.
This example creates an audit by creating child rows on designated parent changes, with control over which attributes are copied into the audit trial.  1 rule:
Auditing is the simplest example of a family of Insert Into From rule extension.


Transaction parameters (Give Raise)

Most updates are updates (Posts, Puts or Deletes) to rows, processed per logic as shown in the example above.  Sometimes, however, transactions require arguments. 
This is example is defined here.


Ignoring Extra Attributes in a POST

The ability to pass arguments to your rules on the URL (arg.ArgName1=1) can be accessed using the req.getUserProperties().get("ArgName1")
One special argument main:customer?arg.IgnoreExtraAttributes=1 will allow you to pass additional attributes in your JSON during a POST or PUT that are not part of the database structure. This can be helpful in passing values from your client system that are useful for updates or special logic processing but are not part of the data model.

Under Construction

This sample is currently under development.  Please contact us if this Use Case is critical to you.

Deep Copy 

Copy services are provided for cloning business objects, including provisions for copying related child data such as the Lineitems for a Purchaseorder.  These are typically called via a single action rule, so cloning a Purchaseorder is 1 rule

The Logic Method is shown below, based on the Insert logic extension:

var arg = req.getUserProperties().get("clone");
log.debug("cloneXX: checking request arg.clone parameter was provided: " + arg);
if (arg !== null && logicContext.logicNestLevel < 1) {
    log.debug("cloning: " + row);
    var deepCopy = ["lineitemsList"];
    SysLogic.copyTo("orders", logicContext, deepCopy, false); 
}


To activate the copy function in the RESTlab, GET the first order, paste it to the request, and PUT with the following url argument, like this:

http://server.acme.com/rest/abl/sample/v1/orders/1?arg.clone=true