Reactive Logic

Reactive Logic
lac42
Reactive programming addresses the common issue of Change Management. It is what makes 
CA Live API Creator
 unique. Reactive programming provides a mechanism for 
CA Live API Creator
 to watch for changes and communicate 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 
    CA Live API Creator
     watches for changes to referenced data and reacts by re-evaluating the referencing variable. When you declare logic, API Creator determines the rules that are referenced by processing them, and then establishes watches on the referenced attributes.
You can deliver update business logic ten times 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.
The following image illustrates reactive logic in API Creator:
CA Technologies  
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. 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
  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, providing 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
Event rules provide access to external/reusable JavaScript functions. The logic engine invokes these on every update, providing key contextual information such as 
row
oldRow
, and 
logicContext
.
For more information about event rules, including event firing and how to create them, see Event Rule Types.
Establish Watches
Watches determine how 
CA Live API Creator
 detects references:
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.
Aggregate rules: sum, count, min, and max
 
CA Live API Creator
 establishes watches on the following:
  • Foreign key.
     Based on relationships. 
    CA Live API Creator
     watches the elements of the foreign key.
  • Sum/Min/Max attribute.
     The value that 
    CA Live API Creator
     is aggregating.
  • Qualification condition.
     The child condition that identifies which child rows 
    CA Live API Creator
     includes in the aggregation.
For example, with the following code, 
CA Live API Creator
 watches order attributes 
customerId
 (the foreign key), 
amountTotal
, and 
paid
:
balance = sum(orders.amountTotal where paid = false)
Formula and validation
 
CA Live API Creator
 scans your code and establishes a watch for every 
row.<attribute>
 reference.
For example, with the following code, 
CA Live API Creator
 watches the 
price
 and 
quantity
 :
amount = row.price * row.quantity
How React Operates
In the case of the 
customer.balance
 rule, the statement obligates 
CA Live API Creator
 to respond to 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 a rule can reference the value of another rule. There are dependencies between rules. In the previous example, the 
Customer.balance
 rule refers to the 
Purchaseorder.amount_total
 rule, itself defined by a rule. A change to the 
Lineitem.amount
 rule affects the 
Customer.balance
 rule.
The following table explains how 
CA Live API Creator
 implements chaining:
Rule Type
React
Parent copy
For child 
parent.<attribute>
 references, 
CA Live API Creator
 visits each child and evaluates the rules that are dependent on the parent attribute.
Aggregates: sum, count, min, max
 
CA Live API Creator
 issues a one-row update to the parent object, which might already be in cache.
CA Live 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:
 
Contrast to Traditional
Without declarative automation, your procedural code must perform this watch/react dependency management, ordering, change detection, and optimization over 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 the sample. It illustrates how the cookie-cutter code watches for changes, reacts to them, and deals with SQL by obscuring the real logic:
CA Technologies  
Complexity Handling
You can extend the declarative logic, in particular with event rules that can use loadable libraries. You can solve complex problems with little effort using declarative logic.
The 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 about declarative logic, see Extensibility.