-
Notifications
You must be signed in to change notification settings - Fork 181
/
fibonacci.js
68 lines (57 loc) · 1.85 KB
/
fibonacci.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";
var nools = require("../index");
var Fibonacci = function (sequence, value) {
this.sequence = sequence;
this.value = value || -1;
};
var Result = function (result) {
this.result = result || -1;
};
var flow = nools.flow("Fibonacci Flow", function (flow) {
flow.rule("Recurse", [
["not", Fibonacci, "f", "f.sequence == 1"],
[Fibonacci, "f1", "f1.sequence != 1"]
], function (facts) {
this.assert(new Fibonacci(facts.f1.sequence - 1));
});
flow.rule("Bootstrap", [
Fibonacci, "f", "f.value == -1 && (f.sequence == 1 || f.sequence == 2)"
], function (facts) {
this.modify(facts.f, function () {
this.value = 1;
});
});
flow.rule("Calculate", [
[Fibonacci, "f1", "f1.value != -1", {sequence: "s1"}],
[Fibonacci, "f2", "f2.value != -1 && f2.sequence == s1 + 1", {sequence: "s2"}],
[Fibonacci, "f3", "f3.value == -1 && f3.sequence == s2 + 1"],
[Result, "r"]
], function (facts) {
var f3 = facts.f3, f1 = facts.f1, f2 = facts.f2;
var v = f3.value = f1.value + facts.f2.value;
facts.r.result = v;
this.modify(f3);
this.retract(f1);
});
});
var r1 = new Result(),
session1 = flow.getSession(new Fibonacci(10), r1),
s1 = new Date;
session1.match().then(function () {
console.log("%d [%dms]", r1.result, new Date - s1);
session1.dispose();
});
var r2 = new Result(),
session2 = flow.getSession(new Fibonacci(150), r2),
s2 = new Date;
session2.match().then(function () {
console.log("%d [%dms]", r2.result, new Date - s2);
session2.dispose();
});
var r3 = new Result(),
session3 = flow.getSession(new Fibonacci(1000), r3),
s3 = new Date;
session3.match().then(function () {
console.log("%d [%dms]", r3.result, new Date - s3);
session3.dispose();
});