norikra 1.0.2-java → 1.0.3-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 484ecce14e3035444d000c902c7172bc66aa0f21
4
- data.tar.gz: f964401ccd096def82fc7d2b0e980fcb634d05c9
3
+ metadata.gz: ff50de153ac29eecef89cb5e329c5060aca9f756
4
+ data.tar.gz: 4be725366d25d3a01f229a2879a48a12f3369de5
5
5
  SHA512:
6
- metadata.gz: 968258b7e2456018e8c557d22802514a198dad763529a40f73edf94c39614ebac3879d9469435899bd353a44b2704615ef47b30516d1609e36136a37f148787e
7
- data.tar.gz: 427efc15c89693c0750147176be8105e7d9bec03a7537fada0e8b612f1e1540274b9ea4d011585a2846e4ebcdfbe61addada81d102070ccf7b524f5f648736ad
6
+ metadata.gz: fe81a8b398ae6ca782174fc693d6b4effa6dc095303b762afadde84fb268b416d102c6e83377096320ef75fa6f5bf7d9eb35ff1ed06b40ef89a8aeeb62f2402f
7
+ data.tar.gz: 3f80c618ddc6875a32f9ed3cc990b9ae0c3e04002a2bc288e3ffc8b731f87a93584b8f4532110010341305337a298756d16651d9ce8f3f7df67156f21deeecfe
data/Changes.md CHANGED
@@ -3,6 +3,8 @@
3
3
  Changes of norikra.
4
4
 
5
5
  ## v1
6
+ * v1.0.3:
7
+ * Fix bug to return Array falsey for Hash output, [] for NULL output
6
8
  * v1.0.2:
7
9
  * Fix bug:
8
10
  * to handle encoding of input strings
@@ -2,6 +2,7 @@ require 'java'
2
2
 
3
3
  require 'norikra/error'
4
4
  require 'norikra/target'
5
+ require 'norikra/listener'
5
6
 
6
7
  require 'norikra/logger'
7
8
  include Norikra::Log
@@ -239,73 +240,6 @@ module Norikra
239
240
  load_udf(plugin_klass)
240
241
  end
241
242
 
242
- class Listener
243
- include com.espertech.esper.client.UpdateListener
244
-
245
- def initialize(query_name, query_group, output_pool, events_statistics)
246
- @query_name = query_name
247
- @query_group = query_group
248
- @output_pool = output_pool
249
- @events_statistics = events_statistics
250
- end
251
-
252
- def type_convert(value)
253
- if value.respond_to?(:getUnderlying)
254
- value = value.getUnderlying
255
- end
256
-
257
- trace "converting", :value => value
258
-
259
- if value.respond_to?(:to_a)
260
- value.to_a.map{|v| type_convert(v) }
261
- elsif value.respond_to?(:to_hash)
262
- Hash[ value.to_hash.map{|k,v| [ Norikra::Field.unescape_name(k), type_convert(v)] } ]
263
- elsif value.respond_to?(:force_encoding)
264
- value.force_encoding('UTF-8')
265
- else
266
- value
267
- end
268
- end
269
-
270
- def update(new_events, old_events)
271
- t = Time.now.to_i
272
- events = new_events.map{|e| [t, type_convert(e)]}
273
- trace "updated event", :query => @query_name, :group => @query_group, :event => events
274
- @output_pool.push(@query_name, @query_group, events)
275
- @events_statistics[:output] += events.size
276
- end
277
- end
278
-
279
- class LoopbackListener < Listener
280
- def initialize(engine, query_name, query_group, events_statistics)
281
- @engine = engine
282
- @query_name = query_name
283
- @query_group = query_group
284
- @events_statistics = events_statistics
285
- @loopback_target = Norikra::Query.loopback(query_group)
286
- end
287
-
288
- def update(new_events, old_events)
289
- t = Time.now.to_i
290
- event_list = new_events.map{|e| type_convert(e) }
291
- trace "loopback event", :query => @query_name, :group => @query_group, :event => event_list
292
- @events_statistics[:output] += event_list.size
293
- #
294
- # We does NOT convert 'container.$0' into container['field'].
295
- # Use escaped names like 'container__0'. That is NOT so confused.
296
- @engine.send(@loopback_target, event_list)
297
- end
298
- end
299
-
300
- ##### Unmatched events are simply ignored
301
- # class UnmatchedListener
302
- # include com.espertech.esper.client.UnmatchedListener
303
- # def update(event)
304
- # # puts "unmatched:\n- " + event.getProperties.inspect
305
- # # ignore
306
- # end
307
- # end
308
-
309
243
  private
310
244
 
311
245
  def open_target(target)
@@ -484,9 +418,9 @@ module Norikra
484
418
  Norikra::Query.rewrite_query(statement_model, event_type_name_map)
485
419
 
486
420
  listener = if Norikra::Query.loopback(query.group)
487
- LoopbackListener.new(self, query.name, query.group, @statistics[:events])
421
+ Norikra::LoopbackListener.new(self, query.name, query.group, @statistics[:events])
488
422
  else
489
- Listener.new(query.name, query.group, @output_pool, @statistics[:events])
423
+ Norikra::Listener.new(query.name, query.group, @output_pool, @statistics[:events])
490
424
  end
491
425
 
492
426
  epl = administrator.create(statement_model)
@@ -0,0 +1,78 @@
1
+ require 'java'
2
+ require 'esper-5.0.0.jar'
3
+ require 'esper/lib/commons-logging-1.1.3.jar'
4
+ require 'esper/lib/antlr-runtime-4.1.jar'
5
+ require 'esper/lib/cglib-nodep-3.1.jar'
6
+
7
+ require 'norikra/field'
8
+ require 'norikra/query'
9
+
10
+ module Norikra
11
+ class Listener
12
+ include com.espertech.esper.client.UpdateListener
13
+
14
+ def initialize(query_name, query_group, output_pool, events_statistics)
15
+ @query_name = query_name
16
+ @query_group = query_group
17
+ @output_pool = output_pool
18
+ @events_statistics = events_statistics
19
+ end
20
+
21
+ def type_convert(value)
22
+ if value.respond_to?(:getUnderlying)
23
+ value = value.getUnderlying
24
+ end
25
+
26
+ trace "converting", :value => value
27
+
28
+ if value.nil?
29
+ value
30
+ elsif value.respond_to?(:to_hash)
31
+ Hash[ value.to_hash.map{|k,v| [ Norikra::Field.unescape_name(k), type_convert(v)] } ]
32
+ elsif value.respond_to?(:to_a)
33
+ value.to_a.map{|v| type_convert(v) }
34
+ elsif value.respond_to?(:force_encoding)
35
+ value.force_encoding('UTF-8')
36
+ else
37
+ value
38
+ end
39
+ end
40
+
41
+ def update(new_events, old_events)
42
+ t = Time.now.to_i
43
+ events = new_events.map{|e| [t, type_convert(e)]}
44
+ trace "updated event", :query => @query_name, :group => @query_group, :event => events
45
+ @output_pool.push(@query_name, @query_group, events)
46
+ @events_statistics[:output] += events.size
47
+ end
48
+ end
49
+
50
+ class LoopbackListener < Listener
51
+ def initialize(engine, query_name, query_group, events_statistics)
52
+ @engine = engine
53
+ @query_name = query_name
54
+ @query_group = query_group
55
+ @events_statistics = events_statistics
56
+ @loopback_target = Norikra::Query.loopback(query_group)
57
+ end
58
+
59
+ def update(new_events, old_events)
60
+ event_list = new_events.map{|e| type_convert(e) }
61
+ trace "loopback event", :query => @query_name, :group => @query_group, :event => event_list
62
+ @events_statistics[:output] += event_list.size
63
+ #
64
+ # We does NOT convert 'container.$0' into container['field'].
65
+ # Use escaped names like 'container__0'. That is NOT so confused.
66
+ @engine.send(@loopback_target, event_list)
67
+ end
68
+ end
69
+
70
+ ##### Unmatched events are simply ignored
71
+ # class UnmatchedListener
72
+ # include com.espertech.esper.client.UnmatchedListener
73
+ # def update(event)
74
+ # # puts "unmatched:\n- " + event.getProperties.inspect
75
+ # # ignore
76
+ # end
77
+ # end
78
+ end
@@ -1,3 +1,3 @@
1
1
  module Norikra
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -0,0 +1,17 @@
1
+ require_relative './spec_helper'
2
+
3
+ require 'norikra/engine'
4
+
5
+ describe Norikra::Engine do
6
+ it 'can be initialized' do
7
+ expect { Norikra::Engine.new(nil, nil) }.not_to raise_error
8
+ end
9
+
10
+ describe '#camelize' do
11
+ it 'make CamelizedString from :snake_case_symbol' do
12
+ engine = Norikra::Engine.new(nil, nil)
13
+ expect(engine.camelize(:symbol)).to eql('Symbol')
14
+ expect(engine.camelize(:snake_case_symbol)).to eql('SnakeCaseSymbol')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,163 @@
1
+ # -*- coding: utf-8 -*-
2
+ require_relative './spec_helper'
3
+
4
+ require 'norikra/listener'
5
+
6
+ class DummyOutputPool
7
+ attr_reader :pool
8
+
9
+ def initialize
10
+ @pool = {}
11
+ end
12
+
13
+ def push(query_name, query_group, events)
14
+ @pool[query_group] ||= {}
15
+ @pool[query_group][query_name] ||= []
16
+ @pool[query_group][query_name].push(*events)
17
+ end
18
+ end
19
+
20
+ describe Norikra::Listener do
21
+ it 'should be initialized' do
22
+ dummy_pool = DummyOutputPool.new
23
+ statistics = {output: 0}
24
+ expect { Norikra::Listener.new('name', 'group', dummy_pool, statistics) }.not_to raise_error
25
+ end
26
+
27
+ describe '#type_convert' do
28
+ dummy_pool = DummyOutputPool.new
29
+ statistics = {output: 0}
30
+ listener = Norikra::Listener.new('name', 'group', dummy_pool, statistics)
31
+
32
+ it 'returns value itself for number, boolean and nil' do
33
+ val = 10001
34
+ expect(listener.type_convert(val)).to eq(val)
35
+
36
+ val = 100.001
37
+ expect(listener.type_convert(val)).to eq(val)
38
+
39
+ val = false
40
+ expect(listener.type_convert(val)).to eq(val)
41
+
42
+ val = true
43
+ expect(listener.type_convert(val)).to eq(val)
44
+
45
+ val = nil
46
+ expect(listener.type_convert(val)).to eq(val)
47
+ end
48
+
49
+ it 'returns String with UTF-8 encoding' do
50
+ val = "a".force_encoding('ASCII-8BIT')
51
+ rval = listener.type_convert(val)
52
+ expect(rval.encoding.to_s).to eql("UTF-8")
53
+ expect(rval).to eql("a")
54
+
55
+ val = "乗鞍".force_encoding('ASCII-8BIT')
56
+ rval = listener.type_convert(val)
57
+ expect(rval.encoding.to_s).to eql("UTF-8")
58
+ expect(rval).to eql("乗鞍")
59
+ end
60
+
61
+ it 'returns Array for to_a responding objects, with type_convert-ed members' do
62
+ s1 = "乗鞍".force_encoding("ASCII-8BIT")
63
+ val = [100, 100.01, false, true, nil, s1]
64
+ rval = listener.type_convert(val)
65
+ expect(rval).to be_a(Array)
66
+ expect(rval[0]).to eql(100)
67
+ expect(rval[1]).to eql(100.01)
68
+ expect(rval[2]).to eq(false)
69
+ expect(rval[3]).to eq(true)
70
+ expect(rval[4]).to be_nil
71
+ expect(rval[5].encoding.to_s).to eql("UTF-8")
72
+ expect(rval[5]).to eql("乗鞍")
73
+ end
74
+
75
+ it 'returns Hash for to_hash responding objects, with unescaped keys and type_convert-ed values' do
76
+ s1 = "乗鞍".force_encoding("ASCII-8BIT")
77
+ val = {
78
+ "100" => 100,
79
+ "100_01" => 100.01,
80
+ "bool$false" => false,
81
+ "bool$true" => true,
82
+ "object$$0" => nil,
83
+ "string" => s1,
84
+ "list" => [0, 1, 2],
85
+ "percentiles" => {
86
+ "10" => 0.1,
87
+ "50" => 0.5,
88
+ "99" => 0.99
89
+ }
90
+ }
91
+ rval = listener.type_convert(val)
92
+ expect(rval).to be_a(Hash)
93
+
94
+ expect(rval["100"]).to eql(100)
95
+ expect(rval["100_01"]).to eql(100.01)
96
+ expect(rval["bool.false"]).to eq(false)
97
+ expect(rval["bool.true"]).to eq(true)
98
+ expect(rval["object.$0"]).to be_nil
99
+ expect(rval["string"].encoding.to_s).to eql("UTF-8")
100
+ expect(rval["string"]).to eql("乗鞍")
101
+ expect(rval["list"]).to eql([0,1,2])
102
+ expect(rval["percentiles"]).to eql({"10" => 0.1, "50" => 0.5, "99" => 0.99})
103
+ end
104
+ end
105
+
106
+ describe '#update' do
107
+ dummy_pool = DummyOutputPool.new
108
+ statistics = {output: 0}
109
+ listener = Norikra::Listener.new('name', 'group', dummy_pool, statistics)
110
+
111
+ it 'pushs events into pool, with current time' do
112
+ listener.update([{"n1" => 100, "s" => "string one"}, {"n1" => 101, "s" => "string two"}], [])
113
+ expect(statistics[:output]).to eql(2)
114
+ expect(dummy_pool.pool['group']['name'].size).to eql(2)
115
+ expect(dummy_pool.pool['group']['name'][0][0]).to be_a(Fixnum)
116
+ expect(dummy_pool.pool['group']['name'][0][1]).to eql({"n1" => 100, "s" => "string one"})
117
+ expect(dummy_pool.pool['group']['name'][1][1]).to eql({"n1" => 101, "s" => "string two"})
118
+
119
+ listener.update([{"n1" => 102, "s" => "string three"}], [])
120
+ expect(statistics[:output]).to eql(3)
121
+ expect(dummy_pool.pool['group']['name'].size).to eql(3)
122
+ end
123
+ end
124
+ end
125
+
126
+ class DummyEngine
127
+ attr_reader :events
128
+
129
+ def initialize
130
+ @events = {}
131
+ end
132
+
133
+ def send(target, events)
134
+ @events[target] ||= []
135
+ @events[target].push(*events)
136
+ end
137
+ end
138
+
139
+ describe Norikra::LoopbackListener do
140
+ it 'should be initialized' do
141
+ dummy_engine = DummyEngine.new
142
+ statistics = {output: 0}
143
+ expect { Norikra::LoopbackListener.new(dummy_engine, 'name', 'LOOPBACK(target1)', statistics) }.not_to raise_error
144
+ end
145
+
146
+ describe '#update' do
147
+ dummy_engine = DummyEngine.new
148
+ statistics = {output: 0}
149
+ listener = Norikra::LoopbackListener.new(dummy_engine, 'name', 'LOOPBACK(target1)', statistics)
150
+
151
+ it 'sends events into engine with target name' do
152
+ listener.update([{"n1" => 100, "s" => "string one"}, {"n1" => 101, "s" => "string two"}], [])
153
+ expect(statistics[:output]).to eql(2)
154
+ expect(dummy_engine.events['target1'].size).to eql(2)
155
+ expect(dummy_engine.events['target1'][0]).to eql({"n1" => 100, "s" => "string one"})
156
+ expect(dummy_engine.events['target1'][1]).to eql({"n1" => 101, "s" => "string two"})
157
+
158
+ listener.update([{"n1" => 102, "s" => "string three"}], [])
159
+ expect(statistics[:output]).to eql(3)
160
+ expect(dummy_engine.events['target1'].size).to eql(3)
161
+ end
162
+ end
163
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norikra
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: java
6
6
  authors:
7
7
  - TAGOMORI Satoshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-29 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mizuno
@@ -232,6 +232,7 @@ files:
232
232
  - lib/norikra/error.rb
233
233
  - lib/norikra/field.rb
234
234
  - lib/norikra/fieldset.rb
235
+ - lib/norikra/listener.rb
235
236
  - lib/norikra/logger.rb
236
237
  - lib/norikra/logger_mizuno_patch.rb
237
238
  - lib/norikra/output_pool.rb
@@ -271,8 +272,10 @@ files:
271
272
  - public/js/jquery.min.js
272
273
  - public/js/norikra.webui.js
273
274
  - script/spec_server_pry
275
+ - spec/engine_spec.rb
274
276
  - spec/field_spec.rb
275
277
  - spec/fieldset_spec.rb
278
+ - spec/listener_spec.rb
276
279
  - spec/output_pool_spec.rb
277
280
  - spec/query_spec.rb
278
281
  - spec/spec_helper.rb
@@ -308,8 +311,10 @@ signing_key:
308
311
  specification_version: 4
309
312
  summary: CEP engine/server with Esper and JRuby
310
313
  test_files:
314
+ - spec/engine_spec.rb
311
315
  - spec/field_spec.rb
312
316
  - spec/fieldset_spec.rb
317
+ - spec/listener_spec.rb
313
318
  - spec/output_pool_spec.rb
314
319
  - spec/query_spec.rb
315
320
  - spec/spec_helper.rb