1 | # Configuration
|
2 |
|
3 | Evejs has a default [`ServiceManager`](api.md#servicemanager) loaded at `eve.system`.
|
4 | This service manager is loaded with a `LocalTransport`, allowing local agents to
|
5 | communicate with each other without need for additional configuration.
|
6 |
|
7 | The default service manager can be configured like:
|
8 |
|
9 | ```js
|
10 | var eve = require('evejs');
|
11 | var HelloAgent = require('./examples/agents/HelloAgent');
|
12 |
|
13 | // configure eve
|
14 | eve.system.init({
|
15 | transports: [
|
16 | {
|
17 | type: 'distribus',
|
18 | }
|
19 | ],
|
20 | timer: {
|
21 | rate: 'discrete'
|
22 | }
|
23 | });
|
24 |
|
25 | // create two agents
|
26 | var agent1 = new HelloAgent('agent1');
|
27 | var agent2 = new HelloAgent('agent2');
|
28 |
|
29 | agent2.sayHello('agent1');
|
30 | ```
|
31 |
|
32 | Configuration 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 |
|
47 | And loaded like:
|
48 |
|
49 | ```
|
50 | var eve = require('evejs');
|
51 |
|
52 | var config = require('./config.json');
|
53 | eve.system.init(config);
|
54 | ```
|
55 |
|
56 |
|
57 | ## Transports
|
58 |
|
59 | One or multiple transports can be configured for the ServiceManager:
|
60 |
|
61 | ```js
|
62 | eve.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 |
|
76 | All transports and their configuration options are described on the page [Transports](transports.md).
|
77 |
|
78 |
|
79 |
|
80 | ## Timer
|
81 |
|
82 | The ServiceManager comes with a configurable timer, which can be used to run
|
83 | simulations in discrete time or hyper time. Evejs uses
|
84 | [hypertimer](https://github.com/enmasseio/hypertimer) for this. By default,
|
85 | the timer runs in real-time.
|
86 |
|
87 | To configure the default timer:
|
88 |
|
89 | ```js
|
90 | eve.system.init({
|
91 | timer: {
|
92 | rate: 'discrete', // a number or 'discrete'
|
93 | deterministic: true, // true or false.
|
94 | }
|
95 | });
|
96 | ```
|
97 |
|
98 | The timer has functions `setTimeout`, `setInterval`, `setTrigger`, `setTime`,
|
99 | `getTime`, and more. To use the timer:
|
100 |
|
101 | ```js
|
102 | var delay = 1000;
|
103 | eve.system.timer.setTimeout(function () {
|
104 | console.log('hello world!');
|
105 | }, delay)
|
106 | ```
|
107 |
|
108 |
|
109 | ## Random
|
110 |
|
111 | The [ServiceManager](api.md#servicemanager) contains a `random` function.
|
112 | This function returns a random value between 0 (inclusive) and 1 (exclusive).
|
113 | The function defaults to the built in `Math.random` function. For simulations,
|
114 | the function can be configured to a deterministic one for reproducible execution.
|
115 | The [`seed-random`](https://github.com/ForbesLindesay/seed-random) module
|
116 | is used for this.
|
117 |
|
118 | To configure the random function:
|
119 |
|
120 | ```js
|
121 | eve.system.init({
|
122 | random: {
|
123 | deterministic: true, // false by default
|
124 | seed: 'my seed' // optional, 'random seed' by default.
|
125 | }
|
126 | });
|
127 | ```
|
128 |
|
129 | Usage:
|
130 |
|
131 | ```js
|
132 | var value = eve.system.random(); // get a random value between 0 and 1
|
133 | ```
|
134 |
|
135 | Available 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.
|