|
|
@@ -2,13 +2,15 @@ require 'date'
|
2
|
2
|
require 'cgi'
|
3
|
3
|
module Agents
|
4
|
4
|
class CodeAgent < Agent
|
|
5
|
+ #cannot_receive_events!
|
|
6
|
+ #cannot_be_scheduled!
|
5
|
7
|
description <<-MD
|
6
|
8
|
Here is an agent that gives you the ability to specify your own code. We have already provided you
|
7
|
9
|
a javascript object that has read access to this agent's memory, events, options and the attributes of the agent.
|
8
|
10
|
We also provide you with a method to create events on the server.
|
9
|
11
|
You will be provided with an instance of the Agent object in javascript, with access to the above data.
|
10
|
12
|
You can create events based on your own logic.
|
11
|
|
- Specifically, you have the following class
|
|
13
|
+ Specifically, you have the following class, lets say, present is a string "js_code".
|
12
|
14
|
|
13
|
15
|
function Agent(m, e, o, agent){
|
14
|
16
|
this.memory = JSON.parse(m);
|
|
|
@@ -16,9 +18,6 @@ module Agents
|
16
|
18
|
this.options = JSON.parse(o);
|
17
|
19
|
this.agent = JSON.parse(agent);
|
18
|
20
|
}
|
19
|
|
- Agent.prototype.print_memory = function(){
|
20
|
|
- return this.memory;
|
21
|
|
- }
|
22
|
21
|
Agent.prototype.run = function(){
|
23
|
22
|
//Implement me
|
24
|
23
|
// Example create a new event with the following code:
|
|
|
@@ -29,7 +28,9 @@ module Agents
|
29
|
28
|
|
30
|
29
|
We will yield control to your implementation in the following way:
|
31
|
30
|
|
|
31
|
+ context.eval(js_code); //this is the code that declares the class Agent, and provides a global create_event method.
|
32
|
32
|
context.eval("a = new Agent(memory, events, options, agent)")
|
|
33
|
+ context.eval(options['code'])
|
33
|
34
|
context.eval("a.run();")
|
34
|
35
|
|
35
|
36
|
You need to provide the run() implementation, as well as other methods it may need to interact with.
|
|
|
@@ -47,11 +48,6 @@ module Agents
|
47
|
48
|
return this.memory;
|
48
|
49
|
}
|
49
|
50
|
Agent.prototype.run = function(){
|
50
|
|
- //have access to this.memory, this.events, this.options, and this.agent;
|
51
|
|
- // do computation...
|
52
|
|
- //...
|
53
|
|
- var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});
|
54
|
|
- create_event(pd);
|
55
|
51
|
}
|
56
|
52
|
H
|
57
|
53
|
end
|
|
|
@@ -70,5 +66,32 @@ module Agents
|
70
|
66
|
context.eval(string)
|
71
|
67
|
context.eval("a.run();")
|
72
|
68
|
end
|
|
69
|
+ def check
|
|
70
|
+ context = V8::Context.new
|
|
71
|
+ context.eval(example_js)
|
|
72
|
+ context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
|
|
73
|
+ context.eval(options['code']) # should override the run function.
|
|
74
|
+ a, m, e, o = [self.attributes.to_json, self.memory.to_json, "".to_json, self.options.to_json]
|
|
75
|
+ string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
|
|
76
|
+ context.eval(string)
|
|
77
|
+ context.eval("a.run();")
|
|
78
|
+ end
|
|
79
|
+
|
|
80
|
+ def receive(incoming_events)
|
|
81
|
+ context = V8::Context.new
|
|
82
|
+ context.eval(example_js)
|
|
83
|
+ context.eval(options['code']) # should override the run function.
|
|
84
|
+ context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
|
|
85
|
+ a, m, e, o = [self.attributes.to_json, self.memory.to_json, incoming_events.to_json, self.options.to_json]
|
|
86
|
+ string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
|
|
87
|
+ context.eval(string)
|
|
88
|
+ context.eval("a.run();")
|
|
89
|
+ end
|
|
90
|
+ def default_options
|
|
91
|
+ js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});create_event(pd); }"
|
|
92
|
+ {
|
|
93
|
+ code: js_code
|
|
94
|
+ }
|
|
95
|
+ end
|
73
|
96
|
end
|
74
|
97
|
end
|