/* ** Test: Signaling of agents between virtual nodes ** Use: jamlib, explorer agent ** Setup: Meshgrid of 4 virtual nodes ** */ var Jam = (process.versions && process.versions.v8)?require('/opt/JAM/lib/jamlib'):require('jamlib'); var rnd = Math.random; // Create the logical JAM node (and world) var myJam = Jam.Jam({ fastcopy:false, print:console.log, verbose:0, }); myJam.init(); myJam.extend([0,1,2],'sprint',function () {return '?'}); myJam.addNodes([{x:1,y:0},{x:0,y:1},{x:1,y:1}]); myJam.connectNodes([{x1:0,y1:0,x2:1,y2:0},{x1:1,y1:0,x2:1,y2:1}, {x1:0,y1:0,x2:0,y2:1},{x1:0,y1:1,x2:1,y2:1}]); myJam.Aios.options.log.node=true; var start,stop; print('Has watchdog: '+(myJam.Aios.watchdog!=undefined)); // Simple explorer agent var ac = function (dir,maxhops,verbose) { // Body variables this.delta={}; this.sensors=[]; this.y=0; this.dir=dir; this.hops=0; this.parent=none; this.MAXHOP=maxhops; this.verbose=verbose; this.ring=true; this.childs=[]; // Activities this.act = { init: function () { if (this.verbose>0) log('Starting on '+myNode()); this.delta=Vector(0,0); this.parent=myParent(); this.hops=0; }, replicate: function () { if (this.verbose) log('Replicating'); trans.update(replicate,migrate); this.childs.push(fork({dir:DIR.EAST,MAXHOP:1})); this.childs.push(fork({dir:DIR.SOUTH,MAXHOP:1})); this.childs.push(fork({dir:DIR.EAST,MAXHOP:2})); trans.update(replicate,notify); log('BC: '+broadcast('siggy',0,'SIGX',{from:me()})); sleep(100); }, percept: function () { if (this.verbose) log('Percepting'); if (this.hops>0 && this.ring) { switch (this.dir) { case DIR.EAST: this.dir=DIR.SOUTH; break; case DIR.SOUTH: this.dir=DIR.WEST; break; case DIR.WEST: this.dir=DIR.NORTH; break; case DIR.NORTH: this.dir=DIR.EAST; break; } return; } }, migrate: function () { if (this.verbose) log('Moving -> '+this.dir); switch (this.dir) { case DIR.NORTH: this.delta.y--; break; case DIR.SOUTH: this.delta.y++; break; case DIR.WEST: this.delta.x--; break; case DIR.EAST: this.delta.x++; break; } if (this.dir!=DIR.ORIGIN) { if (link(this.dir)) { this.hops++; moveto(this.dir); } else { log('No link to '+this.dir); kill(); } } }, notify: function () { if (this.verbose) log('Notification'); send(me(),'SIG1',{from:'master'}); iter(this.childs,function (child) { var s={}; s.from=me(); s['1,-1,0']={x:0,y:0}; send(child,'SIG1',{from:me()}); }); }, wait: function () { if (this.verbose) log('Waiting'); sleep(1000); }, end : function () { if (this.verbose) log('Terminating.'); kill(); } }; // Transitions this.trans = { init: percept, percept: function () { return this.dir==DIR.ORIGIN?replicate: (this.hops