ruby_kpi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_kpi.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Fabio Viola
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # RubyKpi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_kpi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby_kpi
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/ruby_kpi/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,90 @@
1
+ module RubyKpi
2
+
3
+ ##################################################
4
+ #
5
+ # The URI class
6
+ #
7
+ ##################################################
8
+
9
+ class URI
10
+
11
+ # accessor
12
+ attr_reader :value
13
+
14
+ # constructor
15
+ def initialize(value)
16
+ if value
17
+ @value = value
18
+ else
19
+ @value = "http://www.nokia.com/NRC/M3/sib#any"
20
+ end
21
+ end
22
+
23
+ # to string
24
+ def to_str()
25
+ return "<" + @value + ">"
26
+ end
27
+
28
+ end
29
+
30
+
31
+ ##################################################
32
+ #
33
+ # The Literal class
34
+ #
35
+ ##################################################
36
+
37
+ class Literal
38
+
39
+ # accessor
40
+ attr_reader :value
41
+
42
+ # constructor
43
+ def initialize(value)
44
+ @value = value
45
+ end
46
+
47
+ # to string
48
+ def to_str()
49
+ return @value
50
+ end
51
+
52
+ end
53
+
54
+
55
+ ##################################################
56
+ #
57
+ # The Triple class
58
+ #
59
+ ##################################################
60
+
61
+ class Triple
62
+
63
+ # accessor
64
+ attr_reader :subject, :predicate, :object
65
+
66
+ # constructor
67
+ def initialize(subject, predicate, object)
68
+
69
+ # subject
70
+ @subject = subject
71
+
72
+ # predicate
73
+ @predicate = predicate
74
+
75
+ # object
76
+ @object = object
77
+
78
+ end
79
+
80
+ # to string
81
+ def to_str()
82
+
83
+ return "[" + @subject.to_str() + ", " + @predicate.to_str() + ", " + @object.to_str() + "]"
84
+
85
+ end
86
+
87
+ end
88
+
89
+
90
+ end
@@ -0,0 +1,384 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # requirements
4
+ require "xml"
5
+
6
+ module RubyKpi
7
+
8
+ class ReplyMessage
9
+
10
+ attr_reader :content, :message, :message_type, :transaction_type
11
+
12
+ # initialization
13
+ def initialize(message)
14
+
15
+ # attributes needed to parse the message
16
+ @message = message
17
+ @content = nil
18
+
19
+ # message attributes
20
+ @transaction_type = nil
21
+ @message_type = nil
22
+
23
+ # parse the message
24
+ @content = XML::Parser.string(@message).parse
25
+
26
+ end
27
+
28
+ # get_message_type
29
+ def get_message_type()
30
+
31
+ # get the message type
32
+ @message_type = @content.find('//message_type').first.content()
33
+ return @message_type
34
+
35
+ end
36
+
37
+ # get_transaction_type
38
+ def get_transaction_type()
39
+
40
+ # get the transaction type
41
+ @transaction_type = @content.find('//transaction_type').first.content()
42
+ return @transaction_type
43
+
44
+ end
45
+
46
+ # get_subscription_id
47
+ def get_subscription_id()
48
+
49
+ # parse the parameters section
50
+ subscription_id = nil
51
+ pars = @content.root.find('./parameter')
52
+ pars.each do |p|
53
+ if p.attributes.get_attribute("name").value == "subscription_id"
54
+ subscription_id = p.content
55
+ break
56
+ end
57
+ end
58
+
59
+ # return the subscription_id
60
+ return subscription_id
61
+
62
+ end
63
+
64
+ # get_status
65
+ def success?()
66
+
67
+ # find the new root
68
+ pars = @content.root.find('./parameter')
69
+ pars.each do |p|
70
+
71
+ # get and return the status
72
+ if p.attributes.get_attribute("name").value == "status"
73
+ return p.content == "m3:Success" ? true : false
74
+ break
75
+ end
76
+ end
77
+ end
78
+
79
+ # get_rdf_triples
80
+ def get_rdf_triples()
81
+
82
+ mt = get_transaction_type()
83
+
84
+ # It is a query?
85
+ if mt == "QUERY" or mt == "SUBSCRIBE"
86
+
87
+ # Get the triple list
88
+ triple_list = []
89
+ pars = @content.root.find('./parameter')
90
+ pars.each do |p|
91
+
92
+ if p.attributes.get_attribute("name").value == "results"
93
+
94
+ # new root
95
+ nroot = p
96
+ t = p.find('./triple_list').first.find('./triple')
97
+ t.each do |tr|
98
+
99
+ # get subject
100
+ s = tr.find('./subject').first
101
+ s_content = s.content.strip
102
+ s_type = s.attributes.get_attribute("type").value.strip
103
+ if s_type.downcase == "uri"
104
+ subject = URI.new(s_content)
105
+ else
106
+ subject = Literal.new(s_content)
107
+ end
108
+
109
+ # get predicate
110
+ p = tr.find('./predicate').first
111
+ p_content = p.content.strip
112
+ predicate = URI.new(p_content)
113
+
114
+ # get object
115
+ o = tr.find('./object').first
116
+ o_content = o.content.strip
117
+ o_type = o.attributes.get_attribute("type").value.strip
118
+ if o_type.downcase == "uri"
119
+ object = URI.new(o_content)
120
+ else
121
+ object = Literal.new(o_content)
122
+ end
123
+
124
+ # triple
125
+ t = Triple.new(subject, predicate, object)
126
+ triple_list << t
127
+
128
+ end
129
+ end
130
+ end
131
+
132
+ return triple_list
133
+
134
+ else
135
+
136
+ # do nothing
137
+ # TODO: raise an exception
138
+
139
+ end
140
+ end
141
+
142
+ # get_sparql_results
143
+ def get_sparql_results()
144
+
145
+ mt = get_transaction_type()
146
+ if mt == "QUERY" or mt == "SUBSCRIBE"
147
+
148
+ # declare an empty list
149
+ results = []
150
+
151
+ # Get the triple list
152
+ pars = @content.root.find('./parameter')
153
+ pars.each do |p|
154
+ if p.attributes.get_attribute("name").value == "results"
155
+
156
+ # we're on the sparql node
157
+ p.each_element do |sparql|
158
+
159
+ sparql.each_element do |hr|
160
+
161
+ # head/results fields
162
+ hr.each_element do |field|
163
+
164
+ # find the results
165
+ if field.name == "result"
166
+
167
+ # We found a result
168
+ result = []
169
+
170
+ field.each_element do |n|
171
+ variable = []
172
+ variable << n.attributes.first.value
173
+ n.each_element do |v|
174
+ variable << v.name
175
+ variable << v.content
176
+ end
177
+ result << variable
178
+ results << result
179
+ end
180
+
181
+ end
182
+ end
183
+ end
184
+ break
185
+ end
186
+ end
187
+ end
188
+
189
+ # return
190
+ return results
191
+
192
+ else
193
+
194
+ # do nothing
195
+
196
+ end
197
+
198
+ end
199
+
200
+ # get_rdf_triples_from_indication
201
+ def get_rdf_triples_from_indication()
202
+
203
+ # Get NEW and OLD triple list
204
+ added = []
205
+ removed = []
206
+ pars = @content.root.find('./parameter')
207
+ pars.each do |p|
208
+
209
+ # Extract added triples
210
+ if p.attributes.get_attribute("name").value == "new_results"
211
+
212
+ # new root
213
+ nroot = p
214
+ t = p.find('./triple_list').first.find('./triple')
215
+ t.each do |tr|
216
+
217
+ # get subject
218
+ s = tr.find('./subject').first
219
+ s_content = s.content.strip
220
+ s_type = s.attributes.get_attribute("type").value.strip
221
+ if s_type.downcase == "uri"
222
+ subject = URI.new(s_content)
223
+ else
224
+ subject = Literal.new(s_content)
225
+ end
226
+
227
+ # get predicate
228
+ p = tr.find('./predicate').first
229
+ p_content = p.content.strip
230
+ predicate = URI.new(p_content)
231
+
232
+ # get object
233
+ o = tr.find('./object').first
234
+ o_content = o.content.strip
235
+ o_type = o.attributes.get_attribute("type").value.strip
236
+ if o_type.downcase == "uri"
237
+ object = URI.new(o_content)
238
+ else
239
+ object = Literal.new(o_content)
240
+ end
241
+
242
+ # build the triple
243
+ added_triple = Triple.new(subject, predicate, object)
244
+ added << added_triple
245
+
246
+ end
247
+
248
+ # Extract removed triples
249
+ elsif p.attributes.get_attribute("name").value == "obsolete_results"
250
+
251
+ # new root
252
+ nroot = p
253
+ t = p.find('./triple_list').first.find('./triple')
254
+ t.each do |tr|
255
+
256
+ # get subject
257
+ s = tr.find('./subject').first
258
+ s_content = s.content.strip
259
+ s_type = s.attributes.get_attribute("type").value.strip
260
+ if s_type.downcase == "uri"
261
+ subject = URI.new(s_content)
262
+ else
263
+ subject = Literal.new(s_content)
264
+ end
265
+
266
+ # get predicate
267
+ p = tr.find('./predicate').first
268
+ p_content = p.content.strip
269
+ predicate = URI.new(p_content)
270
+
271
+ # get object
272
+ o = tr.find('./object').first
273
+ o_content = o.content.strip
274
+ o_type = o.attributes.get_attribute("type").value.strip
275
+ if o_type.downcase == "uri"
276
+ object = URI.new(o_content)
277
+ else
278
+ object = Literal.new(o_content)
279
+ end
280
+
281
+ # build the triple
282
+ removed_triple = Triple.new(subject, predicate, object)
283
+ removed << removed_triple
284
+
285
+ end
286
+ end
287
+ end
288
+
289
+ # return added and removed triples
290
+ return added, removed
291
+
292
+ end
293
+
294
+ # get_sparql_results_from_indication
295
+ def get_sparql_results_from_indication()
296
+
297
+ puts "method started"
298
+
299
+ # declare an empty list
300
+ added = []
301
+ removed = []
302
+
303
+ # Get the triple list
304
+ pars = @content.root.find('./parameter')
305
+ pars.each do |p|
306
+
307
+ puts p.attributes.get_attribute("name").value
308
+
309
+ if p.attributes.get_attribute("name").value == "new_results"
310
+
311
+ # we're on the sparql node
312
+ p.each_element do |sparql|
313
+
314
+ sparql.each_element do |hr|
315
+
316
+ # head/results fields
317
+ hr.each_element do |field|
318
+
319
+ # find the results
320
+ if field.name == "result"
321
+
322
+ # We found a result
323
+ result = []
324
+
325
+ field.each_element do |n|
326
+ variable = []
327
+ variable << n.attributes.first.value
328
+ n.each_element do |v|
329
+ variable << v.name
330
+ variable << v.content
331
+ end
332
+ result << variable
333
+ added << result
334
+ end
335
+
336
+ end
337
+ end
338
+ end
339
+ break
340
+ end
341
+
342
+ elsif p.attributes.get_attribute("name").value == "obsolete_results"
343
+
344
+ # we're on the sparql node
345
+ p.each_element do |sparql|
346
+
347
+ sparql.each_element do |hr|
348
+
349
+ # head/results fields
350
+ hr.each_element do |field|
351
+
352
+ # find the results
353
+ if field.name == "result"
354
+
355
+ # We found a result
356
+ result = []
357
+
358
+ field.each_element do |n|
359
+ variable = []
360
+ variable << n.attributes.first.value
361
+ n.each_element do |v|
362
+ variable << v.name
363
+ variable << v.content
364
+ end
365
+ result << variable
366
+ removed << result
367
+ end
368
+
369
+ end
370
+ end
371
+ end
372
+ break
373
+ end
374
+
375
+ end
376
+ end
377
+
378
+ return added, removed
379
+
380
+ end
381
+
382
+ end
383
+
384
+ end
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/ruby
2
+
3
+ module RubyKpi
4
+
5
+ # JOIN
6
+ JOIN_REQUEST_TEMPLATE = %{<SSAP_message>
7
+ <node_id>%s</node_id>
8
+ <space_id>%s</space_id>
9
+ <transaction_type>JOIN</transaction_type>
10
+ <message_type>REQUEST</message_type>
11
+ <transaction_id>%s</transaction_id>
12
+ <parameter name = "credentials">XXYYZZ</parameter>
13
+ </SSAP_message>}
14
+
15
+
16
+ # LEAVE
17
+ LEAVE_REQUEST_TEMPLATE = %{<SSAP_message>
18
+ <node_id>%s</node_id>
19
+ <space_id>%s</space_id>
20
+ <transaction_type>LEAVE</transaction_type>
21
+ <message_type>REQUEST</message_type>
22
+ <transaction_id>%s</transaction_id>
23
+ </SSAP_message>}
24
+
25
+
26
+ # INSERT
27
+ INSERT_REQUEST_TEMPLATE = %{<SSAP_message>
28
+ <node_id>%s</node_id>
29
+ <space_id>%s</space_id>
30
+ <transaction_type>INSERT</transaction_type>
31
+ <message_type>REQUEST</message_type>
32
+ <transaction_id>%s</transaction_id>
33
+ <parameter name = "insert_graph" encoding = "RDF-M3">
34
+ <triple_list>%s</triple_list></parameter>
35
+ <parameter name = "confirm">TRUE</parameter>
36
+ </SSAP_message>}
37
+
38
+ # REMOVE
39
+ REMOVE_REQUEST_TEMPLATE = %{<SSAP_message>
40
+ <message_type>REQUEST</message_type>
41
+ <transaction_type>REMOVE</transaction_type>
42
+ <transaction_id>%s</transaction_id>
43
+ <node_id>%s</node_id>
44
+ <space_id>%s</space_id>
45
+ <parameter name="confirm">TRUE</parameter>
46
+ <parameter name="remove_graph" encoding="RDF-M3">
47
+ <triple_list>%s</triple_list></parameter>
48
+ </SSAP_message>}
49
+
50
+ # UPDATE
51
+ UPDATE_REQUEST_TEMPLATE = %{<SSAP_message>
52
+ <node_id>%s</node_id>
53
+ <space_id>%s</space_id>
54
+ <transaction_type>UPDATE</transaction_type>
55
+ <message_type>REQUEST</message_type>
56
+ <transaction_id>%s</transaction_id>
57
+
58
+ <parameter name = "insert_graph" encoding = "RDF-M3">
59
+ <triple_list>%s</triple_list>
60
+ </parameter>
61
+
62
+ <parameter name = "remove_graph" encoding = "RDF-M3">
63
+ <triple_list>%s</triple_list>
64
+ </parameter>
65
+
66
+ <parameter name = "confirm">TRUE</parameter>
67
+ </SSAP_message>}
68
+
69
+ # RDF QUERY
70
+ RDF_QUERY_REQUEST_TEMPLATE = %{<SSAP_message>
71
+ <node_id>%s</node_id>
72
+ <space_id>%s</space_id>
73
+ <transaction_type>QUERY</transaction_type>
74
+ <message_type>REQUEST</message_type>
75
+ <transaction_id>%s</transaction_id>
76
+ <parameter name = "type">RDF-M3</parameter>
77
+ <parameter name = "query">
78
+ <triple_list>%s</triple_list></parameter>
79
+ </SSAP_message>}
80
+
81
+ # SPARQL QUERY
82
+ SPARQL_QUERY_REQUEST_TEMPLATE = %{<SSAP_message>
83
+ <node_id>%s</node_id>
84
+ <space_id>%s</space_id>
85
+ <transaction_type>QUERY</transaction_type>
86
+ <message_type>REQUEST</message_type>
87
+ <transaction_id>%s</transaction_id>
88
+ <parameter name = "type">sparql</parameter>
89
+ <parameter name = "query">%s</parameter>
90
+ </SSAP_message>}
91
+
92
+ # RDF SUBSCRIBE
93
+ RDF_SUBSCRIBE_REQUEST_TEMPLATE = %{<SSAP_message>
94
+ <node_id>%s</node_id>
95
+ <space_id>%s</space_id>
96
+ <transaction_type>SUBSCRIBE</transaction_type>
97
+ <message_type>REQUEST</message_type>
98
+ <transaction_id>%s</transaction_id>
99
+ <parameter name = "type">RDF-M3</parameter>
100
+ <parameter name = "query">
101
+ <triple_list>%s</triple_list></parameter>
102
+ </SSAP_message>}
103
+
104
+ # SPARQL SUBSCRIBE
105
+ SPARQL_SUBSCRIBE_REQUEST_TEMPLATE = %{<SSAP_message>
106
+ <node_id>%s</node_id>
107
+ <space_id>%s</space_id>
108
+ <transaction_type>SUBSCRIBE</transaction_type>
109
+ <message_type>REQUEST</message_type>
110
+ <transaction_id>%s</transaction_id>
111
+ <parameter name = "type">sparql</parameter>
112
+ <parameter name = "query">%s</parameter>
113
+ </SSAP_message>}
114
+
115
+ # UNSUBSCRIBE
116
+ UNSUBSCRIBE_REQUEST_TEMPLATE = %{<SSAP_message>
117
+ <node_id>%s</node_id>
118
+ <space_id>%s</space_id>
119
+ <transaction_type>UNSUBSCRIBE</transaction_type>
120
+ <message_type>REQUEST</message_type>
121
+ <transaction_id>%s</transaction_id>
122
+ <parameter name = "subscription_id">%s</parameter>
123
+ </SSAP_message>}
124
+
125
+ # OTHER TEMPLATES
126
+ TRIPLE_TEMPLATE = %{<triple>
127
+ <subject type = "%s">%s</subject>
128
+ <predicate>%s</predicate>
129
+ <object type = "%s">%s</object>
130
+ </triple>}
131
+
132
+ end