Class: RubyKpi::ReplyMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/ReplyMessage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ ReplyMessage

initialization



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ReplyMessage.rb', line 13

def initialize(message)

  # attributes needed to parse the message
  @message = message
  @content = nil

  # message attributes
  @transaction_type = nil
  @message_type = nil

  # parse the message
  @content = XML::Parser.string(@message).parse

end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/ReplyMessage.rb', line 10

def content
  @content
end

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/ReplyMessage.rb', line 10

def message
  @message
end

#message_typeObject (readonly)

Returns the value of attribute message_type.



10
11
12
# File 'lib/ReplyMessage.rb', line 10

def message_type
  @message_type
end

#transaction_typeObject (readonly)

Returns the value of attribute transaction_type.



10
11
12
# File 'lib/ReplyMessage.rb', line 10

def transaction_type
  @transaction_type
end

Instance Method Details

#get_message_typeObject

get_message_type



29
30
31
32
33
34
35
# File 'lib/ReplyMessage.rb', line 29

def get_message_type()
  
  # get the message type
  @message_type = @content.find('//message_type').first.content()
  return @message_type
  
end

#get_rdf_triplesObject

get_rdf_triples



80
81
82
83
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ReplyMessage.rb', line 80

def get_rdf_triples()

  mt = get_transaction_type()

  # It is a query?
  if  mt == "QUERY" or mt == "SUBSCRIBE"

    # Get the triple list
    triple_list = []
    pars = @content.root.find('./parameter')
    pars.each do |p|

      if p.attributes.get_attribute("name").value == "results"

        # new root
        nroot = p
        t = p.find('./triple_list').first.find('./triple')
        t.each do |tr|
          
          # get subject
          s = tr.find('./subject').first
          s_content = s.content.strip
          s_type = s.attributes.get_attribute("type").value.strip
          if s_type.downcase == "uri"
            subject = URI.new(s_content)
          else
            subject = Literal.new(s_content)
          end
          
          # get predicate
          p = tr.find('./predicate').first
          p_content = p.content.strip
          predicate = URI.new(p_content)
          
          # get object
          o = tr.find('./object').first
          o_content = o.content.strip
          o_type = o.attributes.get_attribute("type").value.strip
          if o_type.downcase == "uri"
            object = URI.new(o_content)
          else
            object = Literal.new(o_content)
          end
          
          # triple
          t = Triple.new(subject, predicate, object)
          triple_list << t
          
        end
      end 
    end
    
    return triple_list

  else
    
    # do nothing
    # TODO: raise an exception

  end
end

#get_rdf_triples_from_indicationObject

get_rdf_triples_from_indication



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ReplyMessage.rb', line 201

def get_rdf_triples_from_indication()

  # Get NEW and OLD triple list
  added = []
  removed = []
  pars = @content.root.find('./parameter')
  pars.each do |p|
    
    # Extract added triples
    if p.attributes.get_attribute("name").value == "new_results"
      
      # new root
      nroot = p
      t = p.find('./triple_list').first.find('./triple')
      t.each do |tr|

        # get subject
        s = tr.find('./subject').first
        s_content = s.content.strip
        s_type = s.attributes.get_attribute("type").value.strip               
        if s_type.downcase == "uri"
          subject = URI.new(s_content)
        else
          subject = Literal.new(s_content)
        end

        # get predicate
        p = tr.find('./predicate').first
        p_content = p.content.strip
        predicate = URI.new(p_content)
        
        # get object
        o = tr.find('./object').first
        o_content = o.content.strip
        o_type = o.attributes.get_attribute("type").value.strip
        if o_type.downcase == "uri"
          object = URI.new(o_content)
        else
          object = Literal.new(o_content)
        end
        
        # build the triple
        added_triple = Triple.new(subject, predicate, object)
        added << added_triple

      end
      
      # Extract removed triples
    elsif p.attributes.get_attribute("name").value == "obsolete_results"

      # new root
      nroot = p
      t = p.find('./triple_list').first.find('./triple')
      t.each do |tr|
        
        # get subject
        s = tr.find('./subject').first
        s_content = s.content.strip
        s_type = s.attributes.get_attribute("type").value.strip               
        if s_type.downcase == "uri"
          subject = URI.new(s_content)
        else
          subject = Literal.new(s_content)
        end
        
        # get predicate
        p = tr.find('./predicate').first
        p_content = p.content.strip
        predicate = URI.new(p_content)
        
        # get object
        o = tr.find('./object').first
        o_content = o.content.strip
        o_type = o.attributes.get_attribute("type").value.strip
        if o_type.downcase == "uri"
          object = URI.new(o_content)
        else
          object = Literal.new(o_content)
        end
        
        # build the triple
        removed_triple = Triple.new(subject, predicate, object)
        removed << removed_triple
        
      end        
    end     
  end       
  
  # return added and removed triples
  return added, removed

end

#get_sparql_resultsObject

get_sparql_results



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ReplyMessage.rb', line 143

def get_sparql_results()

  mt = get_transaction_type()
  if mt == "QUERY" or mt == "SUBSCRIBE"

    # declare an empty list
    results = []

    # Get the triple list
    pars = @content.root.find('./parameter')
    pars.each do |p|
      if p.attributes.get_attribute("name").value == "results"

        # we're on the sparql node
        p.each_element do |sparql|
          
          sparql.each_element do |hr|
            
            # head/results fields
            hr.each_element do |field|
              
              # find the results
              if field.name == "result"
                
                # We found a result
                result = []
                
                field.each_element do |n|
                  variable = []
                  variable << n.attributes.first.value
                  n.each_element do |v|
                    variable << v.name
                    variable << v.content
                  end
                  result << variable
                  results << result
                end

              end
            end        
          end
          break
        end
      end
    end
    
    # return
    return results

  else
    
    # do nothing

  end

end

#get_sparql_results_from_indicationObject

get_sparql_results_from_indication



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/ReplyMessage.rb', line 295

def get_sparql_results_from_indication()

  puts "method started"

  # declare an empty list
  added = []
  removed = []

  # Get the triple list
  pars = @content.root.find('./parameter')
  pars.each do |p|
    
    puts p.attributes.get_attribute("name").value

    if p.attributes.get_attribute("name").value == "new_results"
      
      # we're on the sparql node
      p.each_element do |sparql|
        
        sparql.each_element do |hr|
          
          # head/results fields
          hr.each_element do |field|
            
            # find the results
            if field.name == "result"
              
              # We found a result
              result = []
              
              field.each_element do |n|
                variable = []
                variable << n.attributes.first.value
                n.each_element do |v|
                  variable << v.name
                  variable << v.content
                end
                result << variable
                added << result
              end
              
            end
          end        
        end
        break
      end
      
    elsif p.attributes.get_attribute("name").value == "obsolete_results"

      # we're on the sparql node
      p.each_element do |sparql|
        
        sparql.each_element do |hr|
          
          # head/results fields
          hr.each_element do |field|
            
            # find the results
            if field.name == "result"
              
              # We found a result
              result = []
              
              field.each_element do |n|
                variable = []
                variable << n.attributes.first.value
                n.each_element do |v|
                  variable << v.name
                  variable << v.content
                end
                result << variable
                removed << result
              end
              
            end
          end        
        end
        break
      end
      
    end
  end

  return added, removed

end

#get_subscription_idObject

get_subscription_id



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ReplyMessage.rb', line 47

def get_subscription_id()

  # parse the parameters section
  subscription_id = nil
  pars = @content.root.find('./parameter')
  pars.each do |p|
    if p.attributes.get_attribute("name").value == "subscription_id"
      subscription_id = p.content
      break
    end 
  end
  
  # return the subscription_id
  return  subscription_id

end

#get_transaction_typeObject

get_transaction_type



38
39
40
41
42
43
44
# File 'lib/ReplyMessage.rb', line 38

def get_transaction_type()
  
  # get the transaction type
  @transaction_type = @content.find('//transaction_type').first.content()
  return @transaction_type

end

#success?Boolean

get_status

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ReplyMessage.rb', line 65

def success?()

  # find the new root
  pars = @content.root.find('./parameter')
  pars.each do |p|
    
    # get and return the status
    if p.attributes.get_attribute("name").value == "status"
      return p.content == "m3:Success" ? true : false
      break
    end 
  end
end