code_agent.rb 4.0KB

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