Guest User

Untitled

a guest
Jun 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. @Test
  2. public void testRelaxedFollowedByStrictContiguity() throws Exception {
  3. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  4.  
  5. DataStream<Event> input = env.fromElements(
  6. new Event(1, "pre-condition", 1.0),
  7. new Event(2, "start", 2.0),
  8. new Event(3, "middle", 3.0),
  9. new Event(4, "start", 4.0),
  10. new Event(5, "middle", 5.0),
  11. new Event(6, "end", 6.0),
  12. new Event(7, "end", 3.0)
  13. );
  14.  
  15. Pattern<Event, ?> pattern = Pattern.<Event>begin("pre-condition")
  16. .where(new SimpleCondition<Event>() {
  17. @Override
  18. public boolean filter(Event value) throws Exception {
  19. return value.getName().equalsIgnoreCase("pre-condition");
  20. }
  21. }).followedBy("start")
  22. .where(new SimpleCondition<Event>() {
  23. @Override
  24. public boolean filter(Event value) throws Exception {
  25. return value.getName().equalsIgnoreCase("start");
  26. }
  27. }).next("middle")
  28. .where(new SimpleCondition<Event>() {
  29. @Override
  30. public boolean filter(Event value) throws Exception {
  31. return value.getName().equalsIgnoreCase("middle");
  32. }
  33. }).next("end")
  34. .where(new SimpleCondition<Event>() {
  35. @Override
  36. public boolean filter(Event value) throws Exception {
  37. return value.getName().equalsIgnoreCase("end");
  38. }
  39. });
  40.  
  41. DataStream<String> result = CEP.pattern(input, pattern).select(new PatternSelectFunction<Event, String>() {
  42.  
  43. @Override
  44. public String select(Map<String, List<Event>> pattern) {
  45. StringBuilder builder = new StringBuilder();
  46.  
  47. builder.append(pattern.get("pre-condition").get(0).getId()).append(",")
  48. .append(pattern.get("start").get(0).getId()).append(",")
  49. .append(pattern.get("middle").get(0).getId()).append(",")
  50. .append(pattern.get("end").get(0).getId());
  51.  
  52. return builder.toString();
  53. }
  54. });
  55.  
  56. result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE);
  57.  
  58. // expected sequence of matching event ids
  59. expected = "1,4,5,6";
  60.  
  61. env.execute();
  62. }
  63.  
  64.  
  65. @Test
  66. public void testStrictFollowedByRelaxedContiguity() throws Exception {
  67. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  68.  
  69. DataStream<Event> input = env.fromElements(
  70. new Event(1, "pre-condition", 1.0),
  71. new Event(2, "start", 2.0),
  72. new Event(3, "middle", 3.0),
  73. new Event(4, "start", 4.0),
  74. new Event(5, "middle", 5.0),
  75. new Event(6, "end", 6.0),
  76. new Event(7, "start", 3.0),
  77. new Event(8, "post-condition", 1.0)
  78. );
  79.  
  80. Pattern<Event, ?> pattern = Pattern.<Event>begin("start")
  81. .where(new SimpleCondition<Event>() {
  82. @Override
  83. public boolean filter(Event value) throws Exception {
  84. return value.getName().equalsIgnoreCase("start");
  85. }
  86. }).next("middle")
  87. .where(new SimpleCondition<Event>() {
  88. @Override
  89. public boolean filter(Event value) throws Exception {
  90. return value.getName().equalsIgnoreCase("middle");
  91. }
  92. }).next("end")
  93. .where(new SimpleCondition<Event>() {
  94. @Override
  95. public boolean filter(Event value) throws Exception {
  96. return value.getName().equalsIgnoreCase("end");
  97. }
  98. }).followedBy("post-condition")
  99. .where(new SimpleCondition<Event>() {
  100. @Override
  101. public boolean filter(Event value) throws Exception {
  102. return value.getName().equalsIgnoreCase("post-condition");
  103. }
  104. });
  105.  
  106. DataStream<String> result = CEP.pattern(input, pattern).select(new PatternSelectFunction<Event, String>() {
  107.  
  108. @Override
  109. public String select(Map<String, List<Event>> pattern) {
  110. StringBuilder builder = new StringBuilder();
  111.  
  112. builder .append(pattern.get("start").get(0).getId()).append(",")
  113. .append(pattern.get("middle").get(0).getId()).append(",")
  114. .append(pattern.get("end").get(0).getId()).append(",")
  115. .append(pattern.get("post-condition").get(0).getId());
  116.  
  117. return builder.toString();
  118. }
  119. });
  120.  
  121. result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE);
  122.  
  123. // expected sequence of matching event ids
  124. expected = "4,5,6,8";
  125.  
  126. env.execute();
  127. }
Add Comment
Please, Sign In to add comment