npm

Need private packages and team management tools?Check out npm Orgs. »

@ghg/amplifier

0.12.6 • Public • Published

Greenhouse Amplifier

Installation

$ yarn install -d

Quick example

import Amplifier, { Service, Variable } from '@ghg/amplifier';
 
export default new Amplifier({
  name: 'Amplifier example',
 
  requirements: [
    new Variable({
      name: 'weatherZuidholland',
      plugin: 'Weather',
      type: 'maxTemperature',
      country: 'nl',
      cities: ['Rotterdam', 'Dordrecht'],
    }),
 
    new Service({
      name: 'BeerService',
      plugin: 'Beer',
    }),
  ],
 
  rules: [
    {
      name: `Nice temperature `,
 
      condition(R, { variables }) {
        R.when(!variables.weatherZuidholland > 20);
      },
 
      async consequence(R, { services }) {
        const { BeerService } = services;
 
        await BeerService.bringMeBeer();
 
        R.stop();
      },
    },
  ],
});

Internal supported plugins

This module ships with some interal plugins

  • Adwords (Legacy)
  • Appnexus
  • DV360 (Legacy)
  • Facebook
  • Google Ads
  • Google Sheet
  • LemonPI (Legacy)
  • Outmoove
  • Weather

External plugins

The amplifier also support externally developed plugins. Plugins published with -amplifier-plugin in the package name will be loaded by the PluginManager. These plugins need to extend the the Plugin module eg.

import { Plugin } from "@ghg/amplifier";
 
export default class SamplePlugin extends Plugin {
  // required environment variables
  static requiredEnvVariables = ["BAR"];
 
  // required variables in requirements
  static requiredVariables = ["foo"];
 
  constructor({ options }) {
    super();
 
  }
 
  getName({ name }) {
      return name;
  }
  ...
}

Rules Manger

Explanation of the rule manager. An important part of the Amplifier is the rule manager, rules can be included in the amplifier by adding them to the rules property. Rules are object with an condition that need to be met before the consequence will be executed. Both have access to the R API and the execution context

Flow control API (R)

The first argument of both condition and its corresponding consequence is the R Flow Control API. This api controls the flow of the rules.

Below are the functions available via the Flow Control API.

If you look at the below rule example.

{
	"name": "transaction minimum",
	"priority": 3,
	"on" : true,
	"condition": function(R) {
		R.when(this.transactionTotal < 500);
	},
	"consequence": function(R) {
		this.result = false;
		R.stop();
	}
}

R.when

This function is used to pass the condition expression that we want to evaluate. In the above expression we pass the expression to check whether the transactionTotal attribute of the fact in context is below 500 or not. If the expression passed to R.when evaluates to true, then the condition will execute. Else the rule engine will move to next rule or may terminate if there are no rules left to apply.

R.next

This function is used inside consequence functions. This is used to instruct the rule engine to start applying the next rule on the fact if any.

R.stop

This function is used inside consequence functions to instruct the Rule Engine to stop processing the fact. If this function is called, even if rules are left to be applied, the rule engine will not apply rest of rules on the fact. It is used mostly when we arrive a conclusion on a particular fact and there is no need of any further process on it to generate a result.

As you can see above example, when the transaction is less than 500, we no longer need to process the rule. So stores false in result attribute and calls the stop immediately inside consequence.

R.restart

This function is used inside consequence functions to instruct the rule engine to begin applying the Rules on the fact from first. This function is also internally used by the Rule engine when the fact object is modified by a consequence function and it needs to go through all the rules once again. Using the ignoreFactChanges: true option when initializing a new rule engine will turn off this functionality.

Execution context

argument of both condition and its corresponding consequence is the execution context and will include the resolved variables and services. Note: consequence are async condition are not.

Keywords

none

install

npm i @ghg/amplifier

Downloadsweekly downloads

79

version

0.12.6

license

ISC

last publish

collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
Report a vulnerability