UNPKG

3.42 kBMarkdownView Raw
1# Configuration
2
3Evejs has a default [`ServiceManager`](api.md#servicemanager) loaded at `eve.system`.
4This service manager is loaded with a `LocalTransport`, allowing local agents to
5communicate with each other without need for additional configuration.
6
7The default service manager can be configured like:
8
9```js
10var eve = require('evejs');
11var HelloAgent = require('./examples/agents/HelloAgent');
12
13// configure eve
14eve.system.init({
15 transports: [
16 {
17 type: 'distribus',
18 }
19 ],
20 timer: {
21 rate: 'discrete'
22 }
23});
24
25// create two agents
26var agent1 = new HelloAgent('agent1');
27var agent2 = new HelloAgent('agent2');
28
29agent2.sayHello('agent1');
30```
31
32Configuration can be saved in a separate configuration file like **config.json**:
33
34```json
35{
36 "transports": [
37 {
38 "type": "distribus"
39 }
40 ],
41 "timer": {
42 "rate": "discrete"
43 }
44}
45```
46
47And loaded like:
48
49```
50var eve = require('evejs');
51
52var config = require('./config.json');
53eve.system.init(config);
54```
55
56
57## Transports
58
59One or multiple transports can be configured for the ServiceManager:
60
61```js
62eve.system.init({
63 transports: [
64 {
65 type: 'distribus'
66 },
67 {
68 type: 'ws',
69 url: 'ws://localhost:3000/agents/:id'
70 },
71 ...
72 ]
73});
74```
75
76All transports and their configuration options are described on the page [Transports](transports.md).
77
78
79
80## Timer
81
82The ServiceManager comes with a configurable timer, which can be used to run
83simulations in discrete time or hyper time. Evejs uses
84[hypertimer](https://github.com/enmasseio/hypertimer) for this. By default,
85the timer runs in real-time.
86
87To configure the default timer:
88
89```js
90eve.system.init({
91 timer: {
92 rate: 'discrete', // a number or 'discrete'
93 deterministic: true, // true or false.
94 }
95});
96```
97
98The timer has functions `setTimeout`, `setInterval`, `setTrigger`, `setTime`,
99`getTime`, and more. To use the timer:
100
101```js
102var delay = 1000;
103eve.system.timer.setTimeout(function () {
104 console.log('hello world!');
105}, delay)
106```
107
108
109## Random
110
111The [ServiceManager](api.md#servicemanager) contains a `random` function.
112This function returns a random value between 0 (inclusive) and 1 (exclusive).
113The function defaults to the built in `Math.random` function. For simulations,
114the function can be configured to a deterministic one for reproducible execution.
115The [`seed-random`](https://github.com/ForbesLindesay/seed-random) module
116is used for this.
117
118To configure the random function:
119
120```js
121eve.system.init({
122 random: {
123 deterministic: true, // false by default
124 seed: 'my seed' // optional, 'random seed' by default.
125 }
126});
127```
128
129Usage:
130
131```js
132var value = eve.system.random(); // get a random value between 0 and 1
133```
134
135Available properties:
136
137- `deterministic: boolean`
138 When false, the non-deterministic `Math.random` is used. When true, a
139 deterministic `random` function is used.
140
141- `seed: string`
142 Optional. A custom seed for the deterministic random function.
143 Only applicable when property `deterministic` is true, and `entropy` is false.
144
145- `global: boolean`
146 Optional. false by default. When true, the global `Math.random` will be
147 overwritten by the deterministic one.
148 Only applicable when property `deterministic` is true.
149
150- `entropy: boolean`
151 Optional, false by default. If true, a seed is automatically generated from
152 local data. Only applicable when property `deterministic` is true.