es6-fuzz 3.0.7 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/CHANGELOG.md CHANGED
@@ -1,10 +1,18 @@
1
+ 2022-06-14
2
+ ==========
3
+
4
+ * 4.0.0
5
+ * feature: make the api work with boonJsInputs
6
+ * feature: add a test for outoput names only to consist of alphabet
7
+ letters
8
+
1
9
  2022-06-11
2
10
  ==========
3
11
 
12
+ * (churn): changelog
4
13
  * 3.0.7
5
14
  * fix: revert dep change
6
15
  * refactor: make dependencies lighter
7
- * (churn): changelog
8
16
 
9
17
  2022-06-02
10
18
  ==========
@@ -142,5 +150,3 @@
142
150
  * Fix output ght pages
143
151
  * DOcs
144
152
  * Ghpages
145
- * 2.5.5
146
- * Autorelease
package/lib/logic.js CHANGED
@@ -48,40 +48,51 @@ class Logic {
48
48
  }
49
49
  }
50
50
 
51
+ checkOutputName(name) {
52
+ const reg = /^[a-z]+$/i;
53
+ if (!reg.test(name)) {
54
+ throw Error('Output names can only be strings without space, without numbers and without special chars');
55
+ }
56
+ }
57
+
58
+
59
+
51
60
  init(output, shape) {
61
+ this.checkOutputName(output);
52
62
  this.initCalled = true;
53
63
  let type = TYPE_INIT;
54
64
  this.rules.push({ output, shape, type });
55
65
  return this;
56
66
  }
57
67
  and(output, shape) {
68
+ this.checkOutputName(output);
58
69
  this.checkInitCalled();
59
70
  let type = TYPE_AND;
60
71
  this.rules.push({ output, shape, type });
61
72
  return this;
62
73
  }
63
- or(output, shape) {
74
+ or(output, shape) {+
75
+ this.checkOutputName(output);
64
76
  this.checkInitCalled();
65
77
  let type = TYPE_OR;
66
78
  this.rules.push({ output, shape, type });
67
79
  return this;
68
80
  }
69
81
  not(output, shape) {
82
+ this.checkOutputName(output);
70
83
  this.checkInitCalled();
71
84
  let type = TYPE_NOT;
72
85
  this.rules.push({ output, shape, type });
73
86
  return this;
74
87
  }
75
- defuzzify(value) {
88
+ defuzzify(value, as=undefined) {
76
89
  this.checkInitCalled();
77
90
  let defuzzified = 'none';
78
91
  let fuzzified = 0;
79
92
  let lastShape;
80
-
81
93
  this.rules.forEach(rule => {
82
94
  rule.fuzzy = rule.shape.fuzzify(value);
83
95
  // lets keep the initial value
84
-
85
96
  if (rule.type === TYPE_INIT) {
86
97
  defuzzified = rule.output;
87
98
  fuzzified = rule.fuzzy;
@@ -97,14 +108,28 @@ class Logic {
97
108
  defuzzified = rule.output;
98
109
  fuzzified = rule.fuzzy;
99
110
  // if there is no shape, like for example for a NOT keep the last one
100
- lastShape = rule.shape || lastShape;
111
+ lastShape = rule.shape || lastShape;
101
112
  }
102
113
  });
114
+
115
+
116
+
117
+ var namePrefix = '';
118
+ if (!!as && typeof as === 'string') {
119
+ namePrefix = as + '.';
120
+ }
121
+
122
+ const boonJsInputs = {};
123
+ this.rules.forEach(rule => {
124
+ boonJsInputs[`${namePrefix}${rule.output}`] = rule.output === defuzzified;
125
+ });
126
+
103
127
  /**
104
128
  *
105
129
  * @example fuzzy.defuzzify(10)
106
130
  */
107
131
  return {
132
+ boonJsInputs,
108
133
  fuzzified: fuzzified,
109
134
  defuzzified: defuzzified,
110
135
  rules: this.rules,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es6-fuzz",
3
3
  "description": "fuzzy logic with and for es6",
4
- "version": "3.0.7",
4
+ "version": "4.0.0",
5
5
  "main": "lib/logic.js",
6
6
  "repository": "git@github.com:sebs/es6-fuzz.git",
7
7
  "homepage": "https://github.com/sebs/es6-fuzz",
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+ const Logic = require('../lib/logic');
3
+ const Triangle = require('../lib/curve/triangle');
4
+ const Trapezoid = require('../lib/curve/trapezoid');
5
+ const assert = require('assert');
6
+ const getEvaluator = require('boon-js').getEvaluator;
7
+
8
+ describe('js-bool test', () => {
9
+ var logicHeat = new Logic();
10
+ const optimalTemperature = new Triangle(10, 20, 30);
11
+ const toColdTemperature = new Triangle(0, 10, 15);
12
+ const toHotTemperature = new Triangle(25, 40, 60);
13
+
14
+ logicHeat.init('cold', toColdTemperature)
15
+ logicHeat.or('optimal', optimalTemperature)
16
+ logicHeat.or('hot', toHotTemperature)
17
+
18
+ var logicDistance = new Logic();
19
+ const close = new Triangle(0, 10, 20);
20
+ const far = new Triangle(5, 50, 100);
21
+
22
+ logicDistance.init('close', close)
23
+ logicDistance.or('far', far)
24
+
25
+
26
+ it('provides output for js-bool', () => {
27
+ const res = logicHeat.defuzzify(20, 'heat');
28
+ assert.ok(res.boonJsInputs);
29
+ })
30
+
31
+ it('jsbool output cold is false', () => {
32
+ const res = logicHeat.defuzzify(20);
33
+ assert.equal(res.boonJsInputs.cold, false);
34
+ })
35
+
36
+ it('jsbool output hot is false', () => {
37
+ const res = logicHeat.defuzzify(20);
38
+ assert.equal(res.boonJsInputs.cold, false);
39
+ })
40
+
41
+ it('jsbool output optimal is false', () => {
42
+ const res = logicHeat.defuzzify(20);
43
+ assert.equal(res.boonJsInputs.optimal, true);
44
+ })
45
+
46
+ it('jsbool output cold as heat is false', () => {
47
+ const res = logicHeat.defuzzify(20, 'heat');
48
+ assert.equal(res.boonJsInputs['heat.cold'], false);
49
+ })
50
+
51
+ it('jsbool output hot as heat is false', () => {
52
+ const res = logicHeat.defuzzify(20, 'heat');
53
+ assert.equal(res.boonJsInputs['heat.hot'], false);
54
+ })
55
+
56
+ it('jsbool output optimal as heat is true', () => {
57
+ const res = logicHeat.defuzzify(20, 'heat');
58
+ assert.equal(res.boonJsInputs['heat.optimal'], true);
59
+ });
60
+
61
+
62
+ describe('can combine 2 fuzzy decisions via boonJs', ()=>{
63
+ it.only('monster bites when heat is cold and distance is close', ()=>{
64
+
65
+ const resHeat = logicHeat.defuzzify(2, 'heat');
66
+ const resClose = logicDistance.defuzzify(2, 'distance');
67
+
68
+ const jsBoonInput = { ...resHeat.boonJsInputs, ...resClose.boonJsInputs }
69
+ console.log(jsBoonInput);
70
+
71
+ const monsterBiteTest = getEvaluator(
72
+ 'heat.cold AND distance.close',
73
+ );
74
+
75
+ const monsterSleepTest = getEvaluator(
76
+ 'heat.hot AND distance.far',
77
+ );
78
+
79
+ const resBite = monsterBiteTest(jsBoonInput)
80
+ assert.equal(resBite, true);
81
+
82
+ const resSleep = monsterSleepTest(jsBoonInput)
83
+ assert.equal(resSleep, false);
84
+ });
85
+ });
86
+ });
@@ -43,9 +43,9 @@ describe('logic', () => {
43
43
  var logic = new Logic();
44
44
  var res = logic
45
45
  .init('rage', rageRange)
46
- .not('no rage', rageRange)
46
+ .not('norage', rageRange)
47
47
  .defuzzify(20);
48
- assert.equal(res.toString(), 'no rage');
48
+ assert.equal(res.toString(), 'norage');
49
49
  });
50
50
  });
51
51