Class: Pione::RuleEngine::FlowHandler

Inherits:
BasicHandler show all
Defined in:
lib/pione/rule-engine/flow-handler.rb

Overview

FlowHandler is a rule handler for flow elements.

Direct Known Subclasses

RootHandler

Constant Summary

Constants included from Log::MessageLog

Log::MessageLog::MESSAGE_QUEUE

Instance Attribute Summary

Attributes inherited from BasicHandler

#base_location, #caller_id, #digest, #domain_id, #domain_location, #dry_run, #env, #inputs, #outputs, #package_id, #param_set, #plain_env, #rule_condition, #rule_definition, #rule_name

Instance Method Summary collapse

Methods inherited from BasicHandler

#apply_touch_operation, #create_data_by_touch_operation, #eval_rule_condition, #find_outputs_from_space, #handle, #initialize, #make_location, #make_output_location, #make_output_tuple, #make_rule_process_record, #make_task_process_record, #publish_outputs, #setup_env, #show_outputs, #update_time_by_touch_operation, #write_data_null

Methods included from Log::MessageLog

#debug_message, #debug_message_begin, #debug_message_end, debug_mode, debug_mode=, debug_mode?, message, quiet_mode, quiet_mode=, quiet_mode?, #show, #user_message, #user_message_begin, #user_message_end

Methods included from TupleSpace::TupleSpaceInterface

#process_log, #processing_error, #set_tuple_space, tuple_space_operation, #tuple_space_server, #with_process_log

Constructor Details

This class inherits a constructor from Pione::RuleEngine::BasicHandler

Instance Method Details

#copy_data_into_domain(data, domain) ⇒ DataTuple

Copy the data tuple with the specified domain and return the tuple list.

Parameters:

  • data (DataTuple)

    target data tuple

  • domain (String)

    new domain of the copied data tuple

Returns:

  • (DataTuple)

    new data tuple with the domain or nil



102
103
104
105
106
107
# File 'lib/pione/rule-engine/flow-handler.rb', line 102

def copy_data_into_domain(data, domain)
  return nil unless data
  new_data = data.clone.tap {|x| x.domain = domain}
  write(new_data)
  return new_data
end

#executevoid

This method returns an undefined value.

Start to process flow elements.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pione/rule-engine/flow-handler.rb', line 8

def execute
  # restore data tuples from domain_location
  restore_data_tuples_from_domain_location

  # start rule application
  rule_set = @rule_definition.flow_context.eval(@env)
  RuleApplication.new(self).apply(rule_set.rules.pieces)

  # find outputs
  outputs = find_outputs_from_space

  # lift output data from child domains to this domain
  lift_output_data(outputs)

  # check output validation
  validate_outputs(outputs)

  return outputs
end

#lift_output_data(outputs) ⇒ void

This method returns an undefined value.

Lift output data from this domains to parent domain.



44
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
70
# File 'lib/pione/rule-engine/flow-handler.rb', line 44

def lift_output_data(outputs)
  # we cannot lift up if caller id is unknown
  # NOTE: this is the case that we process root rule
  return if @caller_id.nil?

  outputs.flatten.compact.inject([]) do |lifted, output|
    old_location = output.location
    new_location = make_output_location(output.name)
    unless new_location == old_location or lifted.include?(old_location)
      if old_location.exist?
        # move data from old to new
        begin
          old_location.turn(new_location)
        rescue ArgumentError
          # ignore the error because this is the case that old and new is same file
        end
        # sync cache if the old is cached in this machine
        System::FileCache.sync(old_location, new_location)
        # write lift tuple
        write(TupleSpace::LiftTuple.new(old_location, new_location))
        # push history
        lifted << old_location
      end
    end
    lifted
  end
end

#remove_data_from_domain(data, domain) ⇒ Object

Remove the data from the domain.



110
111
112
# File 'lib/pione/rule-engine/flow-handler.rb', line 110

def remove_data_from_domain(data, domain)
  take!(TupleSpace::DataTuple.new(name: data.name, domain: domain))
end

#remove_finished_tuple(domain) ⇒ void

This method returns an undefined value.

Remove finished tuple.

Parameters:

  • domain (String)

    domain of the finished tuple



90
91
92
# File 'lib/pione/rule-engine/flow-handler.rb', line 90

def remove_finished_tuple(domain)
  take!(TupleSpace::FinishedTuple.new(domain: domain))
end

#restore_data_tuples_from_domain_locationObject

Restore data tuples from the domain location. This reads files in the location and write it as data tuples.



30
31
32
33
34
35
36
37
38
39
# File 'lib/pione/rule-engine/flow-handler.rb', line 30

def restore_data_tuples_from_domain_location
  if @domain_location.exist?
    @domain_location.file_entries.each do |file|
      # ignore dot files
      unless file.basename[0] == "."
        write(TupleSpace::DataTuple.new(@domain_id, file.basename, file, file.mtime))
      end
    end
  end
end

#touch_data_in_domain(data, domain) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/pione/rule-engine/flow-handler.rb', line 114

def touch_data_in_domain(data, domain)
  if target = read!(TupleSpace::DataTuple.new(name: data.name, domain: domain))
    data = target
  end
  new_data = data.clone.tap {|x| x.domain = domain; x.time = Time.now}
  write(new_data)
end

#validate_outputs(outputs) ⇒ Object

Validate outputs.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pione/rule-engine/flow-handler.rb', line 73

def validate_outputs(outputs)
  _outputs = outputs.flatten.compact
  @rule_condition.outputs.each do |condition|
    _condition = condition.eval(@env)
    c1 = _condition.accept_nonexistence?
    c2 = _outputs.any?{|output| _condition.match?(output.name)}
    unless c1 or c2
      raise InvalidOutputError.new(self, outputs)
    end
  end
end