liveBook · Manning

Business rule

As explained in Building User-Friendly DSLs

A declarative statement expressing a policy, constraint, or decision condition for a business domain.

A business rule expresses business logic in the form “Given a context, when a condition, then a consequence.” The condition determines whether the rule fires; the consequence determines what happens when it does. In a rule notation defined on a record type, the context can be implicit because the record type supplies the data shape in which the rule runs. section 14.4 section 14.0

Rules let domain experts state policies in terms they recognize instead of leaving the logic buried in ordinary conditional code. A price quote, for example, can be determined by interacting rules that calculate surcharges, discounts, and fees from rental inputs. Before implementation, the rule set still needs checking for completeness, consistency, and ambiguity. section 1.1 section 14.0

Table 1.1 Natural language expressing car rental business knowledge
If the rental period contains a national holiday, then a surcharge rate of 10% is applied to the rental price for that holiday.
If the length of the rental period exceeds a whole week, then a discount rate of 5% is applied to the per-day rate.
If the car is a genuine gas guzzler, then an additional environmental fee of $10/day is applied.

These rental policies show the core pattern: a condition over a rental is paired with a concrete pricing consequence. section 1.1

A rule attached to a record type needs these parts:

  • Context — the record type whose attributes and data the rule uses. A record type can carry zero or more rules. section 14.1.1 section 14.4
  • Condition — a logical expression that must produce a Boolean value. Type checking rejects a condition with another type. section 14.1.1 section 14.4
  • Consequence — an action that sets or changes a value in the contextual record type. A data-changing consequence is an effect. section 14.4

In a structured DSL, a Business Rule can be represented as a concept with condition and consequence properties. The editor can render the fixed “When,” “then,” punctuation, and other adornments automatically, while guiding domain experts to enter valid constructs rather than editing unrestricted text. section 14.1.1 section 1.2.1

Computing a rental discount

Define a rule on Rental: when the rental period contains a Saturday, add 10 to discount. At runtime, translate the condition and consequence into an if statement. A second rule can add 5 when the rental period starts in December; each applicable rule contributes to the accumulated discount. section 14.0 section 14.2.1

Running a rule means evaluating its condition and applying its consequence only when the condition is true. Rules can generate Runtime code, but the execution model must specify how their effects are collected and applied. Gathering effects from all rules at once is more flexible than grouping execution by the attribute each rule affects. section 14.2.1 section 14.4

How Building User-Friendly DSLs covers it

How deep it goes
Defines rule structure, models it in an AST, type-checks conditions, and translates separate rules to JavaScript execution.
Examples
Uses Rental rules that add discounts for a Saturday-containing period and for a rental starting in December.
What it assumes
Assumes prior DSL implementation techniques while introducing business-rule structure, Boolean conditions, and consequences explicitly.
What you can do after
Build it from scratch by attaching typed condition-and-consequence rules to record types and generating Runtime behavior.
How it connects
Connects record types, logical expressions, Boolean types, AST objects, effects, and execution models.

Also explained in

Compare how 4 books explain business rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.