UNPKG

1.41 kBJavaScriptView Raw
1var babble = require('babble');
2var eve = require('../../index');
3
4function BabbleAgent(id, props) {
5 // execute super constructor
6 eve.Agent.call(this, id);
7
8 this.props = props;
9
10 // babblify the agent
11 this.extend('babble');
12
13 // add a conversation listener
14 this.listen('hi')
15 .listen(function (message, context) {
16 console.log(context.from + ': ' + message);
17 return message;
18 })
19 .decide(function (message, context) {
20 return (message.indexOf('age') != -1) ? 'age' : 'name';
21 }, {
22 'name': babble.tell('hi, my name is ' + this.id),
23 'age': babble.tell('hi, my age is ' + this.props.age)
24 });
25
26 // connect to all transports provided by the system
27 this.connect(eve.system.transports.getAll());
28}
29
30// extend the eve.Agent prototype
31BabbleAgent.prototype = Object.create(eve.Agent.prototype);
32BabbleAgent.prototype.constructor = BabbleAgent;
33
34// have a conversation with an other agent
35BabbleAgent.prototype.talk = function (to) {
36 var name = this.id;
37 var age = this.props.age;
38
39 this.tell(to, 'hi')
40 .tell(function (message, context) {
41 if (Math.random() > 0.5) {
42 return 'my name is ' + name;
43 } else {
44 return 'my age is ' + age;
45 }
46 })
47 .listen(function (message, context) {
48 console.log(context.from + ': ' + message);
49 });
50};
51
52module.exports = BabbleAgent;