|
|
@@ -6,7 +6,7 @@ module Agents
|
6
|
6
|
#cannot_be_scheduled!
|
7
|
7
|
description <<-MD
|
8
|
8
|
Here is an agent that gives you the ability to specify your own code. We have already provided you
|
9
|
|
- a javascript object that has read access to this agent's memory, events, options and the attributes of the agent.
|
|
9
|
+ a javascript object that has read and write access to this agent's memory, and read access to events, options and the attributes of the agent.
|
10
|
10
|
We also provide you with a method to create events on the server.
|
11
|
11
|
You will be provided with an instance of the Agent object in javascript, with access to the above data.
|
12
|
12
|
You can create events based on your own logic.
|
|
|
@@ -24,16 +24,26 @@ module Agents
|
24
|
24
|
//var new_event = JSON.stringify({key1: "val1", key2: "val2"});
|
25
|
25
|
//create_event(pd);
|
26
|
26
|
}
|
27
|
|
- You need to provide the code for the Agent::run function. You code will override any methods already present in the Agent if it has to, and you can use other methods as well with access to the agent properties. You need to at least provide the implementation of Agent.prototype.run so that it can be called periodically, or it can execute when an event happens.
|
|
27
|
+ Agent.prototype.check = function(){
|
|
28
|
+ // Implement me
|
|
29
|
+ }
|
|
30
|
+ Agent.prototype.receive = function(){
|
|
31
|
+ // Implement me
|
|
32
|
+ }
|
|
33
|
+ You need to provide the code for the Agent::check and Agent::receive function. You code will override any methods already present in the Agent if it has to, and you can use other methods as well with access to the agent properties. You need to at least provide the implementation of Agent.prototype.check and Agent.prototype.receive so that it can be called periodically, or it can execute when an event happens.
|
28
|
34
|
|
29
|
35
|
We will yield control to your implementation in the following way:
|
30
|
36
|
|
31
|
37
|
context.eval(js_code); //this is the code that declares the class Agent, and provides a global create_event method.
|
32
|
38
|
context.eval("a = new Agent(events, options, agent)")
|
33
|
39
|
context.eval(options['code'])
|
34
|
|
- context.eval("a.run();")
|
|
40
|
+ If you agent fires periodically {
|
|
41
|
+ context.eval("a.check();")
|
|
42
|
+ } else if your agent responds when an event happens {
|
|
43
|
+ context.eval("a.receive();")
|
|
44
|
+ }
|
35
|
45
|
|
36
|
|
- You need to provide the run() implementation, as well as other methods it may need to interact with.
|
|
46
|
+ If your agent responds to events it receive, you need to implement the receive() method, and if you agent fires periodically, you need to implement check(). If your agent does both, please implement both!
|
37
|
47
|
|
38
|
48
|
MD
|
39
|
49
|
def example_js
|
|
|
@@ -54,6 +64,10 @@ module Agents
|
54
|
64
|
}
|
55
|
65
|
Agent.prototype.run = function(){
|
56
|
66
|
}
|
|
67
|
+ Agent.prototype.check = function(){
|
|
68
|
+ }
|
|
69
|
+ Agent.prototype.receive = function(){
|
|
70
|
+ }
|
57
|
71
|
H
|
58
|
72
|
end
|
59
|
73
|
|
|
|
@@ -68,7 +82,8 @@ module Agents
|
68
|
82
|
true
|
69
|
83
|
end
|
70
|
84
|
|
71
|
|
- def execute_js(incoming_events)
|
|
85
|
+ def execute_js(incoming_events, js_function = "check")
|
|
86
|
+ js_function = (js_function == "check" ? "check" : "receive")
|
72
|
87
|
context = V8::Context.new
|
73
|
88
|
context.eval(example_js)
|
74
|
89
|
context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
|
|
|
@@ -78,7 +93,8 @@ module Agents
|
78
|
93
|
a, e, o = [self.attributes.to_json, incoming_events.to_json, self.options.to_json]
|
79
|
94
|
string = "a = new Agent('#{e}','#{o}','#{a}');"
|
80
|
95
|
context.eval(string)
|
81
|
|
- context.eval("a.run();")
|
|
96
|
+ runner = "a.#{js_function}();"
|
|
97
|
+ context.eval(runner)
|
82
|
98
|
end
|
83
|
99
|
|
84
|
100
|
def check
|
|
|
@@ -90,7 +106,7 @@ module Agents
|
90
|
106
|
end
|
91
|
107
|
|
92
|
108
|
def default_options
|
93
|
|
- js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory(), events: this.events, options: this.options});create_event(pd); }"
|
|
109
|
+ js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory(), events: this.events, options: this.options});create_event(pd); };Agent.prototype.check = function(){ var pd = JSON.stringify({memory: this.memory(), events: this.events, options: this.options});create_event(pd); };Agent.prototype.receive = function(){ var pd = JSON.stringify({memory: this.memory(), events: this.events, options: this.options});create_event(pd); }"
|
94
|
110
|
{
|
95
|
111
|
"code" => js_code,
|
96
|
112
|
'expected_receive_period_in_days' => "2"
|