Docs‎ > ‎

Reactive Logic Tutorial

The reactive logic tutorial is a key element in the training ramp. Using a case-study approach of a familiar but complex database, this tutorial explains the approach for using API Creator. The tutorial progresses from simple (and typical) examples to very complex examples. It illustrates the key design patterns for reactive logic.

Skim the advanced examples to scan the logic. The logic is complex, but a quick scan gives you a good sense of how reactive logic addresses complex problems.

Verify Prerequisites

Ensure that you have completed the following prerequisites:

Process Overview

Use the following workflow to use Business Logic.

Create an API

  1. Create your API ProjectThis creates a default API.
  2. Shape your API by defining custom REST resources.

    For more information about defining custom REST resources, see Define Custom REST Resources.
Your application development team can now get started with the running API. For more information about team development, see Team Development.

Declare your Business Logic

Declare your business logic in API Creator. The following image illustrates the complete logic for this sample. For more information about business logic and reactive logic, see Specify your Business Rules.

The logic is:

  • Automatically invoked. Your logic is bound to database tables and columns. The API Server automatically invokes it on all update operations.
  • Automatically ordered. The listing is organized by table. You can enter the logic in any order. Live API Creator orders the execution based on dependencies and revises the order as you alter the rules.
  • Filterable. See the logic for that topic by selecting a topic link (colored rectangle to the right of the rule).

For more information about topics, including filtering rules by topic, see Topics.

Sample Database

The structure of the Sample Database is illustrated in the following image. (Click the image to open it in a new window.) It is small and familiar but with sufficiently complex structures to let us to explore several interesting use cases.

Load this Project

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 using the following examples.

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.

For more information about the live logic rules, see Quick Reference.

Basic Examples

The following examples are "typically" complex and are a great place to start learning about Business Logic. 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.
    Review the problems/solutions.

Note: These examples are provided in the download, although some comments have been removed 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.
    Open a window (or have a sheet of paper) for the data model and for the rules; then try to solve each problem, and compare your solution to the sample.
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 validation takes place on a parent table ("sample:customers") but the triggering events are changes in the orders and line items (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 when an order is "made ready".

Advanced Examples

The most common patterns we see in logic design are shown in the illustration. The following sections describe the more complicated cases we have seen through extensive experience.

The solutions are short. 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 providing an allocation reusable service via Business Logic Extensibility. In this example, 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 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.


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 (for example, if a bolt increases in price, so do all the kits that use bolts: wings, fuselages, etc).

Budget Rollup

You can roll a budget up an org chart (database structure here).

Bill of Materials Kit Explosion

Details

Learn more here.

This example is found here. The Bill of Materials Kit Explosion example is a requirement within Place Order. When ordering a product that is a kit (for example, 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. One 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.

Ignore Extra Attributes in a POST

You can access the ability to pass arguments to your rules on the URL (arg.ArgName1=1) using the req.getUserProperties().get("ArgName1"). You can pass additional attributes in your JSON during a POST or PUT that are not part of the database structure using the main:customer?arg.IgnoreExtraAttributes=1 argument. 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.

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 using a single action rule, so cloning a Purchaseorder is one rule.

The following code snipet illustrates the logic method 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