Creating APIs‎ > ‎Logic‎ > ‎

Reactive Logic

Reactive programming addresses the common issue of Change Management, by providing a mechanism for watching for changes and communicating them, and include the following classes:

  • Procedural. A variation of the observer software design pattern: Register listener code to observers who invoke the listeners when changes are made. Reactive programming extends this to order listener invocation.
  • Declarative. You provide expressions (and functions) for variables, where API Server watches for changes to referenced data, and reacts by re-evaluating the referencing variable.

Reactive programming is what makes Live API Creator unique: declarative, spreadsheet-like rules. You can deliver update business logic 10X faster using rules.

Reactive Logic is Declarative: What, not How

Logic is declarative, providing active enforcement, re-use over transactions, optimizations, and dependency management/ordering. The last rule in the following diagram illustrates that you can use server-side JavaScript for complete control. You specify business rules with a combination of JavaScript (logic) events and reactive logic, spreadsheet-like expressions (rules) that describe what is to be done, not how to do it:

It is a difficult problem to apply declarative to the general class of all computing. Successful approaches typically narrow the focus to useful problem domains, such as spreadsheets. Live API Creator employs this declarative paradigm to transaction processing: derivation rules (for example, formulas, sums) and validations:

  1. You bind these rules (or, more generally, JavaScript functions) to database tables and columns.
  2. On RESTful updates (POST, PUT, DELETE), Live API Creator does the following:
    1. Watches for changes to referenced data,
    2. Reacts to adjust referencing data, which can
    3. Chain to other rules. Chaining includes references in other tables. SQL and transaction bracketing are automated.

Reactive logic works in conjunction with server-side JavaScript events. This provides complete flexibility, including access to loadable JavaScript libraries.

For more information:

Rules operate differently than conventional programming. Reactive logic has the following key differences to conventional procedural code: 

Key Difference What it means Why it matters
Automatic Invocation API Server calls the logic without the need to explicitly invoke such logic. Eliminates errors where certain paths might fail to invoke required logic.
Automatic Chaining Changes trigger other changes and that chains to adjust the Order total. Automates complex multi-table transaction logic
Automated SQL Watch/React includes multi-table logic. For example, you do not need to issue the SQL to retrieve the order. SQL is a chore to code, and to optimize (pruning, caching).
Automatic Ordering Derivates are executed in a order that reflects their dependencies Simplifies maintenance, since ordering is dictated by dependencies and is recomputed on each change.

Logic Events

You can supply JavaScript table event handlers. The logic engine invokes these on every update, providing key contextual information such as row, oldRow, and logicContext.

For more information about logic events, including event firing and how to supply JavaScript table event handlers, see Logic Event Rule Types.

Establish Watches

When you declare logic, API Creator processes them to determine their references and establish watches on the referenced attributes. You specify your business logic in the following ways:
  • Fill out a form. You use this for parent copies, sum, count, min, max.
  • Provide server-side JavaScript. For formulas and validations, you provide an expression, or more generally a function.

This determines how references are detected:

Rule Type Watch
Parent Copy Parent Copy takes effect when you first associate a child to a parent, either on insert or a change to the foreign key.

Note: Changes to the copied attribute are not watched. To propagate these values, make parent.attribute references in formulas or validations.
Aggregates: sum, count, min, max Watches are established on:
  • Foreign key. These are all based on relationships, so all the elements of the foreign key are watched.
  • Sum/Min/Max attribute. The value being summed, min'd, etc.
  • Qualification condition. The child condition that identifies which child rows are included in the aggregation.
For example:
balance = sum(orders.amountTotal where paid = false)
places watches on order attributes customerId (the foreign key), amountTotal, and paid.
Formula and Validation Your code is scanned, and a watch is established for every row.attribute reference. For example:
amount = row.price * row.quantity
places watches on the price and quantity.

How React Operates

In the case of our customer.balance rule, the statement obligates API Creator to respond to all of the following:

  • Order inserted - balance increased
  • Order deleted - balance decreased (if not paid)
  • Order paid - balance decreased
  • Order unpaid - balance decreased
  • Order amountTotal changed - balance adjusted
  • Order reassigned to different customer - balance increased for new customer, decreased for old

How Chain Operates

Logic can chain, since one rule can reference the value of another rule. There are dependencies between rules. In our previous example, the Customer.balance refers to the Purchaseorder.amount_total, itself defined by a rule. A change to the Lineitem.amount affects the Customer.balance.

The following table explains how API Creator implements chaining:

Rule Type React
Parent Copy There is no chaining for parent copies.

For child parent.attribute references, API Server visits each child and evaluates the rules that are dependent on the parent attribute.
Aggregates: sum, count, min, max API Server issues a one-row update to the parent object, which already be in cache. Select sum queries are expensive, so are not employed.
 Formula and Validation The JavaScript code is executed.

Changes are affected instantly, without the need for code generation or deployment. The logic is fully executable. Update logic is complementary to other forms of business logic, such as process (workflow) logic, application integration, and decision logic.

For more information:

  • About each rule type, including tutorials and training, see Quick Reference.
  • About the architecture, which includes basic background on business logic, see Logic Execution.

Contrast to Traditional

Without such declarative automation, your procedural code must perform all of this watch/react dependency management, ordering, change detection, and optimization over all the use cases. This is how five rules can represent over 400 lines of lines of Java code. The entire code that deals with these User Stories is in the Manual Code.zip file.

The following screen shot illustrates a portion of the Java logic that implements our sample. It illustrates how the real logic is obscured by all the cookie-cutter code to watch for changes, react to them, and deal with SQL

Complexity Handling

Declarative logic is extensible, in particular with server-side JavaScript logic events that can use loadable libraries. You can solve complex problems with little effort using declarative logic. The Reactive Logic tutorial illustrates how you can solve classically complex problems - a Bill of Materials, or allocate a Payment to a set of outstanding orders - with a few rules.

For more information: