Class: RuntimeContext

Inherits:
AbstractRuntimeContext show all
Defined in:
lib/javonet-ruby-sdk/sdk/runtime_context.rb

Overview

Represents a single context which allows interaction with a selected technology. Refers to a single instance of the called runtime within a particular target OS process. This can be either the local currently running process (inMemory) or a particular remote process identified by the IP Address and PORT of the target Javonet instance. Multiple Runtime Contexts can be initialized within one process. Calling the same technology on inMemory communication channel will return the existing instance of runtime context. Calling the same technology on TCP channel but on different nodes will result in unique Runtime Contexts. Within the runtime context, any number of libraries can be loaded and any objects from the target technology can be interacted with, as they are aware of each other due to sharing the same memory space and same runtime instance.

See Also:

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

Constant Summary collapse

@@memory_runtime_contexts =
Hash.new
@@network_runtime_contexts =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime_name, connection_type, tcp_connection_data) ⇒ RuntimeContext

Returns a new instance of RuntimeContext.



25
26
27
28
29
30
31
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 25

def initialize(runtime_name, connection_type, tcp_connection_data)
  @runtime_name = runtime_name
  @connection_type = connection_type
  @tcp_ip_address = tcp_connection_data
  @current_command = nil
  @interpreter = Interpreter.new
end

Instance Attribute Details

#current_commandObject

accessor get method



20
21
22
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 20

def current_command
  @current_command
end

Class Method Details

.get_instance(runtime_name, connection_type, tcp_connection_data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 33

def self.get_instance(runtime_name, connection_type, tcp_connection_data)
  case connection_type
  when ConnectionType::IN_MEMORY
    if @@memory_runtime_contexts.has_key?(runtime_name)
      runtime_ctx = @@memory_runtime_contexts[runtime_name]
      runtime_ctx.current_command = nil
      return runtime_ctx
    else
      runtime_ctx = RuntimeContext.new(runtime_name, connection_type, tcp_connection_data)
      @@memory_runtime_contexts[runtime_name] = runtime_ctx
      return runtime_ctx
    end
  when ConnectionType::TCP
    key = runtime_name.to_s + ":" + tcp_connection_data.to_s
    if @@network_runtime_contexts.has_key?(key)
      runtime_ctx = @@network_runtime_contexts[key]
      runtime_ctx.current_command = nil
      return runtime_ctx
    else
      runtime_ctx = RuntimeContext.new(runtime_name, connection_type, tcp_connection_data)
      @@network_runtime_contexts[key] = runtime_ctx
      return runtime_ctx
    end
  end
end

Instance Method Details

#as_out(*args) ⇒ InvocationContext

Creates a reference type argument that can be passed to a method with an out parameter modifier. This method is used when working with methods from the called runtime that require arguments to be passed by reference. The arguments include the value and optionally the type of the reference. The type must be retrieved from the called runtime using the getType method. After creating the reference, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The value and optionally the type of the reference.

Returns:

See Also:

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


143
144
145
146
147
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 143

def as_out(*args)
  local_command = Command.new(@runtime_name, CommandType::AS_OUT, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#as_ref(*args) ⇒ InvocationContext

Creates a reference type argument that can be passed to a method with a ref parameter modifier. This method is used when working with methods from the called runtime that require arguments to be passed by reference. The arguments include the value and optionally the type of the reference. The type must be retrieved from the called runtime using the getType method. After creating the reference, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The value and optionally the type of the reference.

Returns:

See Also:

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


129
130
131
132
133
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 129

def as_ref(*args)
  local_command = Command.new(@runtime_name, CommandType::AS_REF, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#build_command(command) ⇒ Object



149
150
151
152
153
154
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 149

def build_command(command)
  command.payload.each_index { |i|
    command.payload[i] = encapsulate_payload_item(command.payload[i])
  }
  return command.prepend_arg_to_payload(@current_command)
end

#cast(*args) ⇒ InvocationContext

Casts the provided value to a specific type. This method is used when invoking methods that require specific types of arguments. The arguments include the target type and the value to be cast. The target type must be retrieved from the called runtime using the getType method. After casting the value, it can be used as an argument when invoking methods. #see Refer to this article on Javonet Guides

Parameters:

  • args (Array)

    The target type and the value to be cast.

Returns:



102
103
104
105
106
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 102

def cast(*args)
  local_command = Command.new(@runtime_name, CommandType::CAST, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#encapsulate_payload_item(payload_item) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 156

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])
  end
end

#executeObject

Executes the current command. The initial state of RuntimeContext is non-materialized, wrapping either a single command or a chain of recursively nested commands. Commands become nested through each invocation of methods on RuntimeContext. Each invocation triggers the creation of a new RuntimeContext instance wrapping the current command with a new parent command. The developer can decide at any moment of the materialization for the context, taking full control of the chunks of the expression being transferred and processed on the target runtime.

See Also:

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


65
66
67
68
69
70
71
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 65

def execute
  @response_command = @interpreter.execute(@current_command, @connection_type, @tcp_ip_address)
  @current_command = nil
  if @response_command.command_type == CommandType::EXCEPTION
    raise ExceptionThrower.throw_exception(@response_command)
  end
end

#get_enum_item(*args) ⇒ InvocationContext

Retrieves a specific item from an enum type. This method is used when working with enums from the called runtime. The arguments include the enum type and the name of the item. The enum type must be retrieved from the called runtime using the getType method. After retrieving the item, it can be used as an argument when invoking methods.

Parameters:

  • args (Array)

    The enum type and the name of the item.

Returns:

See Also:

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


115
116
117
118
119
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 115

def get_enum_item(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_ENUM_ITEM, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#get_type(*args) ⇒ InvocationContext

Retrieves a reference to a specific type. The type can be a class, interface or enum. The type can be retrieved from any referenced library.

Parameters:

  • args (Array)

    The full name of the type.

Returns:



90
91
92
93
94
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 90

def get_type(*args)
  local_command = Command.new(@runtime_name, CommandType::GET_TYPE, [*args])
  @current_command = nil
  return InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), false)
end

#health_checkObject



177
178
179
180
181
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 177

def health_check()
  local_command = Command.new(@runtime_name, CommandType::VALUE, ["health_check"])
  @current_command = build_command(local_command)
  execute
end

#load_library(*args) ⇒ RuntimeContext

Adds a reference to a library. Javonet allows you to reference and use modules or packages written in various languages. This method allows you to use any library from all supported technologies. The necessary libraries need to be referenced. The argument is a relative or full path to the library. If the library has dependencies on other libraries, the latter needs to be added first. After referencing the library, any objects stored in this package can be used. Use static classes, create instances, call methods, use fields and properties, and much more.

Parameters:

  • args (Array)

    The relative or full path to the library.

Returns:

See Also:

  • to this {https://www.javonet.com/guides/v2/ruby/getting-started/adding-references-to-libraries article on Javonet Guides}


80
81
82
83
84
85
# File 'lib/javonet-ruby-sdk/sdk/runtime_context.rb', line 80

def load_library(*args)
  local_command = Command.new(@runtime_name, CommandType::LOAD_LIBRARY, [*args])
  @current_command = build_command(local_command)
  self.execute()
  return self
end