Reactive Logic

Reactive Logic
lac31
Reactive programming addresses the common issue of Change Management. It provides a mechanism for watching for changes and communicating them, and includes 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 Creator watches for changes to referenced data, and reacts by re-evaluating the referencing variable. When you declare logic, API Creator processes them to determine their references and establish watches on the referenced attributes.
Reactive programming is what makes
 
 
CA Live API Creator
 unique: declarative, spreadsheet-like rules. You can deliver update business logic 10X faster using rules.
In this article:
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. Reactive logic are 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.
CA Live API Creator
 employs this declarative paradigm to transaction processing: derivation rules, such as formulas and sums, and validations. You bind these rules (or, more generally, JavaScript functions) to database tables and columns. And then, on RESTful updates (POST, PUT, DELETE), 
CA Live API Creator
 does the following:
  1. Watches for changes to referenced data,
  2. Reacts to adjust referencing data, which can 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:
  • About chaining, see the Reactive Logic video.
    For more information about this video, see Videos.
  • About the JavaScript libraries, see Logic Libraries.
Reactive Logic Key Differences to Conventional Code
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 Creator 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.
Establish Watches
Watches determine 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.
API Creator does not watch changes to the copied attribute. 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, with the following code, API Creator watches order attributes 
customerId
 (the foreign key), 
amountTotal
, and 
paid
:
balance = sum(orders.amountTotal where paid = false)
Formula and validation
API Creator scans your code and establishes a watch for every 
row.<attribute>
 reference.
For example, with the following code, API Creator watches the 
price
 and 
quantity
:
amount = row.price * row.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
API Creator does not chain parent copies. For child
parent.<attribute>
references, API Creator visits each child and evaluates the rules that are dependent on the parent attribute.
Aggregates: sum, count, min, max
API Creator issues a one-row update to the parent object, which might already be in cache.
API Creator does not employ select sum queries.
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 Architecture.
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 code that deals with these User Stories is in the  file.
The following image shows the use cases and dependencies:
CA Technologies
The following image illustrates a portion of the Java logic that implements our sample.
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: