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.packer.pattern; |
19 | |
20 | import org.wso2.siddhi.core.event.AtomicEvent; |
21 | import org.wso2.siddhi.core.event.in.StateEvent; |
22 | import org.wso2.siddhi.core.event.ComplexEvent; |
23 | import org.wso2.siddhi.core.statemachine.pattern.LogicPatternState; |
24 | import org.wso2.siddhi.core.statemachine.pattern.PatternState; |
25 | import org.wso2.siddhi.core.stream.packer.SingleStreamPacker; |
26 | import org.wso2.siddhi.core.stream.recevier.pattern.PatternSingleStreamReceiver; |
27 | |
28 | public class PatternStreamPacker extends SingleStreamPacker { |
29 | |
30 | protected PatternSingleStreamReceiver streamReceiver; |
31 | protected PatternState state; |
32 | protected PatternState nextState; |
33 | protected PatternState nextEveryState; |
34 | |
35 | public PatternStreamPacker(PatternState state) { |
36 | this.state = state; |
37 | this.nextState = state.getNextState(); |
38 | this.nextEveryState = state.getNextEveryState(); |
39 | } |
40 | |
41 | public void setStreamReceiver(PatternSingleStreamReceiver streamReceiver) { |
42 | this.streamReceiver = streamReceiver; |
43 | } |
44 | |
45 | @Override |
46 | public void process(ComplexEvent complexEvent) { |
47 | //System.out.println("pp state=" + state.getStateNumber() + " event=" + complexEvent); |
48 | setEventState((StateEvent) complexEvent); |
49 | if (state.isLast()) { |
50 | sendEvent((StateEvent)complexEvent); |
51 | } |
52 | passToStreamReceivers((StateEvent) complexEvent); |
53 | } |
54 | |
55 | protected void setEventState(StateEvent eventBundle) { |
56 | eventBundle.setEventState(state.getStateNumber()); |
57 | } |
58 | |
59 | protected void sendEvent(AtomicEvent atomicEvent) { |
60 | queryProjector.process(atomicEvent); |
61 | } |
62 | |
63 | protected void passToStreamReceivers(StateEvent eventBundle) { |
64 | if (nextState != null) { |
65 | //System.out.println("->" + nextState.getStateNumber()); |
66 | if (nextState instanceof LogicPatternState) { |
67 | //System.out.println("->" + ((LogicPatternState) nextState).getPartnerState().getStateNumber()); |
68 | ((LogicPatternState) nextState).getPartnerState().getPatternSingleStreamReceiver().addToNextEvents(eventBundle); |
69 | } |
70 | nextState.getPatternSingleStreamReceiver().addToNextEvents(eventBundle); |
71 | } |
72 | if (nextEveryState != null) { |
73 | //System.out.println("->" + nextEveryState.getStateNumber()); |
74 | StateEvent newStateEvent = eventBundle.cloneEvent(nextEveryState.getStateNumber()); |
75 | newStateEvent.setEventState(nextEveryState.getStateNumber() - 1); |
76 | if (nextEveryState instanceof LogicPatternState) { |
77 | //System.out.println("->" + ((LogicPatternState) nextEveryState).getPartnerState().getStateNumber()); |
78 | ((LogicPatternState) nextEveryState).getPartnerState().getPatternSingleStreamReceiver().addToNextEvents(newStateEvent); |
79 | } |
80 | nextEveryState.getPatternSingleStreamReceiver().addToNextEvents(newStateEvent); |
81 | } |
82 | } |
83 | } |