@mangar2/decision

2.3.1 • Public • Published

Abstract

Provides a JSON based term evaluator to calculate decisions based on configuration files The syntax of the JSON-terms is derived from Lisp .

Contents

Meta

File decision.js
Abstract Provides a JSON based term evaluator to calculate decisions based on configuration files The syntax of the JSON-terms is derived from Lisp .
Author Volker Böhm
Copyright Copyright ( c ) 2020 Volker Böhm
License This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .

Class Decision

new Decision(variables)

Creates a value class providing the ability to evalutate a json based value . Additionally it explains the value in a juman readable string . Supported operations are

  • [ "or" , t1 , t2 , . . . ]
  • [ "and" , t1 , t2 , . . . ]
  • [ operator , t1 , t2 ] with operators '>' , '<' , '> = ' , '< = ' , ' ! = ' , ' = '
  • [ "+/-" , t1 , t2 ]
  • [ "if" , t1 , then , else ]
  • [ "switch" , t1 , { map } ] where map is an array of variable value maps . Example [ "switch" , 1 , { 1 : "a" , 2 : "b" } ] results in 'a'
  • [ "function" , . . . parameter ] where function is registrated by a call to 'on'

Example

const variables = {
   'a/b': 1,
   'b/c': 2,
   'system/presence': 'awake'
}
const value = new Decision(variables)
const result = value.decide(
     [
          'and',
          ['or', ['<', 'a/b', 10], ['<', 'b/c', 2]],
          ['or', ['>=', 'a/b', 10], ['>=', 'b/c', 2]]
      ]
)
// returns result.value = true and result.reason = 'a/b (1) is < 10 and b/c (2) is >= 2'

Decision Parameters

Name Type Attribute Default Description
variables Object optional { } list of known variables and values

Decision Methods

_ISOStringToDate

_ISOStringToDate (value) => {Date, any}

Converts any value from an ISO formatted date string to a date object If it is not an ISO formatted date string , the original value will be returned

_ISOStringToDate Parameters
Name Type Description
value any any value
_ISOStringToDate returns
Type Description
Date, any a Date object or the original value

eval

eval (jsonTree) => {EvalResult}

Calculates a value based on a json based json tree

eval Parameters
Name Type Description
jsonTree Object json structured boolean logic
eval throws
Type Description
Error on any error in the jsonTree
eval returns
Type Description
EvalResult result of the evaluation

on

on (event, callback)

Sets a callback .

on Parameters
Name Type Description
event string event name ( not case sensitive ) for the callback
callback function ( parameter )
on throws
Type Description
Error if the callback is not 'function'

Class EvalResult

new EvalResult(value, reason)

Creates a result object with a "value" and a "reason" and a list of used variable : value pairs

EvalResult Parameters

Name Type Description
value Date, string, number, boolean evaluation value
reason string value reason in human readable form

EvalResult Members

Name Type description
reason string Reason explaining the result
value Date, string, number, boolean Value of the result
variables Array.<string> List of missing variables found

Package Sidebar

Install

npm i @mangar2/decision

Weekly Downloads

0

Version

2.3.1

License

LGPL-3.0-or-later

Unpacked Size

41.8 kB

Total Files

15

Last publish

Collaborators

  • mangar2