Class: MappingManager

Inherits:
Object
  • Object
show all
Includes:
REXML, StreamListener
Defined in:
lib/MINT-core/manager/mapping_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeMappingManager

Returns a new instance of MappingManager.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/MINT-core/manager/mapping_manager.rb', line 10

def initialize()
  @mappings = {}
  @callbacks = {}
  @file_path = nil

  #callback that will be called if all mappings with attribute == run have been activated == subscribed
  @started_cb = nil

  # contains mappings that have been loaded ans started but whose observations have not finished subscribing
  @waiting_mappings = []

  #will be true if all mappings have been parsed
  @finished_parsing = false
end

Instance Method Details

#build_from_scxml(filename) ⇒ Object

This function parses scxml from a file



31
32
33
34
35
36
# File 'lib/MINT-core/manager/mapping_manager.rb', line 31

def build_from_scxml(filename)
  source = File.new filename
  Document.parse_stream(source, self)
  finished_parsing()
  @mappings
end

#build_from_scxml_string(stringbuffer) ⇒ Object

This function parses scxml directly from the string parameter “stringbuffer”



39
40
41
42
43
# File 'lib/MINT-core/manager/mapping_manager.rb', line 39

def build_from_scxml_string(stringbuffer)
  Document.parse_stream(stringbuffer, self)
  finished_parsing()
  @mappings
end

#finished_parsingObject



25
26
27
28
# File 'lib/MINT-core/manager/mapping_manager.rb', line 25

def finished_parsing
  @finished_parsing = true
  wait_for_all_mappings_activated(nil)
end

#get_mappingsObject



51
52
53
# File 'lib/MINT-core/manager/mapping_manager.rb', line 51

def get_mappings
  @mappings.keys
end

#load(xml_file) ⇒ Object



45
46
47
48
49
# File 'lib/MINT-core/manager/mapping_manager.rb', line 45

def load(xml_file)
  @file_path = File.expand_path(File.dirname(xml_file))
  @mappings = build_from_scxml(xml_file)
  self
end

#register_callback(mapping_name, callback) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/MINT-core/manager/mapping_manager.rb', line 55

def register_callback(mapping_name, callback)
  #If mapping already exists add callback
  #Otherwise save it for later
  if @mappings.has_key? (mapping_name)
    @mappings[mapping_name].state_callback = callback
  else
    @callbacks[mapping_name] = callback
  end
end

#started(&block) ⇒ Object



98
99
100
# File 'lib/MINT-core/manager/mapping_manager.rb', line 98

def started(&block)
  @started_cb = block
end

#tag_start(name, attributes) ⇒ Object

This function defines the actions to be taken for each different tag when the tag is opened



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/MINT-core/manager/mapping_manager.rb', line 66

def tag_start(name, attributes)
  case name
    when 'mim'
       #Do I have to store its name?
    when 'include'
       parser = MINT::MappingParser.new
       mapping = parser.build_from_scxml(@file_path +"/"+ attributes['href'])
       @mappings[mapping.mapping_name] = mapping

       #If a callback has already been register, add it.
       if @callbacks.has_key? mapping.mapping_name
         mapping.state_callback = @callbacks[mapping.mapping_name]
       end

       #TODO check if callback here or in class initialize
       if @mappings[mapping.mapping_name].state_callback
         p "Mapping #{mapping.mapping_name} loaded"
         @mappings[mapping.mapping_name].state_callback.call(mapping.mapping_name, {:id => mapping.id, :mapping_state => :loaded})
       end
       if attributes['start'] == "true"
         @waiting_mappings << mapping.mapping_name
         mapping.start.activated_callback {wait_for_all_mappings_activated(mapping)}

       end
  end
end

#wait_for_all_mappings_activated(mapping) ⇒ Object



93
94
95
96
# File 'lib/MINT-core/manager/mapping_manager.rb', line 93

def wait_for_all_mappings_activated(mapping)
    @waiting_mappings.delete mapping.mapping_name if mapping
    @started_cb.call if @started_cb and @finished_parsing and @waiting_mappings.length == 0
end