From afcffeea921c4c9aa73eadd05cc8f6689003dbb7 Mon Sep 17 00:00:00 2001 From: "Umar M. Sheikh" Date: Mon, 27 Jan 2014 19:58:09 +0500 Subject: [PATCH] working implementation. now the arguments to the js code are passed correctly, the rest is the work of the js code --- app/models/agents/code_agent.rb | 50 ++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/app/models/agents/code_agent.rb b/app/models/agents/code_agent.rb index d9270f22..d1f5557e 100644 --- a/app/models/agents/code_agent.rb +++ b/app/models/agents/code_agent.rb @@ -2,6 +2,39 @@ require 'date' require 'cgi' module Agents class CodeAgent < Agent + description <<-MD + Here is an agent that gives you the ability to specify your own code. We have already provided you + a javascript object that has read access to this agent's memory, events, options and the attributes of the agent. + We also provide you with a method to create events on the server. + You will be provided with an instance of the Agent object in javascript, with access to the above data. + You can create events based on your own logic. + Specifically, you have the following class + + 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(){ + //Implement me + // Example create a new event with the following code: + //var new_event = JSON.stringify({key1: "val1", key2: "val2"}); + //create_event(pd); + } + 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. + + We will yield control to your implementation in the following way: + + context.eval("a = new Agent(memory, events, options, agent)") + context.eval("a.run();") + + You need to provide the run() implementation, as well as other methods it may need to interact with. + + MD def example_js <<-H function Agent(m, e, o, agent){ @@ -17,22 +50,25 @@ module Agents //have access to this.memory, this.events, this.options, and this.agent; // do computation... //... - var pd = JSON.stringify({hello: "doctor", dil: "chori"}); + var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options}); create_event(pd); } H end + + def working? + true + 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} - + a, m, e, o = [self.attributes.to_json, self.memory.to_json, self.events.to_json, self.options.to_json] + string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');" + context.eval(string) + context.eval("a.run();") end end end