initial work on the code agent, almost works!

Umar M. Sheikh 11 years ago
parent
commit
746c092d51
1 changed files with 38 additions and 0 deletions
  1. 38 0
      app/models/agents/code_agent.rb

+ 38 - 0
app/models/agents/code_agent.rb

@@ -0,0 +1,38 @@
1
+require 'date'
2
+require 'cgi'
3
+module Agents
4
+  class CodeAgent < Agent
5
+    def example_js
6
+    <<-H
7
+    function Agent(m, e, o, agent){
8
+    this.memory = JSON.parse(m);
9
+    this.events = JSON.parse(e);
10
+    this.options = JSON.parse(o);
11
+    this.agent = JSON.parse(agent);
12
+    }
13
+    Agent.prototype.print_memory = function(){
14
+      return this.memory;
15
+    }
16
+    Agent.prototype.run = function(){
17
+      //have access to this.memory, this.events, this.options, and this.agent;
18
+      // do computation...
19
+      //...
20
+      var pd = JSON.stringify({hello: "doctor", dil: "chori"});
21
+      create_event(pd);
22
+    }
23
+    H
24
+    end
25
+    def execute_js
26
+      context = V8::Context.new
27
+      context.eval(example_js)
28
+      context.eval(options['code']) # should override the run function.
29
+      #
30
+      context["create_event"] = lambda {|payload| create_event :payload => {:no => "true"}}
31
+      context.eval("a = new Agent(1,1,1,1)")
32
+      context.eval("a.run();")
33
+      context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
34
+      #cxt["create_event"] = lambda {|this, payload| puts a.inspect}
35
+
36
+    end
37
+  end
38
+end