From 746c092d51bb32f7f4d11a39f76dd29bea7e60c1 Mon Sep 17 00:00:00 2001 From: "Umar M. Sheikh" Date: Fri, 24 Jan 2014 18:04:31 +0500 Subject: [PATCH] initial work on the code agent, almost works! --- app/models/agents/code_agent.rb | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/models/agents/code_agent.rb diff --git a/app/models/agents/code_agent.rb b/app/models/agents/code_agent.rb new file mode 100644 index 00000000..d9270f22 --- /dev/null +++ b/app/models/agents/code_agent.rb @@ -0,0 +1,38 @@ +require 'date' +require 'cgi' +module Agents + class CodeAgent < Agent + def example_js + <<-H + function Agent(m, e, o, agent){ + this.memory = JSON.parse(m); + this.events = JSON.parse(e); + this.options = JSON.parse(o); + this.agent = JSON.parse(agent); + } + Agent.prototype.print_memory = function(){ + return this.memory; + } + Agent.prototype.run = function(){ + //have access to this.memory, this.events, this.options, and this.agent; + // do computation... + //... + var pd = JSON.stringify({hello: "doctor", dil: "chori"}); + create_event(pd); + } + H + end + def execute_js + context = V8::Context.new + context.eval(example_js) + context.eval(options['code']) # should override the run function. + # + context["create_event"] = lambda {|payload| create_event :payload => {:no => "true"}} + context.eval("a = new Agent(1,1,1,1)") + context.eval("a.run();") + context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)} + #cxt["create_event"] = lambda {|this, payload| puts a.inspect} + + end + end +end