-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.xml
633 lines (305 loc) · 663 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
79
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
141
142
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
199
200
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
293
294
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>LeetCode daily Challenge</title>
<link href="/2020-11-01-LeetCode-daily-Challenge.html"/>
<url>/2020-11-01-LeetCode-daily-Challenge.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="2020-11-02-349-Intersection-of-Two-Arrays"><a href="#2020-11-02-349-Intersection-of-Two-Arrays" class="headerlink" title="2020- 11 - 02: 349. Intersection of Two Arrays"></a>2020- 11 - 02: 349. Intersection of Two Arrays</h2><p>Problem <a href="https://leetcode.com/problems/intersection-of-two-arrays/" target="_blank" rel="noopener">link</a>.</p><h3 id="Method-1-HashTable"><a href="#Method-1-HashTable" class="headerlink" title="Method 1: HashTable"></a>Method 1: HashTable</h3><p>Using two HashSet. put one array numbers in to a set, and check if numbers the other array are in the set. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">intersection</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums1<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums2<span class="token punctuation">)</span> <span class="token punctuation">{</span> Set<span class="token operator"><</span>Integer<span class="token operator">></span> set1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num <span class="token operator">:</span> nums1<span class="token punctuation">)</span> <span class="token punctuation">{</span> set1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Set<span class="token operator"><</span>Integer<span class="token operator">></span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token operator">:</span> nums2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>set1<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token operator">!</span>result<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> intersection <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>result<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> idx <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token operator">:</span> result<span class="token punctuation">)</span><span class="token punctuation">{</span> intersection<span class="token punctuation">[</span>idx<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> num<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> intersection<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h3 id="Method-2-Sort-Two-pointer"><a href="#Method-2-Sort-Two-pointer" class="headerlink" title="Method 2: Sort + Two pointer"></a>Method 2: Sort + Two pointer</h3><p>Sort the array and use two pointer to find out the numbers that appear in both arrays. Move the pointer with smaller number.</p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">intersection</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums1<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums2<span class="token punctuation">)</span> <span class="token punctuation">{</span> Arrays<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>nums1<span class="token punctuation">)</span><span class="token punctuation">;</span> Arrays<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>nums2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ptr1 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span> ptr2 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> intersection <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>ptr1 <span class="token operator"><</span> nums1<span class="token punctuation">.</span>length <span class="token operator">&&</span> ptr2 <span class="token operator"><</span> nums2<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> num1 <span class="token operator">=</span> nums1<span class="token punctuation">[</span>ptr1<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> num2 <span class="token operator">=</span> nums2<span class="token punctuation">[</span>ptr2<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>num1 <span class="token operator">==</span> num2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>intersection<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> intersection<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>intersection<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">!=</span> num1<span class="token punctuation">)</span> <span class="token punctuation">{</span> intersection<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>num1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> ptr1 <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> ptr2 <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>num1 <span class="token operator">></span> num2<span class="token punctuation">)</span> <span class="token punctuation">{</span> ptr2 <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// num1 < num2</span> ptr1 <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> intersection<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">mapToInt</span><span class="token punctuation">(</span>Integer<span class="token operator">:</span><span class="token operator">:</span>intValue<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-11-01-140-Word-Break-II"><a href="#2020-11-01-140-Word-Break-II" class="headerlink" title="2020-11-01 140. Word Break II"></a>2020-11-01 140. Word Break II</h2><p>Problem <a href="https://leetcode.com/problems/word-break-ii/" target="_blank" rel="noopener">link</a></p><h3 id="Method-Recursion-With-Memorization"><a href="#Method-Recursion-With-Memorization" class="headerlink" title="Method: Recursion With Memorization"></a>Method: Recursion With Memorization</h3><p>Given a string <code>str</code>, if its prefix is in the <code>wordDict</code>, we can recursivly find out the list of breaked words for the substring<code>str'</code> with recursion. and combine the results.<br>Recursion Three key thing:</p><ul><li>boundary case: Once all <code>str</code> is matched, this means we have another variable to record the starting point ,called <code>offset</code></li><li>sub-problem: match the substring </li><li>return value: list of word break string for an input string. </li></ul><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span> List<span class="token operator"><</span>String<span class="token operator">>></span> cache <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> List<span class="token operator"><</span>String<span class="token operator">></span> <span class="token function">wordBreak</span><span class="token punctuation">(</span>String s<span class="token punctuation">,</span> List<span class="token operator"><</span>String<span class="token operator">></span> wordDict<span class="token punctuation">)</span> <span class="token punctuation">{</span> Set<span class="token operator"><</span>String<span class="token operator">></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String word<span class="token operator">:</span> wordDict<span class="token punctuation">)</span> <span class="token punctuation">{</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>word<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token function">dfs</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> set<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> List<span class="token operator"><</span>String<span class="token operator">></span> <span class="token function">dfs</span><span class="token punctuation">(</span>String s <span class="token punctuation">,</span> <span class="token keyword">int</span> offset<span class="token punctuation">,</span> Set<span class="token operator"><</span>String<span class="token operator">></span> dict <span class="token punctuation">)</span> <span class="token punctuation">{</span> List<span class="token operator"><</span>String<span class="token operator">></span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>offset <span class="token operator">==</span> s<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cache<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>offset<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> cache<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>offset<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> offset<span class="token punctuation">;</span> idx <span class="token operator"><</span> s<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String word <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>offset<span class="token punctuation">,</span> idx <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>dict<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>word<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> List<span class="token operator"><</span>String<span class="token operator">></span> next <span class="token operator">=</span> <span class="token function">dfs</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> idx <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> dict<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String nextStr<span class="token operator">:</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">(</span>word <span class="token operator">+</span> <span class="token string">" "</span> <span class="token operator">+</span> nextStr<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> cache<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>offset<span class="token punctuation">)</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-10-31-381-Insert-Delete-GetRandom-O-1-Duplicates-allowed"><a href="#2020-10-31-381-Insert-Delete-GetRandom-O-1-Duplicates-allowed" class="headerlink" title="2020-10-31 381. Insert Delete GetRandom O(1) - Duplicates allowed"></a>2020-10-31 381. Insert Delete GetRandom O(1) - Duplicates allowed</h2><p>Problem <a href="https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/" target="_blank" rel="noopener">link</a></p><h3 id="Method-HashTable-Design"><a href="#Method-HashTable-Design" class="headerlink" title="Method: HashTable + Design"></a>Method: HashTable + Design</h3><p>The most challenge part is to get <code>delete(int val)</code> to be done in O(1) Time. We need to record not only how many time appears in the arraylist as well as their index. We need to get one of the index for the <code>val</code>, and swap with the last element. Update the <code>indexMap</code> first before remove the val from the arraylist. Be careful with the corner case. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">RandomizedCollection</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> nums<span class="token punctuation">;</span> <span class="token keyword">private</span> Random random<span class="token punctuation">;</span> <span class="token keyword">private</span> Map<span class="token operator"><</span>Integer<span class="token punctuation">,</span> Set<span class="token operator"><</span>Integer<span class="token operator">>></span> indexMap<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** Initialize your data structure here. */</span> <span class="token keyword">public</span> <span class="token function">RandomizedCollection</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nums <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>indexMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>random <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">insert</span><span class="token punctuation">(</span><span class="token keyword">int</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span> nums<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> idx <span class="token operator">=</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> indexMap<span class="token punctuation">.</span><span class="token function">putIfAbsent</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> indexMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> indexMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** Removes a value from the collection. Returns true if the collection contained the specified element. */</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">remove</span><span class="token punctuation">(</span><span class="token keyword">int</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>indexMap<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Set<span class="token operator"><</span>Integer<span class="token operator">></span> idxSet <span class="token operator">=</span> indexMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> idx <span class="token operator">=</span> idxSet<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// get one of the index of the val </span> <span class="token keyword">int</span> lastVal <span class="token operator">=</span> nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">swap</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span> idx<span class="token punctuation">,</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//swap the val with the lastVal</span> <span class="token comment" spellcheck="true">// remove `val` and `lastVal` indexin the indexMap</span> idxSet<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">;</span> indexMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>lastVal<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// update`lastVal` index with new one if possible</span> <span class="token comment" spellcheck="true">// why this.nums.size() -1 here, becase after remove,the size decrease one. </span> <span class="token keyword">if</span> <span class="token punctuation">(</span>idx <span class="token operator"><</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> indexMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>lastVal<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// update the lastVal index in this indexMap</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>idxSet<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> indexMap<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> nums<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** Get a random element from the collection. */</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getRandom</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> randIdx <span class="token operator">=</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>randIdx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span>List<span class="token operator"><</span>Integer<span class="token operator">></span> nums<span class="token punctuation">,</span> <span class="token keyword">int</span> idx1<span class="token punctuation">,</span> <span class="token keyword">int</span> idx2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> temp <span class="token operator">=</span> nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>idx1<span class="token punctuation">)</span><span class="token punctuation">;</span> nums<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>idx1<span class="token punctuation">,</span> nums<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>idx2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> nums<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>idx2<span class="token punctuation">,</span> temp<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">/** * Your RandomizedCollection object will be instantiated and called as such: * RandomizedCollection obj = new RandomizedCollection(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */</span></code></pre></details><h2 id="2020-10-30-463-Island-Perimeter"><a href="#2020-10-30-463-Island-Perimeter" class="headerlink" title="2020-10-30 463. Island Perimeter"></a>2020-10-30 463. Island Perimeter</h2><p>Problem <a href="https://leetcode.com/problems/island-perimeter/" target="_blank" rel="noopener">link</a></p><h3 id="Method-Math-Counting"><a href="#Method-Math-Counting" class="headerlink" title="Method: Math(Counting)"></a>Method: Math(Counting)</h3><p>Very simple, if boundary overlap with its neighbour, minuteone.</p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">islandPerimeter</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> grid<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span> rows <span class="token operator">=</span> grid<span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token keyword">int</span> cols <span class="token operator">=</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> r <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> r <span class="token operator"><</span> rows <span class="token punctuation">;</span> r<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> c <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> c <span class="token operator"><</span> cols<span class="token punctuation">;</span> c<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> grid<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> count <span class="token operator">+=</span> <span class="token number">4</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> r <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&&</span> grid<span class="token punctuation">[</span>r<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> count <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> c <span class="token operator">></span> <span class="token number">0</span> <span class="token operator">&&</span> grid<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> count <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> r <span class="token operator"><</span> rows <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&&</span> grid<span class="token punctuation">[</span>r<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> count <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> c <span class="token operator"><</span> cols <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&&</span> grid<span class="token punctuation">[</span>r<span class="token punctuation">]</span><span class="token punctuation">[</span>c<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> count <span class="token operator">-=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> count<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-10-29-129-Sum-Root-to-Leaf-Numbers"><a href="#2020-10-29-129-Sum-Root-to-Leaf-Numbers" class="headerlink" title="2020-10-29 129. Sum Root to Leaf Numbers"></a>2020-10-29 129. Sum Root to Leaf Numbers</h2><p>Problem <a href="https://leetcode.com/problems/sum-root-to-leaf-numbers/" target="_blank" rel="noopener">link</a></p><h3 id="Method-1:-Breadth-first-search-BFS"><a href="#Method-1:-Breadth-first-search-BFS" class="headerlink" title="Method 1: Breadth-first search (BFS)"></a>Method 1: Breadth-first search (BFS)</h3><p>We need to record the <code>temp_sum</code> along the way. So, we need to have a wrapper class of the TreeNode class. Add to result if this is a leaf Node. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */</span><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">class</span> <span class="token class-name">Element</span> <span class="token punctuation">{</span> TreeNode node<span class="token punctuation">;</span> <span class="token keyword">int</span> sum<span class="token punctuation">;</span> <span class="token function">Element</span> <span class="token punctuation">(</span>TreeNode node<span class="token punctuation">,</span> <span class="token keyword">int</span> sum<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>node <span class="token operator">=</span> node<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>sum <span class="token operator">=</span> sum<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sumNumbers</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> result <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> Deque<span class="token operator"><</span>Element<span class="token operator">></span> queue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Element</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> root<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>queue<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Element cur <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>left <span class="token operator">==</span> null <span class="token operator">&&</span> cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>right <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> result <span class="token operator">+=</span> cur<span class="token punctuation">.</span>sum<span class="token punctuation">;</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>left <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> queue<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Element</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>left<span class="token punctuation">,</span> cur<span class="token punctuation">.</span>sum <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>left<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>right <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> queue<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Element</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>right<span class="token punctuation">,</span> cur<span class="token punctuation">.</span>sum <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> cur<span class="token punctuation">.</span>node<span class="token punctuation">.</span>right<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h3 id="Method-2-Depth-first-Search-DFS"><a href="#Method-2-Depth-first-Search-DFS" class="headerlink" title="Method 2: Depth-first Search (DFS)"></a>Method 2: Depth-first Search (DFS)</h3><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">int</span> totalSum <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">sumNumbers</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> null<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> totalSum<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">,</span> <span class="token keyword">int</span> cur<span class="token punctuation">)</span><span class="token punctuation">{</span> cur <span class="token operator">=</span> cur <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> root<span class="token punctuation">.</span>val<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">==</span> null <span class="token operator">&&</span> root<span class="token punctuation">.</span>right <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> totalSum <span class="token operator">+=</span> cur<span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>left <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> cur<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span>right <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span> cur<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-10-28-1207-Unique-Number-of-Occurrences"><a href="#2020-10-28-1207-Unique-Number-of-Occurrences" class="headerlink" title="2020-10-28 1207. Unique Number of Occurrences"></a>2020-10-28 1207. Unique Number of Occurrences</h2><p>Problem <a href="https://leetcode-cn.com/problems/unique-number-of-occurrences/" target="_blank" rel="noopener">link</a></p><h3 id="Method-HashTable"><a href="#Method-HashTable" class="headerlink" title="Method: HashTable"></a>Method: HashTable</h3><p>Counting the numbers with one HashTable and counting the occurrency with another one. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">uniqueOccurrences</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span> Map<span class="token operator"><</span>Integer<span class="token punctuation">,</span> Integer<span class="token operator">></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token operator">:</span> arr<span class="token punctuation">)</span> <span class="token punctuation">{</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>num <span class="token punctuation">,</span>map<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Set<span class="token operator"><</span>Integer<span class="token operator">></span> occurrences <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token operator">:</span> map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>occurrences<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> occurrences<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-10-27-144-Binary-Tree-Preorder-Traversal"><a href="#2020-10-27-144-Binary-Tree-Preorder-Traversal" class="headerlink" title="2020-10-27 144. Binary Tree Preorder Traversal"></a>2020-10-27 144. Binary Tree Preorder Traversal</h2><h3 id="Method-1-DFS-with-recursion"><a href="#Method-1-DFS-with-recursion" class="headerlink" title="Method 1 : DFS with recursion"></a>Method 1 : DFS with recursion</h3><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> <span class="token function">preorderTraversal</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">)</span> <span class="token punctuation">{</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">,</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> result<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>left<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dfs</span><span class="token punctuation">(</span>root<span class="token punctuation">.</span>right<span class="token punctuation">,</span>result <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h3 id="Method-2-DFS-with-stack"><a href="#Method-2-DFS-with-stack" class="headerlink" title="Method 2 : DFS with stack"></a>Method 2 : DFS with stack</h3><p>Using stack can convert the recursion code into iterative one. Notice that Stack has the property of FILO,So when traverse the tree pre-order, we should add its right child to stack and then left child. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */</span><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> <span class="token function">preorderTraversal</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">)</span> <span class="token punctuation">{</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>root <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span> Deque<span class="token operator"><</span>TreeNode<span class="token operator">></span> stack <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>root<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>stack<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> TreeNode cur <span class="token operator">=</span> stack<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cur<span class="token punctuation">.</span>right <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> stack<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>right<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>cur<span class="token punctuation">.</span>left <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> stack<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>cur<span class="token punctuation">.</span>left<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h2 id="2020-10-26-1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number"><a href="#2020-10-26-1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number" class="headerlink" title="2020-10-26 1365. How Many Numbers Are Smaller Than the Current Number"></a>2020-10-26 1365. How Many Numbers Are Smaller Than the Current Number</h2><p>Problem <a href="https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/" target="_blank" rel="noopener">link</a></p><h3 id="Method-1-Bucket-Sort-PreSum"><a href="#Method-1-Bucket-Sort-PreSum" class="headerlink" title="Method 1: Bucket Sort + PreSum"></a>Method 1: Bucket Sort + PreSum</h3><p>Notice that the range of the element in the array is <code>0 <= nums[i] <= 100</code>,So we could use a array with size 101 as the hashtable to count the number of occurency in the array ,and then pre-sum the bucket from the left to right, such that <code>countMap[i]</code> represents the number of numbers which are smaller than or equal to i. </p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">smallerNumbersThanCurrent</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> countMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span><span class="token number">101</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token operator">:</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span> countMap<span class="token punctuation">[</span>num<span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> num <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> num <span class="token operator"><=</span> <span class="token number">100</span><span class="token punctuation">;</span> num<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> countMap<span class="token punctuation">[</span>num<span class="token punctuation">]</span> <span class="token operator">+=</span> countMap<span class="token punctuation">[</span>num<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>nums<span class="token punctuation">.</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> idx <span class="token operator"><</span> result<span class="token punctuation">.</span>length<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nums<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> result<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> result<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> countMap<span class="token punctuation">[</span>nums<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><h3 id="Method-2-Sort"><a href="#Method-2-Sort" class="headerlink" title="Method 2: Sort"></a>Method 2: Sort</h3><p>We could sort with based on their val, but we also needs to carry their position information. After sorting, we could traverse from the left to right, and if the current val is larger than the preivous one, its val is just equal to idx, if its the same as the previous one, then they have the same result.</p><details><summary>Code</summary><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">smallerNumbersThanCurrent</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> len <span class="token operator">=</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> tuples <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>len<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> idx <span class="token operator"><</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> idx<span class="token operator">++</span> <span class="token punctuation">)</span><span class="token punctuation">{</span> tuples<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span>nums<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">,</span> idx<span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Arrays<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>tuples<span class="token punctuation">,</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span> <span class="token keyword">return</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">-</span> b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> counter <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>len<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> idx <span class="token operator"><</span> len<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> curNum <span class="token operator">=</span> tuples<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> curPos <span class="token operator">=</span> tuples<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>idx <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> result<span class="token punctuation">[</span>curPos<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span><span class="token punctuation">{</span> <span class="token keyword">int</span> preNum <span class="token operator">=</span> tuples<span class="token punctuation">[</span>idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> prePos <span class="token operator">=</span> tuples<span class="token punctuation">[</span>idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// if the previous one is smaller than the current, assign by the counter,</span> <span class="token comment" spellcheck="true">// otherwise they have the same result;</span> result<span class="token punctuation">[</span>curPos<span class="token punctuation">]</span> <span class="token operator">=</span> preNum <span class="token operator"><</span> curNum <span class="token operator">?</span> counter<span class="token operator">:</span> result<span class="token punctuation">[</span>prePos<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> counter <span class="token operator">+=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// counter represents how many number are smaller than or equal to the currentNum; </span> <span class="token punctuation">}</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre></details><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> algorithm </category>
</categories>
<tags>
<tag> Java </tag>
<tag> Python </tag>
<tag> Data Structure </tag>
<tag> Algorithm </tag>
<tag> Design </tag>
</tags>
</entry>
<entry>
<title>Reservoir Sampling</title>
<link href="/2020-05-14-reservoir-sampling.html"/>
<url>/2020-05-14-reservoir-sampling.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1.Introduction."></a>1.Introduction.</h2><p>Reservoir Sampling is an online algorithm designed for picking up small amount of sample with uniform probability. To be specific, given a sample of size K with N items processed so far, the chance for any item to be selected is K/N. When the next item comes in, current sample has a chance to survive K/N*N/(N+1)=K/(N+1) while the new item has chance K/(N+1) to be selected.</p><p>The mathematical prove is as follows:</p><div> <img src="../images/rev_sample_1.png" alt="Figure 1" width="100%" height="100%"></div><br><p>Reservoir sample algorithm works as follows: </p><ul><li><ol><li>Create an array samples[0..k-1] and copy first k items of stream[] to it.</li></ol></li><li><ol start="2"><li>When a new data comes in (with index greater than k - 1), generate a random number from 0 to i where i is index of current item in stream[].</li></ol></li><li><ol start="3"><li>Generated another random number j with in range 0 to k-1, replace samples[j] with the new data.</li></ol></li></ul><p>The code for this aglroithm is also straightforward.</p><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> random <span class="token keyword">from</span> typing <span class="token keyword">import</span> List<span class="token punctuation">,</span>Generator <span class="token keyword">class</span> <span class="token class-name">ReservoirSampling</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> capacity<span class="token punctuation">:</span> int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span> self<span class="token punctuation">.</span>_capacity<span class="token punctuation">:</span>int <span class="token operator">=</span> capacity self<span class="token punctuation">.</span>_samples<span class="token punctuation">:</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> self<span class="token punctuation">.</span>_pointer<span class="token punctuation">:</span>int <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">def</span> <span class="token function">reservoir_sampling</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Generator<span class="token punctuation">[</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span><span class="token punctuation">,</span> int<span class="token punctuation">,</span> None<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span> next_val<span class="token punctuation">:</span>int <span class="token operator">=</span> <span class="token keyword">yield</span> self<span class="token punctuation">.</span>_samples <span class="token keyword">if</span> len<span class="token punctuation">(</span>self<span class="token punctuation">.</span>_samples<span class="token punctuation">)</span> <span class="token operator"><</span> self<span class="token punctuation">.</span>_capacity<span class="token punctuation">:</span> self<span class="token punctuation">.</span>_samples<span class="token punctuation">.</span>append<span class="token punctuation">(</span>next_val<span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> rand_pos<span class="token punctuation">:</span> int <span class="token operator">=</span> random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>_pointer<span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># [0, M - 1] </span> <span class="token comment" spellcheck="true"># probability = K / M </span> <span class="token keyword">if</span> rand_pos <span class="token operator"><</span> self<span class="token punctuation">.</span>_capacity<span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># belonging to [0, K - 1] </span> self<span class="token punctuation">.</span>_samples<span class="token punctuation">[</span>rand_pos<span class="token punctuation">]</span> <span class="token operator">=</span> next_val self<span class="token punctuation">.</span>_pointer <span class="token operator">+=</span> <span class="token number">1</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> rs<span class="token punctuation">:</span>ReservoirSampling <span class="token operator">=</span> ReservoirSampling<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> rs_generator <span class="token operator">=</span> rs<span class="token punctuation">.</span>reservoir_sampling<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"init list:{}"</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>next<span class="token punctuation">(</span>rs_generator<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> x <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>rs_generator<span class="token punctuation">.</span>send<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span> </code></pre><p>Result: </p><pre class=" language-bash"><code class="language-bash">init list:<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span>0<span class="token punctuation">]</span><span class="token punctuation">[</span>0, 1<span class="token punctuation">]</span><span class="token punctuation">[</span>0, 1, 2<span class="token punctuation">]</span><span class="token punctuation">[</span>0, 1, 2, 3<span class="token punctuation">]</span><span class="token punctuation">[</span>0, 1, 2, 3, 4<span class="token punctuation">]</span><span class="token punctuation">[</span>0, 1, 5, 3, 4<span class="token punctuation">]</span><span class="token punctuation">[</span>6, 1, 5, 3, 4<span class="token punctuation">]</span><span class="token punctuation">[</span>6, 7, 5, 3, 4<span class="token punctuation">]</span><span class="token punctuation">[</span>6, 7, 5, 3, 8<span class="token punctuation">]</span><span class="token punctuation">[</span>6, 7, 9, 3, 8<span class="token punctuation">]</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Python </tag>
<tag> Big Data </tag>
<tag> sampling algorithm </tag>
</tags>
</entry>
<entry>
<title>Nearest point search in PostGIS</title>
<link href="/2020-05-14-nearest-point-search-in-PostGIS.html"/>
<url>/2020-05-14-nearest-point-search-in-PostGIS.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>A frequently retrieving task is to find the nearest candidate to the target candidate based on speific feature.</p><h2 id="1-Naive-Solution"><a href="#1-Naive-Solution" class="headerlink" title="1. Naive Solution:"></a>1. Naive Solution:</h2><p>The naive way to carry out the query task is to compute all distance pairs for all candidates and order them in ascending order and pick up the smallest one as the result. </p><pre class=" language-sql"><code class="language-sql"><span class="token comment" spellcheck="true">-- Closest poitn to the target geometry with (-116.733:longitude, 54.828: latitude)</span><span class="token keyword">SELECT</span> forecast_longitude <span class="token keyword">as</span> lon<span class="token punctuation">,</span>forecast_latitude <span class="token keyword">as</span> lat<span class="token punctuation">,</span>centroid_point <span class="token keyword">as</span> <span class="token keyword">point</span><span class="token punctuation">,</span> boundary_geo <span class="token keyword">as</span> area<span class="token punctuation">,</span>ST_Distance<span class="token punctuation">(</span> ST_Transform<span class="token punctuation">(</span><span class="token string">'SRID=4326;POINT(-116.733 54.828)'</span>::<span class="token keyword">geometry</span><span class="token punctuation">,</span> <span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">,</span> ST_Transform<span class="token punctuation">(</span>centroid_point<span class="token punctuation">,</span> <span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> distance<span class="token keyword">FROM</span> services_areas<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> distance <span class="token keyword">ASC</span><span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre><p>This native approach takes line time to traverse the whole database. For a larget database and complex feature computation , it is not a reasonable approach.</p><p>Let us look at the time taken to execute the query.</p><pre class=" language-bash"><code class="language-bash">For a large table of candidate features, it is not a reasonable approach.QUERY PLAN-------------------------------------------------------------------------------------------------------------------------------------Limit <span class="token punctuation">(</span>cost<span class="token operator">=</span>3008.20<span class="token punctuation">..</span>3008.20 rows<span class="token operator">=</span>1 width<span class="token operator">=</span>184<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>53.044<span class="token punctuation">..</span>53.044 rows<span class="token operator">=</span>1 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> -<span class="token operator">></span> Sort <span class="token punctuation">(</span>cost<span class="token operator">=</span>3008.20<span class="token punctuation">..</span>3076.36 rows<span class="token operator">=</span>27265 width<span class="token operator">=</span>184<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>53.043<span class="token punctuation">..</span>53.043 rows<span class="token operator">=</span>1 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> Sort Key: <span class="token punctuation">(</span>st_distance<span class="token punctuation">(</span><span class="token string">'010100002073080000FF1FDF9BB84030C19FAD11C8F06A3241'</span>::geometry, st_transform<span class="token punctuation">(</span>centroid_point, 2163<span class="token punctuation">))</span><span class="token punctuation">)</span> Sort Method: top-N heapsort Memory: 25kB -<span class="token operator">></span> Seq Scan on services_areas <span class="token punctuation">(</span>cost<span class="token operator">=</span>0.00<span class="token punctuation">..</span>2871.88 rows<span class="token operator">=</span>27265 width<span class="token operator">=</span>184<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>0.306<span class="token punctuation">..</span>43.524 rows<span class="token operator">=</span>27265 loops<span class="token operator">=</span>1<span class="token punctuation">)</span>Planning Time: 0.476 msExecution Time: 53.069 ms<span class="token punctuation">(</span>7 rows<span class="token punctuation">)</span></code></pre><h2 id="2-Index-based-KNN"><a href="#2-Index-based-KNN" class="headerlink" title="2.Index-based KNN"></a>2.Index-based KNN</h2><p>KNN algorithm is to find the k nearest point to the target, where k is an arbitary positive integer.</p><p>This index-based kNN system works by evaluating distance bweteen bounding box in PostGIS R-index tree.<br>Since the index is built with the bounding box of any geometry, so the distances between any geometries are actually somehow inexact.</p><p>There are two types of index-based operators measuring the distance of two bounding boxes.</p><ul><li><p><-> means “distance between box centers”</p></li><li><p><#> means “distance between box edges”</p></li></ul><p>The bounding box of one <strong>geometry points</strong> is just the point itself, so there is no approximation there. But if the bounding boxes of <strong>polygons</strong> aren’t the same as points. </p><p>The right way to get the nearest point with high-performance yet accurate approach is to pull up top-k<br>number (k could be a really small number if you are confident that the data is in homogeneous distribution)</p><pre class=" language-sql"><code class="language-sql"><span class="token comment" spellcheck="true">-- "Closest" 100 points to target point,</span><span class="token keyword">EXPLAIN</span> ANALYSE<span class="token keyword">WITH</span> closest_candidates <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token keyword">SELECT</span> forecast_longitude <span class="token keyword">as</span> lon<span class="token punctuation">,</span> forecast_latitude <span class="token keyword">as</span> lat<span class="token punctuation">,</span> centroid_point <span class="token keyword">as</span> <span class="token keyword">point</span><span class="token punctuation">,</span> boundary_geo <span class="token keyword">as</span> area <span class="token keyword">FROM</span> services_areas <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> centroid_point <span class="token operator"><</span><span class="token operator">-</span><span class="token operator">></span> ST_Transform<span class="token punctuation">(</span><span class="token string">'SRID=4326;POINT(-116.733 54.828)'</span>::<span class="token keyword">geometry</span><span class="token punctuation">,</span> <span class="token number">3401</span><span class="token punctuation">)</span> <span class="token keyword">LIMIT</span> <span class="token number">100</span> <span class="token punctuation">)</span><span class="token keyword">SELECT</span> lon<span class="token punctuation">,</span>lat<span class="token punctuation">,</span><span class="token keyword">point</span><span class="token punctuation">,</span>area<span class="token punctuation">,</span> ST_Distance<span class="token punctuation">(</span> ST_Transform<span class="token punctuation">(</span> <span class="token string">'SRID=4326;POINT(-116.733 54.828)'</span>::<span class="token keyword">geometry</span><span class="token punctuation">,</span> <span class="token keyword">point</span> <span class="token punctuation">)</span> <span class="token keyword">AS</span> distance<span class="token keyword">FROM</span> closest_candidates<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> distance <span class="token keyword">ASC</span><span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre><p>The execution time is as follows:</p><pre class=" language-bash"><code class="language-bash"> QUERY PLAN----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit <span class="token punctuation">(</span>cost<span class="token operator">=</span>15.38<span class="token punctuation">..</span>15.39 rows<span class="token operator">=</span>1 width<span class="token operator">=</span>136<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>3.150<span class="token punctuation">..</span>3.150 rows<span class="token operator">=</span>1 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> CTE closest_candidates -<span class="token operator">></span> Limit <span class="token punctuation">(</span>cost<span class="token operator">=</span>0.28<span class="token punctuation">..</span>6.38 rows<span class="token operator">=</span>100 width<span class="token operator">=</span>184<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>0.446<span class="token punctuation">..</span>1.730 rows<span class="token operator">=</span>100 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> -<span class="token operator">></span> Index Scan using services_areas_centroid_p_idx on services_areas <span class="token punctuation">(</span>cost<span class="token operator">=</span>0.28<span class="token punctuation">..</span>1665.38 rows<span class="token operator">=</span>27265 width<span class="token operator">=</span>184<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>0.445<span class="token punctuation">..</span>1.683 rows<span class="token operator">=</span>100 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> Order By: <span class="token punctuation">(</span>centroid_point <span class="token operator"><</span>-<span class="token operator">></span> <span class="token string">'0101000020490D00004D804FF20E2BFBC013EF8614392C5741'</span>::geometry<span class="token punctuation">)</span> -<span class="token operator">></span> Sort <span class="token punctuation">(</span>cost<span class="token operator">=</span>9.00<span class="token punctuation">..</span>9.25 rows<span class="token operator">=</span>100 width<span class="token operator">=</span>136<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>3.149<span class="token punctuation">..</span>3.149 rows<span class="token operator">=</span>1 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> Sort Key: <span class="token punctuation">(</span>st_distance<span class="token punctuation">(</span><span class="token string">'010100002073080000FF1FDF9BB84030C19FAD11C8F06A3241'</span>::geometry, st_transform<span class="token punctuation">(</span>closest_candidates.point, 2163<span class="token punctuation">))</span><span class="token punctuation">)</span> Sort Method: top-N heapsort Memory: 25kB -<span class="token operator">></span> CTE Scan on closest_candidates <span class="token punctuation">(</span>cost<span class="token operator">=</span>0.00<span class="token punctuation">..</span>8.50 rows<span class="token operator">=</span>100 width<span class="token operator">=</span>136<span class="token punctuation">)</span> <span class="token punctuation">(</span>actual time<span class="token operator">=</span>0.926<span class="token punctuation">..</span>2.997 rows<span class="token operator">=</span>100 loops<span class="token operator">=</span>1<span class="token punctuation">)</span> Planning Time: 1.225 ms Execution Time: 3.221 ms<span class="token punctuation">(</span>11 rows<span class="token punctuation">)</span></code></pre><p>This will saves a lot of time because traversing the R-tree and heap sort for top-K is at most <strong>logarithmic</strong> time.It’s much faster compared wtih <strong>linear</strong> time.</p><p>For the details of how R-Tree works, I recommend the <a href="http://www.mathcs.emory.edu/~cheung/Courses/554/Syllabus/3-index/R-tree.html" target="_blank" rel="noopener">pdf</a>, which shows how R-tree data structrue in detail, and how insert/search algorithm performed in R-tree. </p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> PostGIS </tag>
<tag> PostgreSQL </tag>
<tag> R-tree </tag>
<tag> SQL </tag>
<tag> Data Structure and algorithm </tag>
</tags>
</entry>
<entry>
<title>Create time-series spatial databases With TimeScaleDB and PostGIS</title>
<link href="/2020-03-17-Create-time-series-spatial-databases-With-TimeScaleDB-and-PostGIS.html"/>
<url>/2020-03-17-Create-time-series-spatial-databases-With-TimeScaleDB-and-PostGIS.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction:"></a>1. Introduction:</h1><p>This post will go through some basic concepts and examples. All the experiments are running on the <code>Ubantu 18.04</code>, <code>TimeScaleDB</code> is a time-series plugin to PostgreSQL, all data storage are delegated to PostgreSQL, It provides index for fast retreiving and storing as well as time-series functions. </p><div> <img src="../images/timescale_db_01.png" alt="TimeScaleDB[figure 1]" width="90%" height="90%"></div><p>Similarly, <code>PostGIS</code> adds extra types (geometry, geography, raster and others) to the PostgreSQL database. It also adds functions and indexes that apply to those types. So, Those can be viewed as plugins to PostgreSQL. </p><p>Putting them together in PostgreSQL can create a time-series spatial databases. The Architecture can be in figure 2. </p><div> <img src="../images/timescale_db_02.jpg" alt="figure 2" width="90%" height="90%"></div><h1 id="2-TimeScaleDB"><a href="#2-TimeScaleDB" class="headerlink" title="2. TimeScaleDB"></a>2. TimeScaleDB</h1><p>In TimeScaleDB, data are automatically split into trunks by time vertically and space horizontally.</p><p>I will go over some key Concepts in the documents, If you want to know more about the Architecture of TimeScaleDB and its differences to other dbs, please refer to their <a href="https://docs.timescale.com/latest/introduction/architecture" target="_blank" rel="noopener">document</a></p><h2 id="2-1-Hypertable"><a href="#2-1-Hypertable" class="headerlink" title="2.1 Hypertable"></a>2.1 Hypertable</h2><p>The Hypertables are a combination of many individual tables holding the data. A hypertable is the abstraction of a single continuous table across all space and time intervals. A hypertable is defined by a standard schema with column names and types, with at least one column specifying a time value, and one (optional) column specifying an additional partitioning key. Indexes on time and partitioning key will be automatically created on hypertable. </p><h2 id="2-2-Chunk"><a href="#2-2-Chunk" class="headerlink" title="2.2 Chunk"></a>2.2 Chunk</h2><p>Each Hypertable is consist of many chunks, where every single chunk is actaully standard database table. Each chunk is corresponding to a specific time interval and a set of the partition key’s space. There is no overlapping between each chunks. </p><p>This shows the relationships between Hypertables and chunks.</p><div> <img src="../images/timescale_db_03.jpg" alt="figure 3" width="90%" height="90%"></div><h1 id="3-Installation"><a href="#3-Installation" class="headerlink" title="3. Installation"></a>3. Installation</h1><p>I installed postgreSQL and TimeScaleDB via <code>apt</code>.</p><h3 id="3-1-Install-ProgreSQL-11-0"><a href="#3-1-Install-ProgreSQL-11-0" class="headerlink" title="3.1 Install ProgreSQL 11.0"></a>3.1 Install ProgreSQL 11.0</h3><p>If you don’t have postgreSQL installed, add PostgreSQL’s third party repository to get the latest PostgreSQL packages. </p><div> <img src="../images/timescale_db_04.jpg" alt="figure 4" width="90%" height="90%"></div><p>Then, adding TimescaleDB’s third party repository and install TimescaleDB, which will download any dependencies it needs from the PostgreSQL repo.</p><div> <img src="../images/timescale_db_05.jpg" alt="figure 5" width="90%" height="90%"></div><p>The scripts are as follows:</p><pre class=" language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"deb http://apt.postgresql.org/pub/repos/apt/ <span class="token variable"><span class="token variable">$(</span>lsb_release -c -s<span class="token variable">)</span></span>-pgdg main"</span> <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">tee</span> /etc/apt/sources.list.d/pgdg.list<span class="token function">wget</span> --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc <span class="token operator">|</span> <span class="token function">sudo</span> apt-key add -<span class="token function">sudo</span> <span class="token function">apt-get</span> update<span class="token comment" spellcheck="true"># Add TimeScaleDB PPA</span><span class="token function">sudo</span> add-apt-repository ppa:timescale/timescaledb-ppa<span class="token function">sudo</span> <span class="token function">apt-get</span> update<span class="token comment" spellcheck="true"># Now install appropriate package for PG version</span><span class="token function">sudo</span> apt <span class="token function">install</span> timescaledb-postgresql-11</code></pre><p>After installing TimeScaleDB on your single machine, it is recommended to tune the db with the script.<br><code>sudo timescaledb-tune</code></p><div> <img src="../images/timescale_db_06.jpg" alt="figure 6" width="90%" height="90%"></div><h1 id="3-2-Connect-to-PostgreSQL-TimeScaleDB-and-Setup-Password"><a href="#3-2-Connect-to-PostgreSQL-TimeScaleDB-and-Setup-Password" class="headerlink" title="3.2 Connect to PostgreSQL/TimeScaleDB and Setup Password"></a>3.2 Connect to PostgreSQL/TimeScaleDB and Setup Password</h1><p>Let us connect to the database and create a database called tutorial to make sure PostgreSQL and TimeScaleDB is installed.</p><p>Connecting to the server by <code>psgl</code> tool, the super user <code>postgres</code> has no DB password set on Ubuntu OS by default. </p><div> <img src="../images/timescale_db_07.jpg" alt="figure 7" width="90%" height="90%"></div><p>You could set up your own password on your db.</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">ALTER</span> <span class="token keyword">USER</span> postgres PASSWORD <span class="token string">'yourPassWord'</span><span class="token punctuation">;</span></code></pre><p>After that you could use the command </p><pre class=" language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># Connect to PostgreSQL, using a superuser named 'postgres'</span>psql -U postgres -h localhost</code></pre><div> <img src="../images/timescale_db_08.jpg" alt="figure 8" width="90%" height="90%"></div><br><p>Now, we can connect to the database we have, and add TimeScaleDB plugin to it.</p><pre class=" language-sql"><code class="language-sql"><span class="token comment" spellcheck="true">-- Connect to the database</span>\<span class="token number">c</span> tutorial<span class="token comment" spellcheck="true">-- Extend the database with TimescaleDB</span><span class="token keyword">CREATE</span> EXTENSION <span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span> timescaledb <span class="token keyword">CASCADE</span><span class="token punctuation">;</span></code></pre><div> <img src="../images/timescale_db_09.jpg" alt="figure 9" width="90%" height="90%"></div><h1 id="3-3-PostGIS-Installation"><a href="#3-3-PostGIS-Installation" class="headerlink" title="3.3 PostGIS Installation"></a>3.3 PostGIS Installation</h1><p>We could install PostGIS with apt.</p><pre class=" language-bash"><code class="language-bash"><span class="token function">sudo</span> apt update<span class="token function">sudo</span> apt <span class="token function">install</span> postgis postgresql-11-postgis-2.5</code></pre><p>After installation, go to the tutorial database we have setted up and create postgis extesion on this db. </p><p>The result is shown in figure 10. </p><div> <img src="../images/timescale_db_10.jpg" alt="figure 10" width="90%" height="90%"></div><h1 id="4-Go-through-the-NYC-taxicab-data-in-offical-Document"><a href="#4-Go-through-the-NYC-taxicab-data-in-offical-Document" class="headerlink" title="4. Go through the NYC taxicab data in offical Document."></a>4. Go through the NYC taxicab data in offical Document.</h1><p>In the offical tutorial, they use Cloud version of TimscaleDB, it is similar to community version in this example. </p><p>Get and extract the dataset</p><pre><code>wget -O - https://timescaledata.blob.core.windows.net/datasets/nyc_data.tar.gz | tar xz</code></pre><p>It contains three files:</p><ul><li>nyc_data_contagg.sql: A SQL file for table schemes</li><li>nyc_data_rides.csv: CSV file contains the ride dataset.</li></ul><h2 id="4-1-Create-the-Database"><a href="#4-1-Create-the-Database" class="headerlink" title="4.1 Create the Database."></a>4.1 Create the Database.</h2><pre class=" language-sql"><code class="language-sql"><span class="token comment" spellcheck="true"># create the database called nyc_data</span><span class="token keyword">CREATE</span> <span class="token keyword">DATABASE</span> nyc_data<span class="token punctuation">;</span><span class="token comment" spellcheck="true"># connected to the nyc_data base.</span>\<span class="token number">c</span> nyc_data<span class="token comment" spellcheck="true"># Use TimeScale extension</span><span class="token keyword">CREATE</span> EXTENSION <span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span> timescaledb <span class="token keyword">CASCADE</span><span class="token punctuation">;</span></code></pre><h1 id="4-2-Define-scheme-and-Prepare-the-data"><a href="#4-2-Define-scheme-and-Prepare-the-data" class="headerlink" title="4.2 Define scheme and Prepare the data"></a>4.2 Define scheme and Prepare the data</h1><p>The dataset comes from New York City Taxi and Limousine Commission (NYC TLC). NYC TLC collects ride-specific data from every vehicle in its fleet, generating data from millions of rides every day. They connect rides data in the following way:</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token string">"rides"</span><span class="token punctuation">(</span> vendor_id <span class="token keyword">TEXT</span><span class="token punctuation">,</span> pickup_datetime <span class="token keyword">TIMESTAMP</span> WITHOUT TIME ZONE <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> dropoff_datetime <span class="token keyword">TIMESTAMP</span> WITHOUT TIME ZONE <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> passenger_count <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> trip_distance <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> pickup_longitude <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> pickup_latitude <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> rate_code <span class="token keyword">INTEGER</span><span class="token punctuation">,</span> dropoff_longitude <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> dropoff_latitude <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> payment_type <span class="token keyword">INTEGER</span><span class="token punctuation">,</span> fare_amount <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> extra <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> mta_tax <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> tip_amount <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> tolls_amount <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> improvement_surcharge <span class="token keyword">NUMERIC</span><span class="token punctuation">,</span> total_amount <span class="token keyword">NUMERIC</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h2 id="explanation"><a href="#explanation" class="headerlink" title="explanation"></a>explanation</h2><ul><li>Pickup date and time (as a timestamp)</li><li>Pickup location (latitude and longitude)</li><li>Drop off date and time (as a timestamp)</li><li>Drop off location (latitude and longitude)</li><li>Trip distance (in miles)</li><li>Fares (in USD)</li><li>Passenger count</li><li>Rate type (e.g, standard, airport, etc.)</li><li>Payment type (Cash, credit card, etc.)</li></ul><h2 id="Convert-to-hypertable"><a href="#Convert-to-hypertable" class="headerlink" title="Convert to hypertable"></a>Convert to hypertable</h2><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> create_hypertable<span class="token punctuation">(</span><span class="token string">'rides'</span><span class="token punctuation">,</span> <span class="token string">'pickup_datetime'</span><span class="token punctuation">,</span> <span class="token string">'payment_type'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> create_default_indexes<span class="token operator">=</span><span class="token operator">></span><span class="token boolean">FALSE</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span> rides <span class="token punctuation">(</span>vendor_id<span class="token punctuation">,</span> pickup_datetime <span class="token keyword">desc</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span> rides <span class="token punctuation">(</span>pickup_datetime <span class="token keyword">desc</span><span class="token punctuation">,</span> vendor_id<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span> rides <span class="token punctuation">(</span>rate_code<span class="token punctuation">,</span> pickup_datetime <span class="token keyword">DESC</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span> rides <span class="token punctuation">(</span>passenger_count<span class="token punctuation">,</span> pickup_datetime <span class="token keyword">desc</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>This means that converting rides table conditions to hypertable with time partitioning on <code>pickup_datetime</code> and space partitioning (2 partitions) on <code>payment_type</code> (using hashing), not creating default indexes on time&partitioning columns.</p><p>For details of how to create the hypertable, please refer to the documentation <a href="https://docs.timescale.com/latest/api#create_hypertable" target="_blank" rel="noopener">here</a></p><br><p>This script create the <code>payments_type</code> table and defines all acceptable payment_types.</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span> <span class="token string">"payment_types"</span><span class="token punctuation">(</span> payment_type <span class="token keyword">INTEGER</span><span class="token punctuation">,</span> description <span class="token keyword">TEXT</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> payment_types<span class="token punctuation">(</span>payment_type<span class="token punctuation">,</span> description<span class="token punctuation">)</span> <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'credit card'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'cash'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'no charge'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'dispute'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">'unknown'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">'voided trip'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><br>Similarly, rates code.<pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token keyword">IF</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span> <span class="token string">"rates"</span><span class="token punctuation">(</span> rate_code <span class="token keyword">INTEGER</span><span class="token punctuation">,</span> description <span class="token keyword">TEXT</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> rates<span class="token punctuation">(</span>rate_code<span class="token punctuation">,</span> description<span class="token punctuation">)</span> <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'standard rate'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'JFK'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'Newark'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'Nassau or Westchester'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">'negotiated fare'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">'group ride'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Use the following command to import schemes to the database.</p><pre class=" language-bash"><code class="language-bash"> psql -U postgres -d nyc_data -h localhost <span class="token operator"><</span> nyc_data.sql</code></pre><p>After that We could look at the structure of tables</p><ul><li>rides</li></ul><div> <img src="../images/timescale_db_11.jpg" width="90%" height="90%"></div><ul><li>rates</li></ul><div> <img src="../images/timescale_db_12.jpg" width="90%" height="90%"></div><ul><li>payment_types</li></ul><div> <img src="../images/timescale_db_13.jpg" width="90%" height="90%"></div><h2 id="4-2-Import-the-data-and-perform-the-query"><a href="#4-2-Import-the-data-and-perform-the-query" class="headerlink" title="4.2 Import the data and perform the query."></a>4.2 Import the data and perform the query.</h2><ul><li>Loading the data in the csv file to the database.</li></ul><div> <img src="../images/timescale_db_14.jpg" width="90%" height="90%"></div><h3 id="4-2-1-Simple-Query-task-How-many-rides-took-place-on-each-day"><a href="#4-2-1-Simple-Query-task-How-many-rides-took-place-on-each-day" class="headerlink" title="4.2.1 Simple Query task: How many rides took place on each day?"></a>4.2.1 Simple Query task: How many rides took place on each day?</h3><p>Since PostgreSQL fully support traditional SQL, all required is to run a simple sql to count the number of rides group by <code>pick_up_date</code> and order by it.</p><p>The sql scripts is as follows:</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> DATE_TRUNC<span class="token punctuation">(</span><span class="token string">'day'</span><span class="token punctuation">,</span> pickup_datetime<span class="token punctuation">)</span> <span class="token keyword">AS</span> day<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> daily_rides<span class="token keyword">FROM</span> rides <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> day<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> day <span class="token keyword">ASC</span></code></pre><p>The result is shown in figure 15. </p><div> <img src="../images/timescale_db_15.jpg" width="90%" height="90%"></div><h3 id="4-2-2-Simple-Query-task-2-How-many-rides-took-place-on-each-day"><a href="#4-2-2-Simple-Query-task-2-How-many-rides-took-place-on-each-day" class="headerlink" title="4.2.2 Simple Query task 2: How many rides took place on each day?"></a>4.2.2 Simple Query task 2: How many rides took place on each day?</h3><p>What’s the average individual passenger travelling fare before 2016-01-08.</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> date_trunc<span class="token punctuation">(</span><span class="token string">'day'</span><span class="token punctuation">,</span> pickup_datetime<span class="token punctuation">)</span> <span class="token keyword">AS</span> day<span class="token punctuation">,</span> <span class="token function">avg</span><span class="token punctuation">(</span>fare_amount<span class="token punctuation">)</span><span class="token keyword">FROM</span> rides<span class="token keyword">WHERE</span> passenger_count <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">AND</span> pickup_datetime <span class="token operator"><</span> <span class="token string">'2016-01-08'</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> day <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> day<span class="token punctuation">;</span></code></pre><p>We can see how the sql script executed by adding <code>EXPLAIN</code> keyword.</p><div> <img src="../images/timescale_db_16.jpg" width="100%" height="90%"></div><p>From the result shown in Figure 16, we can see that it first parellelly scan chunks partationed by time range with filter on passenger count and datetime. Then, Group by day and sorting/aggregating the result.</p><h3 id="4-2-3-Simple-Query-Task-3-How-many-rides-took-place-for-each-type-of-rides-in-Jan-2016"><a href="#4-2-3-Simple-Query-Task-3-How-many-rides-took-place-for-each-type-of-rides-in-Jan-2016" class="headerlink" title="4.2.3 Simple Query Task 3: How many rides took place for each type of rides in Jan, 2016 ?"></a>4.2.3 Simple Query Task 3: How many rides took place for each type of rides in Jan, 2016 ?</h3><ul><li>sql</li></ul><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> rates<span class="token punctuation">.</span>description<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>rides<span class="token punctuation">.</span>vendor_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> num_trips<span class="token keyword">FROM</span> rides<span class="token keyword">JOIN</span> rates <span class="token keyword">ON</span> rides<span class="token punctuation">.</span>rate_code <span class="token operator">=</span> rates<span class="token punctuation">.</span>rate_code<span class="token keyword">WHERE</span> pickup_datetime <span class="token operator"><</span> <span class="token string">'2016-02-01'</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> rates<span class="token punctuation">.</span>description<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> rates<span class="token punctuation">.</span>description<span class="token punctuation">;</span></code></pre><ul><li>query result </li></ul><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> rates<span class="token punctuation">.</span>description<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>rides<span class="token punctuation">.</span>vendor_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> num_trips<span class="token keyword">FROM</span> rides<span class="token keyword">JOIN</span> rates <span class="token keyword">ON</span> rides<span class="token punctuation">.</span>rate_code <span class="token operator">=</span> rates<span class="token punctuation">.</span>rate_code<span class="token keyword">WHERE</span> pickup_datetime <span class="token operator"><</span> <span class="token string">'2016-02-01'</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> rates<span class="token punctuation">.</span>description<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> rates<span class="token punctuation">.</span>description<span class="token punctuation">;</span></code></pre><h3 id="4-2-4-Time-series-Specail-Query-Task-How-many-rides-took-place-every-5-minutes-for-the-first-day-of-2016"><a href="#4-2-4-Time-series-Specail-Query-Task-How-many-rides-took-place-every-5-minutes-for-the-first-day-of-2016" class="headerlink" title="4.2.4 Time-series Specail Query Task : How many rides took place every 5 minutes for the first day of 2016?"></a>4.2.4 Time-series Specail Query Task : How many rides took place every 5 minutes for the first day of 2016?</h3><p>In vanalia PostgreSQL, What we only have is the <code>DATE_TRUNC</code> function, however if we want to measure the data on arbitary time-interval, <code>time_bucket</code> defined in PostgreSQL allows for arbitary interval , rather than standard minute, hour, day…</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> time_bucket<span class="token punctuation">(</span><span class="token string">'5 minute'</span><span class="token punctuation">,</span> pickup_datetime<span class="token punctuation">)</span> <span class="token keyword">AS</span> five_min<span class="token punctuation">,</span> <span class="token function">count</span><span class="token punctuation">(</span>vendor_id<span class="token punctuation">)</span><span class="token keyword">FROM</span> rides<span class="token keyword">WHERE</span> pickup_datetime <span class="token operator"><</span> <span class="token string">'2016-01-02 00:00'</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> five_min<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> five_min<span class="token keyword">LIMIT</span> <span class="token number">12</span><span class="token punctuation">;</span></code></pre><div> <img src="../images/timescale_db_18.jpg" width="100%" height="90%"></div><h3 id="4-2-5-Time-series-spatial-Query-Task-How-many-rides-on-New-Year’s-morning-originated-from-within-400m-of-Times-Square-in-30-minute-buckets"><a href="#4-2-5-Time-series-spatial-Query-Task-How-many-rides-on-New-Year’s-morning-originated-from-within-400m-of-Times-Square-in-30-minute-buckets" class="headerlink" title="4.2.5 Time-series spatial Query Task: How many rides on New Year’s morning originated from within 400m of Times Square, in 30 minute buckets?"></a>4.2.5 Time-series spatial Query Task: How many rides on New Year’s morning originated from within 400m of Times Square, in 30 minute buckets?</h3><p>This requires that we make use of the pickup latitude and longitude columns in our rides hypertable. To use the pickup location, we will need to get our hypertable ready for geospatial queries.</p><p>We could use the PostGIS, projecting lontitude and latitude with US National Atlas Equal Area - EPSG:2163 standard. <a href="https://postgis.net/docs/ST_Distance.html" target="_blank" rel="noopener">More Information</a></p><p>The sql script is as follows: </p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> EXTENSION postgis<span class="token punctuation">;</span><span class="token comment" spellcheck="true">-- Create geometry columns for each of our (lat,long) points</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> rides <span class="token keyword">ADD</span> <span class="token keyword">COLUMN</span> pickup_geom <span class="token keyword">geometry</span><span class="token punctuation">(</span><span class="token keyword">POINT</span><span class="token punctuation">,</span><span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> rides <span class="token keyword">ADD</span> <span class="token keyword">COLUMN</span> dropoff_geom <span class="token keyword">geometry</span><span class="token punctuation">(</span><span class="token keyword">POINT</span><span class="token punctuation">,</span><span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><ul><li>Convert the latitude and longitude points into geometry coordinates (units in meters)</li></ul><pre class=" language-sql"><code class="language-sql"><span class="token keyword">UPDATE</span> rides <span class="token keyword">SET</span> pickup_geom <span class="token operator">=</span> ST_Transform<span class="token punctuation">(</span>ST_SetSRID<span class="token punctuation">(</span>ST_MakePoint<span class="token punctuation">(</span>pickup_longitude<span class="token punctuation">,</span>pickup_latitude<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">4326</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">UPDATE</span> rides <span class="token keyword">SET</span> dropoff_geom <span class="token operator">=</span> ST_Transform<span class="token punctuation">(</span>ST_SetSRID<span class="token punctuation">(</span>ST_MakePoint<span class="token punctuation">(</span>dropoff_longitude<span class="token punctuation">,</span>dropoff_latitude<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">4326</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><br><p>Times Square is located at (<code>lat</code>, <code>lon</code>) (<code>40.7589</code>,<code>-73.9851</code>).</p><p>Up till now, we have all the information to perform the query: How many rides on New Year’s morning originated from within 400m of Times Square, in 30 minute buckets? </p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> time_bucket<span class="token punctuation">(</span><span class="token string">'30 minutes'</span><span class="token punctuation">,</span> pickup_datetime<span class="token punctuation">)</span> <span class="token keyword">AS</span> thirty_min<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> near_times_sq<span class="token keyword">FROM</span> rides<span class="token keyword">WHERE</span> ST_Distance<span class="token punctuation">(</span>pickup_geom<span class="token punctuation">,</span> ST_Transform<span class="token punctuation">(</span>ST_SetSRID<span class="token punctuation">(</span>ST_MakePoint<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">73.9851</span><span class="token punctuation">,</span><span class="token number">40.7589</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">4326</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">2163</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">400</span><span class="token operator">AND</span> pickup_datetime <span class="token operator"><</span> <span class="token string">'2016-01-01 13:00'</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> thirty_min<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> thirty_min<span class="token punctuation">;</span></code></pre><div> <img src="../images/timescale_db_19.jpg" width="100%" height="90%"></div><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> SQL, PostgreSQL, Time-series Database, spatial databases, data storage </tag>
</tags>
</entry>
<entry>
<title>Learning SQL BY Task Series (1) Movie Rating</title>
<link href="/2020-03-10-rating.html"/>
<url>/2020-03-10-rating.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Problem-Description"><a href="#1-Problem-Description" class="headerlink" title="1. Problem Description:"></a>1. Problem Description:</h1><p>You are given serveral tables as follows: </p><ol><li>Table: <code>Movies</code></li></ol><pre class=" language-bash"><code class="language-bash">+---------------+---------+<span class="token operator">|</span> Column Name <span class="token operator">|</span> Type <span class="token operator">|</span>+---------------+---------+<span class="token operator">|</span> movie_id <span class="token operator">|</span> int <span class="token operator">|</span><span class="token operator">|</span> title <span class="token operator">|</span> varchar <span class="token operator">|</span>+---------------+---------+movie_id is the primary key <span class="token keyword">for</span> this table.title is the name of the movie.</code></pre><ol start="2"><li>Table: <code>Users</code></li></ol><pre class=" language-bash"><code class="language-bash">+---------------+---------+<span class="token operator">|</span> Column Name <span class="token operator">|</span> Type <span class="token operator">|</span>+---------------+---------+<span class="token operator">|</span> user_id <span class="token operator">|</span> int <span class="token operator">|</span><span class="token operator">|</span> name <span class="token operator">|</span> varchar <span class="token operator">|</span>+---------------+---------+user_id is the primary key <span class="token keyword">for</span> this table.</code></pre><ol start="3"><li>Table: <code>Movie_Rating</code></li></ol><pre class=" language-bash"><code class="language-bash">+---------------+---------+<span class="token operator">|</span> Column Name <span class="token operator">|</span> Type <span class="token operator">|</span>+---------------+---------+<span class="token operator">|</span> movie_id <span class="token operator">|</span> int <span class="token operator">|</span><span class="token operator">|</span> user_id <span class="token operator">|</span> int <span class="token operator">|</span><span class="token operator">|</span> rating <span class="token operator">|</span> int <span class="token operator">|</span><span class="token operator">|</span> created_at <span class="token operator">|</span> <span class="token function">date</span> <span class="token operator">|</span>+---------------+---------+<span class="token punctuation">(</span>movie_id, user_id<span class="token punctuation">)</span> is the primary key <span class="token keyword">for</span> this table.This table contains the rating of a movie by a user <span class="token keyword">in</span> their review.created_at is the user ' s review date. </code></pre><p>Your task is to write the Query Scripts to complete two subtasks:</p><ul><li><p>Find the name of the user who has rated <strong>the greatest number of the movies</strong>.<br>In case of a tie, return <strong>lexicographically smaller</strong> user name.</p></li><li><p>Find the movie name with <strong>the highest average rating in February 2020</strong>.<br>In case of a tie, return <strong>lexicographically smaller</strong> movie name.</p></li></ul><h1 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h1><pre class=" language-bash"><code class="language-bash">Movies table:+-------------+--------------+<span class="token operator">|</span> movie_id <span class="token operator">|</span> title <span class="token operator">|</span>+-------------+--------------+<span class="token operator">|</span> 1 <span class="token operator">|</span> Avengers <span class="token operator">|</span><span class="token operator">|</span> 2 <span class="token operator">|</span> Frozen 2 <span class="token operator">|</span><span class="token operator">|</span> 3 <span class="token operator">|</span> Joker <span class="token operator">|</span>+-------------+--------------+Users table:+-------------+--------------+<span class="token operator">|</span> user_id <span class="token operator">|</span> name <span class="token operator">|</span>+-------------+--------------+<span class="token operator">|</span> 1 <span class="token operator">|</span> Daniel <span class="token operator">|</span><span class="token operator">|</span> 2 <span class="token operator">|</span> Monica <span class="token operator">|</span><span class="token operator">|</span> 3 <span class="token operator">|</span> Maria <span class="token operator">|</span><span class="token operator">|</span> 4 <span class="token operator">|</span> James <span class="token operator">|</span>+-------------+--------------+Movie_Rating table:+-------------+--------------+--------------+-------------+<span class="token operator">|</span> movie_id <span class="token operator">|</span> user_id <span class="token operator">|</span> rating <span class="token operator">|</span> created_at <span class="token operator">|</span>+-------------+--------------+--------------+-------------+<span class="token operator">|</span> 1 <span class="token operator">|</span> 1 <span class="token operator">|</span> 3 <span class="token operator">|</span> 2020-01-12 <span class="token operator">|</span><span class="token operator">|</span> 1 <span class="token operator">|</span> 2 <span class="token operator">|</span> 4 <span class="token operator">|</span> 2020-02-11 <span class="token operator">|</span><span class="token operator">|</span> 1 <span class="token operator">|</span> 3 <span class="token operator">|</span> 2 <span class="token operator">|</span> 2020-02-12 <span class="token operator">|</span><span class="token operator">|</span> 1 <span class="token operator">|</span> 4 <span class="token operator">|</span> 1 <span class="token operator">|</span> 2020-01-01 <span class="token operator">|</span><span class="token operator">|</span> 2 <span class="token operator">|</span> 1 <span class="token operator">|</span> 5 <span class="token operator">|</span> 2020-02-17 <span class="token operator">|</span> <span class="token operator">|</span> 2 <span class="token operator">|</span> 2 <span class="token operator">|</span> 2 <span class="token operator">|</span> 2020-02-01 <span class="token operator">|</span> <span class="token operator">|</span> 2 <span class="token operator">|</span> 3 <span class="token operator">|</span> 2 <span class="token operator">|</span> 2020-03-01 <span class="token operator">|</span><span class="token operator">|</span> 3 <span class="token operator">|</span> 1 <span class="token operator">|</span> 3 <span class="token operator">|</span> 2020-02-22 <span class="token operator">|</span> <span class="token operator">|</span> 3 <span class="token operator">|</span> 2 <span class="token operator">|</span> 4 <span class="token operator">|</span> 2020-02-25 <span class="token operator">|</span> +-------------+--------------+--------------+-------------+Result table:+--------------+<span class="token operator">|</span> results <span class="token operator">|</span>+--------------+<span class="token operator">|</span> Daniel <span class="token operator">|</span><span class="token operator">|</span> Frozen 2 <span class="token operator">|</span>+--------------+</code></pre><p>Daniel and Maria have rated 3 movies (“Avengers”, “Frozen 2” and “Joker”) but <strong><em>Daniel</em></strong> is smaller lexicographically.<br>Frozen 2 and Joker have a rating average of 3.5 in February but <strong><em>Frozen 2</em></strong> is smaller lexicographically.</p><h1 id="Prepared-SQL-KnowLedge"><a href="#Prepared-SQL-KnowLedge" class="headerlink" title="Prepared SQL KnowLedge"></a>Prepared SQL KnowLedge</h1><ul><li><p>Since we need to write two indepent qeury and union each result to get the result table. In SQL, There are two union operators,which are<br><code>UNION</code> and <code>UNION ALL</code>, They all combines the result set of two or more SELECT statements. However, <code>UNION ALL</code> allows duplicates rows, while <code>UNION</code> will only contains distinct values. <a href="https://www.w3schools.com/sql/sql_ref_union.asp" target="_blank" rel="noopener">Reference</a></p></li><li><p>In this subtask 1, we should first sort by the number of ratings and if any two are the same, we should sort by its user name lexicographically. So, In order to sort in this way, Order cause should be written as follows:</p></li></ul><br><pre class=" language-SQL"><code class="language-SQL"> # ORDER BY column1 DESC, column2 This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two or more rows are equal.</code></pre><h1 id="Solutions"><a href="#Solutions" class="headerlink" title="Solutions:"></a>Solutions:</h1><ol><li>For the first task, we could <code>GROUP BY</code> the user_id, and <code>COUNT</code> the number of movies (represented by movie_id) which the user rated. Sort by the number, and its lexicographically smaller user name. USE <code>LIMIT</code> to get the first record. </li></ol><p><strong><em> Query script for your reference.</em></strong></p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> u<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>movie_id<span class="token punctuation">)</span> <span class="token keyword">as</span> max_rating_num<span class="token keyword">FROM</span> Movie_Rating <span class="token keyword">AS</span> m<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> Users <span class="token keyword">AS</span> u<span class="token keyword">ON</span> m<span class="token punctuation">.</span>user_id <span class="token operator">=</span> u<span class="token punctuation">.</span>user_id<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> m<span class="token punctuation">.</span>user_id<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> max_rating_num <span class="token keyword">DESC</span><span class="token punctuation">,</span>u<span class="token punctuation">.</span>name<span class="token keyword">LIMIT</span> <span class="token number">1</span> </code></pre><ol start="2"><li>For the second task, its quite similar to the first query. <strong><em>Notice</em></strong> that Since what we really concern about is the ratings in February 2020. So Before we group the rows and compute the <code>AVE</code>, we should filter the dta in February 2020 by <code>WHERE</code>CAUSE. Still, the moving_rating table is the primary table, so we use the left join with the movie table to get its name. </li></ol><p><strong><em> Query script for your reference.</em></strong> </p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> m<span class="token punctuation">.</span>title<span class="token punctuation">,</span> <span class="token function">AVG</span><span class="token punctuation">(</span>r<span class="token punctuation">.</span>rating<span class="token punctuation">)</span> <span class="token keyword">as</span> movie_rating <span class="token keyword">FROM</span> Movie_Rating <span class="token keyword">AS</span> r<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> Movies <span class="token keyword">AS</span> m<span class="token keyword">ON</span> m<span class="token punctuation">.</span>movie_id <span class="token operator">=</span> r<span class="token punctuation">.</span>movie_id<span class="token keyword">WHERE</span> MONTH<span class="token punctuation">(</span>r<span class="token punctuation">.</span>created_at<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">AND</span> YEAR<span class="token punctuation">(</span>r<span class="token punctuation">.</span>created_at<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">2020</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> r<span class="token punctuation">.</span>movie_id<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> movie_rating <span class="token keyword">DESC</span><span class="token punctuation">,</span> m<span class="token punctuation">.</span>title<span class="token keyword">LIMIT</span> <span class="token number">1</span></code></pre><h1 id="Final-Answer"><a href="#Final-Answer" class="headerlink" title="Final Answer:"></a>Final Answer:</h1><p>Combination with <code>UNION</code> keyword.</p><pre class=" language-sql"><code class="language-sql"><span class="token punctuation">(</span><span class="token keyword">SELECT</span> u<span class="token punctuation">.</span>name <span class="token keyword">AS</span> results<span class="token keyword">FROM</span> Movie_Rating <span class="token keyword">AS</span> m<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> Users <span class="token keyword">AS</span> u<span class="token keyword">ON</span> m<span class="token punctuation">.</span>user_id <span class="token operator">=</span> u<span class="token punctuation">.</span>user_id<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> m<span class="token punctuation">.</span>user_id<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>movie_id<span class="token punctuation">)</span> <span class="token keyword">DESC</span><span class="token punctuation">,</span>u<span class="token punctuation">.</span>name<span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token keyword">UNION</span><span class="token punctuation">(</span><span class="token keyword">SELECT</span> m<span class="token punctuation">.</span>title <span class="token keyword">AS</span> results<span class="token keyword">FROM</span> Movie_Rating <span class="token keyword">AS</span> r<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> Movies <span class="token keyword">AS</span> m<span class="token keyword">ON</span> m<span class="token punctuation">.</span>movie_id <span class="token operator">=</span> r<span class="token punctuation">.</span>movie_id<span class="token keyword">WHERE</span> MONTH<span class="token punctuation">(</span>r<span class="token punctuation">.</span>created_at<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">AND</span> YEAR<span class="token punctuation">(</span>r<span class="token punctuation">.</span>created_at<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">2020</span><span class="token keyword">GROUP</span> <span class="token keyword">BY</span> r<span class="token punctuation">.</span>movie_id<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token function">AVG</span><span class="token punctuation">(</span>r<span class="token punctuation">.</span>rating<span class="token punctuation">)</span> <span class="token keyword">DESC</span><span class="token punctuation">,</span> m<span class="token punctuation">.</span>title<span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">)</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> SQL </tag>
</tags>
</entry>
<entry>
<title>Learning Netty from scratch - 1. Blocking, Non-Blocking && Synchronous, Asynchronous</title>
<link href="/2019-08-10-Learning-Netty-stage-1.html"/>
<url>/2019-08-10-Learning-Netty-stage-1.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Let’s try to understand these 4 concept by an example. </p><p>Suppose, You want to boil some water. Suppose you are Process A, and the kettle which can boil water is Process B. The task here is that you would like to boil water by your kettle.</p><p>Synchronous : You have a super old-styled kettle so that it will not notice you when it finishes the task, you have to observe it all the time to make sure everything works fine. In other words. <em><em> You have Processs A calls B, B will not notify A until it completes.</em></em> </p><p>Asynchronous: Nowadays, Kettles will automatically have someway to notify you when they accomplish the task. In other words, ** When A calls B, B will immediately return A an message shows that B have recived the task.After B finish the task, B will notify A in someway.</p><p>The biggest difference between synchronous and asynchronous is that <em><em> how the callee returns the result </em></em> . If the callee return the result until it finish the whole request from the caller, this is synchronous. And, if the callee return a notice to the caller once it receive the request and after it completes, it try to nofity the caller, this is asychronize.</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
</entry>
<entry>
<title>LeetCode 863 All nodes distance k in binary tree</title>
<link href="/2019-07-16-all-nodes-distance-k-in-binary-tree.html"/>
<url>/2019-07-16-all-nodes-distance-k-in-binary-tree.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Problem-Description"><a href="#1-Problem-Description" class="headerlink" title="1. Problem Description"></a>1. Problem Description</h1><p>We are given a binary tree (with root node root), a target node, and an integer value K.</p><p>Return a list of the values of all nodes that have a distance K from the target node. The answer can be returned in any order.</p><h2 id="Example"><a href="#Example" class="headerlink" title="Example"></a>Example</h2><pre><code>Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2Output: [7,4,1]Explanation: The nodes that are a distance 2 from the target node (with value 5)have values 7, 4, and 1.</code></pre><div> <img src="../images/leetcode_863.png" alt="Smiley face" width="35%" height="35%"></div><br><h1 id="2-My-Thought-About-This-Problem"><a href="#2-My-Thought-About-This-Problem" class="headerlink" title="2. My Thought About This Problem:"></a>2. My Thought About This Problem:</h1><p>At first glance, this problem is a Tree Problem. However, think a little bit more, <em><em> tree is a special kind of Undirected Graph<strong>. Each Node in tree has **at most three edges</strong>, one for left child, one for right child, and other one for its parent. **A small trick</em></em> here is that the parent of root is root itself. The parent information can be stored in a Map when traversing the tree once. </p><p>After building all the relationships, the rest of work is to find Distance K nodes from the target node. This can be traversed level by level. This type of search is typically breath-first search. To prevent traverse back to the nodes we have traversed in previous step. We need another additional data structure which is the set to keep track of those nodes which have been traversed before.</p><p>You could pratice on Leetcode online IDE <a href="https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/" target="_blank" rel="noopener">here</a>.</p><p>Code</p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */</span><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> <span class="token function">distanceK</span><span class="token punctuation">(</span>TreeNode root<span class="token punctuation">,</span> TreeNode target<span class="token punctuation">,</span> <span class="token keyword">int</span> K<span class="token punctuation">)</span><span class="token punctuation">{</span> Map<span class="token operator"><</span>TreeNode<span class="token punctuation">,</span> TreeNode<span class="token operator">></span> parentsMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">buildMap</span><span class="token punctuation">(</span>root<span class="token punctuation">,</span> root<span class="token punctuation">,</span>parentsMap<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">findKdistanceNode</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> K<span class="token punctuation">,</span> parentsMap<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* * Traverse the tree in DFS way and record every node's parent. */</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">buildMap</span><span class="token punctuation">(</span>TreeNode node<span class="token punctuation">,</span> TreeNode parent<span class="token punctuation">,</span> Map<span class="token operator"><</span>TreeNode<span class="token punctuation">,</span> TreeNode<span class="token operator">></span> parentsMap<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>node <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> parentsMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>node<span class="token punctuation">,</span>parent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>left <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token function">buildMap</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>left<span class="token punctuation">,</span> node<span class="token punctuation">,</span> parentsMap<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>right <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token function">buildMap</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>right<span class="token punctuation">,</span> node<span class="token punctuation">,</span> parentsMap<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* * Given the target node and distance K, find the K distance Nodes. */</span> <span class="token keyword">private</span> <span class="token keyword">static</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> <span class="token function">findKdistanceNode</span><span class="token punctuation">(</span>TreeNode target<span class="token punctuation">,</span> <span class="token keyword">int</span> K<span class="token punctuation">,</span> Map<span class="token operator"><</span>TreeNode<span class="token punctuation">,</span> TreeNode<span class="token operator">></span> parentsMap<span class="token punctuation">)</span><span class="token punctuation">{</span> Set<span class="token operator"><</span>TreeNode<span class="token operator">></span> traversed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Deque<span class="token operator"><</span>TreeNode<span class="token operator">></span> queue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayDeque</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span> traversed<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> level <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> level<span class="token operator"><</span> K<span class="token punctuation">;</span> level<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> queueSize <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">;</span> idx <span class="token operator"><</span> queueSize<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// traverse all its parents. </span> TreeNode curNode <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">traverseAndMark</span><span class="token punctuation">(</span>curNode<span class="token punctuation">.</span>left<span class="token punctuation">,</span> traversed<span class="token punctuation">,</span> queue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">traverseAndMark</span><span class="token punctuation">(</span>curNode<span class="token punctuation">.</span>right<span class="token punctuation">,</span> traversed<span class="token punctuation">,</span> queue<span class="token punctuation">)</span><span class="token punctuation">;</span> TreeNode curNodeParent <span class="token operator">=</span> parentsMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>curNode<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">traverseAndMark</span><span class="token punctuation">(</span>curNodeParent<span class="token punctuation">,</span> traversed<span class="token punctuation">,</span> queue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//convert to the required result format.</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> resultList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>node <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> resultList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>node<span class="token punctuation">.</span>val<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> resultList<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* * Traverse those nodes which have not been visited and add to queue. */</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">traverseAndMark</span><span class="token punctuation">(</span>TreeNode node<span class="token punctuation">,</span> Set<span class="token operator"><</span>TreeNode<span class="token operator">></span> traversed<span class="token punctuation">,</span>Deque<span class="token operator"><</span>TreeNode<span class="token operator">></span> queue<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>node <span class="token operator">!=</span> null <span class="token operator">&&</span> <span class="token operator">!</span>traversed<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> traversed<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span><span class="token punctuation">;</span> queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Recursion </tag>
<tag> Data Structure and Algorithm </tag>
<tag> Tree </tag>
<tag> Undirected Graph </tag>
<tag> Breath-First Search(BFS) </tag>
<tag> Queue </tag>
</tags>
</entry>
<entry>
<title>Leetcode 1104 Path in Zigzag Labelled Binary Tree</title>
<link href="/2019-07-02-pathInZigZagLabelledBinaryTree.html"/>
<url>/2019-07-02-pathInZigZagLabelledBinaryTree.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Problem-Description"><a href="#1-Problem-Description" class="headerlink" title="1. Problem Description"></a>1. Problem Description</h1><p>In an infinite <strong>binary tree</strong> where <strong>every node has two children</strong>, the nodes are labelled in row order.</p><p>In the <strong>odd</strong> numbered rows (ie., the first, third, fifth,…), the labelling is <strong>left to right</strong>, while in the <strong>even</strong> numbered rows (second, fourth, sixth,…), the labelling is <strong>right to left</strong>.</p><div> <img src="../images/tree.png" alt="Smiley face" width="50%" height="50%"></div><br><p><strong>Challenge:</strong> </p><p>Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.</p><p><strong>Example 1:</strong></p><pre><code>Input: label = 14Output: [1,3,4,14]</code></pre><p><strong>Example 2:</strong></p><pre><code>Input: label = 26Output: [1,2,6,10,26]</code></pre><h1 id="2-My-Thought-About-This-Problem"><a href="#2-My-Thought-About-This-Problem" class="headerlink" title="2. My Thought About This Problem:"></a>2. My Thought About This Problem:</h1><p>Tree data structure itself is recursive, Hence, Many problems regarding to tree can be solved recursively. In this problem, we could think that in order to find the path of the label of a node, how about <strong>finding the label of its parent node in the first place</strong>. If we can do that, the problem can be solved recursively, until the root node which has label of 1. </p><h1 id="3-Difficulty-find-the-label-of-its-parent-node"><a href="#3-Difficulty-find-the-label-of-its-parent-node" class="headerlink" title="3. Difficulty: find the label of its parent node"></a>3. Difficulty: find the label of its parent node</h1><p>The tough thing here is to find the label of its parent node. What conditions we have not used yet? Well, the binary tree is complete, and the trees label is in ZigZag Order. </p><p>The tree is binary and complete:</p><ol><li><p>Given the label, we could compute the depth of the tree by taking the logrithm of two. Let us say the result is k. Then, we also know the range of that level, which is<br>[$2^k$, $2^{k+1}$-1].</p></li><li><p>According to ZigZag property, we know that <strong>the largest element in the previous layer follows by the smallest element of the current layer</strong>.Therefore, we know that the smallest element in the previous layer is $2^k$ - 1. </p></li></ol><p>3.If we could know the difference between the smallest element in the previous and its parent, then we can derive its parent label ! </p><ol start="4"><li>Now we only have one condition haven’t applied, which is the tree is <strong>complete</strong>. Recall that, <strong>Heap</strong> itseulf is a complete binary tree but it can be stored in an array. The parent of a node with index <em>i</em> in heap is array[i/2]. The offset of current node divides two is the difference we want.</li></ol><p>By previous analysis, the code is relatively easy to write.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> <span class="token function">pathInZigZagTree</span><span class="token punctuation">(</span><span class="token keyword">int</span> label<span class="token punctuation">)</span><span class="token punctuation">{</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">bottom2upTraverse</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> label<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> list<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">bottom2upTraverse</span><span class="token punctuation">(</span>List<span class="token operator"><</span>Integer<span class="token operator">></span> list<span class="token punctuation">,</span> <span class="token keyword">int</span> label<span class="token punctuation">)</span><span class="token punctuation">{</span> list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>label<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>label <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// get the level of the label in the tree;</span> <span class="token keyword">int</span> level <span class="token operator">=</span> <span class="token function">log2</span><span class="token punctuation">(</span>label<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// get the index of the label on its level</span> <span class="token keyword">int</span> idx <span class="token operator">=</span> label <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span>level<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// according to ZigZag Tree property, find the previous label to traverse;</span> <span class="token keyword">int</span> nextLabel <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span>level<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> idx<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// traverse recursively.</span> <span class="token function">bottom2upTraverse</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span>nextLabel<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">log2</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">/</span> Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Recursion </tag>
<tag> Data Structure and Algorithm </tag>
<tag> Complete Binary Tree </tag>
<tag> ZigZag </tag>
<tag> Math </tag>
</tags>
</entry>
<entry>
<title>Leetcode problem 1105 filling-bookcase-shelves</title>
<link href="/2019-07-02-filling-bookcase-shelves.html"/>
<url>/2019-07-02-filling-bookcase-shelves.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Problem-Description"><a href="#Problem-Description" class="headerlink" title="Problem Description"></a>Problem Description</h1><p>We have a sequence of books: the i-th book has thickness books[i][0] and height books[i][1].</p><p>We want to place these books <strong>in order</strong> onto bookcase shelves that have total width shelf_width.</p><p>We choose some of the books to place on this shelf (such that the sum of their thickness is <= shelf_width), then build another level of shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.</p><p>Note again that at each step of the above process, <strong><strong>the order of the books we place is the same order as the given sequence of books</strong></strong>. For example, if we have an ordered list of 5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.</p><p>Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.</p><p>Example: </p><div> <img src="../images/shelves.png" alt="Smiley face" width="30%" height="30%"></div><br><pre><code>Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4Output: 6Explanation:The sum of the heights of the 3 shelves are 1 + 3 + 2 = 6.Notice that book number 2 does not have to be on the first shelf.</code></pre><p>The full description of this problem can be viewed on Leetcode platform <a href="https://leetcode.com/problems/filling-bookcase-shelves/" target="_blank" rel="noopener">filling book case shelves</a></p><h1 id="How-to-solve-this-problem"><a href="#How-to-solve-this-problem" class="headerlink" title="How to solve this problem ?"></a>How to solve this problem ?</h1><p>The trival way to think this problem is explore all the combination of books and find the optimal solution(brute-fore way). That is going to be exponential time complexity which is not desirable.</p><p>Notice there is one important constraint which is <strong><strong>the order of the books we place is the same order as the given sequence of books</strong></strong>. This means that what we can do is only to place books <strong>in order</strong>. Then, another solution may occur,we could use backtracking algorithm with branch and bound. To be specific, we could treat all the combinations of placement of books as a recursion tree. We could memorize some previous states for later traverse. Traversing the recursion tree is going to be in the manner of depth-first search. </p><p>The codeo of backtracking way for solving this problem is as follows:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//using cache to memorize intermediate states</span> <span class="token comment" spellcheck="true">//1st-dim : number of books + 1;</span> <span class="token comment" spellcheck="true">//2ed-dim : number of shelf_width+1</span> <span class="token keyword">private</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cache<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">minHeightShelves</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> books<span class="token punctuation">,</span> <span class="token keyword">int</span> shelf_width<span class="token punctuation">)</span> <span class="token punctuation">{</span> cache <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>books<span class="token punctuation">.</span>length<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>shelf_width<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">dfs</span><span class="token punctuation">(</span>books<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> shelf_width<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> shelf_width<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* * @parameters: * cur: cur bookIdx * widthLeft: left width for the level where cur book is * curHeight: current height at the layer of cur book */</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> books<span class="token punctuation">,</span> <span class="token keyword">int</span> cur<span class="token punctuation">,</span> <span class="token keyword">int</span> widthLeft<span class="token punctuation">,</span> <span class="token keyword">int</span> curHeight<span class="token punctuation">,</span> <span class="token keyword">int</span> shelf_width<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// if we have explore all books.</span> <span class="token keyword">if</span><span class="token punctuation">(</span>cur<span class="token operator">==</span>books<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> curHeight<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// if we have traversd the state before , just return. </span> <span class="token keyword">if</span><span class="token punctuation">(</span>cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span><span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span> cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Option one: </span> <span class="token comment" spellcheck="true">// No matter whether the curretn layer if full, we could place the current book in the next layer , and explore </span> cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span> <span class="token operator">=</span> curHeight <span class="token operator">+</span> <span class="token function">dfs</span><span class="token punctuation">(</span>books<span class="token punctuation">,</span>cur<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>shelf_width<span class="token operator">-</span>books<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>books<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>shelf_width<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Option two:</span> <span class="token comment" spellcheck="true">// if the current layer can afford that book. explore with new widthLeft, and </span> <span class="token comment" spellcheck="true">// update current height.</span> <span class="token keyword">if</span><span class="token punctuation">(</span>widthLeft<span class="token operator">>=</span>books<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span> <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token function">dfs</span><span class="token punctuation">(</span>books<span class="token punctuation">,</span>cur<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>widthLeft<span class="token operator">-</span>books<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>curHeight<span class="token punctuation">,</span>books<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>shelf_width<span class="token punctuation">)</span><span class="token punctuation">,</span>cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> cache<span class="token punctuation">[</span>cur<span class="token punctuation">]</span><span class="token punctuation">[</span>widthLeft<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>To make the code more simple, we could try to optimize the solution by dynamic programming ? How can we think about that? Using an array with length equals to the size of books plus one for storage of the current optimal(minimal) height of the first i-th books. The state function is going to be either it can be put in the new level or, it can <strong>merge with previous books One by One as a unit</strong> until no extra space left.These two options are the same as the DFS solution. </p><p>Here is the code with full comments</p><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">minHeightShelves</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> books<span class="token punctuation">,</span> <span class="token keyword">int</span> shelf_width<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//for storage of the current optimal(minimal) height of the first i-th books</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> dp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>books<span class="token punctuation">.</span>length<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// init value: with no book, the optimal height is 0; </span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//iterate through all books</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> idx <span class="token operator"><</span> dp<span class="token punctuation">.</span>length<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// current book height and width</span> <span class="token keyword">int</span> height <span class="token operator">=</span> books<span class="token punctuation">[</span>idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> width <span class="token operator">=</span> books<span class="token punctuation">[</span>idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Option 1: place book idx-th on a new shelve </span> dp<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span>idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> height<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Option 2: try to place on the previous shelf with the constraint of width</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> idx<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator">></span><span class="token number">0</span> <span class="token operator">&&</span> width <span class="token operator">+</span> books<span class="token punctuation">[</span>j<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator"><=</span> shelf_width<span class="token punctuation">;</span> j<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">{</span> height <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>height<span class="token punctuation">,</span> books<span class="token punctuation">[</span>j<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// merge with previous book, height might change</span> width <span class="token operator">+=</span> books<span class="token punctuation">[</span>j<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// update width</span> dp<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>dp<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">,</span>dp<span class="token punctuation">[</span>j<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> height<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// update optimal solution if it is the optimal</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> dp<span class="token punctuation">[</span>books<span class="token punctuation">.</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Data Structure and Algorithm </tag>
<tag> Dynamic programming </tag>
<tag> backtracking </tag>
<tag> Depth-first Search (DFS) </tag>
<tag> memorization </tag>
</tags>
</entry>
<entry>
<title>Implements a mini Spring Framework</title>
<link href="/2019-06-26-mini-spring-mvc.html"/>
<url>/2019-06-26-mini-spring-mvc.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>I have been using Spring framework to build my own projects for several time. In order to have a deeper understanding of how Spring framework works and the core concepts of Spring framework ( i.e Inversion of Control , dependency injection…) I decide to implements a mini-spring framework. So that, it can works with basic requirement such as response to the HTTP request.</p><h1 id="1-Prepared-Work"><a href="#1-Prepared-Work" class="headerlink" title="1. Prepared Work"></a>1. Prepared Work</h1><h2 id="1-1-Requirement-analysis-To-achieve-basic-of-Spring-Framework-we-need-to-analysis-which-parts-should-we-implements"><a href="#1-1-Requirement-analysis-To-achieve-basic-of-Spring-Framework-we-need-to-analysis-which-parts-should-we-implements" class="headerlink" title="1.1 Requirement analysis, To achieve basic of Spring Framework, we need to analysis which parts should we implements."></a>1.1 Requirement analysis, To achieve basic of Spring Framework, we need to analysis which parts should we implements.</h2><ol><li><p>Spring Core Module, includes core(core is the utility package to support spring framework ), beans(bean factory for managing the life cycle of Spring project) and context(context is the interfaces package to interact with clients).</p></li><li><p>Spring Web Module ( Integrate Application Server(Tomcat) as the container for Servlet ) And Spring MVC</p></li><li><p>Spring-starter, Starter the application like Spring-boot-starter.</p></li></ol><h2 id="1-2-Build-our-project"><a href="#1-2-Build-our-project" class="headerlink" title="1.2 Build our project"></a>1.2 Build our project</h2><p>Here ,I use gradle instead of maven as the project management tool because of the high performance of gradle. It is also more convenient than maven.</p><p>This is the project structure of our Spring project. By the way, I use IntelligentJ IDEA as the dev tool.</p><p>We will have two modules in our project. One is the framework module and the other one is test module. Framework module is to implements our Spring Framework Project. Test Module is how the client could use our mini spring framework.</p><pre><code>├───.gradle│ ├───4.10.3│ │ ├───fileChanges│ │ ├───fileHashes│ │ ├───taskHistory│ │ └───vcsMetadata-1│ ├───buildOutputCleanup│ └───vcs-1├───.idea│ └───modules├───framework│ └───src│ ├───main│ │ ├───java│ │ │ └───com│ │ │ └───example│ │ │ └───tan│ │ │ ├───beans│ │ │ ├───context│ │ │ ├───core│ │ │ ├───starter│ │ │ └───web│ │ └───resources│ └───test│ ├───java│ └───resources├───gradle│ └───wrapper└───test └───src ├───main │ ├───java │ └───resources └───test ├───java └───resources</code></pre><p>In the text file ,we will have the starter class which is the entry point of our project. run the following command to see if our project works</p><pre><code>grandle buildjava -jar test/build/libs/test-1.0-SNAPSHOT.jar</code></pre><p>An error occurs</p><pre><code>no main manifest attribute, in test/build/libs/test-1.0-SNAPSHOT.jar</code></pre><p>What we need to do is to add manifest attribute regarding to the jar pacakge.</p><p>In the build.grandle file in the text module , we should point out the Main-Class of our application and as the same time compile all the jars related to it recursively.</p><pre><code>jar{ manifest{ attributes "Main-Class":"com.example.tan.Application" } from{ configurations.compile.collect{ it.isDirectory() ? it: zipTree(it) } }}</code></pre><p>How to connect framework module to our test module.</p><p>First thing is when compiling the text module, it should compile the framework as well.</p><p>Add framework module to dependencies of text module</p><pre><code>dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile(project(':framework'))}</code></pre><p>Create a Starter class for the framework.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>example<span class="token punctuation">.</span>tan<span class="token punctuation">.</span>starter<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * This is the starter of our Mini Spring Framework. * It has run method takes two argument. The first one is the * Class Object of the client, so the framework can get the Application class information * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MiniApplication</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">,</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello, Mini Spring Framework"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>In test client, we could invoke that method</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Application</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> MiniApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>Application<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Package and run the jar file</p><pre><code>$ gradle build$ java -jar test/build/libs/test-1.0-SNAPSHOT.jarhello worldHello, Mini Spring Framework</code></pre><p>Until Now, our project could start and run. In the next section, we will integrate the web Server.</p><h1 id="2-Integrate-Web-Container"><a href="#2-Integrate-Web-Container" class="headerlink" title="2. Integrate Web Container."></a>2. Integrate Web Container.</h1><p>Web Server usually listen to a TCP port, dispatch request to php, python scripts (for Ngnix Server) Or Java code (for Tomcat Server) and collect response. Web Server has no responsibility to deal with the business logic. It provides connection for operating system and application codes to improve performance.</p><p>Having knowing the mechanism of Web Server, now let us integrate the most popular web server in java, which is tomcat. From Tomcat 7, it could be embedded to our program.</p><p>Here, I choose Tomcat [8.5.34] (<a href="https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core/8.5.34" target="_blank" rel="noopener">https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core/8.5.34</a>)</p><p>First, add dependency to the framework module</p><pre class=" language-json"><code class="language-json">dependencies <span class="token punctuation">{</span> testCompile group<span class="token operator">:</span> 'junit'<span class="token punctuation">,</span> name<span class="token operator">:</span> 'junit'<span class="token punctuation">,</span> version<span class="token operator">:</span> '<span class="token number">4.12</span>' // https<span class="token operator">:</span>//mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core compile group<span class="token operator">:</span> 'org.apache.tomcat.embed'<span class="token punctuation">,</span> name<span class="token operator">:</span> 'tomcat-embed-core'<span class="token punctuation">,</span> version<span class="token operator">:</span> '<span class="token number">8.5</span>.<span class="token number">34</span>'<span class="token punctuation">}</span></code></pre><p>Second, implements the class for starting the Tomcat Server.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>example<span class="token punctuation">.</span>tan<span class="token punctuation">.</span>web<span class="token punctuation">.</span>server<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>LifecycleException<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>catalina<span class="token punctuation">.</span>startup<span class="token punctuation">.</span>Tomcat<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * This class is for starting the embed Tomcat Server */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TomcatServer</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> Tomcat tomcat<span class="token punctuation">;</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// TODO: arguments for start the tomcat server</span> <span class="token keyword">public</span> <span class="token function">TomcatServer</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>args <span class="token operator">=</span> args<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">startServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> LifecycleException <span class="token punctuation">{</span> tomcat <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Tomcat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> tomcat<span class="token punctuation">.</span><span class="token function">setPort</span><span class="token punctuation">(</span><span class="token number">8080</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// set listening port</span> tomcat<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Create await thread by anomoynous inner class</span> Thread awaitThread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token string">"tomcat_await_thread"</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> TomcatServer<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>tomcat<span class="token punctuation">.</span><span class="token function">getServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> awaitThread<span class="token punctuation">.</span><span class="token function">setDaemon</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> awaitThread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>I should mention a key concept here, which is called <strong>daemon thread.</strong> The official defination of daemon thread is that A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection. from Stack Overflow problem. <a href="https://stackoverflow.com/questions/2213340/what-is-a-daemon-thread-in-java" target="_blank" rel="noopener">What is a daemon thread in Java?</a> In other word, the JVM can only halts if and only if all all non-daemon threads finish, and any remaining daemon threads are abandoned at the same time.</p><p>The setDaemon(boolean) method is to change the Thread daemon properties before the thread starts. Here, I set the Daemon of awaitThread to be false, means that Although the client terminates, the Tomcat Server continues to run in awaiting state.</p><p>Now, we can start the server In MINI Application class.</p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * This is the starter of our Mini Spring Framework. * It has run method takes two argument. The first one is the * Class Object of the client, so the framework can get the Application class information * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MiniApplication</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">,</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello, Mini Spring Framework"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Start the server when call the method</span> TomcatServer tomcatServer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TomcatServer</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> tomcatServer<span class="token punctuation">.</span><span class="token function">startServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">LifecycleException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Now, since we do not have servlet yet, so it can not have the response. Tomcat Server is the application server, and serlvet runs in that container and defines a way of communication between the application server and the client.Servlet is a interface, each application server like tomcat should implements servlet. So that the servlet client created can deal with Http Request and Http Response. </p><p>Here, for testing, create a context cotainer and a text serlvet, add that servlet to the container.</p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * This class is for starting the embed Tomcat Server */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TomcatServer</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> Tomcat tomcat<span class="token punctuation">;</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// TODO: arguments for start the tomcat server</span> <span class="token keyword">public</span> <span class="token function">TomcatServer</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>args <span class="token operator">=</span> args<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">startServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> LifecycleException <span class="token punctuation">{</span> tomcat <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Tomcat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> tomcat<span class="token punctuation">.</span><span class="token function">setPort</span><span class="token punctuation">(</span><span class="token number">8080</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// set listening port</span> tomcat<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Context context <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StandardContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">setPath</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">addLifecycleListener</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Tomcat<span class="token punctuation">.</span>FixContextListener</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//add default listener</span> TestServlet servlet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TestServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Tomcat<span class="token punctuation">.</span><span class="token function">addServlet</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span><span class="token string">"testServlet"</span><span class="token punctuation">,</span>servlet<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setAsyncSupported</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> context<span class="token punctuation">.</span><span class="token function">addServletMappingDecoded</span><span class="token punctuation">(</span><span class="token string">"/test.json"</span><span class="token punctuation">,</span><span class="token string">"testServlet"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//add servlet mapping</span> tomcat<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addChild</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// add context container to the host container</span> <span class="token comment" spellcheck="true">// Create await thread by anomoynous inner class</span> Thread awaitThread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token string">"tomcat_await_thread"</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> TomcatServer<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>tomcat<span class="token punctuation">.</span><span class="token function">getServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> awaitThread<span class="token punctuation">.</span><span class="token function">setDaemon</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> awaitThread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Build the jar, and rerun it, we could see </p><div> <img src="../images/mini_spring_01.png" alt="Smiley face" width="60%" height="60%"></div><br><h1 id="3-Implements-Controller-for-maintenance-of-multiple-Servlets"><a href="#3-Implements-Controller-for-maintenance-of-multiple-Servlets" class="headerlink" title="3. Implements Controller for maintenance of multiple Servlets"></a>3. Implements Controller for maintenance of multiple Servlets</h1><p>In section 2, the test servlet is hard-coded, which is not what we want. </p><p>Traditional J2EE manage serlvets in following ways:</p><ol><li><p>Tomcat server is responsible for dispatching servlets.</p></li><li><p>The Registration of Servlets are in the web.xml files under the ClassPath folder</p></li><li><p>Every Service is corresponding to one Servlet class, and it should implements all the Servlet interface.</p></li></ol><p>This way has several disadvantages. First, all the configurations are in the web.xml, which is really complicated. Second, implements all methods of Servlet interface is redundent. Many servlets dealing with business logic only needs the service methods.</p><p>How Spring Framework do ? </p><p>Spring Has one <strong>Dispatcher Servlet</strong> for dispatching servlets according to URL Mapping. All the requests should be received by the dispacther servlet in the first place. Servlets in Spring are simplied as <strong>Mapping Handler</strong>. It uses <strong>@RequestMapping annotation</strong> to indicate that this class is a controller.<br>Using annotation is quite easy. </p><p>Therefore, What we need to do right now is a Dispatcher Serlvet and a set of Annotations. </p><p>Controller Annotation, </p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Documented</span><span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>TYPE<span class="token punctuation">)</span><span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">Controller</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre><p>In jdk 5.0, java defines 4 meta-annotation types, which are</p><p>1.@Target,<br>2.@Retention,<br>3.@Documented,<br>4.@Inherited</p><p>I will introduce them one by one, since it has been used quite frequently.</p><p><strong>@Target</strong>: @Target describes the types of element annotation can be decorated. ElementTypes have following<br>values:<br>1.CONSTRUCTOR<br>2.FIELD<br>3.LOCAL_VARIABLE<br>4.METHOD<br>5.PACKAGE<br>6.PARAMETER<br>7.TYPE: includes Class, Interface Enum.</p><p><strong>@Retention</strong> : @Retention describes the annotations exists times(lifecyle of the annotation ), Some annotations are ignored in complie stage, some annotations are not accepted by the ClassLoader,the others can be loaded in to JVM so they are valid in Runtime. </p><p>Those three stages corresponding to three Retention Poicy which are 1.SOURCE, 2.CLASS, 3.RUNTIME.</p><p><strong>@Documented</strong>: can be instanciated by the documentation tools like javadoc.</p><p><strong>@Inherited</strong> : This is a annotation for annotations.It means that subclasses of annotated classes are considered having the same annotation as their superclass.</p><p>All these annotations should be valid during runtime. </p><p>@RequestMapping should have url as the value and @RequestParam should have the parameter name as the value. </p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Documented</span><span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>METHOD<span class="token punctuation">)</span><span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">RequestMapping</span> <span class="token punctuation">{</span> String <span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Documented</span><span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>PARAMETER<span class="token punctuation">)</span><span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">RequestParam</span> <span class="token punctuation">{</span> String <span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Having annotations is not enougth, we should also tell the spring framework about classes using those annotations.ClassLoader is responsible for accomplishing this task.</p><p>What is ClassLoader for ? </p><ol><li>Through <strong>ClassName</strong> to get the bits stream .</li><li>Parse the stream to get the Class instance.</li><li>it is also responsible for loading the classPath static resources </li></ol><p>So, the next challenge would be utilize classloader to get the classes given the package name. </p><p>Attached is the detailed utility classes for loading the classes under the packages </p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Scan all the classes */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ClassScanner</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * Select jar resources and get all classes under the jar resources * @param packageName * @return * @throws IOException * @throws ClassNotFoundException */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> <span class="token function">scanClasses</span><span class="token punctuation">(</span>String packageName<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException<span class="token punctuation">,</span> ClassNotFoundException <span class="token punctuation">{</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> classList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String path <span class="token operator">=</span> packageName<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">,</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ClassLoader classLoader <span class="token operator">=</span> Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getContextClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Enumeration<span class="token operator"><</span>URL<span class="token operator">></span> resources <span class="token operator">=</span> classLoader<span class="token punctuation">.</span><span class="token function">getResources</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>resources<span class="token punctuation">.</span><span class="token function">hasMoreElements</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> URL resource <span class="token operator">=</span> resources<span class="token punctuation">.</span><span class="token function">nextElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>resource<span class="token punctuation">.</span><span class="token function">getProtocol</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"jar"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> JarURLConnection jarURLConnection <span class="token operator">=</span> <span class="token punctuation">(</span>JarURLConnection <span class="token punctuation">)</span>resource<span class="token punctuation">.</span><span class="token function">openConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String jarFilePath <span class="token operator">=</span> jarURLConnection<span class="token punctuation">.</span><span class="token function">getJarFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> classList<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span><span class="token function">getClassesFromJar</span><span class="token punctuation">(</span>jarFilePath<span class="token punctuation">,</span>path<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> classList<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * Get all the CLass objects from the jar file Path * @param jarFilePath * @param path * @return * @throws IOException * @throws ClassNotFoundException */</span> <span class="token keyword">private</span> <span class="token keyword">static</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> <span class="token function">getClassesFromJar</span><span class="token punctuation">(</span>String jarFilePath<span class="token punctuation">,</span>String path<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException<span class="token punctuation">,</span> ClassNotFoundException <span class="token punctuation">{</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> classes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> JarFile jarFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JarFile</span><span class="token punctuation">(</span>jarFilePath<span class="token punctuation">)</span><span class="token punctuation">;</span> Enumeration<span class="token operator"><</span>JarEntry<span class="token operator">></span> jarEntries <span class="token operator">=</span> jarFile<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>jarEntries<span class="token punctuation">.</span><span class="token function">hasMoreElements</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> JarEntry jarEntry <span class="token operator">=</span> jarEntries<span class="token punctuation">.</span><span class="token function">nextElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String entryName <span class="token operator">=</span> jarEntry<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//e.g com/example/tan/Text.class</span> <span class="token keyword">if</span><span class="token punctuation">(</span>entryName<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span> <span class="token operator">&&</span> entryName<span class="token punctuation">.</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string">".class"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String classFullName <span class="token operator">=</span> entryName<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">,</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>entryName<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span> classes<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>classFullName<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> classes<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Test By the framework package , we can get all the classes with proper names </p><pre><code>com.example.tan.Applicationcom.example.tan.Controller.GradeControllercom.example.tan.core.ClassScannercom.example.tan.starter.MiniApplicationcom.example.tan.web.mvc.Controllercom.example.tan.web.mvc.RequestMappingcom.example.tan.web.mvc.RequestParamcom.example.tan.web.server.TomcatServer$1com.example.tan.web.server.TomcatServercom.example.tan.web.servlet.DispatcherServlet</code></pre><h3 id="Mapping-Handler-Using-Java-Reflection-at-runtime"><a href="#Mapping-Handler-Using-Java-Reflection-at-runtime" class="headerlink" title="Mapping Handler(Using Java Reflection at runtime)"></a>Mapping Handler(Using Java Reflection at runtime)</h3><p>The controller methods are invoked by Mapping Handler where each mapping handler has 4 field, uri, controller class object, method object and arguments in type of String array. The core method in Mapping handler is the handle Method which can execute the controller at run time.</p><p>The process of how Spring MVC handles the request is as follows:</p><div> <img src="../images/Spring_mvc_01.png" alt="Smiley face" width="50%" height="50%"></div><br><p>Dispatcher Servlet will choose the Correct Controller to deal with the Handler.How can we do that? </p><p>Well, we would have Handler Manager as the container for registering all the controller at runtime to the HandlerManager container. Each Handler in the HandlerManager has a handle method and return boolean value to indicate whether it is the method to handle the request URI. If it is the mapping Handler to handle the URI, it will invoke the method with parameters given the class objects.</p><p>The HandlerManager container will create one-to-one handler instance for each request URI. </p><p>The codes are as follows:</p><p>Dispatcher Servlet implements the Servlet interface and override handle method</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DispatcherServlet</span> <span class="token keyword">implements</span> <span class="token class-name">Servlet</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span>ServletConfig config<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> ServletConfig <span class="token function">getServletConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">service</span><span class="token punctuation">(</span>ServletRequest request<span class="token punctuation">,</span> ServletResponse response<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span> HandlerManager<span class="token punctuation">.</span>mappingHandlerList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>handler <span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>handler<span class="token punctuation">.</span><span class="token function">handle</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>response<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IllegalAccessException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InstantiationException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InvocationTargetException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">getServletInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>HandlerManager. </p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Container for All Handler Manager */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HandlerManager</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> List<span class="token operator"><</span>MappingHandler<span class="token operator">></span> mappingHandlerList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">resolveMappingHandler</span><span class="token punctuation">(</span>List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> classList<span class="token punctuation">)</span><span class="token punctuation">{</span> classList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>cls <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// if the class has the annotation Controller, create the Mapping Handler Instance</span> <span class="token comment" spellcheck="true">// and add to the container - MappingHandlerList</span> <span class="token keyword">if</span><span class="token punctuation">(</span>cls<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>Controller<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">parseHandlerFromController</span><span class="token punctuation">(</span>cls<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">parseHandlerFromController</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">)</span><span class="token punctuation">{</span> Method<span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">getDeclaredMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Method method<span class="token operator">:</span> methods<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>method<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>RequestMapping<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// Get the request URI by @RequestMapping</span> String uri <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">getDeclaredAnnotation</span><span class="token punctuation">(</span>RequestMapping<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Get the parameters list with annotation @RequestParam</span> List<span class="token operator"><</span>String<span class="token operator">></span> paramNameList <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Parameter parameter<span class="token operator">:</span>method<span class="token punctuation">.</span><span class="token function">getParameters</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>parameter<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>RequestParam<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> paramNameList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>parameter<span class="token punctuation">.</span><span class="token function">getDeclaredAnnotation</span><span class="token punctuation">(</span>RequestParam<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">value</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// convert into array of String</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> params <span class="token operator">=</span> paramNameList<span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span>paramNameList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// register the mappingHandler in HandlerManager</span> MappingHandler mappingHandler <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MappingHandler</span><span class="token punctuation">(</span>uri<span class="token punctuation">,</span> method<span class="token punctuation">,</span> cls<span class="token punctuation">,</span> params<span class="token punctuation">)</span><span class="token punctuation">;</span> HandlerManager<span class="token punctuation">.</span>mappingHandlerList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>mappingHandler<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Mapping Handler </p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Handle the URI request Mapping to invoke the method in Controller at run time. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MappingHandler</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String uri<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//reqeust URI</span> <span class="token keyword">private</span> Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> controller<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// controller</span> <span class="token keyword">private</span> Method method<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// method needed to be execute in the Mapping Handler</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// args for that method.</span> <span class="token keyword">public</span> <span class="token function">MappingHandler</span><span class="token punctuation">(</span>String uri<span class="token punctuation">,</span> Method method<span class="token punctuation">,</span> Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> controller<span class="token punctuation">,</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>uri <span class="token operator">=</span> uri<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>method <span class="token operator">=</span> method<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>controller <span class="token operator">=</span> controller<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>args <span class="token operator">=</span> args<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 1. Verify the Mapping Handler can deal with the request or not. * * 2. Get the parameters values from the request. * * 3.Create the Controller object by reflection and invoke the method with parameters. * * 4. Write the response to ServletResponse and return true; * * @param request * @param response * @return */</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">handle</span><span class="token punctuation">(</span>ServletRequest request<span class="token punctuation">,</span> ServletResponse response<span class="token punctuation">)</span> <span class="token keyword">throws</span> IllegalAccessException<span class="token punctuation">,</span> InstantiationException<span class="token punctuation">,</span> InvocationTargetException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span> String requestUri <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>HttpServletRequest<span class="token punctuation">)</span>request<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRequestURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>requestUri<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>uri<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Object<span class="token punctuation">[</span><span class="token punctuation">]</span> parameters <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">[</span>args<span class="token punctuation">.</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> idx <span class="token operator"><</span> args<span class="token punctuation">.</span>length<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> parameters<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Object controller <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>controller<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object res <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>controller<span class="token punctuation">,</span> parameters<span class="token punctuation">)</span><span class="token punctuation">;</span> response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Finally, Don’t forget to scan all the class objects and register all the controller handler mapping to the HandlerManager when starting the application. </p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MiniApplication</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">,</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello, Mini Spring Framework"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Start the server when call the method</span> TomcatServer tomcatServer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TomcatServer</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> tomcatServer<span class="token punctuation">.</span><span class="token function">startServer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> classList <span class="token operator">=</span> ClassScanner<span class="token punctuation">.</span><span class="token function">scanClasses</span><span class="token punctuation">(</span>cls<span class="token punctuation">.</span><span class="token function">getPackage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// print the class name (for testing and logging)</span> classList<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>it <span class="token operator">-</span><span class="token operator">></span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// add all the MappingHandler to the Controller</span> HandlerManager<span class="token punctuation">.</span><span class="token function">resolveMappingHandler</span><span class="token punctuation">(</span>classList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">LifecycleException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h1 id="4-Spring-Bean-Management-Inversion-of-Control-and-Dependancy-Injection"><a href="#4-Spring-Bean-Management-Inversion-of-Control-and-Dependancy-Injection" class="headerlink" title="4. Spring Bean Management - Inversion of Control and Dependancy Injection"></a>4. Spring Bean Management - Inversion of Control and Dependancy Injection</h1><p>We have already known and implements a simplied version of Controller and Handler Mapping. We know that Entity Classes and Services, Daos have very complex dependence relationships. This means that creaing one Service may depends on serval other services. <em><em> Circular Dependencies</em></em> would occur at that time. Spring Bean and Bean Factory tried to avoid the problem and unify the managements of all the beans lifecyle. Let us dig in. </p><p> <em><em> Inversion of Control & Dependancy Injection </em></em></p><p>Normally, if one object A has a field which is another object Z , we say A depends on Z. If we want to create an instance of object A, what we should do first is to create one object Z in A. In order to Decouple, What we need is a factory to delegate the responsibility of creating Z and through dependancy injection to set the property in A. </p><p>First, we definitely need anotations, simply @Bean and @Autowired in our mini spring mvc.</p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Documented</span><span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>FIELD<span class="token punctuation">)</span><span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">Autowired</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Documented</span><span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span><span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>TYPE<span class="token punctuation">)</span><span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">Bean</span> <span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre><p>Bean Factory has the responsibilities to store all the beans and has getBean() and init() method to aquire and create beans. Since beans can be aquired by serval Threads, using thread-safe ConcurrentHashMap would be a better option. </p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BeanFactory</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//Mapping from Class object to Bean Object</span> <span class="token keyword">private</span> <span class="token keyword">static</span> Map<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">,</span>Object<span class="token operator">></span> classToBean <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConcurrentHashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * Get Bean Object by its class Object. * @param cls * @return */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> Object <span class="token function">getBean</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> classToBean<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span>cls<span class="token punctuation">,</span>null<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * Init Bean, * Create those beans which are independent first, and then create beans after there dependencies have been created * @param classList * @throws Exception */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">initBean</span><span class="token punctuation">(</span>List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> classList<span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> List<span class="token operator"><</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">>></span> toCreate <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span>classList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>toCreate<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> remainSize <span class="token operator">=</span> toCreate<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> idx <span class="token operator"><</span> toCreate<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">finishCreate</span><span class="token punctuation">(</span>toCreate<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> toCreate<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>idx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>toCreate<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> remainSize<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Exception</span><span class="token punctuation">(</span><span class="token string">"dependency Cycle!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 1. If the Class is annotated by @Bean of @Controller, * create an instance and register it in the bean factory. * * 2. If the bean has some fields which are @Autowired. * If its dependent bean has been created, we can create the bean and put it into the Map. * Otherwise, don't put the bean into the map. instead, create the dependent bean first * * @param cls * @return * @throws IllegalAccessException * @throws InstantiationException */</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">boolean</span> <span class="token function">finishCreate</span><span class="token punctuation">(</span>Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> cls<span class="token punctuation">)</span> <span class="token keyword">throws</span> IllegalAccessException<span class="token punctuation">,</span> InstantiationException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// could be even more, like @Serivce, @Repository @Component ... </span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>cls<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>Bean<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token operator">!</span> cls<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>Controller<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> Object bean <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// if the bean class has field which needs to be autowired.(depends on other classes)</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Field field <span class="token operator">:</span> cls<span class="token punctuation">.</span><span class="token function">getDeclaredFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>field<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>Autowired<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Class<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span> fieldType <span class="token operator">=</span> field<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object reliantBean <span class="token operator">=</span> BeanFactory<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span>fieldType<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>reliantBean <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> field<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> field<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>bean<span class="token punctuation">,</span>reliantBean<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> classToBean<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>cls<span class="token punctuation">,</span> bean<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>One Bean may depends on other beans if that bean is autowired with @autowired annotation. </p><h1 id="5-Test-our-framework-via-Small-Application"><a href="#5-Test-our-framework-via-Small-Application" class="headerlink" title="5. Test our framework via Small Application"></a>5. Test our framework via Small Application</h1><p>Here, I generate a grade controller and a grade service as a small demo. Simply get the grade by Student uid and name. </p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Controller</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GradeController</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> GradeService gradeService<span class="token punctuation">;</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/get_grade.do"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> Integer <span class="token function">getGrade</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestParam</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">,</span> <span class="token annotation punctuation">@RequestParam</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"uid"</span><span class="token punctuation">)</span> String uid<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> gradeService<span class="token punctuation">.</span><span class="token function">calGrade</span><span class="token punctuation">(</span>Integer<span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>uid<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token annotation punctuation">@Bean</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GradeService</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> Integer <span class="token function">calGrade</span><span class="token punctuation">(</span>Integer uid<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>uid <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">100</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>uid <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">90</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token number">70</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Test the result via PostMan API.</p><div> <img src="../images/mini_spring_02.png" alt="Smiley face" width="60%" height="60%"></div><br><p>Great, the result is as expected. Here, we have implements the basic of Spring MVC (annotation-based). </p><p>There are lot of things to be improved, like the Exceptions check, XML-based bean registration, Cache-mechanism , new features like view resolver, Spring AOP, so on and so forth.</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Spring </tag>
<tag> MVC </tag>
<tag> Web Server </tag>
<tag> Gradle </tag>
<tag> Java reflection </tag>
<tag> Annotation </tag>
<tag> Thread </tag>
<tag> PostMan API </tag>
</tags>
</entry>
<entry>
<title>Design Pattern factory_pattern</title>
<link href="/2019-06-19-design-pattern-factory-pattern.html"/>
<url>/2019-06-19-design-pattern-factory-pattern.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Let’s-first-look-at-how-java-use-the-factory-method"><a href="#Let’s-first-look-at-how-java-use-the-factory-method" class="headerlink" title="Let’s first look at how java use the factory method."></a>Let’s first look at how java use the factory method.</h1><p>At beginning, in order to unify all the manipulations on different SQL type databases (i.e MySQL, SQL Server) in java, An unified interface need to be created, this is the reason why JDBC APIs had been created. Our manipulation is based on abstraction, without considering things like how different drivers for distinct SQL server have been implements. Both our client And JDBC API will not consider the details of how a driver would be created. The <strong>Driver class</strong> is the <strong>abstract factory</strong> which defines a set of roles how to create a connection. For different SQL server companies. they must implements the driver interface in order to create connections.</p><p>This is driver interface.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>logging<span class="token punctuation">.</span>Logger<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * The interface that every driver class must implement. * <P>The Java SQL framework allows for multiple database drivers. * * <P>Each driver should supply a class that implements * the Driver interface. * * <P>The DriverManager will try to load as many drivers as it can * find and then for any given connection request, it will ask each * driver in turn to try to connect to the target URL. * * <P>It is strongly recommended that each Driver class should be * small and standalone so that the Driver class can be loaded and * queried without bringing in vast quantities of supporting code. * * <P>When a Driver class is loaded, it should create an instance of * itself and register it with the DriverManager. This means that a * user can load and register a driver by calling: * <p> * {@code Class.forName("foo.bah.Driver")} * <p> * A JDBC driver may create a {@linkplain DriverAction} implementation in order * to receive notifications when {@linkplain DriverManager#deregisterDriver} has * been called. * @see DriverManager * @see Connection * @see DriverAction */</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Driver</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * Attempts to make a database connection to the given URL. * The driver should return "null" if it realizes it is the wrong kind * of driver to connect to the given URL. This will be common, as when * the JDBC driver manager is asked to connect to a given URL it passes * the URL to each loaded driver in turn. * * <P>The driver should throw an <code>SQLException</code> if it is the right * driver to connect to the given URL but has trouble connecting to * the database. * * <P>The {@code Properties} argument can be used to pass * arbitrary string tag/value pairs as connection arguments. * Normally at least "user" and "password" properties should be * included in the {@code Properties} object. * <p> * <B>Note:</B> If a property is specified as part of the {@code url} and * is also specified in the {@code Properties} object, it is * implementation-defined as to which value will take precedence. For * maximum portability, an application should only specify a property once. * @param url the URL of the database to which to connect * @param info a list of arbitrary string tag/value pairs as * connection arguments. Normally at least a "user" and * "password" property should be included. * @return a <code>Connection</code> object that represents a * connection to the URL * @exception SQLException if a database access error occurs or the url is * {@code null} */</span> Connection <span class="token function">connect</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Properties info<span class="token punctuation">)</span> <span class="token keyword">throws</span> SQLException<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Other methods.</span><span class="token punctuation">}</span></code></pre><p>It has very detailed illustration, connect() method is to create a database connection. It is the create method that every abstract factory interface must have. It return A <strong>Connection</strong>. Connection is abstract product which is created by Driver. As long as clients pass the correct parameters to the Dirver.connect() method, it will give us a object of type connection</p><p>How about the connection ? Let us look at the source code of Connection.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>PreparedStatement<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>SQLException<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <P>A connection (session) with a specific * database. SQL statements are executed and results are returned * within the context of a connection. * <P> */</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Connection</span> <span class="token keyword">extends</span> <span class="token class-name">Wrapper</span> <span class="token punctuation">{</span> Statement <span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> SQLException<span class="token punctuation">;</span> PreparedStatement <span class="token function">prepareStatement</span><span class="token punctuation">(</span>String sql<span class="token punctuation">)</span> <span class="token keyword">throws</span> SQLException<span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Connection itself is also a abstract factory, it could create prepared statement which can be executed later. That is really convenient for users. they <strong>don’t need to consider</strong> <strong>_concrete_</strong> drivers and <strong>_concrete_</strong> connections. As user, we only depends on abstract factories and abstract products. Since we don’t need to consider the concrete products and factories. so that our codes may apply to all subclasses of our abstract products. This can make the client written code reusable. Platform independence is a good example, here, operation system is the abstract class or interface while Linux, Windows, Mac are concrete products.</p><p>Java also have many other classes or interfaces use the factory design pattern. (i.e. Thread Pools).</p><p>Let’s try to write some small demos to have a deeper understanding of factory method and abstract method.</p><h1 id="Understanding-factory-method-and-abstract-factory-by-small-demos"><a href="#Understanding-factory-method-and-abstract-factory-by-small-demos" class="headerlink" title="Understanding factory method and abstract factory by small demos."></a>Understanding factory method and abstract factory by small demos.</h1><p>If you want to design a UI Container, it has two components, let’s say, alert messages and button. Different component has different event. We want to create both. How can we do that ? what if we want to add a new component to the container ?</p><p>Well, we could have a abstract factory called Component Factory, it create abstract component.</p><p>Component Factory interface.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Abstract factory: it has the responsibility for creating component */</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> Component <span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Component</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Abstract Product: Component */</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Concrete Components. Basically, we have two types of component. One is Button Component and the other one would be AlertMessageComponent. They have their corresponding factory for charge of creation.</p><p>ButtonComponent</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Concrete product: Confirm Button */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfirmButton</span> <span class="token keyword">implements</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I am a confirm Button, you can click me"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>ConfirmButtonFactory</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Concrete factory for creating confirm button component */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfirmButtonFactory</span> <span class="token keyword">implements</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ConfirmButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Concrete factory for creating confirm button component */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfirmButtonFactory</span> <span class="token keyword">implements</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ConfirmButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>The same for AlertMessageComponent</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Concrete product: AlertMessage */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AlertMessage</span> <span class="token keyword">implements</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"i am a alert Message"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Concrete Factory for Alert message component */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AlertMessageFactory</span> <span class="token keyword">implements</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">AlertMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Client-usage code</p><pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> component<span class="token punctuation">.</span>AlertMessageFactory<span class="token punctuation">;</span><span class="token keyword">import</span> component<span class="token punctuation">.</span>Component<span class="token punctuation">;</span><span class="token keyword">import</span> component<span class="token punctuation">.</span>ConfirmButtonFactory<span class="token punctuation">;</span><span class="token keyword">import</span> component<span class="token punctuation">.</span>ComponentFactory<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//Create factory</span> ComponentFactory comfirmButtonFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConfirmButtonFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ComponentFactory alertMessageFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AlertMessageFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// create component by the factory</span> Component alermsg <span class="token operator">=</span> alertMessageFactory<span class="token punctuation">.</span><span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// invoke the method.</span> alermsg<span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Component button <span class="token operator">=</span> comfirmButtonFactory<span class="token punctuation">.</span><span class="token function">createComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> button<span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Output</p><pre><code>I am a confirm Button, you can click meI am a alert MessageProcess finished with exit code 0</code></pre><p>We can see that factory method follows the <strong>Open-close design principle</strong>.If we have new products, in our case, new component, we just need to write two more classes, which are concrete component and concrete factory. And the usage by the client will also follows the same pattern. At the same time, we don’t need to invade other concrete components and other concrete Factory. It makes our code much more scalable and reusable.</p><h1 id="What-if-Each-Component-has-two-version-One-is-displaying-for-ios-the-other-one-is-for-android"><a href="#What-if-Each-Component-has-two-version-One-is-displaying-for-ios-the-other-one-is-for-android" class="headerlink" title="What if Each Component has two version. One is displaying for ios , the other one is for android."></a>What if Each Component has two version. One is displaying for ios , the other one is for android.</h1><p>First, we need two components to extends those concert components.</p><p>IosAlertMessage, MdAlertMessage.</p><p>IosConfirmButton, MdConfirmButton.</p><p>We will have two more factory. One is called AndroidComponent Factory, the other one is called AlertMessage Factory. They are responsible for creating ios/andriod component.</p><p>Now, let us write the code.</p><p>In the first place, we could change the factory.</p><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Abstract factory: it has the responsibility for creating component */</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> Component <span class="token function">createConfirmButtonComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Component <span class="token function">createAlertMessageComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Android Component Factory && IOS component Factory</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AndroidComponentFactory</span> <span class="token keyword">implements</span> <span class="token class-name">ComponentFactory</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createConfirmButtonComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MdConfirmButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createAlertMessageComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MdAlertMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * Concrete component factory for creating component for Ios system. * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IosComponentFactory</span> <span class="token keyword">implements</span> <span class="token class-name">ComponentFactory</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createConfirmButtonComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">IosConfirmButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Component <span class="token function">createAlertMessageComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">IosAlertMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Concrete components for two operating systems</p><p>IosAlertMessage</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IosAlertMessage</span> <span class="token keyword">extends</span> <span class="token class-name">AlertMessage</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I am for Ios"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>IosConfirmButton</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IosConfirmButton</span> <span class="token keyword">extends</span> <span class="token class-name">ConfirmButton</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"i am a confirm button for ios"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>IosAlertMessage</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> component<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IosAlertMessage</span> <span class="token keyword">extends</span> <span class="token class-name">AlertMessage</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I am for Ios"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>The test function</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//Create factory</span> ComponentFactory isoComponentFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IosComponentFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ComponentFactory mdCompnentFactory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AndroidComponentFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// create two component for Ios</span> isoComponentFactory<span class="token punctuation">.</span><span class="token function">createAlertMessageComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> isoComponentFactory<span class="token punctuation">.</span><span class="token function">createConfirmButtonComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"------------------------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// create two component for Android</span> mdCompnentFactory<span class="token punctuation">.</span><span class="token function">createAlertMessageComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> mdCompnentFactory<span class="token punctuation">.</span><span class="token function">createConfirmButtonComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">event</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Result</p><pre><code>i am a alert MessageI am for IosI am a confirm Button, you can click mei am a confirm button for ios------------------------------i am a alert Messagei am for ios alert message for andriodI am a confirm Button, you can click mei am the confirm button for andriod</code></pre><p>Now, we may have some intuition about the difference between abstract factory and factory method. abstract factory pattern is designed for <strong>a set of related or dependent products</strong> which have some <strong>common features</strong>. Concrete Products have different levels. In our example , IOS button component is one level lower than ConfirmButtonComponent. And Concrete Factories are For creating a set of related component with the same level.</p><p>For client usage, if we want to create an app in IOs system. We would use the Ios Component Factory to create all the components that we want. If the opreating system has been changed. the only thing need to be changed is to switch the concrete factory. Since we use generic interface of the factory to create the concrete objects, other codes will not be changed.</p><p>Now, looking at the definition would be much clearer.</p><p><strong>Definition from Wikipedia is:</strong></p><p>Factory method</p><div style="background-color: lightcyan">It is a <b>creational pattern</b> that uses <b>factory methods</b> to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creaing objects by callying a factory method - either specified in an in interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes - rather than by calling a constructor.</div><p>Abstract Factory</p><div style="background-color: lightcyan">The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.[1] In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.</div><p>Reference:</p><ol><li><p>Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 107ff. ISBN 0-201-63361-2.</p></li><li><p>Freeman, Eric; Robson, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Hendrickson, Mike; Loukides, Mike (eds.). “Head First Design Patterns” (paperback). 1. O’REILLY: 156. ISBN 978-0-596-00712-6. Retrieved 2012-09-12.</p></li></ol><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> OOD </tag>
<tag> Design Pattern </tag>
<tag> Factory Method </tag>
<tag> Java Source Code </tag>
<tag> Abstract Factory </tag>
<tag> JDBC </tag>
</tags>
</entry>
<entry>
<title>Import RDF files to Neo4j database</title>
<link href="/2019-05-23-import_rdf_to_neo4j_db.html"/>
<url>/2019-05-23-import_rdf_to_neo4j_db.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction"></a>1. Introduction</h1><p>In this blog, I will introduce how to import RDF(Resource Description Framework) files to graph database (Neo4j), As we known, RDF is a framework for describing resources on the web in XML format, it is designed to be machine-readable so that computer can understand RDF easily. This means that RDFs are not easily to be read by people. For deeper visualization and analysis data represented by RDF format, we need to transfer those data to Neo4j database.</p><h1 id="2-How-data-is-stored-in-RDF-files"><a href="#2-How-data-is-stored-in-RDF-files" class="headerlink" title="2. How data is stored in RDF files"></a>2. How data is stored in RDF files</h1><p>RDF statement is a triple, a combination of Resource, Property, and property-value, which is also refer to Subject, predicate and objects. Written as</p><pre><code>(Subject, Predicate, Object)</code></pre><p>Subject and predicate are resources while Object can be either resources or literals. If the object is literals, this means that itself cannot be appeared in another RDF statement as subject.</p><h2 id="2-1-How-RDF-can-be-related-to-Graph-Database"><a href="#2-1-How-RDF-can-be-related-to-Graph-Database" class="headerlink" title="2.1 How RDF can be related to Graph Database"></a>2.1 How RDF can be related to Graph Database</h2><p>We need to define set of rules so that it is possible for importing XML/RDF.</p><h3 id="2-1-1-Rule-One"><a href="#2-1-1-Rule-One" class="headerlink" title="2.1.1 Rule One"></a>2.1.1 Rule One</h3><p>Since every single subject has its URI(universal resources identifier). Therefore, subjects of triples are mapped into nodes n graph database. A node in neo4j represents an RDF resources will have label Resource and with a property uri with the resources uri.</p><pre><code>(Subject, Predicate, Object) => (Resource {uri:Subject})</code></pre><h3 id="2-1-2-Rule-Two"><a href="#2-1-2-Rule-Two" class="headerlink" title="2.1.2 Rule Two"></a>2.1.2 Rule Two</h3><p>If the object of a RDF triple is literal, then predicate and object should be mapped into node property and property value.</p><pre><code>(Subject,Predicate,Object) && isLiteral(Object) => (:Resource {uri:Subject, Property:Object})</code></pre><h3 id="2-1-3-Rule-Three"><a href="#2-1-3-Rule-Three" class="headerlink" title="2.1.3 Rule Three"></a>2.1.3 Rule Three</h3><p>If the object of a RDF triple is also a resource. Then, it should map to other node with URI as the universal identifier, and predicate will be viewed as the relationship between the subject and object.</p><pre><code>(Subject,Predicate,Object) && !isLiteral(Object) => (:Resource {uri:Sbject})-[:Predicate]->(:Resource {uri:Object})</code></pre><h3 id="2-2-Some-Exceptions"><a href="#2-2-Some-Exceptions" class="headerlink" title="2.2 Some Exceptions."></a>2.2 Some Exceptions.</h3><p>The previous three rules defines a generic way for mapping rdf nodes into nodes and relationships in neo4j database. However, there are some exceptions</p><h3 id="2-2-1-Exception-One"><a href="#2-2-1-Exception-One" class="headerlink" title="2.2.1 Exception One,"></a>2.2.1 Exception One,</h3><p>In rdf files, we would have triple rdf:type statements, The rdf:type statements are usually mapped into categories in Neo4j. By the 3rd rule, we may have many nodes linked to one specific category. In order to have less relationships and make retrieving easier, we would treate the object as the label of that node.</p><pre><code>(Subject ,rdf:type, Category) => (:Category {uri:Subject})</code></pre><h3 id="2-2-2-Exception-two"><a href="#2-2-2-Exception-two" class="headerlink" title="2.2.2 Exception two."></a>2.2.2 Exception two.</h3><p>In rdf files, we could only define one-to-one dependency relationships for subjects and objects. If we would have one-to-many dependency relationships, we would have a Blank node as anonymous resources so that many objects would link to anonymous resource in this way. For easily retrieving those virtual node, we would have to create index on them. The neo4j database will give every single blank node an unique id to avoid clash.</p><p>The Cypher Query Statement for creating index is as follows</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span> : BNode<span class="token punctuation">(</span>uri<span class="token punctuation">)</span></code></pre><h3 id="2-2-3-Exception-three"><a href="#2-2-3-Exception-three" class="headerlink" title="2.2.3 Exception three."></a>2.2.3 Exception three.</h3><p>Literal object usually have datatypes associate with them. If datatypes are not explicit declared in the RDF triples , by default, they should be loaded as String type.</p><h2 id="3-Existing-tools-for-Importing-RDFs-into-Neo4j-Database"><a href="#3-Existing-tools-for-Importing-RDFs-into-Neo4j-Database" class="headerlink" title="3. Existing tools for Importing RDFs into Neo4j Database."></a>3. Existing tools for Importing RDFs into Neo4j Database.</h2><p>If we already have some parsers as existing tools for importing rdf files to neo4j, then we don’t need to implements the codes for these rules from scratch. As client, what we only need to do is to call their APIs. I would recommend the <a href="https://github.com/jbarrasa/neosemantics" target="_blank" rel="noopener">neosemantics</a>, it said that Graph+Semantics: Import/Export RDF from Neo4j. Model mapping, inferencing and more…</p><h2 id="3-1-installation"><a href="#3-1-installation" class="headerlink" title="3.1 installation:"></a>3.1 installation:</h2><p>Since the latest stable version of Neo4j is 3.5.5, please dowload <a href="https://github.com/jbarrasa/neosemantics/releases" target="_blank" rel="noopener">neosemantics-3.5.0.2.jar</a> jar file from the released area. If wanting to know how it is implements, we could download the source code and build the jar package by our self.</p><ol><li><p>copy the jar file to your</p><p>[neo4j_home]/plugins</p></li><li><p>ADD the following code to [your_neo_home]/conf/neo4j.conf</p><pre><code>dbms.unmanaged_extension_classes=semantics.extension=/rdf</code></pre></li><li><p>restart the server and call the following statement, if the reponse is “ok”, then it has been succesfully installed.</p><pre><code>:GET /rdf/ping</code></pre></li></ol><h2 id="3-2-Learning-how-to-use-neosemantics-to-import-RDF-files-to-the-Neo4j-Database"><a href="#3-2-Learning-how-to-use-neosemantics-to-import-RDF-files-to-the-Neo4j-Database" class="headerlink" title="3.2 Learning how to use neosemantics to import RDF files to the Neo4j Database."></a>3.2 Learning how to use neosemantics to import RDF files to the Neo4j Database.</h2><p>In my project, my goal is to import SenticNet5 [<a href="https://sentic.net/downloads/" target="_blank" rel="noopener">https://sentic.net/downloads/</a>] to the graph database. Let us explore the sentic net by looking the RDF files.</p><p>senticNet is basically a large dictionary consists of hundreds and thousands of words, every single word has some similarity.</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>Description</span> <span class="token attr-name"><span class="token namespace">rdf:</span>about</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/a_little<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>type</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/concept<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>a little<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>text</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>semantics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/least<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/little<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/small_amount<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/shortage<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/scarce<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>semantics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sentics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pleasantness</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.99<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>pleasantness</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>attention</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>attention</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sensitivity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sensitivity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aptitude</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.70<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>aptitude</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sentics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>moodtags</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/sadness<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/disgust<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>moodtags</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>polarity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>negative<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>intensity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.84<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>intensity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>polarity</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token namespace">rdf:</span>Description</span><span class="token punctuation">></span></span></code></pre><p>Every word has many semantics which is also a resources. And Each word has moodtags which is the catogries of each word. In addition, each word has 4-dimentional sentics wich are pleasantness, attention, sensitivity and aptitude with value from -1 to 1. The total score can be computed in some way called polarity.</p><p>Although it is in RDF format, it is actually not a valid RDF. As we said before, it is not allowed to have one-to-many relationships. We need to refactor those code to make it valid in the first place.</p><p>For each concept tag under semantics or mood tags, they need a form that allows the rdf:Description to be omitted. This can be done by putting an rdf:parseType=”Resource” attribute on the containing property element that turns the property element into a property-and-node element, which can itself have both property elements and property attributes. This is the definition of Blank Node.</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>Description</span> <span class="token attr-name"><span class="token namespace">rdf:</span>about</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/a_little<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>type</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/concept<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>a little<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>text</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>semantics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/least<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/little<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/small_amount<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/shortage<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/scarce<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>semantics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sentics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pleasantness</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.99<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>pleasantness</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>attention</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>attention</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sensitivity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sensitivity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aptitude</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.70<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>aptitude</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sentics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>moodtags</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/sadness<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/disgust<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>moodtags</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>polarity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>negative<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>intensity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>datatype</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema#float<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>-0.84<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>intensity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>polarity</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token namespace">rdf:</span>Description</span><span class="token punctuation">></span></span></code></pre><p>Simply call the API, we will get the triples imported into the graph database.For more details about the usage of API, the author of <a href="https://github.com/jbarrasa/neosemantics" target="_blank" rel="noopener">neosemantics</a> has a very detailed explanation on github page and his blogs.</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CALL</span> semantics<span class="token punctuation">.</span>importRDF<span class="token punctuation">(</span>"<span class="token keyword">file</span>:<span class="token comment" spellcheck="true">///C:/Users/OneDrive/Desktop/try.xml",</span><span class="token string">"RDF/XML"</span><span class="token punctuation">,</span>{ shortenUrls: <span class="token boolean">true</span><span class="token punctuation">,</span> typesToLabels: <span class="token boolean">true</span><span class="token punctuation">,</span> commitSize: <span class="token number">9000</span> }<span class="token punctuation">)</span></code></pre><p>Notice: Be careful at the file path . The valid file URI in Windows would be something like ‘file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc’</p><div> <img src="../images/neo4j_rdf_1.png" alt="Smiley face" width="90%" height="90%"></div><p>We could see that the parser will automatically generate the namespace and labels for both nodes and relationships. These ns0 , ns1… are not suitable to read and query. Hence, let us define the namespace prefix before we importing rdf triples.</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token punctuation">(</span>:NamespacePrefixDefinition {<span class="token punctuation">`</span><span class="token operator"><</span>http:<span class="token comment" spellcheck="true">//sentic.net/`>: 'sentic_net', `http://sentic.net/api/`: 'keyword'});</span></code></pre><p>For ‘<a href="http://sentic.net/" target="_blank" rel="noopener">http://sentic.net/</a>‘, the mapping of url would be senticnet, and the ‘/api/‘ would be called keyword by my own definition.</p><div> <img src="../images/neo4j_rdf_2.png" alt="Smiley face" width="90%" height="90%"></div><p>The result Here looks more reasonable. when we want to find all other similar word. We will follow only two relationships, sentic_net_sementic for finding the blankNode and sentic_net_concept to find all similar word resources.</p><p>For fast retrieve blank Node , resources and other things, do not forget to create Index for those nodes and relationships that are frequently retrieved`</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span>: Resource<span class="token punctuation">(</span>uri<span class="token punctuation">)</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span>: URI<span class="token punctuation">(</span>uri<span class="token punctuation">)</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span>: BNode<span class="token punctuation">(</span>uri<span class="token punctuation">)</span><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token keyword">ON</span>: Class<span class="token punctuation">(</span>uri<span class="token punctuation">)</span></code></pre><p>Since we would like to retrieve all the words with pleasantness, attention in some range or with a specific value. Instead importing them to key value pairs as properties. we would treat them as other resources.</p><p>Therefore, slightly change the rdf file.</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sentics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pleasantness</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/pleasantness/0.99<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>attention</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/attention/0<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sensitivity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/sensitivity/0<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aptitude</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/aptitude/-0.70<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sentics</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>polarity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>negative<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>intensity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/intensity/-0.84<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>polarity</span><span class="token punctuation">></span></span></code></pre><p>In addition, design the URL for each attribute as follows,</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token punctuation">(</span>:NamespacePrefixDefinition { <span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/`: 'sentic_net',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/api/`: 'keyword',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/pleasantness/`: 'pleasantness',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/attention/`: 'attention',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/sensitivity/`: 'sensitivity',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/aptitude/`: 'aptitude',</span><span class="token punctuation">`</span>http:<span class="token comment" spellcheck="true">//sentic.net/intensity/`: 'intensity'</span>}<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>ReRun the code, the graph is as follows:</p><div> <img src="/images/neo4j_rdf_3.png" alt="Smiley face" width="90%" height="90%"></div><div> <img src="/images/neo4j_rdf_4.png" alt="Smiley face" width="90%" height="90%"></div><p>Those APIs are correct for describing pleasantness, attention and other attributes with correct value.</p><h1 id="4-Automatically-refactor-the-code-By-dom4j"><a href="#4-Automatically-refactor-the-code-By-dom4j" class="headerlink" title="4. Automatically refactor the code By dom4j"></a>4. Automatically refactor the code By dom4j</h1><p>We have successfully import the manually edited rdf file to the neo4j database. Key problems here we need to write codes for automatically modify rdf description to the format that we want.</p><p>Here, I use dom4j framework for manipulating the xml/rdf file. Dom4j is an open source framework for processing XML which is integrated with XPath and fully supports DOM, SAX, JAXP and the Java platform such as Java 2 Collections.(More about dom4j, visit the github page(<a href="https://github.com/dom4j/dom4j" target="_blank" rel="noopener">https://github.com/dom4j/dom4j</a>)). And I use maven as the project management tool.</p><p>In pom.xml, we add the dependencies</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.dom4j<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>dom4j<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>2.1.1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!-- https://mvnrepository.com/artifact/jaxen/jaxen --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>jaxen<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>jaxen<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.2.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span></code></pre><p>Notice that jaxen, the XPath Engine for Java, would not compatible to the version of dom4j here. If you want to jaxen for finding Nodes in DOM tree fast, I think using previous version of dom4j would be a choice. Here since the rdf file is not that complicated, I use iterator to iterate and find tags.</p><p>First, write the factory utility class for read and xml files</p><pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> org<span class="token punctuation">.</span>dom4j<span class="token punctuation">.</span>Document<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>dom4j<span class="token punctuation">.</span>DocumentException<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>dom4j<span class="token punctuation">.</span>io<span class="token punctuation">.</span>SAXReader<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>FileWriter<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RdfUtil</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> Document <span class="token function">readDoc</span><span class="token punctuation">(</span>String fileName <span class="token punctuation">)</span> <span class="token keyword">throws</span> DocumentException <span class="token punctuation">{</span> SAXReader reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SAXReader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Document document <span class="token operator">=</span> reader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> document<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">writeDoc</span><span class="token punctuation">(</span>String outputFileName<span class="token punctuation">,</span> Document document<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> FileWriter out <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>outputFileName<span class="token punctuation">)</span><span class="token punctuation">;</span> document<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>out<span class="token punctuation">)</span><span class="token punctuation">;</span> out<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>The enum class for the designed URI for conversion of float values</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">enum</span> URIEnum <span class="token punctuation">{</span> <span class="token function">Pleasantness_URI</span><span class="token punctuation">(</span><span class="token string">"http://sentic.net/pleasantness/"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Attention_URI</span><span class="token punctuation">(</span><span class="token string">"http://sentic.net/attention/"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Sensitivity_URI</span><span class="token punctuation">(</span><span class="token string">"http://sentic.net/sensitivity/"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Aptitude_URI</span><span class="token punctuation">(</span><span class="token string">"http://sentic.net/aptitude/"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Intensity_URI</span><span class="token punctuation">(</span><span class="token string">"http://sentic.net/intensity/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">private</span> String uri<span class="token punctuation">;</span> <span class="token function">URIEnum</span><span class="token punctuation">(</span>String uri<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>uri <span class="token operator">=</span> uri<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> uri<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Traverse the tree data structure and modify the document. Here if you want to remove the text of a tag, you should use the setText() to set the text to be null</p><pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> org<span class="token punctuation">.</span>dom4j<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Iterator<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">traverseRdf</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> Document <span class="token function">traverseAndModify</span><span class="token punctuation">(</span>Document document<span class="token punctuation">)</span><span class="token punctuation">{</span> Element root <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getRootElement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Element description <span class="token operator">=</span> root<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// iterate through one rdf:description</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>Iterator<span class="token operator"><</span>Element<span class="token operator">></span> it <span class="token operator">=</span> description<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Element element <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String elementTag <span class="token operator">=</span> element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// add rdf:parseType = "Resource" to create blankNode</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"semantics"</span><span class="token punctuation">)</span> <span class="token operator">||</span> elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"sentics"</span><span class="token punctuation">)</span> <span class="token operator">||</span> elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"moodtags"</span><span class="token punctuation">)</span> <span class="token operator">||</span> elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"polarity"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">addParseType</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// if it is sentics tag, add resource and remove data type</span> <span class="token keyword">if</span><span class="token punctuation">(</span>elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"sentics"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">addResourcesAndRemoveDataType</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>elementTag<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"polarity"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">modifyIntensity</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> document<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">addParseType</span><span class="token punctuation">(</span>Element element<span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:parseType"</span><span class="token punctuation">,</span><span class="token string">"Resource"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">addResourcesAndRemoveDataType</span><span class="token punctuation">(</span>Element sentics<span class="token punctuation">)</span><span class="token punctuation">{</span> Iterator<span class="token operator"><</span>Element<span class="token operator">></span> senticsIterator <span class="token operator">=</span> sentics<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>senticsIterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Element element <span class="token operator">=</span> senticsIterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"pleasantness"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:resource"</span><span class="token punctuation">,</span> URIEnum<span class="token punctuation">.</span>Pleasantness_URI<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>element<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"attention"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:resource"</span><span class="token punctuation">,</span> URIEnum<span class="token punctuation">.</span>Attention_URI<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>element<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"sensitivity"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:resource"</span><span class="token punctuation">,</span> URIEnum<span class="token punctuation">.</span>Sensitivity_URI<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>element<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"aptitude"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:resource"</span><span class="token punctuation">,</span> URIEnum<span class="token punctuation">.</span>Aptitude_URI<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>element<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// remove first atttribute: rdf:datatype</span> Attribute attribute <span class="token operator">=</span> element<span class="token punctuation">.</span><span class="token function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>attribute <span class="token operator">!=</span> null <span class="token punctuation">)</span> element<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>attribute<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// and remove the text</span> element<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">modifyIntensity</span><span class="token punctuation">(</span>Element polarity<span class="token punctuation">)</span><span class="token punctuation">{</span> Iterator<span class="token operator"><</span>Element<span class="token operator">></span> polarityIterator <span class="token operator">=</span> polarity<span class="token punctuation">.</span><span class="token function">elementIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>polarityIterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Element element <span class="token operator">=</span> polarityIterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>element<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">"intensity"</span><span class="token punctuation">)</span><span class="token punctuation">{</span> element<span class="token punctuation">.</span><span class="token function">addAttribute</span><span class="token punctuation">(</span><span class="token string">"rdf:resource"</span><span class="token punctuation">,</span> URIEnum<span class="token punctuation">.</span>Intensity_URI<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>element<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> element<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Attribute attribute <span class="token operator">=</span> element<span class="token punctuation">.</span><span class="token function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>attribute <span class="token operator">!=</span> null <span class="token punctuation">)</span> element<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>attribute<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Main function</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> DocumentException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span> Document document <span class="token operator">=</span> RdfUtil<span class="token punctuation">.</span><span class="token function">readDoc</span><span class="token punctuation">(</span> <span class="token string">"file_name.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Document editDocument <span class="token operator">=</span> traverseRdf<span class="token punctuation">.</span><span class="token function">traverseAndModify</span><span class="token punctuation">(</span>document<span class="token punctuation">)</span><span class="token punctuation">;</span> RdfUtil<span class="token punctuation">.</span><span class="token function">writeDoc</span><span class="token punctuation">(</span><span class="token string">"output_file_name.xml"</span><span class="token punctuation">,</span>editDocument<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>The automatically generated xml would be as follows, which is the same as the “a little” one.</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>RDF</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>rdf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/1999/02/22-rdf-syntax-ns#<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>Description</span> <span class="token attr-name"><span class="token namespace">rdf:</span>about</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/a_little_hungry<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">rdf:</span>type</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/concept<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>a little hungry<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>text</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>semantics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/get_full<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/hunger_go_away<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/feel_full<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/hunger<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/full<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>semantics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sentics</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pleasantness</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/pleasantness/0.757<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>pleasantness</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>attention</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/attention/0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>attention</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>sensitivity</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/sensitivity/0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sensitivity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aptitude</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/aptitude/0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>aptitude</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>sentics</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>moodtags</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/joy<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>concept</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/api/en/concept/joy<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>concept</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>moodtags</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>polarity</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">rdf:</span>parseType</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Resource<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>positive<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>intensity</span> <span class="token attr-name"><span class="token namespace">rdf:</span>resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://sentic.net/intensity/0.757<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>intensity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>polarity</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token namespace">rdf:</span>Description</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token namespace">rdf:</span>RDF</span><span class="token punctuation">></span></span></code></pre><p>By all this new word rdf to the neo4j data base, we could see that those two have the same sensitivity and attention value equals to zero. Therefore, they are actually connect to the same resources with uri <a href="http://sentic.net//attention/0" target="_blank" rel="noopener">http://sentic.net//attention/0</a>“ and <a href="http://sentic.net//sensitivity/0" target="_blank" rel="noopener">http://sentic.net//sensitivity/0</a>“</p><div> <img src="/images/neo4j_rdf_5.png" alt="Smiley face" width="100%" height="100%"></div><p>The rest of work would be how to import the whole large-scale rdf to the neo4j Database.</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> NoSQL </tag>
<tag> XML </tag>
<tag> RDF </tag>
<tag> Neo4j </tag>
<tag> dom4j </tag>
<tag> java </tag>
</tags>
</entry>
<entry>
<title>OGM in Neo4j</title>
<link href="/2019-05-21-OGM-in-Neo4j.html"/>
<url>/2019-05-21-OGM-in-Neo4j.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-What-is-OGM"><a href="#1-What-is-OGM" class="headerlink" title="1. What is OGM :"></a>1. What is OGM :</h1><blockquote><p>An OGM (Object Graph Mapper) maps nodes and relationships in the graph to objects and references in a domain model. Object instances are mapped to nodes while object references are mapped using relationships, or serialized to properties (e.g. references to a Date). JVM primitives are mapped to node or relationship properties. An OGM abstracts the database and provides a convenient way to persist your domain model in the graph and query it without using low level drivers. It also provides the flexibility to the developer to supply custom queries where the queries generated by Neo4j-OGM are insufficient. (<a href="https://neo4j.com/docs/ogm-manual/current/introduction/#introduction:ogm" target="_blank" rel="noopener">reference</a>)</p></blockquote><p>In previous post, we query by using lower level driver, it is really time-consuming, and we should manually map those statement result to Objects as well.Neo4j-OGM library provides a pure java library. The Neo4j library can persist (annotated) domain objects using Neo4j. It uses Cypher statements to handle those operations in Neo4j.</p><h1 id="2-Get-start-configuration"><a href="#2-Get-start-configuration" class="headerlink" title="2. Get start: configuration"></a>2. Get start: configuration</h1><p>Create a Spring Boot project by using spring boot starter <a href="https://start.spring.io/" target="_blank" rel="noopener">Link</a>. Configuration is as follows:</p><div> <img src="/images/neo4j_ogm_1.png" alt="Smiley face" width="85%" height="85%"></div><blockquote><p>For building an application, your build automation tool needs to be configured to include the Neo4j-OGM dependencies. Neo4j-OGM dependencies consist of neo4j-ogm-core, together with the relevant dependency declarations on the driver you want to use.</p></blockquote><p>porm.xml File:</p><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>modelVersion</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>parent</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>2.1.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>relativePath</span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!-- lookup parent from repository --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>parent</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.neo4j.movie<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>movie<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>0.0.1-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>name</span><span class="token punctuation">></span></span>movie<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>name</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>description</span><span class="token punctuation">></span></span>ogm demo project for neo4j in Spring Boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>description</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>properties</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>java.version</span><span class="token punctuation">></span></span>1.8<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>java.version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>properties</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependencies</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>scope</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.neo4j<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>neo4j-ogm-core<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.1.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!-- Only add if you're using the Bolt driver --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.neo4j<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>neo4j-ogm-bolt-driver<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.1.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>scope</span><span class="token punctuation">></span></span>runtime<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>scope</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--Spring supports neo4j data blinding--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.data<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-data-neo4j<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>4.1.1.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>build</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>plugins</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>plugin</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>plugin</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>plugins</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>build</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>project</span><span class="token punctuation">></span></span></code></pre><p>Bolt Configuration</p><blockquote><p>The default Bolt port is 7687. Otherwise, a port can be specified with bolt://username:password@localhost:portnumber. Also, the bolt driver allows you to define a connection pool size, which refers to the maximum number of sessions per URL. This property is optional and defaults to 50.</p></blockquote><p>In <strong>application.properties</strong> file, we set up the username, password and URI.</p><pre><code>URI=bolt://user:password@localhostusername="neo4j"password="root"</code></pre><p>Create the Configuration Class. @EnableNeo4jRepositories(basePackages = “com.neo4j.movie.repository”)</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>config<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>session<span class="token punctuation">.</span>SessionFactory<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Value<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Bean<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Configuration<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>repository<span class="token punctuation">.</span>config<span class="token punctuation">.</span>EnableNeo4jRepositories<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>transaction<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>EnableTransactionManagement<span class="token punctuation">;</span><span class="token annotation punctuation">@Configuration</span><span class="token annotation punctuation">@ComponentScan</span><span class="token punctuation">(</span><span class="token string">"com.neo4j.movie"</span><span class="token punctuation">)</span><span class="token annotation punctuation">@EnableTransactionManagement</span><span class="token annotation punctuation">@EnableNeo4jRepositories</span><span class="token punctuation">(</span>basePackages <span class="token operator">=</span> <span class="token string">"com.neo4j.movie.repository"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GraphDBConfiguration</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${URI}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String URI<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${username}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String username<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${password}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String password<span class="token punctuation">;</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>config<span class="token punctuation">.</span>Configuration <span class="token function">getConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>config<span class="token punctuation">.</span>Configuration<span class="token punctuation">.</span>Builder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span>URI<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">credentials</span><span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> SessionFactory <span class="token function">getSessionFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">SessionFactory</span><span class="token punctuation">(</span><span class="token function">getConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"com.neo4j.movie.entity"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>The creation of SessionFactory Should point out the package which stores the Java Bean Objects.</p><p>@EnableNeo4jRepositories This is let the Spring Framework knows the repository packages to be scanned. There are three types of drivers, bolt, http and embedded driver.</p><p>@Configuration tells spring it is a configuration class</p><p>@ComponentScan(“com.neo4j.movie”) tells the spring dependency injection ranges, which means all of classes with annotations from “com.neo4j.movie” packages are recognized as Spring Bean.</p><p>@EnableTransactionManagement supports transaction management when interacting with database.</p><h1 id="3-Entity-Class"><a href="#3-Entity-Class" class="headerlink" title="3. Entity Class"></a>3. Entity Class</h1><p>Since each node and relationship in the graph should have an identifier, Neo4j-OGM uses this to identify and re-connect the entity to the graph in memory. Identifier may be either a primary id or a native graph id. Native graph id is the id generated by the Neo4j database, while primary id is generated by the user, with @Id and @GeneratedValue annotation. Notice that, Neo4j will reuse deleted node id’s, so it is not recommended to use native graph id, it is recommended users come up with their own unique identifier for their objects.</p><p>Hence, we create a abstract entity class and let other relationships and nodes extend that class. So that each object will have a primary id(unique).</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>entity<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>GeneratedValue<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Id<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Entity</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Id</span> <span class="token annotation punctuation">@GeneratedValue</span> <span class="token keyword">private</span> Long id<span class="token punctuation">;</span> <span class="token keyword">public</span> Long <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> id<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>We have two types of nodes which are movies and person.</p><p>Java Bean with @NodeEntity will be recognized as nodes in the graph. @NodeEntity has a property called label to specify the label assigned to the classes. if not specified, it will default to the simple class name of the entity.At the same time each parent class also contributes a label to the entity (with the exception of java.lang.Object).Person has two fields, which are name and born.</p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@NodeEntity</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token keyword">extends</span> <span class="token class-name">Entity</span><span class="token punctuation">{</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> Integer born<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Integer <span class="token function">getBorn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> born<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBorn</span><span class="token punctuation">(</span>Integer born<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>born <span class="token operator">=</span> born<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@NodeEntity</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Movie</span> <span class="token keyword">extends</span> <span class="token class-name">Entity</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String title<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> released<span class="token punctuation">;</span> <span class="token keyword">private</span> String tagline<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Movie</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> title<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTitle</span><span class="token punctuation">(</span>String title<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>title <span class="token operator">=</span> title<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getReleased</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> released<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setReleased</span><span class="token punctuation">(</span><span class="token keyword">int</span> released<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>released <span class="token operator">=</span> released<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getTagline</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> tagline<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTagline</span><span class="token punctuation">(</span>String tagline<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>tagline <span class="token operator">=</span> tagline<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>RelationShips for person: person may active in the movie. @RelationShip annotation allows user to the type of relationship and direction as well as the direction. By default, the direction is assumed to be OUTGOING. Use Relationship.INCOMING and Relationship.OutCOMING to specify the direction.</p><pre class=" language-java"><code class="language-java">NodeEntity<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token keyword">extends</span> <span class="token class-name">Entity</span><span class="token punctuation">{</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> Integer born<span class="token punctuation">;</span> <span class="token annotation punctuation">@Relationship</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token string">"ACTED_IN"</span><span class="token punctuation">,</span>direction <span class="token operator">=</span> Relationship<span class="token punctuation">.</span>OUTGOING<span class="token punctuation">)</span> <span class="token keyword">private</span> Set<span class="token operator"><</span>Movie<span class="token operator">></span> movies <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Integer <span class="token function">getBorn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> born<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setBorn</span><span class="token punctuation">(</span>Integer born<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>born <span class="token operator">=</span> born<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Set<span class="token operator"><</span>Movie<span class="token operator">></span> <span class="token function">getMovies</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> movies<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setMovies</span><span class="token punctuation">(</span>Set<span class="token operator"><</span>Movie<span class="token operator">></span> movies<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>movies <span class="token operator">=</span> movies<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>entity<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>NodeEntity<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>ogm<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Relationship<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Set<span class="token punctuation">;</span><span class="token annotation punctuation">@NodeEntity</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Movie</span> <span class="token keyword">extends</span> <span class="token class-name">Entity</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String title<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> released<span class="token punctuation">;</span> <span class="token keyword">private</span> String tagline<span class="token punctuation">;</span> <span class="token annotation punctuation">@Relationship</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token string">"ACTED_IN"</span><span class="token punctuation">,</span> direction <span class="token operator">=</span> Relationship<span class="token punctuation">.</span>INCOMING<span class="token punctuation">)</span> <span class="token keyword">private</span> Set<span class="token operator"><</span>Person<span class="token operator">></span> actPerson<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Movie</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> title<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Set<span class="token operator"><</span>Person<span class="token operator">></span> <span class="token function">getActPerson</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> actPerson<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setActPerson</span><span class="token punctuation">(</span>Set<span class="token operator"><</span>Person<span class="token operator">></span> actPerson<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>actPerson <span class="token operator">=</span> actPerson<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTitle</span><span class="token punctuation">(</span>String title<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>title <span class="token operator">=</span> title<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getReleased</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> released<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setReleased</span><span class="token punctuation">(</span><span class="token keyword">int</span> released<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>released <span class="token operator">=</span> released<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getTagline</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> tagline<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTagline</span><span class="token punctuation">(</span>String tagline<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>tagline <span class="token operator">=</span> tagline<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Relationship Entities:</p><p>Some RelationShips also have properties and property value. A relationship entity must be annotated with @RelationshipEntity and also the type of relationship. @StartNode and @EndNode will indicate to Neo4j-OGM the start and end node of this relationship.</p><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@RelationshipEntity</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token string">"ACTED_IN"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Role</span> <span class="token keyword">extends</span> <span class="token class-name">Entity</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@StartNode</span> <span class="token keyword">private</span> Person person<span class="token punctuation">;</span> <span class="token annotation punctuation">@EndNode</span> <span class="token keyword">private</span> Movie movie<span class="token punctuation">;</span> <span class="token keyword">private</span> List<span class="token operator"><</span>String<span class="token operator">></span> roles <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Role</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Person <span class="token function">getPerson</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> person<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setPerson</span><span class="token punctuation">(</span>Person person<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>person <span class="token operator">=</span> person<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Movie <span class="token function">getMovie</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> movie<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setMovie</span><span class="token punctuation">(</span>Movie movie<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>movie <span class="token operator">=</span> movie<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> List<span class="token operator"><</span>String<span class="token operator">></span> <span class="token function">getRoles</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> roles<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setRoles</span><span class="token punctuation">(</span>List<span class="token operator"><</span>String<span class="token operator">></span> roles<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>roles <span class="token operator">=</span> roles<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="4-Repository-And-Test"><a href="#4-Repository-And-Test" class="headerlink" title="4. Repository And Test."></a>4. Repository And Test.</h2><p>The repository interface is really like how JPA works.</p><p>In previous post, we use pure Java API to find Actor By Movie Title. Here ,we do the same thing.</p><p>PersonRepository</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>repository<span class="token punctuation">;</span><span class="token keyword">import</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>entity<span class="token punctuation">.</span>Person<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Query<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>repository<span class="token punctuation">.</span>Neo4jRepository<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>data<span class="token punctuation">.</span>repository<span class="token punctuation">.</span>query<span class="token punctuation">.</span>Param<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Set<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">PersonRepository</span> <span class="token keyword">extends</span> <span class="token class-name">Neo4jRepository</span><span class="token operator"><</span>Person<span class="token punctuation">,</span> Long<span class="token operator">></span><span class="token punctuation">{</span> Person <span class="token function">findByName</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> String name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Query</span><span class="token punctuation">(</span><span class="token string">"MATCH (movie: Movie {title:$title})<-[:ACTED_IN]-(person:Person) RETURN person"</span><span class="token punctuation">)</span> Set<span class="token operator"><</span>Person<span class="token operator">></span> <span class="token function">findActorByMovieTitle</span><span class="token punctuation">(</span><span class="token annotation punctuation">@Param</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">)</span> String title<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Unit Test</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>repository<span class="token punctuation">;</span><span class="token keyword">import</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movie<span class="token punctuation">.</span>entity<span class="token punctuation">.</span>Person<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Test<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>runner<span class="token punctuation">.</span>RunWith<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span>Autowired<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>SpringBootTest<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>test<span class="token punctuation">.</span>context<span class="token punctuation">.</span>junit4<span class="token punctuation">.</span>SpringRunner<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Set<span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token keyword">static</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Assert<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span>SpringRunner<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token annotation punctuation">@SpringBootTest</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PersonRepositoryTest</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> PersonRepository personRepository<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findActorByMovieTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Set<span class="token operator"><</span>Person<span class="token operator">></span> personSet <span class="token operator">=</span> personRepository<span class="token punctuation">.</span><span class="token function">findActorByMovieTitle</span><span class="token punctuation">(</span><span class="token string">"The Matrix"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Person person<span class="token operator">:</span>personSet<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">assertEquals</span><span class="token punctuation">(</span>personSet<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>The outPut is as follows:</p><pre><code>Emil EifremHugo WeavingLaurence FishburneCarrie-Anne MossKeanu ReevesProcess finished with exit code 0</code></pre><p>The results are the same compared with my previous post.</p><p>I will play around with more examples, and go through the documentation <a href="https://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/" target="_blank" rel="noopener">spring-data-neo4j</a>.</p><p>To be continued…</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> Cypher Query Language </tag>
<tag> SpringBoot </tag>
<tag> OGM </tag>
<tag> Unit Test </tag>
</tags>
</entry>
<entry>
<title>DesignPattern - Iterator Pattern</title>
<link href="/2019-05-16-IteratorPattern.html"/>
<url>/2019-05-16-IteratorPattern.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Definition"><a href="#1-Definition" class="headerlink" title="1. Definition:"></a>1. Definition:</h1><p>Let us first look at the strict definition of Decorator Pattern,</p><blockquote><p><strong>Iterator Pattern provides a way to access the elements of an aggregate object sequentially without knowing its underlying representation. (<a href="https://en.wikipedia.org/wiki/Iterator_pattern" target="_blank" rel="noopener">Wiki_Link_For_More_Info</a>)</strong></p></blockquote><br><p>Iterator Patterns has multiple advantages.</p><ol><li>It allows us to iterate different absurb data structure uniformly, making the iteration trivial. That is the traversal operation is defined for an aggregate object <strong>without chaning its interface</strong>.</li><li>It enables the traversal of data structure <strong>without knowing the detailed representation of that data structure</strong>. That is <strong>encapsulation benefit</strong>.</li><li>Since it is a kind of <strong>lazy evaluation</strong>, i.e. we ask one and it gives us one at a time, therefore our collection can also be <strong>infinite</strong>, and at the same time we can <strong>pause at any where</strong> we want.</li></ol><h1 id="2-General-UML-of-Iterator-Pattern"><a href="#2-General-UML-of-Iterator-Pattern" class="headerlink" title="2. General UML of Iterator Pattern"></a>2. General UML of Iterator Pattern</h1><p>Then, let us look the General UML of Iterator Pattern.</p><div> <img src="/images/iterator_1.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> UML for iterator pattern</div></div><p>We have two interfaces, Iterable and iterator. Once a concreate class implements Iterable interface, it must override the iterator() method. It create a concreate Iterator by passing itself to the constructor of concreate Iterator. This means that given a concrete iterable ,it produces a concreate iterator. The concreate iterator may also have the private access to its related iterable reference so that it can access the field or methods defined in iterable. Iterator Pattern allows designer to decouple the enumerate functionality with others and unify by the interface.</p><h1 id="3-Example"><a href="#3-Example" class="headerlink" title="3. Example"></a>3. Example</h1><p>Suppose We have a collection of labels (of Type String) on one Node and we want to enumerate them. The codes are as follows:</p><p>Iterable Interface:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Iterable</span><span class="token operator"><</span>T<span class="token operator">></span> <span class="token punctuation">{</span> Iterator<span class="token operator"><</span>T<span class="token operator">></span> <span class="token function">getIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Iterator Interface:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Iterator</span><span class="token operator"><</span>T<span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword">boolean</span> <span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> T <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Concreate Class which implements the iterable interface.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NodeLabels</span> <span class="token keyword">implements</span> <span class="token class-name">Iterable</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> labels<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">NodeLabels</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> labels<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>labels <span class="token operator">=</span> labels<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getLabels</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> labels<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLabels</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> labels<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>labels <span class="token operator">=</span> labels<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> <span class="token function">getIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">NodeLabelsIterator</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Concreate Class which implements the iterator interface.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NodeLabelsIterator</span> <span class="token keyword">implements</span> <span class="token class-name">Iterator</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> NodeLabels nodeLabels<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> pointer<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">NodeLabelsIterator</span><span class="token punctuation">(</span>NodeLabels nodeLabels<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nodeLabels <span class="token operator">=</span> nodeLabels<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>pointer <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>pointer <span class="token operator"><</span> nodeLabels<span class="token punctuation">.</span><span class="token function">getLabels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>nodeLabels<span class="token punctuation">.</span><span class="token function">getLabels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token operator">++</span>pointer<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Test Function(Client)</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> labels <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span><span class="token string">"Person"</span><span class="token punctuation">,</span><span class="token string">"Movie"</span><span class="token punctuation">,</span><span class="token string">"CAT"</span><span class="token punctuation">,</span><span class="token string">"DOG"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> Iterable<span class="token operator"><</span>String<span class="token operator">></span> iterable <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NodeLabels</span><span class="token punctuation">(</span>labels<span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> iterator <span class="token operator">=</span> iterable<span class="token punctuation">.</span><span class="token function">getIterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>OutPut:</p><pre><code>PersonMovieCATDOGProcess finished with exit code 0</code></pre><p>Explain:</p><ol><li>In the interface, we usually use generic T to represent any possible Object Type.In the concreate class implementation, we should specify the types.</li><li>In the concreate iterator class, we should have a pointer pointing to where we have traversed.</li><li>Concreate iterable is responsible for create its concreate iterator by passing itself to the iterator constructor. Client has no aware of that.</li><li>Client can only have the access to the iterable interface and iterator interface.</li></ol><p>UML diagram</p><div> <img src="/images/iterator_2.jpg" alt="Smiley face" width="30%" height="20%"></div><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<tags>
<tag> OOD </tag>
<tag> Design Pattern </tag>
<tag> Iterator Pattern </tag>
<tag> Java </tag>
<tag> Java Generic </tag>
</tags>
</entry>
<entry>
<title>Introduction - how to use neo4j in java</title>
<link href="/2019-05-14-neo4j-java-introduction.html"/>
<url>/2019-05-14-neo4j-java-introduction.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction"></a>1. Introduction</h1><p>In this post, I will build a small web application in java by learning how to connect to as well as manipulate on neo4j database. Neo4j offers a rich set of integration possibilities for developers using Java or other JVM-languages. Following the instruction on the website, we will build a small web app for the movie database.</p><h1 id="2-Create-a-project-And-add-neo4j-driver-dependencies"><a href="#2-Create-a-project-And-add-neo4j-driver-dependencies" class="headerlink" title="2.Create a project And add neo4j driver dependencies"></a>2.Create a project And add neo4j driver dependencies</h1><p>Here, I use IntelliJ IDEA as dev tool and Maven as project management tool. So, select maven-archetype-webapp.</p><div> <img src="/images/neo4j_java_1.png" alt="Smiley face" width="50%" height="50%"></div><p>After that, go to the maven repository website(<a href="https://mvnrepository.com/artifact/org.neo4j.app/neo4j-server/3.5.5" target="_blank" rel="noopener">Link</a>) and add dirver dependency to pom.xml.</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.neo4j.app<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>neo4j-server<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>3.5.5<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.neo4j.driver<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>neo4j-java-driver<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>1.7.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span></code></pre><h1 id="3-Play-with-the-movie-dataSet"><a href="#3-Play-with-the-movie-dataSet" class="headerlink" title="3. Play with the movie dataSet."></a>3. Play with the movie dataSet.</h1><p>Using the data provided by the neo4j guides. Run all CQL commands and get the result.</p><p>Instruction from the guide</p><pre><code>The Movie Graph is a mini graph application containing actors and directors that are related through the movies they've collaborated on.This guide will show you how to:Create: insert movie data into the graphFind: retrieve individual movies and actorsQuery: discover related actors and directorsSolve: the Bacon Path</code></pre><p>After executing the CQL languages, what we will get the graph database</p><div> <img src="/images/neo4j_java_2.png" alt="Smiley face" width="50%" height="50%"></div><h1 id="4-Establish-Connection"><a href="#4-Establish-Connection" class="headerlink" title="4. Establish Connection."></a>4. Establish Connection.</h1><p>In order to use the diver, make connection to the neo4j database. we should do the following things.</p><ol><li><p>Provide a connection URL with username and password. The URL has the format: bolt://username:password@localhost</p></li><li><p>Get a driver instance</p></li><li><p>From the driver, create a session.</p></li><li><p>invoke session.run() method by parsing two parameters. 1). Cypher Query Language with parameters 2). Parameters given in the type of java map, with key-value pairs.</p></li><li><p>It will return result of Statement Result(<a href="https://neo4j.com/docs/api/java-driver/current/org/neo4j/driver/v1/StatementResult.html" target="_blank" rel="noopener">see Documantation for detail</a> ).It extends the Iterator class. so, it has hasNext() method and next() method to iterate through the result.</p></li><li><p>Close and session and close the database.</p></li></ol><p><strong>Cypher Query Language with parameters</strong> :</p><p>For Query String reuse and injection prevention. we will passing the parameters by Parameter Binding. In their document, they say:</p><pre><code>Cypher supports querying with parameters. This means developers don't have to resort to string building to create a query. Additionally, parameters make caching of execution plans much easier for Cypher, thus leading to faster query execution times.</code></pre><p>Where to use.</p><pre><code>Parameters can be used for:literals and expressionsnode and relationship idsfor explicit indexes only: index values and queries</code></pre><p>Where cannot be used.</p><pre><code>Parameters cannot be used for the following constructs, as these form part of the query structure that is compiled into a query plan:property keys; so, MATCH (n) WHERE n.$param = 'something' is invalidrelationship typeslabels</code></pre><p>There are multiple excellent Examples to use CQL With parameters in <a href="https://neo4j.com/docs/cypher-manual/3.5/syntax/parameters/" target="_blank" rel="noopener">neo4j documentation</a></p><p>Here, I wrote codes to test connection with Unit Test.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movies<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Test<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>HashMap<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Map<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EnvTest</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testConnection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Driver driver <span class="token operator">=</span> GraphDatabase<span class="token punctuation">.</span><span class="token function">driver</span><span class="token punctuation">(</span> <span class="token string">"bolt://localhost:7687"</span><span class="token punctuation">,</span> AuthTokens<span class="token punctuation">.</span><span class="token function">basic</span><span class="token punctuation">(</span><span class="token string">"neo4j"</span><span class="token punctuation">,</span> <span class="token string">"root"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Server is up and Running"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// find all the author names for a given movie name</span> String query <span class="token operator">=</span> <span class="token string">"MATCH (movie: Movie {title:$title})<-[:ACTED_IN]-(person:Person) RETURN person.name as actor"</span><span class="token punctuation">;</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span>Object<span class="token operator">></span> titleMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> titleMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">,</span><span class="token string">"The Matrix"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span><span class="token punctuation">{</span> Session session <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> StatementResult statementResult <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>query<span class="token punctuation">,</span>titleMap<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>statementResult<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>statementResult<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"actor"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> driver<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Result</p><pre><code>INFO: Direct driver instance 99451533 created for server address localhost:7687Server is up and Running"Emil Eifrem""Hugo Weaving""Laurence Fishburne""Carrie-Anne Moss""Keanu Reeves"May 14, 2019 3:45:49 PM org.neo4j.driver.internal.logging.JULogger infoINFO: Closing driver instance 99451533May 14, 2019 3:45:49 PM org.neo4j.driver.internal.logging.JULogger infoINFO: Closing connection pool towards localhost:7687Process finished with exit code 0</code></pre><p>Each Result is in the type of <strong>_org.neo4j.driver.v1.Record_</strong>, Record is a form of ordered map and, as such, contained values can be accessed by either positional index or textual key.<a href="https://neo4j.com/docs/api/java-driver/current/org/neo4j/driver/v1/Record.html" target="_blank" rel="noopener">doc</a>.</p><p>Record can either be node or relationships. When we retrieve data from the graph database, we either want to retrieve nodes(with properties and values) or a path contains nodes and relationships. Here are two small demos for retrieving nodes and paths.</p><h1 id="5-Small-Demo"><a href="#5-Small-Demo" class="headerlink" title="5. Small Demo"></a>5. Small Demo</h1><p>Preparation: Let us create a DriverUtil class to wrap the manipulations on the driver</p><h2 id="5-1-Retrieve-Node"><a href="#5-1-Retrieve-Node" class="headerlink" title="5.1 Retrieve Node."></a>5.1 Retrieve Node.</h2><p>Here is the sample code for querying a person by giving its name.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movies<span class="token punctuation">;</span><span class="token keyword">import</span> Util<span class="token punctuation">.</span>DriverUtil<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Test<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>types<span class="token punctuation">.</span>Node<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>HashMap<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Iterator<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Map<span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token keyword">static</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Assert<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FindPersonByName</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findPersonByName</span><span class="token punctuation">(</span>String queryName<span class="token punctuation">)</span><span class="token punctuation">{</span> Driver driver <span class="token operator">=</span> DriverUtil<span class="token punctuation">.</span><span class="token function">getDriver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String queryPersonByName <span class="token operator">=</span> <span class="token string">"MATCH (person:Person) WHERE person.name = $name RETURN person"</span><span class="token punctuation">;</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span>Object<span class="token operator">></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span>queryName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span><span class="token punctuation">(</span>Session session <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> StatementResult statementResult <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>queryPersonByName<span class="token punctuation">,</span> map<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>statementResult<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Record record <span class="token operator">=</span> statementResult<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> List<span class="token operator"><</span>Value<span class="token operator">></span> valueList <span class="token operator">=</span> record<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// Retrieve the values of the underlying map</span> <span class="token function">assertEquals</span><span class="token punctuation">(</span>valueList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// each value is a node actually, so convert to node type</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Value value<span class="token operator">:</span> valueList<span class="token punctuation">)</span><span class="token punctuation">{</span> Node node <span class="token operator">=</span> value<span class="token punctuation">.</span><span class="token function">asNode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// iterate through node labels</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> labels <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">labels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Node labels"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>labels<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>labels<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// iterate through node property-value pairs</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Node key-value pairs "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> keys <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>keys<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String property <span class="token operator">=</span> keys<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object propertyValue <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>property<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"property: "</span><span class="token operator">+</span> property<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"propertyValue: "</span><span class="token operator">+</span> propertyValue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">try</span><span class="token punctuation">{</span> DriverUtil<span class="token punctuation">.</span><span class="token function">closeDriver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> FindPersonByName fd <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">findPersonByName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fd<span class="token punctuation">.</span><span class="token function">findPersonByName</span><span class="token punctuation">(</span><span class="token string">"Emil Eifrem"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Result</p><pre><code>INFO: Direct driver instance 530653666 created for server address localhost:7687Node labelsPersonNode key-value pairsproperty: namepropertyValue: "Emil Eifrem"property: bornpropertyValue: 1978INFO: Closing connection pool towards localhost:7687Disconnected from the target VM, address: '127.0.0.1:57996', transport: 'socket'Process finished with exit code 0</code></pre><p>We can see the Result Object by debugging mode.</p><div> <img src="/images/neo4j_java_3.png" alt="Smiley face" width="50%" height="50%"></div><p>We can see that Record itself is an unordered Hash Map, and keys are the Strings of the Return Statement in SQL. And the Value object itself is also a HashMap.</p><p>If value itself is a node. it contains</p><ol><li>A List of labels</li><li>A List of properties, where properties itself is also a hash map, mapping from property keys to property values.</li></ol><p>For all their usages ,see <a href="https://neo4j.com/docs/api/java-driver/current/org/neo4j/driver/v1/types/MapAccessor.html" target="_blank" rel="noopener">document</a>.</p><h2 id="5-2-Retrieve-Path-relationship"><a href="#5-2-Retrieve-Path-relationship" class="headerlink" title="5.2 Retrieve Path(relationship)."></a>5.2 Retrieve Path(relationship).</h2><p>Suppose we want to see if a person ACTED_IN a movie and what is his/her role in the movie. If that particular people does not ACTED_IN that movie, return false.</p><p>e.g “Keanu Reeves” ACTED_IN “Johnny Mnemonic” as “Johnny Mnemonic”</p><div> <img src="/images/neo4j_java_4.png" alt="Smiley face" width="50%" height="50%"></div><p>Lets see the path Object.</p><p>It contains:</p><ol><li>An ArrayList of nodes</li><li>An ArrayList of relationships</li><li>An ArrayList of segments which contains the start node, relation and the end node.</li></ol><div> <img src="/images/neo4j_java_5.png" alt="Smiley face" width="50%" height="50%"></div><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>movies<span class="token punctuation">;</span><span class="token keyword">import</span> Util<span class="token punctuation">.</span>DriverUtil<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>junit<span class="token punctuation">.</span>Test<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>types<span class="token punctuation">.</span>Node<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>types<span class="token punctuation">.</span>Path<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>neo4j<span class="token punctuation">.</span>driver<span class="token punctuation">.</span>v1<span class="token punctuation">.</span>types<span class="token punctuation">.</span>Relationship<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>HashMap<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Iterator<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>List<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Map<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FindRolesByPersonNameAndMovieName</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">findRolesByPersonNameAndMovieName</span><span class="token punctuation">(</span>String actorName<span class="token punctuation">,</span> String movieName<span class="token punctuation">)</span><span class="token punctuation">{</span> Driver driver <span class="token operator">=</span> DriverUtil<span class="token punctuation">.</span><span class="token function">getDriver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String queryRoles <span class="token operator">=</span> <span class="token string">"MATCH p =(person:Person)-[ACTED_IN]->(movie:Movie) WHERE person.name = $actorName "</span> <span class="token operator">+</span> <span class="token string">"AND movie.title = $movieName RETURN p "</span><span class="token punctuation">;</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span>Object<span class="token operator">></span> parameters <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> parameters<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"actorName"</span><span class="token punctuation">,</span>actorName<span class="token punctuation">)</span><span class="token punctuation">;</span> parameters<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"movieName"</span><span class="token punctuation">,</span>movieName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span><span class="token punctuation">(</span>Session session <span class="token operator">=</span> driver<span class="token punctuation">.</span><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> StatementResult statementResult <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>queryRoles<span class="token punctuation">,</span>parameters<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****relationship*******"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>statementResult<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Record record <span class="token operator">=</span> statementResult<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> List<span class="token operator"><</span>Value<span class="token operator">></span> valuesList <span class="token operator">=</span> record<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// if the movie and actor do not match each other, no searching result.</span> <span class="token keyword">if</span><span class="token punctuation">(</span>valuesList <span class="token operator">==</span> null <span class="token operator">||</span> valuesList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// find the relationship between the movie node and person node</span> Path path <span class="token operator">=</span> valuesList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">asPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// find all the relationships(edges) along the path</span> Iterator<span class="token operator"><</span>Relationship<span class="token operator">></span> relationships <span class="token operator">=</span> path<span class="token punctuation">.</span><span class="token function">relationships</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// find all the nodes along the path</span> Iterator<span class="token operator"><</span>Node<span class="token operator">></span> nodes <span class="token operator">=</span> path<span class="token punctuation">.</span><span class="token function">nodes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// iterate edges along the path</span> <span class="token keyword">while</span><span class="token punctuation">(</span>relationships<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Relationship relationship <span class="token operator">=</span> relationships<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> relationship<span class="token punctuation">.</span><span class="token function">startNodeId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> relationship<span class="token punctuation">.</span><span class="token function">endNodeId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String relationType <span class="token operator">=</span> relationship<span class="token punctuation">.</span><span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"start from Node Id: "</span><span class="token operator">+</span> relationship<span class="token punctuation">.</span><span class="token function">startNodeId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"end from Node Id: "</span><span class="token operator">+</span> relationship<span class="token punctuation">.</span><span class="token function">endNodeId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The relation type is:"</span> <span class="token operator">+</span>relationType<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"RelationShip key value pairs"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> relationKeys <span class="token operator">=</span> relationship<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>relationKeys<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String relationKey <span class="token operator">=</span> relationKeys<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object relationValue <span class="token operator">=</span> relationship<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>relationKey<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>relationKey <span class="token operator">+</span><span class="token string">":"</span> <span class="token operator">+</span>relationValue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// iterate all nodes along the path</span> <span class="token keyword">while</span><span class="token punctuation">(</span>nodes<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****Node*******"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Node node <span class="token operator">=</span> nodes<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Node Id: "</span> <span class="token operator">+</span> node<span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> labels <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">labels</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Node labels"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>labels<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>labels<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// iterate through node property-value pairs</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Node key-value pairs "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Iterator<span class="token operator"><</span>String<span class="token operator">></span> keys <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span>keys<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> String property <span class="token operator">=</span> keys<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object propertyValue <span class="token operator">=</span> node<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>property<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>property<span class="token operator">+</span><span class="token string">":"</span><span class="token operator">+</span>propertyValue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">finally</span> <span class="token punctuation">{</span> DriverUtil<span class="token punctuation">.</span><span class="token function">closeDriver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> FindRolesByPersonNameAndMovieName findRole <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FindRolesByPersonNameAndMovieName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> findRole<span class="token punctuation">.</span><span class="token function">findRolesByPersonNameAndMovieName</span><span class="token punctuation">(</span><span class="token string">"Keanu Reeves"</span><span class="token punctuation">,</span><span class="token string">"Johnny Mnemonic"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Result</p><pre><code>*****relationship*******start from Node Id: 1end from Node Id: 161The relation type is:ACTED_INRelationShip key value pairsroles:"Johnny Mnemonic"*****Node*******Node Id: 1Node labelsPersonNode key-value pairsname:"Keanu Reeves"born:1964*****Node*******Node Id: 161Node labelsMovieNode key-value pairstagline:"The hottest data on earth. In the coolest head in town"title:"Johnny Mnemonic"released:1995</code></pre><p>Notice that: the return type is a path, So, in CQL, We name the path as p. If we want to return all, Using Return *, see CQL manual,<a href="https://neo4j.com/docs/cypher-manual/current/clauses/return" target="_blank" rel="noopener">link</a>.</p><h2 id="6-OGM-Mapping-And-Mapping-to-JSON"><a href="#6-OGM-Mapping-And-Mapping-to-JSON" class="headerlink" title="6. OGM Mapping And Mapping to JSON"></a>6. OGM Mapping And Mapping to JSON</h2><p>To be continued.</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> DB </category>
</categories>
<tags>
<tag> Graph Database </tag>
<tag> NoSQL </tag>
<tag> IntelliJ IDEA </tag>
<tag> data visualization </tag>
<tag> Spark </tag>
<tag> ORM </tag>
<tag> Maven </tag>
<tag> XML </tag>
<tag> CQL </tag>
</tags>
</entry>
<entry>
<title>How to Design your Own Online Shopping Website like amazon -- DB design</title>
<link href="/2019-05-13-online-shopping-project-db-design.html"/>
<url>/2019-05-13-online-shopping-project-db-design.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction"></a>1. Introduction</h1><p>Before designing the database for our small online shopping system, let’s review three normalization forms.</p><ol><li><strong>1NF:</strong> remove repeated/ redundant data</li><li><strong>2NF:</strong> make sure that all the fields in the table are dependent <strong>only</strong> on primary key.</li><li><strong>3NF:</strong> remove <strong>calculated(transitive)</strong> fields</li></ol><p>Now let us discuss about the important entities in this system.</p><p>1.In the first place, we must have users.</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>user_id</td><td>unique primary key for each user</td></tr><tr><td>user_name</td><td>user’s name</td></tr><tr><td>user_password</td><td>password for each user</td></tr></tbody></table><p>2.There must be multiple product categories.</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>category_id</td><td>unique primary key for each product category</td></tr><tr><td>category_name</td><td>product category name</td></tr></tbody></table><p>3.Then,there should be <strong>multiple products in one category</strong>, and <strong>each product should be in only one category</strong>.(For some scenario, it could be in multiple category). At the same time, one <strong>one category should have many combinations of properties</strong>.(For example, laptops have properties like, CPU, Memory, Grapics,etc).SO, there is a <strong>many-to-many relationships between properties and products</strong>. therefore, we should create a table to store the relationship between them to satisfy the 3NF forms. Hence, we could design a table called property_value. One property has multiple property values and one product has many property values as well.</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>property_id</td><td>unique primary key for each property</td></tr><tr><td>property_category_id</td><td>foreign key from category table</td></tr><tr><td>property_name</td><td>name of each property</td></tr></tbody></table><p>Product Table</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>product_id</td><td>product_id</td></tr><tr><td>product_subtitle</td><td>subtitle for products</td></tr><tr><td>original_price</td><td></td></tr><tr><td>promote_price</td><td></td></tr><tr><td>create_time</td><td>on sale time</td></tr></tbody></table><p>Property Value Table (Relationships)</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>id</td><td>property Value primary key</td></tr><tr><td>property_id</td><td>foreign key product_id</td></tr><tr><td>product_id</td><td>foreign key product_id</td></tr><tr><td>propertye_value</td><td></td></tr><tr><td>promote_price</td><td></td></tr><tr><td>create_time</td><td>on sale time</td></tr></tbody></table><p>Each product has many product image to be shown, therefore, we could create a table to store product images</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>product_image_id</td><td>product_id</td></tr><tr><td>product_id</td><td>foreign key constraints for product id</td></tr><tr><td>image_type</td><td>indicating whether it is details figure and display image</td></tr></tbody></table><p>For product reviews, one product has many reviews and each user can post many reviews. Therefore, reviews table has two foreign key constraints</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>review_id</td><td>primary key</td></tr><tr><td>review_content</td><td>content for review in varchar type</td></tr><tr><td>user_id</td><td>foreign key constraints for user id</td></tr><tr><td>product_id</td><td>foreign key constraints for product id</td></tr><tr><td>create_date</td><td>review generated date</td></tr></tbody></table><p>Finally, the most important thing is the order and the ordered item.</p><table><thead><tr><th>Field(Attribute)</th><th>Explanation</th></tr></thead><tbody><tr><td>order_id</td><td>primary key</td></tr><tr><td>order_code</td><td>unique order code</td></tr><tr><td>order_address</td><td>order_address</td></tr><tr><td>order_receiver</td><td>order_receiver</td></tr><tr><td>order_mobile</td><td>order_mobile number</td></tr><tr><td>order_user_message</td><td>order_user_message</td></tr><tr><td>create_date</td><td>create_date</td></tr><tr><td>pay_date</td><td>pay_date</td></tr><tr><td>delivery_date</td><td>delivery_date</td></tr><tr><td>confirmDate</td><td>confirmDate</td></tr><tr><td>order_status</td><td>order_status</td></tr><tr><td>user_id:</td><td>FOREIGN KEY constraints</td></tr></tbody></table><p>user table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token keyword">user</span> <span class="token punctuation">(</span> user_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> user_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> user_password <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>category table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> category <span class="token punctuation">(</span> category_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> category_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>category_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>property table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> property <span class="token punctuation">(</span> property_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> category_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> property_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>property_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_property_category <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>category_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> category <span class="token punctuation">(</span>category_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>product_table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> product <span class="token punctuation">(</span> product_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> product_name <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> product_subtitle <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> original_price <span class="token keyword">float</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> promote_price <span class="token keyword">float</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> stock <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> category_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> create_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_product_category <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>category_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> category <span class="token punctuation">(</span>category_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>property value table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> property_value <span class="token punctuation">(</span> property_value_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> product_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> property_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> property_value <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>property_value_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_propertyvalue_property <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>property_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> property <span class="token punctuation">(</span>property_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_propertyvalue_product <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> product <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>product iamge table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> product_image <span class="token punctuation">(</span> product_image_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> product_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> product_image_type <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_image_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_productimage_product <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> product <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>review table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> review <span class="token punctuation">(</span> review_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> review_content <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">4000</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> user_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> product_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> create_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>review_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_review_product <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> product <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_review_user <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> <span class="token keyword">user</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>Order table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> order_ <span class="token punctuation">(</span> order_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> order_code <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_address <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_post <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_receiver <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_mobile <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_user_message <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> create_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> pay_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> delivery_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> confirm_date <span class="token keyword">datetime</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> user_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_status <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>order_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_order_user <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> <span class="token keyword">user</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>order_item table</p><pre class=" language-sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> order_item <span class="token punctuation">(</span> order_item_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span> product_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> user_id <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> order_item_number <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>order_item_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_orderitem_user <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> <span class="token keyword">user</span> <span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_orderitem_product <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> product <span class="token punctuation">(</span>product_id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">CONSTRAINT</span> fk_orderitem_order <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>order_id<span class="token punctuation">)</span> <span class="token keyword">REFERENCES</span> order_ <span class="token punctuation">(</span>order_id<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre><p>Generate the E-R diagram by MySQL WORKBENCH.</p><div> <img src="/images/db_er_figure.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> ER diagram by MySQL</div></div><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> MySQL DB </category>
</categories>
<tags>
<tag> Software Requirements Analysis </tag>
<tag> MySQL Database Design </tag>
<tag> E-R diagram </tag>
</tags>
</entry>
<entry>
<title>How to Design your Own Online Shopping Website like amazon -- Requirements Analysis</title>
<link href="/2019-05-13-online-shopping-project-software-Requirment-Analysis.html"/>
<url>/2019-05-13-online-shopping-project-software-Requirment-Analysis.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Basically, for design an website, we mainly have 4 parts.</p><ol><li><p>Front-End display: This part is mainly about how to display the data in MySQL database, like the index page, detailed products pages, shopping carts pages and shopping categories pages.</p></li><li><p>Interaction Between Front-End and Back-End: When the clients or the admin have a serial of actions like buying a product, add a product to their shopping carts, log in and log out to the web site. This part is how you design controllers and tiny URL BY Using HTTP protocol (e.g GET and POST Method).</p></li><li><p>Back-end Management : This part is to maintain the data used in the website, like the product managements, products picture managements, user managements ,order managements ,so on and so forth.</p></li><li><p>Databases and Entities Design. In this part, we will carefully design the schema of our MySQL database. We will create the schema for each table and establish the relationships between tables. Based on those tables, we will create the classes mapping to tables.</p></li></ol><p>I will go deep into requirements analysis for each part in another blog.</p><p>Here, I will list all the tech stacks used in this project.</p><table><thead><tr><th>Database</th><th>MySQL</th></tr></thead><tbody><tr><td>server-side template engine</td><td>Java Server Page(JSP)</td></tr><tr><td>Front-End FrameWork</td><td>Bootstrap, JQuery</td></tr><tr><td>Communication between Front-end and Back-end</td><td>AJAX(In JSON data type)</td></tr><tr><td>Project Management Tool</td><td>Maven</td></tr><tr><td>Database Aceess</td><td>Mybatis</td></tr><tr><td>Web Container</td><td>Tomcat</td></tr><tr><td>Web Architecture</td><td>Spring MVC</td></tr><tr><td>Dev tools</td><td>IntelliJ IDEA</td></tr></tbody></table><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> Java </category>
<category> MySQL </category>
<category> Javascript </category>
</categories>
<tags>
<tag> Java </tag>
<tag> Software Requirements Analysis </tag>
<tag> Javascript </tag>
<tag> JSP </tag>
<tag> Bootstrap </tag>
<tag> MySQL </tag>
<tag> Front-end </tag>
<tag> Back-end </tag>
<tag> Database Design </tag>
<tag> Spring MVC </tag>
</tags>
</entry>
<entry>
<title>Introduction to CRUD operations in Neo4j</title>
<link href="/2019-05-13-CRUD-neo4j.html"/>
<url>/2019-05-13-CRUD-neo4j.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="1-Introduction"><a href="#1-Introduction" class="headerlink" title="1. Introduction:"></a>1. Introduction:</h2><p>In this section, we are going to learn and summarize the most recent used CRUD operations in Neo4j.We are going to use SET, Remove, Delete and Merge(Create and MATCH) clause.</p><h2 id="2-Merge-Command"><a href="#2-Merge-Command" class="headerlink" title="2 Merge Command"></a>2 Merge Command</h2><p>Merge commend is used to either create a node with labels or properties if the node does not exist in the database or select the nodes with particular conditions if the nodes are already presents in the database.</p><h3 id="2-1-Using-Merge-command-with-labels-and-properties"><a href="#2-1-Using-Merge-command-with-labels-and-properties" class="headerlink" title="2.1 Using Merge command with labels and properties."></a>2.1 Using Merge command with labels and properties.</h3><p>Example</p><pre class=" language-bash"><code class="language-bash">$ Merge<span class="token punctuation">(</span>Jeson:Man<span class="token punctuation">)</span> <span class="token keyword">return</span> Jeson, labels<span class="token punctuation">(</span>Jeson<span class="token punctuation">)</span></code></pre><p>Since it does not exists in the database, it will create a new node and a new label Man and return the node and labels of that specific node.</p><pre class=" language-bash"><code class="language-bash">$ Merge<span class="token punctuation">(</span>Jack:Man<span class="token punctuation">{</span>Name:<span class="token string">"Jack"</span>,Gender:<span class="token string">"male"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">return</span> Jack</code></pre><p>When running this merge command, it will firstly look at if there exists a node jack with corresponding key-value pairs, if it does not exits, it will create a new node and return the node.</p><pre class=" language-bash"><code class="language-bash">$ Merge<span class="token punctuation">(</span>Jack:Man<span class="token punctuation">{</span>Name:<span class="token string">"Jack"</span>,Age:<span class="token string">"30"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">return</span> Jack</code></pre><p>It will seperately create a node with the same name as before but differnt properties.<br>Then, if we run the following comand</p><pre class=" language-bash"><code class="language-bash">$ Merge<span class="token punctuation">(</span>Man<span class="token punctuation">{</span>Name:<span class="token string">"Jack"</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">return</span> Man.Name, Man.Gender, Man.Age</code></pre><p>It will find all the nodes with name Jack and label Man, and the properties related to jack</p><div> <img src="/images/neo4j_intro_crud1.png" alt="Smiley face" width="50%" height="50%"></div><h3 id="2-2-Using-Merge-command-to-create-Node-with-existing-node-properties"><a href="#2-2-Using-Merge-command-to-create-Node-with-existing-node-properties" class="headerlink" title="2.2 Using Merge command to create Node with existing node properties."></a>2.2 Using Merge command to create Node with existing node properties.</h3><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>n<span class="token punctuation">)</span> WHERE EXISTS<span class="token punctuation">(</span>n.Gender<span class="token punctuation">)</span>Merge <span class="token punctuation">(</span>gender:Gender<span class="token punctuation">{</span>Gender:n.Gender<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">return</span> gender</code></pre><div> <img src="/images/neo4j_intro_crud2.png" alt="Smiley face" width="50%" height="50%"></div><p>We could see that we have created all gender with matching all the nodes with Gender properties. </p><h3 id="2-3-Merge-command-with-on-create-clause"><a href="#2-3-Merge-command-with-on-create-clause" class="headerlink" title="2.3 Merge command with on create clause."></a>2.3 Merge command with on create clause.</h3><p>When creating a new node, we would like to set the properties for this node at the same time, ON CREATE clause will allows the user to set properties at creation time.</p><pre class=" language-bash"><code class="language-bash">Merge <span class="token punctuation">(</span>Yuki:Person<span class="token punctuation">{</span>Name:<span class="token string">"Yuki"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>ON CREATE SET Yuki.Gender <span class="token operator">=</span> <span class="token string">"Female"</span>RETURN Yuki.Name,Yuki.Gender</code></pre><div> <img src="/images/neo4j_intro_crud3.png" alt="Smiley face" width="50%" height="50%"></div><h3 id="2-4-Merge-command-with-on-match-clause"><a href="#2-4-Merge-command-with-on-match-clause" class="headerlink" title="2.4 Merge command with on match clause."></a>2.4 Merge command with on match clause.</h3><p>When traversing from the graph database to find some nodes with some conditions, we need to set some properties to those node at the same time for updating the information regarding to this node.</p><pre class=" language-bash"><code class="language-bash">Merge <span class="token punctuation">(</span>person:Person<span class="token punctuation">)</span>ON MATCH SET person.Is_Person <span class="token operator">=</span> TRUERETURN person.Name, person.Is_Person</code></pre><p>Using merge command we could find all the node with label Person and add a new property Is_Person to be true</p><div> <img src="/images/neo4j_intro_crud4.png" alt="Smiley face" width="50%" height="50%"></div><h3 id="2-4-Merge-command-for-relationships"><a href="#2-4-Merge-command-for-relationships" class="headerlink" title="2.4 Merge command for relationships"></a>2.4 Merge command for relationships</h3><pre class=" language-bash"><code class="language-bash">Merge <span class="token punctuation">(</span>mika:Person<span class="token punctuation">{</span>Name:<span class="token string">"mika"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>ON CREATE SET mika.Gender <span class="token operator">=</span> <span class="token string">"Male"</span>RETURN mika.Name,mika.Gender</code></pre><p>Suppose Yuki has a boyfriend mika. we want to create relationships between them.We could run the following commands</p><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>girl:Person<span class="token punctuation">)</span>,<span class="token punctuation">(</span>boy:Person<span class="token punctuation">)</span>WHERE girl.Name <span class="token operator">=</span> <span class="token string">"Yuki"</span> AND boy.Name <span class="token operator">=</span> <span class="token string">"mika"</span>MERGE <span class="token punctuation">(</span>girl<span class="token punctuation">)</span>-<span class="token punctuation">[</span>relationship:Has_A_Boy_Friend<span class="token punctuation">]</span>-<span class="token operator">></span><span class="token punctuation">(</span>boy<span class="token punctuation">)</span>RETURN girl,boy</code></pre><div> <img src="/images/neo4j_intro_crud5.png" alt="Smiley face" width="50%" height="50%"></div><h2 id="3-SET-Command"><a href="#3-SET-Command" class="headerlink" title="3 SET Command"></a>3 SET Command</h2><p>SET Clause is to set and remove properties of nodes. If Yuki goes to Tokyo University, we could run the following command. We could use set clause to add school properties to Yuki, and remove the Is_Person property we have set before.</p><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>Yuki:Person<span class="token punctuation">)</span>WHERE Yuki.Name <span class="token operator">=</span> <span class="token string">"Yuki"</span>SET Yuki.School <span class="token operator">=</span> <span class="token string">"Tokyo University"</span>SET Yuki.Is_Person <span class="token operator">=</span> NULLRETURN Yuki</code></pre><div> <img src="/images/neo4j_intro_crud6.png" alt="Smiley face" width="50%" height="50%"></div><p>We could also use SET Clause to set multiple labels for some nodes,e.g,add two labels to Yuki which are Student and Gril.</p><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>Yuki:Person<span class="token punctuation">)</span>WHERE Yuki.Name <span class="token operator">=</span> <span class="token string">"Yuki"</span>SET Yuki:Student:GrilRETURN Yuki</code></pre><div> <img src="/images/neo4j_intro_crud7.png" alt="Smiley face" width="50%" height="50%"></div><h2 id="4-DELETE-Command"><a href="#4-DELETE-Command" class="headerlink" title="4 DELETE Command"></a>4 DELETE Command</h2><p>DELETE command is to delete specific nodes or relationships between nodes. Here we will learn some of the basic usage of delete clause in CQL.</p><h3 id="4-1-DELETE-ALL-NODES"><a href="#4-1-DELETE-ALL-NODES" class="headerlink" title="4.1 DELETE ALL NODES"></a>4.1 DELETE ALL NODES</h3><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>node<span class="token punctuation">)</span>DETACH DELETE node</code></pre><p>DETACH is to firstly delete the relationships between the nodes that we are going to delete.</p><h3 id="4-2-DELETE-GROUPS-OF-NODES-WITH-CONDITIONS"><a href="#4-2-DELETE-GROUPS-OF-NODES-WITH-CONDITIONS" class="headerlink" title="4.2 DELETE GROUPS OF NODES WITH CONDITIONS"></a>4.2 DELETE GROUPS OF NODES WITH CONDITIONS</h3><p>It is quite similar but still notice that when deleting a node, we should delete the relationships first.</p><pre class=" language-bash"><code class="language-bash">MATCH <span class="token punctuation">(</span>mika:Person<span class="token punctuation">)</span>WHERE mika.Name <span class="token operator">=</span> <span class="token string">"mika"</span>DETACH DELETE mika</code></pre><p>Next, I will go through an example to have a overall understanding of CRUD By SQL.</p><p>TO BE CONTINUED</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> DB </category>
</categories>
<tags>
<tag> Graph Database </tag>
<tag> NoSQL </tag>
<tag> database </tag>
<tag> dynamic schema </tag>
<tag> Cypher Query Language </tag>
<tag> CRUD </tag>
</tags>
</entry>
<entry>
<title>neo4j_1_introducion</title>
<link href="/2019-05-09-neo4j-1-introducion.html"/>
<url>/2019-05-09-neo4j-1-introducion.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="1-Download-and-installation"><a href="#1-Download-and-installation" class="headerlink" title="1. Download and installation"></a>1. Download and installation</h2><p>Neo4j is a very popular, widely-used No-SQL graph database. Let us first try to download and install neo4j on our own laptop. Go to their official webpages, and try to download it on your own system.<a href="https://neo4j.com/download-center/" target="_blank" rel="noopener">Download Center</a> .(I am using Win10 and for learning usage, download Neo4j Community Edition 3.5.5). After download the starting file, let us try to start the server next.</p><h3 id="1-1-start-the-server"><a href="#1-1-start-the-server" class="headerlink" title="1.1 start the server"></a>1.1 start the server</h3><p>open an console and cd to your the install directory of neo4j. run the following command</p><pre class=" language-bash"><code class="language-bash">$ <span class="token function">cd</span> <span class="token operator"><</span>your the <span class="token function">install</span> directory of neo4j<span class="token operator">></span>$ bin\neo4j console</code></pre><p>you will get</p><pre><code>INFO ======== Neo4j 3.5.5 ========INFO Starting...INFO Bolt enabled on 127.0.0.1:7687.INFO Started.Remote interface available at http://localhost:7474/</code></pre><p>then, goes to your browser and enter <a href="http://localhost:7474/" target="_blank" rel="noopener">http://localhost:7474/</a>. you will see the following web interface.</p><div> <img src="/images/neo4j_intro_1.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> Figure 1.1</div></div><h3 id="1-2-Authenticated-connection-to-the-database"><a href="#1-2-Authenticated-connection-to-the-database" class="headerlink" title="1.2 Authenticated connection to the database."></a>1.2 Authenticated connection to the database.</h3><p>Connect using the default username ‘neo4j’ with default password ‘neo4j’. It will leading to a instruction for you to set your own password for conection.</p><p>Now we are done.</p><div> <img src="/images/neo4j_intro_2.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> Figure 1.2</div></div><h2 id="2-Neo4j-Data-Model-amp-Architecture"><a href="#2-Neo4j-Data-Model-amp-Architecture" class="headerlink" title="2. Neo4j: Data Model & Architecture"></a>2. Neo4j: Data Model & Architecture</h2><p>Neo4j uses Native Graph Processing Engineer(GPE) to store the graph. To be specific, data are stored in the form of <strong><em>Nodes</em></strong> and properties related to that data are stored as <strong><em>key-value pairs</em></strong>, where Key is the property name and value is property value. For example, if I have one worker named Kitty and has some properties like, Id, gender ,department etc. So the node looks like the following:</p><div> <img src="/images/neo4j_intro_3.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> Data in Neo4j</div></div><p>Here, the node has a <strong><em>label</em></strong> called worker. As we known, each work is employee of one company. So, an edge describes the relationship between labels.</p><div> <img src="/images/neo4j_intro_4.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px">relationship between labels</div></div><p>In order to do some manipulations on the database, let us first know about the data type supported in Cypher Query Language (CQL).</p><h2 id="2-Cypher-Query-Language-CQL"><a href="#2-Cypher-Query-Language-CQL" class="headerlink" title="2. Cypher Query Language (CQL)."></a>2. Cypher Query Language (CQL).</h2><h3 id="2-1-Creating-Single-and-Multiple-Nodes-with-label"><a href="#2-1-Creating-Single-and-Multiple-Nodes-with-label" class="headerlink" title="2.1 Creating Single and Multiple Nodes with label"></a>2.1 Creating Single and Multiple Nodes with label</h3><p>Syntax:</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Node_Name_1<span class="token punctuation">)</span>,<span class="token punctuation">(</span>Node_Name_2<span class="token punctuation">)</span>,<span class="token punctuation">..</span>.</code></pre><p>Example</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Student<span class="token punctuation">)</span>,<span class="token punctuation">(</span>School<span class="token punctuation">)</span>,<span class="token punctuation">(</span>Subject<span class="token punctuation">)</span></code></pre><p>When we creating a node, we could add labels to that node, the syntax is :<br>Syntax:</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Node_Name:Node_label<span class="token punctuation">)</span></code></pre><p>Example</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Kitty:Worker<span class="token punctuation">)</span></code></pre><p>To view all the nodes we have created before, using match clause without any conditions to get all results</p><pre class=" language-bash"><code class="language-bash">$ MATCH <span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token keyword">return</span> n</code></pre><p>We can see all the nodes created before, notice that, each node in the graph has its own id, and neo4j automatically generated id for each node.</p><div> <img src="/images/neo4j_intro_5.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> Data in Neo4j</div></div><p>Each node can have not only one labels, but also have multiple labels.</p><p>Syntax:</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Node_Name:Node_label_1:Node_label_2:Node_label_3:<span class="token punctuation">..</span>.<span class="token punctuation">)</span></code></pre><p>Example</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>John:Teacher:Student:Professor<span class="token punctuation">)</span></code></pre><h3 id="2-2-Creating-Nodes-with-properties"><a href="#2-2-Creating-Nodes-with-properties" class="headerlink" title="2.2 Creating Nodes with properties"></a>2.2 Creating Nodes with properties</h3><p>As above discussion, nodes are actually key-value pairs,<br>Syntax:</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Node_Name:Node_label<span class="token punctuation">{</span><span class="token operator"><</span>key<span class="token operator">></span>:<span class="token operator"><</span>value<span class="token operator">></span>, <span class="token operator"><</span>key<span class="token operator">></span>:<span class="token operator"><</span>value<span class="token operator">></span>, <span class="token punctuation">..</span>.<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre><p>Example</p><pre class=" language-bash"><code class="language-bash">$ Create <span class="token punctuation">(</span>Kitty:Worker<span class="token punctuation">{</span>Gender:<span class="token string">"Female"</span>, Department:<span class="token string">"IT service"</span><span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre><p>After run the MATCH CLAUSE, we could see the result.</p><div> <img src="/images/neo4j_intro_6.png" alt="Smiley face" width="50%" height="50%"></div><h3 id="2-3-Creating-Nodes-with-properties"><a href="#2-3-Creating-Nodes-with-properties" class="headerlink" title="2.3 Creating Nodes with properties"></a>2.3 Creating Nodes with properties</h3><p>Before we establishing relationship between different nodes, let’s look at how to use where clause.<br>Syntax:</p><pre class=" language-bash"><code class="language-bash">$ MATCH<span class="token punctuation">(</span>node:Node_label<span class="token punctuation">)</span> WHERE node.property<span class="token operator">=</span><span class="token string">"some_value"</span> RETURN node<span class="token punctuation">;</span></code></pre><p>Example : find all the female employees.</p><pre class=" language-bash"><code class="language-bash">$ MATCH <span class="token punctuation">(</span>worker:Worker<span class="token punctuation">)</span> WHERE worker.Gender <span class="token operator">=</span> <span class="token string">"Female"</span> RETURN worker<span class="token punctuation">;</span></code></pre><h3 id="2-4-Create-Relations-between-nodes"><a href="#2-4-Create-Relations-between-nodes" class="headerlink" title="2.4 Create Relations between nodes."></a>2.4 Create Relations between nodes.</h3><p>Syntax: Create nodes and creating relationships between them</p><pre class=" language-bash"><code class="language-bash">CREATE <span class="token punctuation">(</span>node1<span class="token punctuation">)</span>,<span class="token punctuation">(</span>node2<span class="token punctuation">)</span>,<span class="token punctuation">(</span>node3<span class="token punctuation">)</span>,<span class="token punctuation">(</span>node1<span class="token punctuation">)</span>-<span class="token punctuation">[</span>r:Relation1<span class="token punctuation">]</span>-<span class="token operator">></span> <span class="token punctuation">(</span>node2<span class="token punctuation">)</span>,<span class="token punctuation">(</span>node2<span class="token punctuation">)</span> -<span class="token punctuation">[</span>r:Relation1<span class="token punctuation">]</span>-<span class="token operator">></span> <span class="token punctuation">(</span>node3<span class="token punctuation">)</span></code></pre><p>Syntax : Select existing nodes and creating relationships between them.</p><pre class=" language-bash"><code class="language-bash">MATCH<span class="token punctuation">(</span>a:Node_label<span class="token punctuation">)</span>,<span class="token punctuation">(</span>b:Node_label<span class="token punctuation">)</span> WHERE a.ID <span class="token operator">=</span> ??? AND b.ID<span class="token operator">=</span>??? CREATE <span class="token punctuation">(</span>b<span class="token punctuation">)</span>-<span class="token punctuation">[</span>r:R<span class="token punctuation">]</span> -<span class="token operator">></span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span></code></pre><p>Example:</p><pre class=" language-bash"><code class="language-bash">CREATE<span class="token punctuation">(</span>Tony:Software_Engineer<span class="token punctuation">{</span>name: <span class="token string">"Tony"</span>, Gender: <span class="token string">"Male"</span>, Department: <span class="token string">"IT Dev"</span>, Age: 35<span class="token punctuation">}</span><span class="token punctuation">)</span>,<span class="token punctuation">(</span>IBM:Company<span class="token punctuation">{</span>name:<span class="token string">"IBM"</span><span class="token punctuation">}</span><span class="token punctuation">)</span>, <span class="token punctuation">(</span>Tony<span class="token punctuation">)</span>-<span class="token punctuation">[</span>r:Is_Employee_Of<span class="token punctuation">]</span>-<span class="token operator">></span><span class="token punctuation">(</span>IBM<span class="token punctuation">)</span> RETURN Tony, IBM</code></pre><div> <img src="/images/neo4j_intro_7.png" alt="Smiley face" width="50%" height="50%"></div><p>Example: Create relationships between existing nodes.</p><pre class=" language-bash"><code class="language-bash">MATCH<span class="token punctuation">(</span>software_eigneer:Software_Engineer<span class="token punctuation">)</span>,<span class="token punctuation">(</span>company:Company<span class="token punctuation">)</span> WHERE software_eigneer.name <span class="token operator">=</span> <span class="token string">"Tony"</span> AND company.name<span class="token operator">=</span><span class="token string">"IBM"</span> CREATE <span class="token punctuation">(</span>company<span class="token punctuation">)</span>-<span class="token punctuation">[</span>relation:Hires<span class="token punctuation">]</span> -<span class="token operator">></span> <span class="token punctuation">(</span>software_eigneer<span class="token punctuation">)</span> RETURN software_eigneer, company</code></pre><p>Results:Java</p><div> <img src="/images/neo4j_intro_8.png" alt="Smiley face" width="50%" height="50%"></div><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> DB </category>
</categories>
<tags>
<tag> Graph Database </tag>
<tag> NoSQL </tag>
<tag> dynamic schema </tag>
<tag> Cypher Query Language </tag>
<tag> introduction </tag>
</tags>
</entry>
<entry>
<title>Design-Pattern -- Decorator Pattern</title>
<link href="/2019-05-08-DecoratorPattern.html"/>
<url>/2019-05-08-DecoratorPattern.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="1-Definition"><a href="#1-Definition" class="headerlink" title="1. Definition:"></a>1. Definition:</h1><p>Let us first look at the strict definition of Decorator Pattern,</p><blockquote><p><strong>Decorator Pattern attaches additional responsibilities to one Object dynamically, without affecting the behaviour of other objects from the same class. Decorator provides a flexible alternative to sub-classes for extending functionalities. (<a href="https://en.wikipedia.org/wiki/Decorator_pattern" target="_blank" rel="noopener">Wiki_Link_For_More_Info</a>)</strong></p></blockquote><br><p>Detail of the definition:</p><ul><li><strong>dynamically</strong> : The meaning at dynamically means that those attached responsibilities are not <strong>*hard-coded</strong> in to the object class <strong>_at compile time_</strong>. Instead, these responsibilities are added <strong>_at runtime time_</strong> by different Decorator. At the same time, one type of object could have multiple types of decorators, so a client can choose any one or any combination of them to decorate the object at runtime.</li><li><strong>flexible alternative</strong>: decorator pattern using <strong>_Composition_</strong> rather than <strong>_Inheritance_</strong>. Since inheritance is not used, the original class functionalities will not be changed.</li></ul><p>Before going to the UML of decorator pattern, let us look at the design principles it bases on.</p><ol><li><p><strong>Open Close Principle</strong>: Software entities like classes, modules and functions should be <strong>_open for extension_</strong> but <strong>_closed for modifications_</strong>. When the requirements have been changed for one class, it is not recommended to go into that class and change the code, instead, using abstract class and concrete classes for implementing their behaviour. In different scenario, using different concrete classes. In decorator pattern, Component are usually interface or abstract class.</p></li><li><p><strong>Single responsibilities Principle</strong>: A class should have <strong>_only one reason_</strong> to change. This means that if we have 2 reasons to change for a class, we have to split the functionality in two classes. In decorator pattern, each concrete decorator has only changed one responsibility of the original class. In order to get multiple responsibilities changed, using composition of decorators.</p></li></ol><p>Now, let us draw a picture to see what a decorator and component relation actually is.</p><div> <img src="/images/decorator_1.png" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> decorator and Component relation</div></div><p>Notice</p><ol><li>Decorator <strong>_has-a_</strong> component of that type. Decorator is a wrapper of component. It has a reference or pointer to the component object so that it can invoke the method defines in that component.</li><li>Meanwhile, Decorator <strong>_is-a_</strong> component. Decorator itself must also be a component of that type. So, a decorator can be viewed as a component and wrapped by another Decorator. (Very similar to recursion.)</li></ol><h1 id="2-General-UML"><a href="#2-General-UML" class="headerlink" title="2. General UML:"></a>2. General UML:</h1><div> <img src="/images/decorator_2.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> UML for decorator pattern</div></div><p>Here, I implement the UML as an example with slightly differences interface Component:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Component</span> <span class="token keyword">implements</span> <span class="token class-name">IComponent</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I am a base method in concrete Component"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Abstract Class decorator:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Decorator</span> <span class="token keyword">implements</span> <span class="token class-name">IComponent</span><span class="token punctuation">{</span><span class="token comment" spellcheck="true">// it is a component</span> <span class="token keyword">private</span> IComponent component<span class="token punctuation">;</span><span class="token comment" spellcheck="true">// it has-a component</span> <span class="token keyword">public</span> <span class="token function">Decorator</span><span class="token punctuation">(</span>IComponent component<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>component <span class="token operator">=</span> component<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> component<span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// invoke base method</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Concrete decorator A:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DecoratorA</span> <span class="token keyword">extends</span> <span class="token class-name">Decorator</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token function">DecoratorA</span><span class="token punctuation">(</span>IComponent component<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>component<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">preOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"this is a pre-operation defined in decoratorA"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* * Add a pre-operation before base Operation * */</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">preOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Concrete decorator B:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DecoratorB</span> <span class="token keyword">extends</span> <span class="token class-name">Decorator</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//decorator can have new properties and method.</span> <span class="token keyword">private</span> String newSate <span class="token operator">=</span> <span class="token string">"I am a new State"</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">DecoratorB</span><span class="token punctuation">(</span>IComponent component<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>component<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">postOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"this is a post-operation defined in decoratorB"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">postOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setNewSate</span><span class="token punctuation">(</span>String newSate<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>newSate <span class="token operator">=</span> newSate<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getNewSate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> newSate<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Test Main_function:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> IComponent component <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Component</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> component<span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"------------------------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> IComponent concreteDecortorA <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DecoratorA</span><span class="token punctuation">(</span>component<span class="token punctuation">)</span><span class="token punctuation">;</span> concreteDecortorA<span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// operation after decorating by A</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"------------------------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> IComponent concrteDecortorB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DecoratorB</span><span class="token punctuation">(</span>component<span class="token punctuation">)</span><span class="token punctuation">;</span> concrteDecortorB<span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// operation after decorating by B</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"------------------------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// decorator of decorator, by passing concreteDecortorA as parameter of decoratorB</span> IComponent compositeDecortor <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DecoratorB</span><span class="token punctuation">(</span>concreteDecortorA<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// first invoke operation defined by concreteDecortorA, concreteDecortorA will invoke the operation defined by the</span> <span class="token comment" spellcheck="true">// base component</span> compositeDecortor<span class="token punctuation">.</span><span class="token function">operation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Output:</p><pre><code>I am a base method in concrete Component------------------------------this is a pre-operation defined in decoratorAI am a base method in concrete Component------------------------------I am a base method in concrete Componentthis is a post-operation defined in decoratorB------------------------------this is a pre-operation defined in decoratorAI am a base method in concrete Componentthis is a post-operation defined in decoratorB</code></pre><p>Comments: For composite decorator, it takes another decorator as a component, and when it invoke the operation() method, it will invoke the operation defined by the component(which is also a decorator) , and that component recursively invoke the base component(which is actually a real component), it invokes the base method ,and returns the result to its decorator, and that decorator returns the result to the composite decorator.</p><h1 id="3-Java-IO-InputStream"><a href="#3-Java-IO-InputStream" class="headerlink" title="3. Java IO - InputStream:"></a>3. Java IO - InputStream:</h1><p>java.io.inputStream uses the decorator Pattern. From the book <a href="https://www.amazon.com/dp/0596007124/?tag=stackoverfl08-20" target="_blank" rel="noopener">Head First Design Patterns</a>,we can see how they use decorator Pattern in java to add functionalities for reading data from files.</p><div> <img src="/images/decorator_3.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> how decorator pattern used in java.io</div></div><div> <img src="/images/decorator_4.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> java.io.inputStream hierarchy </div></div><p>The usage of decorator pattern in java.io also points to the _drawback_ of decorator pattern. There are _so many decorators implementation classes_. It can be confusing for clients who call those decorator-API. It is somehow hard when those clients can not tell the difference between each decorators. However, once they know, they can combine different decorators for their own applications.</p><p>Now, let us create a own type of Input Stream and test it.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UpperCaseInputStream</span> <span class="token keyword">extends</span> <span class="token class-name">FileInputStream</span> <span class="token punctuation">{</span><span class="token comment" spellcheck="true">// is-a</span> <span class="token comment" spellcheck="true">// Constructors which matches the super class FileInputStream</span> <span class="token keyword">private</span> FileInputStream fileInputStream<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//has-a</span> <span class="token keyword">public</span> <span class="token function">UpperCaseInputStream</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span>FileInputStream fileInputStream<span class="token punctuation">)</span> <span class="token keyword">throws</span> FileNotFoundException <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>fileInputStream <span class="token operator">=</span> fileInputStream<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">UpperCaseInputStream</span><span class="token punctuation">(</span>File file<span class="token punctuation">,</span>FileInputStream fileInputStream<span class="token punctuation">)</span> <span class="token keyword">throws</span> FileNotFoundException <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>fileInputStream <span class="token operator">=</span> fileInputStream<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">UpperCaseInputStream</span><span class="token punctuation">(</span>FileDescriptor fdObj<span class="token punctuation">,</span>FileInputStream fileInputStream<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>fdObj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>fileInputStream <span class="token operator">=</span> fileInputStream<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token keyword">int</span> nextByte <span class="token operator">=</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>nextByte <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">return</span> Character<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>nextByte<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token keyword">int</span> totalBytes <span class="token operator">=</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>totalBytes <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">convertToUppercase</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> totalBytes<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> totalBytes<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> off<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token keyword">int</span> totalBytes <span class="token operator">=</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> off<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>totalBytes <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// convert all the character in byte array to uppercase letter</span> <span class="token function">convertToUppercase</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> off<span class="token punctuation">,</span> off <span class="token operator">+</span> totalBytes<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> totalBytes<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">convertToUppercase</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token punctuation">,</span> <span class="token keyword">int</span> end<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> idx <span class="token operator">=</span> start<span class="token punctuation">;</span> idx <span class="token operator"><</span> end<span class="token punctuation">;</span> idx<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> b<span class="token punctuation">[</span>idx<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">)</span> Character<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>b<span class="token punctuation">[</span>idx<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>Test our UpperCaseInputStream:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UpperCaseInputStreamTest</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> String filePath <span class="token operator">=</span> <span class="token string">"data/text_file.txt"</span><span class="token punctuation">;</span> FileInputStream fileinputStream <span class="token operator">=</span> null<span class="token punctuation">;</span> UpperCaseInputStream upperCaseInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1\. Create a component with one base class: FileInputStream</span> fileinputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//2\. Create our customized decorator with the base component</span> upperCaseInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UpperCaseInputStream</span><span class="token punctuation">(</span>filePath<span class="token punctuation">,</span> fileinputStream<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//3.1Test read once at a time</span> <span class="token comment" spellcheck="true">/*try { do{ System.out.print((char) upperCaseInputStream.read()); }while (upperCaseInputStream.read() != -1); } catch (IOException e) { e.printStackTrace(); }*/</span> <span class="token comment" spellcheck="true">//3.2 Test read into byte array buffer</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> length<span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>length <span class="token operator">=</span> upperCaseInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> length<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//4\. release the resources</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> upperCaseInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fileinputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>OutPut:</p><pre><code>BCDEFGHIJKLMNGKIERERU</code></pre><p>text_file.txt :</p><pre><code>bcdefghijklmnGKiereru</code></pre><p>Hence, our UpperCaseInputStream meets the expectation.</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> Java </category>
</categories>
<tags>
<tag> OOD </tag>
<tag> Design Pattern </tag>
<tag> Java </tag>
<tag> Decorator Pattern </tag>
<tag> IO </tag>
<tag> InputStream </tag>
</tags>
</entry>
<entry>
<title>Design-Pattern -- Observer Pattern</title>
<link href="/2019-05-07-ObserverPattern.html"/>
<url>/2019-05-07-ObserverPattern.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="1-Definition"><a href="#1-Definition" class="headerlink" title="1. Definition:"></a>1. Definition:</h2><p> Let us first look at the strict definition of Observer Pattern, </p><blockquote style="text-align:justify;background-color: #ccfff5"> **Observer Pattern defines a One-to-many dependencies between objects. Once the Object change state, all of their dependencies are notified and changed automatically.**</blockquote> <br><p> There are several terminology used in the Observer Pattern , which are as follows:</p><ul><li><p><strong>Observable</strong> : This object has some attributes which may change frequently. It is also known as Subject.</p></li><li><p><strong>Observer</strong>: The object which has to receive those frequently modified attributes from the Observable objects. They are also known as Object.</p></li><li><p><strong>One-to-many dependencies</strong>: There is only one Observable objects and many Observers related to it. This means that the Observable object has a collection of Observers who subscribe it.</p></li></ul><p>The following figure shows their relationship.</p><div> <img src="/images/observer_1.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px">1 - n relationship </div></div><p>Observer Pattern usually used in the scenario that one object are active to have some properties changed quite often while at the same time, some objects wants to know some information from that object as soon as possible. For example, one temperature sensor versus some distributed diplays who show the temperature in differnt way, one writer who post articles versus several readers who subscribe him or her.</p><h2 id="2-UML"><a href="#2-UML" class="headerlink" title="2. UML:"></a>2. UML:</h2><p> Now, let us see the general UML diagram for Observer pattern.</p><div> <img src="/images/observer_2.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> UML diagram</div></div><p>For observable interface it defines three behaviours of observable object.</p><ol><li>add method by passing an object who implements observer interface;</li><li>delete method by passing an object who implements observer interface;</li><li>nofityAll method: it iterates all observers and call the update method defined in each observer.</li></ol><p>For the Concrete Observable who implements the interface, it need to have two properties, which are</p><ol><li>State state : the frequently changed state</li><li>A Collection of Observers who want to get the state from the Observable, it can be either list, set or dictionary depending on different application for insertion, deletion and iteration of observers.</li></ol><p>Two important things to be understand about Observer Pattern.</p><ol><li><p><strong>The constructer of Concrete Observer should have one argument which is the reference of the observable it want to subscribe. Not only Observable should have a set of observers, but also each observer should know the follower so that they can invoke the getState() method to update the state.</strong></p></li><li><p><strong>The arguments type in constructors and methods should be in interface type rather than entities cos the <em>interface design principle</em> and <em>least knowledge design principle</em>.We could casting them to entity type if necessary in some methods.</strong></p></li></ol><h2 id="3-Example-and-Implementation-in-Java"><a href="#3-Example-and-Implementation-in-Java" class="headerlink" title="3. Example and Implementation in Java:"></a>3. Example and Implementation in Java:</h2><p>Now, let us see an Example and implement it by using java.</p><p>Example:</p><pre><code> There is a Publishing House who can publish new books. Meanwhile, here are readers who can follow and unfollow this publishing house. Once the publish house publish a new book, it will broadcast notification to all its followers. Write program to mimic the process.</code></pre><p>First, implement two interfaces.<br>IObservable:</p><pre><code>public interface IObservable { void addObserver(IObserver observer); void removeObserver(IObserver observer); void notifyAllObservers();}</code></pre><p>IObserver:</p><pre><code>public interface IObserver { void update();}</code></pre><p>Then, implements Publisher and Reader as concrete implementation classes.<br>Observable: Publisher</p><pre><code>import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;public class Publisher implements IObservable { private String publisherName; private Set<IObserver> readers; // set of observers which are in type of Reader class private List<String> books ; // list of new published book //no-args constructors public Publisher() { super(); readers = new HashSet<>(); books = new ArrayList<>(); } public Publisher(String publisherName){ this.publisherName = publisherName; readers = new HashSet<>(); books = new ArrayList<>(); } public void publishNewBook(String bookName){ if(bookName == null) return; books.add(bookName); this.notifyAllObservers(); } @Override public void addObserver(IObserver observer) { // if the observer is null or if it is not an instance of Reader, return; if (observer == null || !(observer instanceof Reader)) return; // if it already subscribed the publisher if (readers.contains(observer)) return; readers.add(observer); Reader newReader = (Reader)observer; // cast to Reader object System.out.println(newReader.getReaderName()+" has subscribed " + this.getPublisherName()); } @Override public void removeObserver(IObserver observer) { if(readers.contains(observer)){ readers.remove(observer); Reader reader = (Reader)observer; System.out.println(reader.getReaderName()+" has unfollowed " + this.getPublisherName()); } } @Override public synchronized void notifyAllObservers() { // iterate all observers and notify them. for(IObserver reader: readers){ reader.update(); } } // Getters public String getLatestPublishedBookName(){ return books.get(books.size()-1); } public String getPublisherName() { return publisherName; } public void setPublisherName(String publisherName) { this.publisherName = publisherName; }}</code></pre><p>Observer: Reader</p><pre><code>public class Reader implements IObserver { private String readerName; private Publisher publisher; public Reader(String name, Publisher publisher){ this.readerName = name; this.publisher = publisher;//dependencies injection } @Override public void update() { System.out.println(this.readerName + " Knows that the publisher published a new book, called " + this.publisher.getLatestPublishedBookName()); } public String getReaderName() { return readerName; }}</code></pre><p>Finally, write the main function to mimic the process.</p><pre><code>public class ObserverPatternTest { public static void main(String[] args) throws InterruptedException { Publisher publisher = new Publisher("BOOK STORE 1"); Reader tom = new Reader("Tom",publisher); Reader ben = new Reader("Ben",publisher); Reader alice = new Reader("Alice",publisher); publisher.addObserver(tom); publisher.addObserver(ben); publisher.addObserver(alice); publisher.publishNewBook("Introduction to Design Pattern "); System.out.println("A few months later"); Thread.currentThread().sleep(1000); Reader mike = new Reader("Mike",publisher); publisher.addObserver(mike); // new subscriber publisher.removeObserver(tom); Thread.currentThread().sleep(1000); publisher.publishNewBook("Algorithms and Data Structure"); }}</code></pre><p>Output:</p><pre><code>Tom has subscribed BOOK STORE 1Ben has subscribed BOOK STORE 1Alice has subscribed BOOK STORE 1Ben Knows that the publisher published a new book, called Introduction to Design PatternTom Knows that the publisher published a new book, called Introduction to Design PatternAlice Knows that the publisher published a new book, called Introduction to Design PatternA few months laterMike has subscribed BOOK STORE 1Tom has unfollowed BOOK STORE 1Ben Knows that the publisher published a new book, called Algorithms and Data StructureMike Knows that the publisher published a new book, called Algorithms and Data StructureAlice Knows that the publisher published a new book, called Algorithms and Data Structure</code></pre><p>We can view the automatically generated UML diagram by IntelliJ IDEA.</p><div> <img src="/images/observer_3.jpg" alt="Smiley face" width="50%" height="50%"> <div class="text" style="text-align:center;font-size:10px"> UML diagram by IntelliJ</div></div><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<categories>
<category> Java </category>
</categories>
<tags>
<tag> OOD </tag>
<tag> Design Pattern </tag>
<tag> Java </tag>
<tag> Observer Pattern </tag>
<tag> IntelliJ IDEA </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2019-05-04-hello-world.html"/>
<url>/2019-05-04-hello-world.html</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><p>DataBase</p><p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><pre class=" language-bash"><code class="language-bash">$ hexo server</code></pre><p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><pre class=" language-bash"><code class="language-bash">$ hexo generate</code></pre><p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><pre class=" language-bash"><code class="language-bash">$ hexo deploy</code></pre><p>More info: <a href="https://hexo.io/docs/deployment.html" target="_blank" rel="noopener">Deployment</a></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
</entry>
</search>