|
| 1 | +# Drafting fields |
| 2 | + |
| 3 | +Title: Neuron-JS vs json-rules-engine: choosing the right JSON rules engine |
| 4 | + |
| 5 | +Alias: neuron-js-vs-json-rules-engine |
| 6 | + |
| 7 | +Description: A practical migration guide for teams comparing Neuron-JS with json-rules-engine, focused on TypeScript ownership, validation, explainability, and deterministic JSON business rules. |
| 8 | + |
| 9 | +Tags: neuron-js, typescript, rules-engine, json-rules-engine, migration, business-rules, ai-agents |
| 10 | + |
| 11 | +# Markdown body |
| 12 | + |
| 13 | +## The short version |
| 14 | + |
| 15 | +`json-rules-engine` is the established Node.js default for JSON rules. It is mature, recognizable, and built around facts, conditions, events, and rule priorities. |
| 16 | + |
| 17 | +Neuron-JS takes a narrower position: **JSON business rules for TypeScript systems that need validation before runtime and explanation after execution**. |
| 18 | + |
| 19 | +That difference matters when business rules become product infrastructure instead of a helper library. |
| 20 | + |
| 21 | +Use `json-rules-engine` when your system already thinks in facts and events, and the existing ecosystem fit is more important than a built-in validation and explanation contract. |
| 22 | + |
| 23 | +Use Neuron-JS when the rule layer must be: |
| 24 | + |
| 25 | +- stored as JSON, |
| 26 | +- generated or edited outside deployment cycles, |
| 27 | +- validated before runtime, |
| 28 | +- executed through an approved TypeScript registry, |
| 29 | +- explained for support, audit, or tests. |
| 30 | + |
| 31 | +## What each tool is optimized for |
| 32 | + |
| 33 | +`json-rules-engine` gives teams a known model: |
| 34 | + |
| 35 | +- facts describe the current world, |
| 36 | +- conditions decide whether something matches, |
| 37 | +- events describe what happened, |
| 38 | +- priorities help control rule order. |
| 39 | + |
| 40 | +That is a good model. If your business logic already lives there and works, migration is not automatically valuable. |
| 41 | + |
| 42 | +Neuron-JS is optimized for a different pressure point: **safe dynamic logic in TypeScript applications**. |
| 43 | + |
| 44 | +The core model is: |
| 45 | + |
| 46 | +- `Neuron` owns the approved rule vocabulary, |
| 47 | +- `Synapse` executes a serializable script, |
| 48 | +- schemas validate generated or stored JSON, |
| 49 | +- output summaries and explanation traces make the decision reviewable. |
| 50 | + |
| 51 | +That makes it useful when rules become a governance problem, not only an execution problem. |
| 52 | + |
| 53 | +## When Neuron-JS is the better fit |
| 54 | + |
| 55 | +Choose Neuron-JS when you need a tighter contract around JSON business rules. |
| 56 | + |
| 57 | +Good signs: |
| 58 | + |
| 59 | +- AI agents generate candidate rules and you need to reject malformed scripts before execution. |
| 60 | +- Product or operations teams need rule changes without a code deployment. |
| 61 | +- The application must keep a constrained vocabulary of approved conditions and actions. |
| 62 | +- Support needs to answer “why did this decision happen?” with a trace, not a guess. |
| 63 | +- Backend and browser code need the same deterministic rule definition. |
| 64 | + |
| 65 | +The point is not to be more abstract. The point is to make dynamic logic safer. |
| 66 | + |
| 67 | +## When json-rules-engine is the better fit |
| 68 | + |
| 69 | +Keep or choose `json-rules-engine` when: |
| 70 | + |
| 71 | +- facts, events, and priorities match your mental model, |
| 72 | +- your current rule assets are stable, |
| 73 | +- the team values ecosystem familiarity over a different contract, |
| 74 | +- validation and explanation can remain project-owned, |
| 75 | +- migration would add risk without real operational gain. |
| 76 | + |
| 77 | +A migration that does not improve safety, clarity, or operating speed is not engineering. It is churn. |
| 78 | + |
| 79 | +## Migration pattern |
| 80 | + |
| 81 | +Do not translate keywords mechanically. Translate the decision. |
| 82 | + |
| 83 | +A `json-rules-engine` rule often looks like this: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "conditions": { |
| 88 | + "all": [ |
| 89 | + { "fact": "customerTier", "operator": "equal", "value": "gold" }, |
| 90 | + { "fact": "cartTotal", "operator": "greaterThanInclusive", "value": 100 } |
| 91 | + ] |
| 92 | + }, |
| 93 | + "event": { |
| 94 | + "type": "apply-discount", |
| 95 | + "params": { "percent": 15 } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +In Neuron-JS, the same business decision becomes a script with explicit rule, condition, parameter, and action types: |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "id": "gold-customer-discount", |
| 105 | + "rules": [ |
| 106 | + { |
| 107 | + "id": "gold-order-over-threshold", |
| 108 | + "type": "simple_rule", |
| 109 | + "options": {}, |
| 110 | + "conditions": [ |
| 111 | + { |
| 112 | + "id": "cart-total-threshold", |
| 113 | + "type": "compare_two_numbers", |
| 114 | + "options": {}, |
| 115 | + "params": [ |
| 116 | + { "id": "cart-total", "name": "op1", "type": "simple_number", "value": "125", "options": {} }, |
| 117 | + { "id": "comparison", "name": "comp", "type": "comparator", "value": ">=", "options": {} }, |
| 118 | + { "id": "threshold", "name": "op2", "type": "simple_number", "value": "100", "options": {} } |
| 119 | + ] |
| 120 | + } |
| 121 | + ], |
| 122 | + "actions": [ |
| 123 | + { |
| 124 | + "id": "discount-score", |
| 125 | + "type": "add_two_numbers", |
| 126 | + "options": {}, |
| 127 | + "params": [ |
| 128 | + { "id": "base-discount", "name": "op1", "type": "simple_number", "value": "10", "options": {} }, |
| 129 | + { "id": "tier-bonus", "name": "op2", "type": "simple_number", "value": "5", "options": {} } |
| 130 | + ] |
| 131 | + } |
| 132 | + ] |
| 133 | + } |
| 134 | + ] |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +Then validate and explain it: |
| 139 | + |
| 140 | +```typescript |
| 141 | +import { |
| 142 | + Neuron, |
| 143 | + Synapse, |
| 144 | + explainExecution, |
| 145 | + summarizeExecutionOutput, |
| 146 | + validateExecutionContext, |
| 147 | + validateScript, |
| 148 | +} from '@sebasoft/neuron-js'; |
| 149 | + |
| 150 | +const context = { messages: [], state: { customerTier: 'gold', cartTotal: 125 } }; |
| 151 | + |
| 152 | +const scriptValidation = validateScript(script); |
| 153 | +const contextValidation = validateExecutionContext(context); |
| 154 | + |
| 155 | +if (!scriptValidation.ok || !contextValidation.ok) { |
| 156 | + throw new Error('Invalid migrated rule input'); |
| 157 | +} |
| 158 | + |
| 159 | +const result = new Synapse(new Neuron()).execute(script, context); |
| 160 | +const output = summarizeExecutionOutput(result); |
| 161 | +const explanation = explainExecution({ script, result }); |
| 162 | +``` |
| 163 | + |
| 164 | +## Migration checklist |
| 165 | + |
| 166 | +- Map each fact to a parameter or context field. |
| 167 | +- Map each operator to an approved condition type. |
| 168 | +- Map each event to an approved action type. |
| 169 | +- Preserve rule order, priority, and conflict behavior with tests. |
| 170 | +- Validate every migrated script before execution. |
| 171 | +- Snapshot explanation traces for high-value rules. |
| 172 | +- Keep `json-rules-engine` if the existing model is already doing the job cleanly. |
| 173 | + |
| 174 | +## Documentation |
| 175 | + |
| 176 | +The full migration page is here: |
| 177 | + |
| 178 | +[Neuron-JS vs json-rules-engine](https://sebasoft.github.io/neuron-js/comparisons/json-rules-engine.html) |
| 179 | + |
| 180 | +Start with the comparison hub if you are deciding between multiple rules approaches: |
| 181 | + |
| 182 | +[Neuron-JS comparison and migration guides](https://sebasoft.github.io/neuron-js/comparisons/) |
0 commit comments