1 | /* |
2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
3 | * |
4 | * WSO2 Inc. licenses this file to you under the Apache License, |
5 | * Version 2.0 (the "License"); you may not use this file except |
6 | * in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, |
12 | * software distributed under the License is distributed on an |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | * KIND, either express or implied. See the License for the |
15 | * specific language governing permissions and limitations |
16 | * under the License. |
17 | */ |
18 | package org.wso2.siddhi.core.stream.recevier.pattern; |
19 | |
20 | import org.wso2.siddhi.core.event.Event; |
21 | import org.wso2.siddhi.core.event.ListEvent; |
22 | import org.wso2.siddhi.core.event.StreamEvent; |
23 | import org.wso2.siddhi.core.event.in.InListEvent; |
24 | import org.wso2.siddhi.core.event.in.StateEvent; |
25 | import org.wso2.siddhi.core.statemachine.pattern.CountPatternState; |
26 | import org.wso2.siddhi.core.stream.StreamProcessor; |
27 | |
28 | public class CountPatternSingleStreamReceiver extends PatternSingleStreamReceiver { |
29 | |
30 | private int min = -1; |
31 | private int max = -1; |
32 | private boolean passed; |
33 | |
34 | public CountPatternSingleStreamReceiver(CountPatternState state, |
35 | StreamProcessor firstSimpleStreamProcessor, |
36 | int complexEventSize) { |
37 | super(state, firstSimpleStreamProcessor, complexEventSize); |
38 | this.min = state.getMin(); |
39 | this.max = state.getMax(); |
40 | |
41 | } |
42 | |
43 | @Override |
44 | public void receive(StreamEvent event) { |
45 | //System.out.println("cr state=" + currentState + " event=" + event + " ||currentEvents=" + currentEvents); |
46 | for (StateEvent currentEvent : currentEvents) { |
47 | |
48 | if (currentEvent.getEventState() <= (state.getStateNumber())) { |
49 | ListEvent listEvent = (ListEvent) currentEvent.getStreamEvent(currentState); |
50 | if (listEvent == null) { |
51 | listEvent = new InListEvent(max); |
52 | currentEvent.setStreamEvent(currentState, listEvent); |
53 | } |
54 | setPassed(false); |
55 | // //System.out.println("---" + currentEvent); |
56 | if (!listEvent.addEvent(((Event) event))) { |
57 | continue; |
58 | } |
59 | // //System.out.println("-+-" + currentEvent); |
60 | firstSimpleStreamProcessor.process(currentEvent); |
61 | |
62 | if (!isPassed()) { |
63 | listEvent.removeLast(); //to stop aggregation of not passed events |
64 | nextEvents.add(currentEvent); //only to add to itself |
65 | } |
66 | } |
67 | } |
68 | // currentEvents.clear(); |
69 | // } |
70 | } |
71 | |
72 | // protected InComplexEvent createFirstEvent(int complexEventSize) { |
73 | // return new InComplexEvent(new SingleEventBundle[complexEventSize]); |
74 | // } |
75 | |
76 | public synchronized void addToNextEvents(StateEvent stateEvent) { |
77 | // //System.out.println("in"); |
78 | if (min == 0) { |
79 | state.getPatternStreamPacker().process(stateEvent); |
80 | // if (stateEvent.getEventState() < currentState) { |
81 | // if (nextState != null) { |
82 | // if (nextState instanceof LogicPatternState) { |
83 | // ((LogicPatternState) nextState).getPartnerState().getPatternSingleStreamReceiver().addToNextEvents(stateEvent); |
84 | // } |
85 | // nextState.getPatternSingleStreamReceiver().addToNextEvents(stateEvent); |
86 | //// nextEvents.add(stateEvent);//to keep the reference of the next events |
87 | // } |
88 | // if (nextEveryState != null) { |
89 | // StateEvent newComplexEvent = stateEvent.cloneEvent(nextEveryState.getStateNumber()); |
90 | // newComplexEvent.setEventState(nextEveryState.getStateNumber()-1); |
91 | // if (nextEveryState instanceof LogicPatternState) { |
92 | // ((LogicPatternState) nextEveryState).getPartnerState().getPatternSingleStreamReceiver().addToNextEvents(newComplexEvent); |
93 | // } |
94 | // nextEveryState.getPatternSingleStreamReceiver().addToNextEvents(newComplexEvent); |
95 | // } |
96 | // } |
97 | } else { |
98 | nextEvents.add(stateEvent); |
99 | } |
100 | |
101 | } |
102 | |
103 | public synchronized void setPassed(boolean passed) { |
104 | this.passed = passed; |
105 | } |
106 | |
107 | public synchronized boolean isPassed() { |
108 | return passed; |
109 | } |
110 | |
111 | public void addOnlyToNextEvents(StateEvent stateEvent) { |
112 | nextEvents.add(stateEvent); |
113 | } |
114 | } |