Нет описания http://j1x-huginn.herokuapp.com

code_agent.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'date'
  2. require 'cgi'
  3. module Agents
  4. class CodeAgent < Agent
  5. description <<-MD
  6. Here is an agent that gives you the ability to specify your own code. We have already provided you
  7. a javascript object that has read access to this agent's memory, events, options and the attributes of the agent.
  8. We also provide you with a method to create events on the server.
  9. You will be provided with an instance of the Agent object in javascript, with access to the above data.
  10. You can create events based on your own logic.
  11. Specifically, you have the following class
  12. function Agent(m, e, o, agent){
  13. this.memory = JSON.parse(m);
  14. this.events = JSON.parse(e);
  15. this.options = JSON.parse(o);
  16. this.agent = JSON.parse(agent);
  17. }
  18. Agent.prototype.print_memory = function(){
  19. return this.memory;
  20. }
  21. Agent.prototype.run = function(){
  22. //Implement me
  23. // Example create a new event with the following code:
  24. //var new_event = JSON.stringify({key1: "val1", key2: "val2"});
  25. //create_event(pd);
  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.
  28. We will yield control to your implementation in the following way:
  29. context.eval("a = new Agent(memory, events, options, agent)")
  30. context.eval("a.run();")
  31. You need to provide the run() implementation, as well as other methods it may need to interact with.
  32. MD
  33. def example_js
  34. <<-H
  35. function Agent(m, e, o, agent){
  36. this.memory = JSON.parse(m);
  37. this.events = JSON.parse(e);
  38. this.options = JSON.parse(o);
  39. this.agent = JSON.parse(agent);
  40. }
  41. Agent.prototype.print_memory = function(){
  42. return this.memory;
  43. }
  44. Agent.prototype.run = function(){
  45. //have access to this.memory, this.events, this.options, and this.agent;
  46. // do computation...
  47. //...
  48. var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});
  49. create_event(pd);
  50. }
  51. H
  52. end
  53. def working?
  54. true
  55. end
  56. def execute_js
  57. context = V8::Context.new
  58. context.eval(example_js)
  59. context.eval(options['code']) # should override the run function.
  60. context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
  61. a, m, e, o = [self.attributes.to_json, self.memory.to_json, self.events.to_json, self.options.to_json]
  62. string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
  63. context.eval(string)
  64. context.eval("a.run();")
  65. end
  66. end
  67. end