- 
                Notifications
    You must be signed in to change notification settings 
- Fork 504
Condition Sharing #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            chris-pardy
  merged 4 commits into
  CacheControl:master
from
RateGravity:shared-conditions
  
      
      
   
  Jul 12, 2023 
      
    
  
     Merged
                    Condition Sharing #339
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| 'use strict' | ||
| /* | ||
| * This is an advanced example demonstrating rules that re-use a condition defined | ||
| * in the engine. | ||
| * | ||
| * Usage: | ||
| * node ./examples/10-condition-sharing.js | ||
| * | ||
| * For detailed output: | ||
| * DEBUG=json-rules-engine node ./examples/10-condition-sharing.js | ||
| */ | ||
|  | ||
| require('colors') | ||
| const { Engine } = require('json-rules-engine') | ||
|  | ||
| async function start () { | ||
| /** | ||
| * Setup a new engine | ||
| */ | ||
| const engine = new Engine() | ||
|  | ||
| /** | ||
| * Condition that will be used to determine if a user likes screwdrivers | ||
| */ | ||
| engine.setCondition('screwdriverAficionado', { | ||
| all: [ | ||
| { | ||
| fact: 'drinksOrangeJuice', | ||
| operator: 'equal', | ||
| value: true | ||
| }, | ||
| { | ||
| fact: 'enjoysVodka', | ||
| operator: 'equal', | ||
| value: true | ||
| } | ||
| ] | ||
| }) | ||
|  | ||
| /** | ||
| * Rule for identifying people who should be invited to a screwdriver social | ||
| * - Only invite people who enjoy screw drivers | ||
| * - Only invite people who are sociable | ||
| */ | ||
| const inviteRule = { | ||
| conditions: { | ||
| all: [ | ||
| { | ||
| condition: 'screwdriverAficionado' | ||
| }, | ||
| { | ||
| fact: 'isSociable', | ||
| operator: 'equal', | ||
| value: true | ||
| } | ||
| ] | ||
| }, | ||
| event: { type: 'invite-to-screwdriver-social' } | ||
| } | ||
| engine.addRule(inviteRule) | ||
|  | ||
| /** | ||
| * Rule for identifying people who should be invited to the other social | ||
| * - Only invite people who don't enjoy screw drivers | ||
| * - Only invite people who are sociable | ||
| */ | ||
| const otherInviteRule = { | ||
| conditions: { | ||
| all: [ | ||
| { | ||
| not: { | ||
| condition: 'screwdriverAficionado' | ||
| } | ||
| }, | ||
| { | ||
| fact: 'isSociable', | ||
| operator: 'equal', | ||
| value: true | ||
| } | ||
| ] | ||
| }, | ||
| event: { type: 'invite-to-other-social' } | ||
| } | ||
| engine.addRule(otherInviteRule) | ||
|  | ||
| /** | ||
| * Register listeners with the engine for rule success and failure | ||
| */ | ||
| engine | ||
| .on('success', async (event, almanac) => { | ||
| const accountId = await almanac.factValue('accountId') | ||
| console.log( | ||
| `${accountId}` + | ||
| 'DID'.green + | ||
| ` meet conditions for the ${event.type.underline} rule.` | ||
| ) | ||
| }) | ||
| .on('failure', async (event, almanac) => { | ||
| const accountId = await almanac.factValue('accountId') | ||
| console.log( | ||
| `${accountId} did ` + | ||
| 'NOT'.red + | ||
| ` meet conditions for the ${event.type.underline} rule.` | ||
| ) | ||
| }) | ||
|  | ||
| // define fact(s) known at runtime | ||
| let facts = { | ||
| accountId: 'washington', | ||
| drinksOrangeJuice: true, | ||
| enjoysVodka: true, | ||
| isSociable: true | ||
| } | ||
|  | ||
| // first run, using washington's facts | ||
| await engine.run(facts) | ||
|  | ||
| facts = { | ||
| accountId: 'jefferson', | ||
| drinksOrangeJuice: true, | ||
| enjoysVodka: false, | ||
| isSociable: true, | ||
| accountInfo: {} | ||
| } | ||
|  | ||
| // second run, using jefferson's facts; facts & evaluation are independent of the first run | ||
| await engine.run(facts) | ||
| } | ||
|  | ||
| start() | ||
|  | ||
| /* | ||
| * OUTPUT: | ||
| * | ||
| * washington DID meet conditions for the invite-to-screwdriver-social rule. | ||
| * washington did NOT meet conditions for the invite-to-other-social rule. | ||
| * jefferson did NOT meet conditions for the invite-to-screwdriver-social rule. | ||
| * jefferson DID meet conditions for the invite-to-other-social rule. | ||
| */ | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍