| 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.ComplexEvent; |
| 21 | import org.wso2.siddhi.core.event.ListEvent; |
| 22 | import org.wso2.siddhi.core.event.in.StateEvent; |
| 23 | import org.wso2.siddhi.core.statemachine.sequence.CountSequenceState; |
| 24 | import org.wso2.siddhi.core.statemachine.sequence.OrSequenceState; |
| 25 | import org.wso2.siddhi.core.stream.recevier.sequence.CountSequenceSingleStreamReceiver; |
| 26 | import org.wso2.siddhi.query.api.utils.SiddhiConstants; |
| 27 | |
| 28 | public class CountSequenceStreamPacker extends SequenceStreamPacker { |
| 29 | |
| 30 | public CountSequenceStreamPacker(CountSequenceState state) { |
| 31 | super(state); |
| 32 | } |
| 33 | |
| 34 | public void process(ComplexEvent complexEvent) { |
| 35 | //System.out.println("cp state=" + state.getStateNumber() + " event=" + complexEvent); |
| 36 | ((CountSequenceSingleStreamReceiver) streamReceiver).setPassed(true); |
| 37 | if (((StateEvent) complexEvent).getEventState() <= (state.getStateNumber())) { |
| 38 | setEventState((StateEvent) complexEvent); |
| 39 | int activeEvents = 0; |
| 40 | ListEvent listEvent = ((ListEvent) ((StateEvent) complexEvent).getStreamEvent(state.getStateNumber())); |
| 41 | if (listEvent != null) { |
| 42 | activeEvents = listEvent.getActiveEvents(); |
| 43 | } |
| 44 | // //System.out.println(" active "+activeEvents); |
| 45 | // int activeEvents = ((SingleEventList) ((StateEvent) complexEvent).getStreamEvent(state.getStateNumber())).getActiveEvents(); |
| 46 | if (state.isLast()) { |
| 47 | sendEvent((StateEvent)complexEvent); |
| 48 | } |
| 49 | //passToStreamReceivers |
| 50 | if (activeEvents < ((CountSequenceState) state).getMin()) { |
| 51 | //System.out.println("->" + state.getStateNumber()); |
| 52 | ((CountSequenceSingleStreamReceiver) streamReceiver).addOnlyToNextEvents((StateEvent) complexEvent); |
| 53 | } else if (activeEvents >= ((CountSequenceState) state).getMin() && activeEvents <= ((CountSequenceState) state).getMax()||activeEvents >= ((CountSequenceState) state).getMin() && ((CountSequenceState) state).getMax()== SiddhiConstants.UNLIMITED) { |
| 54 | if (nextState != null) { |
| 55 | //System.out.println("->" + nextState.getStateNumber()); |
| 56 | if (nextState instanceof OrSequenceState) { |
| 57 | //System.out.println("->" + ((OrSequenceState) nextState).getPartnerState().getStateNumber()); |
| 58 | ((OrSequenceState) nextState).getPartnerState().getSequenceSingleStreamReceiver().addToNextEvents((StateEvent) complexEvent); |
| 59 | } |
| 60 | nextState.getSequenceSingleStreamReceiver().addToNextEvents((StateEvent) complexEvent); |
| 61 | ((CountSequenceSingleStreamReceiver) streamReceiver).addOnlyToNextEvents((StateEvent) complexEvent); |
| 62 | } |
| 63 | // } else if (activeEvents >= ((CountSequenceState) state).getMin() && activeEvents <= ((CountSequenceState) state).getMax()||activeEvents >= ((CountSequenceState) state).getMin() && ((CountSequenceState) state).getMax()==RegexElement.UNLIMITED) { |
| 64 | // //System.out.println("->" + state.getStateNumber()); |
| 65 | // ((CountSequenceSingleStreamReceiver) streamReceiver).addOnlyToNextEvents((StateEvent) complexEvent); |
| 66 | //// if (state.getNextStateListSize() > 0) { |
| 67 | //// for (State nextState : state.getNextStates()) { |
| 68 | //// StateEvent complexEvent = ((StateEvent) complexEvent).cloneEvent(); |
| 69 | //// if (nextState instanceof LogicState) { |
| 70 | //// ((LogicState) nextState).getPartnerState().getPatternSingleStreamReceiver().addToNextEvents(complexEvent); |
| 71 | //// } |
| 72 | //// nextState.getPatternSingleStreamReceiver().addToNextEvents(complexEvent); |
| 73 | //// } |
| 74 | //// } |
| 75 | |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |