Class: InvocationContext
- Inherits:
-
AbstractInvocationContext
- Object
- AbstractInvocationContext
- InvocationContext
- Includes:
- Enumerable
- Defined in:
- lib/javonet-ruby-sdk/sdk/invocation_context.rb
Overview
InvocationContext is a class that represents a context for invoking commands. It implements several interfaces for different types of interactions. This class is used to construct chains of invocations, representing expressions of interaction that have not yet been executed.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #[]=(i, value) ⇒ Object
- #build_command(command) ⇒ Object
-
#create_instance(*args) ⇒ InvocationContext
Creates a new instance of a class in the target runtime.
-
#create_null ⇒ InvocationContext
Creates a null object of a specific type in the target runtime.
- #current_command ⇒ Object
- #encapsulate_payload_item(payload_item) ⇒ Object
-
#execute ⇒ InvocationContext
Executes the current command.
- #execute_async ⇒ Object
-
#get_enum_name ⇒ InvocationContext
Retrieves the name of an enum from the target runtime.
-
#get_enum_value ⇒ InvocationContext
Retrieves the value of an enum from the target runtime.
-
#get_index(*indexes) ⇒ InvocationContext
Retrieves the value at a specific indexes in an array from the target runtime.
-
#get_instance_field(field_name) ⇒ InvocationContext
Retrieves the value of an instance field from the target runtime.
-
#get_instance_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves an instance method as a delegate from the target runtime.
-
#get_rank ⇒ InvocationContext
Retrieves the rank of an array from the target runtime.
-
#get_ref_value ⇒ InvocationContext
Retrieves the value of a reference from the target runtime.
-
#get_size ⇒ InvocationContext
Retrieves the number of elements from the target runtime.
-
#get_static_field(field_name) ⇒ InvocationContext
Retrieves the value of a static field from the target runtime.
-
#get_static_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves a static method as a delegate from the target runtime.
-
#get_value ⇒ Object
Retrieves the value of the current command from the target runtime.
-
#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false) ⇒ InvocationContext
constructor
A new instance of InvocationContext.
-
#invoke_generic_method(method_name, *args) ⇒ InvocationContext
Invokes a generic method on the target runtime.
-
#invoke_generic_static_method(method_name, *args) ⇒ InvocationContext
Invokes a generic static method on the target runtime.
-
#invoke_instance_method(method_name, *args) ⇒ InvocationContext
Invokes an instance method on the target runtime.
-
#invoke_static_method(method_name, *args) ⇒ InvocationContext
Invokes a static method on the target runtime.
-
#iterator ⇒ Object
def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address) proc do if command.command_type == CommandType::REFERENCE && is_executed == true destruct_command = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload) interpreter.execute(destruct_command, connection_type, tcp_ip_address) end end end.
- #response_command ⇒ Object
-
#retrieve_array ⇒ Object
Retrieves an array from the target runtime.
-
#set_index(indexes, value) ⇒ InvocationContext
Sets the value at a specific index in an array in the target runtime.
-
#set_instance_field(field_name, value) ⇒ InvocationContext
Sets the value of an instance field in the target runtime.
-
#set_static_field(field_name, value) ⇒ InvocationContext
Sets the value of a static field in the target runtime.
Methods inherited from AbstractInvocationContext
Constructor Details
#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false) ⇒ InvocationContext
Returns a new instance of InvocationContext.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 13 def initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false) @is_executed = is_executed @runtime_name = runtime_name @connection_type = connection_type @tcp_ip_address = tcp_ip_address @current_command = command @response_command = nil # generates error: ruby/lib/javonet.rb-ruby-sdk/sdk/internal/invocation_context.rb:17: warning: finalizer references object to be finalized # ObjectSpace.define_finalizer(self, self.finalize(@current_command, @is_executed, @interpreter, @connection_type, @tcp_ip_address)) end |
Instance Method Details
#[](i) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 41 def [](i) if not [CommandType::REFERENCE, CommandType::ARRAY_GET_ITEM].include? @current_command.command_type raise "Object is not iterable" else invocation_context_iterator = InvocationContextIterator.new(self) invocation_context_iterator[i] end end |
#[]=(i, value) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 50 def []=(i, value) if not [CommandType::REFERENCE, CommandType::ARRAY_SET_ITEM, CommandType::ARRAY_GET_ITEM].include? @current_command.command_type raise "Object is not iterable" else invocation_context_iterator = InvocationContextIterator.new(self) invocation_context_iterator[i] = value end end |
#build_command(command) ⇒ Object
283 284 285 286 287 288 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 283 def build_command(command) command.payload.each_index { |i| command.payload[i] = encapsulate_payload_item(command.payload[i]) } command.prepend_arg_to_payload(@current_command) end |
#create_instance(*args) ⇒ InvocationContext
Creates a new instance of a class in the target runtime.
116 117 118 119 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 116 def create_instance(*args) local_command = Command.new(@runtime_name, CommandType::CREATE_CLASS_INSTANCE, [*args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#create_null ⇒ InvocationContext
Creates a null object of a specific type in the target runtime.
241 242 243 244 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 241 def create_null local_command = Command.new(@runtime_name, CommandType::CREATE_NULL, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#current_command ⇒ Object
310 311 312 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 310 def current_command @current_command end |
#encapsulate_payload_item(payload_item) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 290 def encapsulate_payload_item(payload_item) if payload_item.is_a? Command payload_item.payload.each_index { |i| payload_item.payload[i] = encapsulate_payload_item(payload_item.payload[i]) } return payload_item elsif payload_item.is_a? InvocationContext return payload_item.current_command elsif payload_item.is_a? Array copied_array = payload_item.map { |item| encapsulate_payload_item(item) } return Command.new(@runtime_name, CommandType::ARRAY, copied_array) elsif TypesHandler.primitive_or_none?(payload_item) return Command.new(@runtime_name, CommandType::VALUE, payload_item.nil? ? [nil] : [*payload_item]) else raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}." end end |
#execute ⇒ InvocationContext
Executes the current command. Because invocation context is building the intent of executing particular expression on target environment, we call the initial state of invocation context as non-materialized. The non-materialized context wraps either single command or chain of recursively nested commands. Commands are becoming nested through each invocation of methods on Invocation Context. Each invocation triggers the creation of new Invocation Context instance wrapping the current command with new parent command valid for invoked method. Developer can decide on any moment of the materialization for the context taking full control of the chunks of the expression being transferred and processed on target runtime.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 67 def execute @response_command = Interpreter.execute(@current_command, @connection_type, @tcp_ip_address) if @response_command.command_type == CommandType::EXCEPTION exception = ExceptionThrower.throw_exception(@response_command) SdkMessageHelper.("SdkException", exception.) raise exception end if @response_command.command_type == CommandType::CREATE_CLASS_INSTANCE @current_command = @response_command @is_executed = true return self end InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, @response_command, true) end |
#execute_async ⇒ Object
85 86 87 88 89 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 85 def execute_async Thread.new do self.execute end end |
#get_enum_name ⇒ InvocationContext
Retrieves the name of an enum from the target runtime.
217 218 219 220 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 217 def get_enum_name local_command = Command.new(@runtime_name, CommandType::GET_ENUM_NAME, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_enum_value ⇒ InvocationContext
Retrieves the value of an enum from the target runtime.
225 226 227 228 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 225 def get_enum_value local_command = Command.new(@runtime_name, CommandType::GET_ENUM_VALUE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_index(*indexes) ⇒ InvocationContext
Retrieves the value at a specific indexes in an array from the target runtime.
163 164 165 166 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 163 def get_index(*indexes) local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_ITEM, [*indexes]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_instance_field(field_name) ⇒ InvocationContext
Retrieves the value of an instance field from the target runtime.
144 145 146 147 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 144 def get_instance_field(field_name) local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_FIELD, [field_name]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_instance_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves an instance method as a delegate from the target runtime. # @see Refer to this article on Javonet Guides
261 262 263 264 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 261 def get_instance_method_as_delegate(method_name, *args) local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_METHOD_AS_DELEGATE, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_rank ⇒ InvocationContext
Retrieves the rank of an array from the target runtime.
189 190 191 192 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 189 def get_rank local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_RANK, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_ref_value ⇒ InvocationContext
Retrieves the value of a reference from the target runtime.
233 234 235 236 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 233 def get_ref_value local_command = Command.new(@runtime_name, CommandType::GET_REF_VALUE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_size ⇒ InvocationContext
Retrieves the number of elements from the target runtime.
181 182 183 184 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 181 def get_size local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_SIZE, []) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_static_field(field_name) ⇒ InvocationContext
Retrieves the value of a static field from the target runtime.
125 126 127 128 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 125 def get_static_field(field_name) local_command = Command.new(@runtime_name, CommandType::GET_STATIC_FIELD, [field_name]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_static_method_as_delegate(method_name, *args) ⇒ InvocationContext
Retrieves a static method as a delegate from the target runtime.
251 252 253 254 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 251 def get_static_method_as_delegate(method_name, *args) local_command = Command.new(@runtime_name, CommandType::GET_STATIC_METHOD_AS_DELEGATE, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#get_value ⇒ Object
Retrieves the value of the current command from the target runtime.
279 280 281 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 279 def get_value @current_command.payload[0] end |
#invoke_generic_method(method_name, *args) ⇒ InvocationContext
Invokes a generic method on the target runtime.
209 210 211 212 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 209 def invoke_generic_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_generic_static_method(method_name, *args) ⇒ InvocationContext
Invokes a generic static method on the target runtime.
199 200 201 202 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 199 def invoke_generic_static_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_STATIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_instance_method(method_name, *args) ⇒ InvocationContext
Invokes an instance method on the target runtime.
107 108 109 110 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 107 def invoke_instance_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_INSTANCE_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#invoke_static_method(method_name, *args) ⇒ InvocationContext
Invokes a static method on the target runtime.
96 97 98 99 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 96 def invoke_static_method(method_name, *args) local_command = Command.new(@runtime_name, CommandType::INVOKE_STATIC_METHOD, [method_name, *args]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#iterator ⇒ Object
def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address)
proc do
if command.command_type == CommandType::REFERENCE && is_executed == true
destruct_command = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload)
interpreter.execute(destruct_command, connection_type, tcp_ip_address)
end
end
end
33 34 35 36 37 38 39 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 33 def iterator if not [CommandType::REFERENCE, CommandType::ARRAY_GET_ITEM, CommandType::ARRAY_SET_ITEM].include? @current_command.command_type raise "Object is not iterable" else InvocationContextIterator.new(self) end end |
#response_command ⇒ Object
314 315 316 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 314 def response_command @response_command end |
#retrieve_array ⇒ Object
Retrieves an array from the target runtime.
269 270 271 272 273 274 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 269 def retrieve_array local_command = Command.new(@runtime_name, CommandType::RETRIEVE_ARRAY, []) local_inv_ctx = InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) local_inv_ctx.execute local_inv_ctx.response_command.payload end |
#set_index(indexes, value) ⇒ InvocationContext
Sets the value at a specific index in an array in the target runtime.
173 174 175 176 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 173 def set_index(indexes, value) local_command = Command.new(@runtime_name, CommandType::ARRAY_SET_ITEM, [indexes, value]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#set_instance_field(field_name, value) ⇒ InvocationContext
Sets the value of an instance field in the target runtime.
154 155 156 157 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 154 def set_instance_field(field_name, value) local_command = Command.new(@runtime_name, CommandType::SET_INSTANCE_FIELD, [field_name, value]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |
#set_static_field(field_name, value) ⇒ InvocationContext
Sets the value of a static field in the target runtime.
135 136 137 138 |
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 135 def set_static_field(field_name, value) local_command = Command.new(@runtime_name, CommandType::SET_STATIC_FIELD, [field_name, value]) InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command)) end |