json-rules-engine icon indicating copy to clipboard operation
json-rules-engine copied to clipboard

Feature/add value pipes support

Open migzzi opened this issue 3 years ago • 1 comments

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 avatar Sep 19 '22 14:09 migzzi

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

migzzi avatar Sep 20 '22 12:09 migzzi

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

yoni333 avatar Feb 19 '23 13:02 yoni333

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

CacheControl avatar Oct 03 '23 20:10 CacheControl

@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 avatar Oct 06 '23 02:10 chris-pardy

@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.

chris-pardy avatar Oct 17 '23 15:10 chris-pardy

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.

yoni333 avatar Oct 18 '23 05:10 yoni333