intermediate commit to get access to memory using ruby lambda

Umar M. Sheikh 11 anos atrás
pai
commit
7d0f1da488
1 arquivos alterados com 34 adições e 5 exclusões
  1. 34 5
      app/models/agents/code_agent.rb

+ 34 - 5
app/models/agents/code_agent.rb

@@ -45,7 +45,16 @@ module Agents
45 45
     this.agent = JSON.parse(agent);
46 46
     }
47 47
     Agent.prototype.print_memory = function(){
48
-      return this.memory;
48
+      return JSON.stringify(this.memory);
49
+    }
50
+    Agent.prototype.memry = function(key,value){
51
+      if (typeof(key) != "undefined" && typeof(value) != "undefined") {
52
+        this.memory = JSON.parse(access_memory(JSON.stringify(key), JSON.stringify(value)));
53
+        return JSON.stringify(this.memory);
54
+      } else {
55
+        this.memory = JSON.parse(access_memory());
56
+        return JSON.stringify(this.memory);
57
+      }
49 58
     }
50 59
     Agent.prototype.run = function(){
51 60
     }
@@ -53,13 +62,32 @@ module Agents
53 62
     end
54 63
 
55 64
     def working?
65
+      return false if recent_error_logs?
66
+      if options['expected_update_period_in_days'].present?
67
+        return false unless event_created_within?(options['expected_update_period_in_days'])
68
+      end
69
+      if options['expected_receive_period_in_days'].present?
70
+        return false unless last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago
71
+      end
56 72
       true
57 73
     end
74
+    def setter_and_getter_memory(incoming_events = "")
75
+      context = V8::Context.new
76
+      context.eval(example_js)
77
+      context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
78
+      context["access_memory"] = lambda {|a, x, y| x && y ? (memory[x] = y; memory.to_json) : memory.to_json }
58 79
 
80
+      context.eval(options['code']) # should override the run function.
81
+      a, m, e, o = [self.attributes.to_json, self.memory.to_json, incoming_events.to_json, self.options.to_json]
82
+      string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
83
+      context.eval(string)
84
+      context.eval("a.memry()")    
85
+    end
59 86
     def execute_js(incoming_events)
60 87
       context = V8::Context.new
61 88
       context.eval(example_js)
62 89
       context["create_event"] = lambda {|x,y| puts x; puts y; create_event payload: JSON.parse(y)}
90
+
63 91
       context.eval(options['code']) # should override the run function.
64 92
       a, m, e, o = [self.attributes.to_json, self.memory.to_json, incoming_events.to_json, self.options.to_json]
65 93
       string = "a = new Agent('#{m}','#{e}','#{o}','#{a}');"
@@ -75,10 +103,11 @@ module Agents
75 103
     end
76 104
 
77 105
     def default_options
78
-    js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});create_event(pd); }"
79
-    {
80
-      code: js_code
81
-    }
106
+      js_code = "Agent.prototype.run = function(){ var pd = JSON.stringify({memory: this.memory, events: this.events, options: this.options});create_event(pd); }"
107
+      {
108
+        "code" => js_code,
109
+        'expected_receive_period_in_days' => "2"
110
+      }
82 111
     end
83 112
   end
84 113
 end