now, the memory behaves dynamically, still to remove testing code

Umar M. Sheikh 11 years ago
parent
commit
0315576a79
1 changed files with 16 additions and 17 deletions
  1. 16 17
      app/models/agents/code_agent.rb

+ 16 - 17
app/models/agents/code_agent.rb

@@ -29,7 +29,7 @@ module Agents
29 29
       We will yield control to your implementation in the following way:
30 30
 
31 31
           context.eval(js_code); //this is the code that declares the class Agent, and provides a global create_event method.
32
-          context.eval("a = new Agent(memory, events, options, agent)")
32
+          context.eval("a = new Agent(events, options, agent)")
33 33
           context.eval(options['code'])
34 34
           context.eval("a.run();")
35 35
 
@@ -38,22 +38,18 @@ module Agents
38 38
     MD
39 39
     def example_js
40 40
     <<-H
41
-    function Agent(m, e, o, agent){
42
-    this.memory = JSON.parse(m);
41
+    function Agent(e, o, agent){
43 42
     this.events = JSON.parse(e);
44 43
     this.options = JSON.parse(o);
45 44
     this.agent = JSON.parse(agent);
46 45
     }
47
-    Agent.prototype.print_memory = function(){
48
-      return JSON.stringify(this.memory);
49
-    }
50
-    Agent.prototype.memry = function(key,value){
46
+    Agent.prototype.memory = function(key,value){
51 47
       if (typeof(key) != "undefined" && typeof(value) != "undefined") {
52
-        this.memory = JSON.parse(access_memory(JSON.stringify(key), JSON.stringify(value)));
53
-        return JSON.stringify(this.memory);
48
+        var mem = JSON.parse(access_memory(JSON.stringify(key), JSON.stringify(value)));
49
+        return JSON.stringify(mem);
54 50
       } else {
55
-        this.memory = JSON.parse(access_memory());
56
-        return JSON.stringify(this.memory);
51
+        var mem = JSON.parse(access_memory());
52
+        return JSON.stringify(mem);
57 53
       }
58 54
     }
59 55
     Agent.prototype.run = function(){
@@ -78,20 +74,23 @@ module Agents
78 74
       context["access_memory"] = lambda {|a, x, y| x && y ? (memory[x] = y; memory.to_json) : memory.to_json }
79 75
 
80 76
       context.eval(options['code']) # should override the run function.
81
-      a, m, e, o = [self.attributes.to_json, self.memory.to_json, incoming_events.to_json, self.options.to_json]
82
-      string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
77
+      a, e, o = [self.attributes.to_json, incoming_events.to_json, self.options.to_json]
78
+      string = "a = new Agent('#{e}','#{o}','#{a}');"
83 79
       context.eval(string)
84
-      context.eval("a.memry()")    
80
+      binding.pry
81
+      context.eval("a.memory()")
85 82
     end
86 83
     def execute_js(incoming_events)
87 84
       context = V8::Context.new
88 85
       context.eval(example_js)
89 86
       context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
87
+      context["access_memory"] = lambda {|a, x, y| x && y ? (memory[x] = y; memory.to_json) : memory.to_json }
90 88
 
91 89
       context.eval(options['code']) # should override the run function.
92
-      a, m, e, o = [self.attributes.to_json, self.memory.to_json, incoming_events.to_json, self.options.to_json]
93
-      string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
90
+      a, e, o = [self.attributes.to_json, incoming_events.to_json, self.options.to_json]
91
+      string = "a = new Agent('#{e}','#{o}','#{a}');"
94 92
       context.eval(string)
93
+      context.eval("a.memory('5','6')") # set memory for testing
95 94
       context.eval("a.run();")
96 95
     end
97 96
     def check
@@ -103,7 +102,7 @@ module Agents
103 102
     end
104 103
 
105 104
     def default_options
106
-      js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});create_event(pd); }"
105
+      js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory(), events: this.events, options: this.options});create_event(pd); }"
107 106
       {
108 107
         "code" => js_code,
109 108
         'expected_receive_period_in_days' => "2"