Skip to content

Commit eb41354

Browse files
committed
docs: clarify logical grouping (Sum of Products) and precedence
1 parent b9e3c30 commit eb41354

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

docs/concepts/implementation-examples.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,45 @@ const context: MyAppContext = {
143143
const result = engine.execute(script, context);
144144
console.log(result.context.messages); // [{ type: 'info', text: 'Alice: Good morning!' }]
145145
```
146+
147+
## 6. Logical Grouping (AND/OR)
148+
`neuron-js` uses a "Sum of Products" approach to logic. Conditions are grouped into blocks.
149+
- **AND**: All conditions within a block must be true.
150+
- **OR**: If any block is true, the entire rule evaluates to true.
151+
152+
### How to trigger OR
153+
Setting `orCondition: true` on a condition starts a **new block**. This new block is joined to the previous block with an `OR` operator.
154+
155+
### Example: (A AND B) OR (C AND D)
156+
```json
157+
{
158+
"id": "complex-logic",
159+
"conditions": [
160+
{ "id": "A", "type": "is_gold_member" },
161+
{ "id": "B", "type": "has_high_balance" },
162+
{
163+
"id": "C",
164+
"type": "is_new_user",
165+
"options": { "orCondition": true }
166+
},
167+
{ "id": "D", "type": "has_promo_code" }
168+
]
169+
}
170+
```
171+
**Evaluation Logic**: `(A && B) || (C && D)`
172+
173+
## 7. Condition Inversion (NOT)
174+
Any condition can be negated by setting `inverted: true` in its options. This is handled by the runtime, so your plugin implementation doesn't need to worry about it.
175+
176+
```json
177+
{
178+
"id": "not-blocked",
179+
"conditions": [
180+
{
181+
"type": "is_user_blocked",
182+
"options": { "inverted": true }
183+
}
184+
]
185+
}
186+
```
187+
**Evaluation Logic**: `!is_user_blocked`

docs/concepts/scripts-and-rules.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ A `Rule` is a self-contained logical unit. It consists of two main parts:
1919
```
2020

2121
## Condition Logic
22-
Conditions are evaluated sequentially. By default, they follow an **AND** logic (all must be true).
22+
Conditions are evaluated sequentially and follow a **Sum of Products** logic (ANDs grouped by ORs).
2323

24-
### Advanced Condition Features:
25-
- **Inversion**: A condition can be "inverted" (NOT logic).
26-
- **OR Grouping**: A condition can be marked as an `orCondition`. If the next condition is an `orCondition`, they are treated as a group where only one needs to pass.
24+
### AND Logic
25+
By default, all conditions in the list are joined by **AND**. Every condition must evaluate to `true` for the actions to trigger.
26+
27+
### OR Grouping
28+
You can create an **OR** relationship by setting `orCondition: true` in a condition's options. This starts a **new block** of conditions.
29+
- Each block is evaluated as an **AND** group.
30+
- All blocks are joined by **OR**.
31+
- If **any** block evaluates to `true`, the rule fires.
32+
33+
**Example result**: `(Block 1 AND Block 1) OR (Block 2 AND Block 2)`
34+
35+
### Advanced Features:
36+
- **Inversion**: Set `inverted: true` to negate a condition (NOT logic).
2737
- **Disabling**: Rules or individual conditions can be disabled via options without removing them from the script.
2838

2939
## Sequential Execution

0 commit comments

Comments
 (0)