Class: ZenRuby::Engine
- Inherits:
-
Object
- Object
- ZenRuby::Engine
- Defined in:
- lib/zen-engine-ruby.rb
Instance Method Summary collapse
-
#create_decision(content) ⇒ Object
Create a decision from a JSON string directly; bypasses the loader.
-
#create_decision!(content) ⇒ Object
Create a decision from a JSON string directly; bypasses the loader.
- #evaluate(key, context, trace: false, max_depth: 10) ⇒ Object
- #evaluate!(key, context, trace: false, max_depth: 10) ⇒ Object
-
#get_decision(key) ⇒ Object
Fetch a decision by providing a key to the decision JSON file.
- #get_decision!(key) ⇒ Object
-
#initialize(loader: nil, custom_handler: nil) ⇒ Engine
constructor
A new instance of Engine.
Constructor Details
#initialize(loader: nil, custom_handler: nil) ⇒ Engine
Returns a new instance of Engine.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/zen-engine-ruby.rb', line 168 def initialize(loader: nil, custom_handler: nil) if loader @loader_callback = Proc.new do |key| content_json = loader.call(key) loader_result = ZenDecisionLoaderResult.new # Allocate memory that won't be managed by Ruby # (if we use MemoryPointer here, it'll cause double-free errors) loader_result[:content] = LibC.strdup(content_json) loader_result end end if custom_handler raise NotImplementedError, "Custom Node handler not implemented yet" # @custom_node_callback = Proc.new do |request_str| # request = JSON.parse(request_str) # custom_handler.call(request).tap do |a| # puts a # end # end end raw_pointer = ZenRuby.zen_engine_new_native(@loader_callback, @custom_node_callback) # Because we need to do custom cleanup (via `zen_engine_free`) rather than # just the usual `free` performed by MemoryPointer, we use AutoPointer. @engine_ptr = FFI::AutoPointer.new(raw_pointer, ZenRuby.method(:zen_engine_free)) end |
Instance Method Details
#create_decision(content) ⇒ Object
Create a decision from a JSON string directly; bypasses the loader.
228 229 230 231 232 |
# File 'lib/zen-engine-ruby.rb', line 228 def create_decision(content) # Caller is responsible for freeing: content and ZenResult. raw_result = ZenRuby.zen_engine_create_decision(@engine_ptr, content) ZenDecisionResult.from_raw_result(raw_result) end |
#create_decision!(content) ⇒ Object
Create a decision from a JSON string directly; bypasses the loader.
235 236 237 238 239 |
# File 'lib/zen-engine-ruby.rb', line 235 def create_decision!(content) result = create_decision(content) raise "Error creating decision: #{result.error_code} #{result.details}" if result.error result end |
#evaluate(key, context, trace: false, max_depth: 10) ⇒ Object
200 201 202 203 204 205 206 207 |
# File 'lib/zen-engine-ruby.rb', line 200 def evaluate(key, context, trace: false, max_depth: 10) = ZenEngineEvaluationOptions.new [:trace] = trace [:max_depth] = max_depth raw_result = ZenRuby.zen_engine_evaluate(@engine_ptr, key, context.to_json, ) ZenRuby::Result.from_raw_result(raw_result) end |
#evaluate!(key, context, trace: false, max_depth: 10) ⇒ Object
209 210 211 |
# File 'lib/zen-engine-ruby.rb', line 209 def evaluate!(key, context, trace: false, max_depth: 10) ZenRuby.unwrap_result!(evaluate(key, context, trace:, max_depth:)) end |
#get_decision(key) ⇒ Object
Fetch a decision by providing a key to the decision JSON file. Uses the loader to fetch/read the actual JSON data for the Decision graph.
215 216 217 218 219 |
# File 'lib/zen-engine-ruby.rb', line 215 def get_decision(key) # Caller is responsible for freeing: key and ZenResult. raw_result = ZenRuby.zen_engine_get_decision(@engine_ptr, key) ZenDecisionResult.from_raw_result(raw_result) end |
#get_decision!(key) ⇒ Object
221 222 223 224 225 |
# File 'lib/zen-engine-ruby.rb', line 221 def get_decision!(key) result = get_decision(key) raise "Error getting decision for #{key}: #{result.error_code} #{result.details}" if result.error result end |