Class: InvocationContext

Inherits:
AbstractInvocationContext show all
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.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/foundations/invocation-context article on Javonet Guides}

Instance Method Summary collapse

Methods inherited from AbstractInvocationContext

#get_enum_item

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
23
# 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
  @interpreter = Interpreter.new
  # 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



42
43
44
45
46
47
48
49
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 42

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



51
52
53
54
55
56
57
58
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 51

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



284
285
286
287
288
289
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 284

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.

Parameters:

  • args (Array)

    The arguments to class constructor

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to create the instance.

See Also:



117
118
119
120
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 117

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_nullInvocationContext

Creates a null object of a specific type in the target runtime.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to create a null object.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/null-handling/create-null-object article on Javonet Guides}


242
243
244
245
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 242

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_commandObject



311
312
313
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 311

def current_command
  @current_command
end

#encapsulate_payload_item(payload_item) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 291

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
    payload_item.each_index { |i|
      payload_item[i] = encapsulate_payload_item(payload_item[i])
    }
    return Command.new(@runtime_name, CommandType::ARRAY, [*payload_item])
  else
    return Command.new(@runtime_name, CommandType::VALUE, payload_item.nil? ? [nil] : [*payload_item])
  end
end

#executeInvocationContext

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.

Returns:

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/foundations/execute-method article on Javonet Guides}


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 68

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.send_message_to_app_insights("SdkException", exception.message)
    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_asyncObject



86
87
88
89
90
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 86

def execute_async
  Thread.new do
    self.execute
  end
end

#get_enum_nameInvocationContext

Retrieves the name of an enum from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the enum name.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/enums/using-enum-type article on Javonet Guides}


218
219
220
221
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 218

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_valueInvocationContext

Retrieves the value of an enum from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the enum value.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/enums/using-enum-type article on Javonet Guides}


226
227
228
229
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 226

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.

Parameters:

  • indexes (Array)

    The indexes to pass to the get index command.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the index.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Javonet Guides}


164
165
166
167
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 164

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.

Parameters:

  • field_name (String)

    The name of the instance field to get.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the instance field.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-instance-fields-and-properties article on Javonet Guides}


145
146
147
148
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 145

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

Parameters:

  • method_name (String)

    The name of the instance method to retrieve as a delegate.

  • args (Array)

    The arguments to pass to the instance method.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to get the instance method as a delegate.



262
263
264
265
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 262

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_rankInvocationContext

Retrieves the rank of an array from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the rank.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/arrays-and-collections/multidimensional-arrays article on Javonet Guides}


190
191
192
193
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 190

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_valueInvocationContext

Retrieves the value of a reference from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the ref value.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/methods-arguments/passing-arguments-by-reference-with-ref-keyword article on Javonet Guides}


234
235
236
237
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 234

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_sizeInvocationContext

Retrieves the number of elements from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the size.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Javonet Guides}


182
183
184
185
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 182

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.

Parameters:

  • field_name (String)

    The name of the static field to get.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the static field.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-static-fields-and-properties article on Javonet Guides}


126
127
128
129
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 126

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.

Parameters:

  • method_name (String)

    The name of the static method to retrieve as a delegate.

  • args (Array)

    The arguments to pass to the static method.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to get the static method as a delegate.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/delegates-and-events/using-delegates article on Javonet Guides}


252
253
254
255
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 252

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_valueObject

Retrieves the value of the current command from the target runtime.

Returns:

  • (Object)

    The value of the current command.

See Also:



280
281
282
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 280

def get_value
  @current_command.payload[0]
end

#invoke_generic_method(method_name, *args) ⇒ InvocationContext

Invokes a generic method on the target runtime.

Parameters:

  • method_name (String)

    The name of the generic method to invoke.

  • args (Array)

    The arguments to pass to the generic method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the generic method.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/generics/calling-generic-instance-method article on Javonet Guides}


210
211
212
213
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 210

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.

Parameters:

  • method_name (String)

    The name of the generic static method to invoke.

  • args (Array)

    The arguments to pass to the generic static method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the generic static method.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/generics/calling-generic-static-method article on Javonet Guides}


200
201
202
203
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 200

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.

Parameters:

  • method_name (String)

    The name of the instance method to invoke.

  • args (Array)

    The arguments to pass to the instance method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the instance method.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/calling-methods/creating-instance-and-calling-instance-methods article on Javonet Guides}


108
109
110
111
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 108

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.

Parameters:

  • method_name (String)

    The name of the static method to invoke.

  • args (Array)

    The arguments to pass to the static method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the static method.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/calling-methods/invoking-static-method article on Javonet Guides}


97
98
99
100
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 97

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

#iteratorObject

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


34
35
36
37
38
39
40
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 34

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_commandObject



315
316
317
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 315

def response_command
  @response_command
end

#retrieve_arrayObject

Retrieves an array from the target runtime.

Returns:

  • (Object)

    A new InvocationContext instance that wraps the command to retrieve the array.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Javonet Guides}


270
271
272
273
274
275
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 270

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.

Parameters:

  • indexes (Array)

    The indexes where new value should be set

  • value (Object)

    The new value to set at the indexes.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the index.

See Also:



174
175
176
177
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 174

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.

Parameters:

  • field_name (String)

    The name of the instance field to set.

  • value (Object)

    The new value of the instance field.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the instance field.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-instance-fields-and-properties article on Javonet Guides}


155
156
157
158
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 155

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.

Parameters:

  • field_name (String)

    The name of the static field to set.

  • value (Object)

    The new value of the static field.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the static field.

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-static-fields-and-properties article on Javonet Guides}


136
137
138
139
# File 'lib/javonet-ruby-sdk/sdk/invocation_context.rb', line 136

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