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.sequence; |
19 | |
20 | import org.wso2.siddhi.core.event.AtomicEvent; |
21 | import org.wso2.siddhi.core.event.ComplexEvent; |
22 | import org.wso2.siddhi.core.event.in.StateEvent; |
23 | import org.wso2.siddhi.core.statemachine.sequence.OrSequenceState; |
24 | import org.wso2.siddhi.core.statemachine.sequence.SequenceState; |
25 | import org.wso2.siddhi.core.stream.packer.SingleStreamPacker; |
26 | import org.wso2.siddhi.core.stream.recevier.sequence.SequenceSingleStreamReceiver; |
27 | |
28 | public class SequenceStreamPacker extends SingleStreamPacker { |
29 | |
30 | protected SequenceSingleStreamReceiver streamReceiver; |
31 | protected SequenceState state; |
32 | protected SequenceState nextState; |
33 | |
34 | public SequenceStreamPacker(SequenceState state) { |
35 | this.state = state; |
36 | this.nextState = state.getNextState(); |
37 | } |
38 | |
39 | public void setStreamReceiver(SequenceSingleStreamReceiver streamReceiver) { |
40 | this.streamReceiver = streamReceiver; |
41 | } |
42 | |
43 | @Override |
44 | public void process(ComplexEvent complexEvent) { |
45 | //System.out.println("sp state=" + state.getStateNumber() + " event=" + complexEvent); |
46 | setEventState((StateEvent) complexEvent); |
47 | if (state.isLast()) { |
48 | sendEvent((StateEvent)complexEvent); |
49 | } |
50 | passToStreamReceivers((StateEvent) complexEvent); |
51 | } |
52 | |
53 | protected void setEventState(StateEvent eventBundle) { |
54 | eventBundle.setEventState(state.getStateNumber()); |
55 | } |
56 | |
57 | protected void sendEvent(AtomicEvent atomicEvent) { |
58 | queryProjector.process(atomicEvent); |
59 | } |
60 | |
61 | protected void passToStreamReceivers(StateEvent eventBundle) { |
62 | if (nextState != null) { |
63 | //System.out.println("->" + nextState.getStateNumber()); |
64 | if (nextState instanceof OrSequenceState) { |
65 | //System.out.println("->" + ((OrSequenceState) nextState).getPartnerState().getStateNumber()); |
66 | ((OrSequenceState) nextState).getPartnerState().getSequenceSingleStreamReceiver().addToNextEvents(eventBundle); |
67 | } |
68 | nextState.getSequenceSingleStreamReceiver().addToNextEvents(eventBundle); |
69 | } |
70 | } |
71 | } |