Class: Pione::RuleHandler::BasicHandler

Inherits:
Object
  • Object
show all
Includes:
TupleSpaceServerInterface
Defined in:
lib/pione/rule-handler/basic-handler.rb

Overview

BasicHandler is a base class for rule handlers.

Direct Known Subclasses

ActionHandler, FlowHandler, SystemHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TupleSpaceServerInterface

tuple_space_operation, #tuple_space_server

Constructor Details

#initialize(ts_server, rule, inputs, params, call_stack, opts = {}) ⇒ BasicHandler

Create a new handler for rule.

Parameters:

  • ts_server (TupleSpaceServer)

    tuple space server

  • rule (Rule)

    rule instance

  • inputs (Array<Data,Array<Data>>)

    input tuples

  • opts (Hash) (defaults to: {})

    optionals

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pione/rule-handler/basic-handler.rb', line 45

def initialize(ts_server, rule, inputs, params, call_stack, opts={})
  # check arguments
  raise ArgumentError.new(inputs) unless inputs.kind_of?(Array)
  raise ArgumentError.new(inputs) unless inputs.size == rule.inputs.size
  raise ArgumentError.new(params) unless params.kind_of?(Parameters)

  # set tuple space server
  set_tuple_space_server(ts_server)

  # set informations
  @rule = rule
  @inputs = inputs
  @outputs = []
  @params = @rule.params.merge(params)
  @original_params = params
  @content = rule.body
  @domain = get_handling_domain(opts)
  @variable_table = VariableTable.new(@params.data)
  @base_uri = read(Tuple[:base_uri].any).uri
  @dry_run = begin read0(Tuple[:dry_run].any).availability rescue false end
  @task_id = ID.task_id(@inputs, @params)
  @call_stack = call_stack

  setup_variable_table
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



30
31
32
# File 'lib/pione/rule-handler/basic-handler.rb', line 30

def base_uri
  @base_uri
end

#call_stackObject (readonly)

Returns the value of attribute call_stack.



34
35
36
# File 'lib/pione/rule-handler/basic-handler.rb', line 34

def call_stack
  @call_stack
end

#domainObject (readonly)

Returns the value of attribute domain.



32
33
34
# File 'lib/pione/rule-handler/basic-handler.rb', line 32

def domain
  @domain
end

#inputsObject (readonly)

Returns the value of attribute inputs.



27
28
29
# File 'lib/pione/rule-handler/basic-handler.rb', line 27

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



28
29
30
# File 'lib/pione/rule-handler/basic-handler.rb', line 28

def outputs
  @outputs
end

#paramsObject (readonly)

Returns the value of attribute params.



29
30
31
# File 'lib/pione/rule-handler/basic-handler.rb', line 29

def params
  @params
end

#ruleObject (readonly)

Returns the value of attribute rule.



26
27
28
# File 'lib/pione/rule-handler/basic-handler.rb', line 26

def rule
  @rule
end

#task_idObject (readonly)

Returns the value of attribute task_id.



31
32
33
# File 'lib/pione/rule-handler/basic-handler.rb', line 31

def task_id
  @task_id
end

#variable_tableObject (readonly)

Returns the value of attribute variable_table.



33
34
35
# File 'lib/pione/rule-handler/basic-handler.rb', line 33

def variable_table
  @variable_table
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



126
127
128
129
130
131
132
# File 'lib/pione/rule-handler/basic-handler.rb', line 126

def ==(other)
  return false unless @rule == other.rule
  return false unless @inputs == other.inputs
  return false unless @outputs == other.outputs
  return false unless @params == other.params
  return true
end

#handleArray<Data,Array<Data>>

Handles the rule and returns the outputs.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pione/rule-handler/basic-handler.rb', line 84

def handle
  name = self.class.message_name

  # show begin message
  user_message_begin("Start %s Rule: %s" % [name, handler_digest])

  # call stack
  debug_message("call stack:")
  @call_stack.each_with_index do |domain, i|
    debug_message("%s:%s" % [i, domain], 1)
  end

  # execute the rule
  outputs = execute

  # show output list
  debug_message("%s Rule %s Result:" % [name, handler_digest])

  @outputs.compact.each_with_index do |output, i|
    if output.kind_of?(Array)
      output.each_with_index do |o, ii|
        debug_message("%s,%s:%s" % [i, ii, o.name], 1)
      end
    else
      debug_message("%s:%s" % [i, output.name], 1)
    end
  end

  # show end message
  user_message_end "End %s Rule: %s" % [name, handler_digest]

  return outputs.compact
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



138
139
140
# File 'lib/pione/rule-handler/basic-handler.rb', line 138

def hash
  @rule.hash + @inputs.hash + @outputs.hash + @params.hash
end

#root?Boolean

Returns true if it is root rule handler.

Returns:

  • (Boolean)

    true if it is root rule handler



121
122
123
# File 'lib/pione/rule-handler/basic-handler.rb', line 121

def root?
  self.kind_of?(RootHandler)
end

#setenv(env) ⇒ void

This method returns an undefined value.

Puts environment variable into pione variable table.

Parameters:



75
76
77
78
79
# File 'lib/pione/rule-handler/basic-handler.rb', line 75

def setenv(env)
  env.each do |key, value|
    @variable_table.set(Variable.new("ENV_" + key), PioneString.new(value))
  end
end