CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.54k stars 455 forks source link

Feature/add value pipes support #314

Closed migzzi closed 8 months ago

migzzi commented 1 year ago

Add support for pipes to manipulate fact values.

Here is an example of using a pipe to transform the balance currency from euros to USD and another one for applying a discount to the product.

const discount = 0.5;
const rule = {
    conditions: {
      all: [{
        fact: 'account',
        path: '$.balance',
        params: {
          accountType: 'eurosAccount'
        },
        pipes: [ { name: 'euroToUSD' } ],
        operator: 'greaterThanInclusive', 
        value: {
          fact: 'product',
          path: '$.price',
          pipes: [ { name: 'scale', args: [discount]}],
          params: {
            productId: 'giftCard'
          }
        }
      }]
    },
    event: { type: 'customer-can-partially-afford-gift-card' }
  }

 engine.addPipe('euroToUSD', (val) => val * 1.1)
migzzi commented 1 year ago

@CacheControl I need your review on this if you got some time.

yoni333 commented 1 year ago

i think this feature is out of the scope of rule engine. it is nice to have.... but it will break the pure rule engine concept. pipes will move as to the STATE MACHINE zone

CacheControl commented 9 months ago

re-opening for visibility and to hear more opinions on this feature; I can see it being valuable for certain situations.

chris-pardy commented 9 months ago

@CacheControl my thought on this is that we could make the Condition constructor pluggable, so that it's easy to drop in custom condition classes. Additionally a way to plug in custom Almanacs could fill this role.

Over all what I think we should focus on for improvements is less new features and more ways to plug in new features by providing points at which other code can step in.

chris-pardy commented 9 months ago

@CacheControl see #356 for part of the work to support something like this. The ability to plug in custom condition classes in order to add arbitrary logic. In this case the support for pipes would need to live in the condition.

yoni333 commented 9 months ago

in my work place we was need to add some changes in the source code in order to get data on array conditions - to know the index inside the array that fail the condition check. so we pass and expose the "this" of the inner rule object .. so it is needed for better data type "Array" results.