| 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.sequence; |
| 19 | |
| 20 | import org.wso2.siddhi.core.event.StreamEvent; |
| 21 | import org.wso2.siddhi.core.event.in.StateEvent; |
| 22 | import org.wso2.siddhi.core.util.SchedulerQueue; |
| 23 | import org.wso2.siddhi.core.statemachine.sequence.SequenceState; |
| 24 | import org.wso2.siddhi.core.stream.StreamElement; |
| 25 | import org.wso2.siddhi.core.stream.StreamProcessor; |
| 26 | import org.wso2.siddhi.core.stream.recevier.StreamReceiver; |
| 27 | |
| 28 | import java.util.LinkedList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | public class SequenceSingleStreamReceiver implements StreamReceiver, StreamElement { |
| 32 | protected int complexEventSize; |
| 33 | protected SequenceState state; |
| 34 | protected SequenceState nextState; |
| 35 | protected StreamProcessor firstSimpleStreamProcessor; |
| 36 | protected List<StateEvent> currentEvents = new LinkedList<StateEvent>(); |
| 37 | protected List<StateEvent> nextEvents = new LinkedList<StateEvent>(); |
| 38 | // private final boolean first; |
| 39 | protected final int currentState; |
| 40 | |
| 41 | |
| 42 | public SequenceSingleStreamReceiver(SequenceState state, |
| 43 | StreamProcessor firstSimpleStreamProcessor, |
| 44 | int complexEventSize) { |
| 45 | this.state = state; |
| 46 | this.nextState = state.getNextState(); |
| 47 | this.currentState = state.getStateNumber(); |
| 48 | this.complexEventSize = complexEventSize; |
| 49 | this.firstSimpleStreamProcessor = firstSimpleStreamProcessor; |
| 50 | this.firstSimpleStreamProcessor.setPrevious(this); |
| 51 | // init(state, complexEventSize); |
| 52 | } |
| 53 | |
| 54 | public void init() { |
| 55 | if (state.isFirst()) { |
| 56 | //first event |
| 57 | addToNextEvents(new StateEvent(new StreamEvent[complexEventSize])); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void receive(StreamEvent streamEvent) { |
| 63 | if (streamEvent instanceof SequenceResetEvent) { |
| 64 | //System.out.println("Before Sequence Reset || currentEvents=" +currentEvents.size()+" nextEvents="+nextEvents.size()); |
| 65 | currentEvents.clear(); |
| 66 | nextEvents.clear(); |
| 67 | //System.out.println("After Sequence Reset || currentEvents=" +currentEvents.size()+" nextEvents="+nextEvents.size()); |
| 68 | }else { |
| 69 | sendForProcess((StreamEvent) streamEvent); |
| 70 | } |
| 71 | init(); |
| 72 | |
| 73 | // currentEvents.clear(); |
| 74 | // } |
| 75 | } |
| 76 | |
| 77 | protected void sendForProcess(StreamEvent event) { |
| 78 | //System.out.println("sr state=" +currentState+" event="+ event+" ||currentEvents="+currentEvents); |
| 79 | for (StateEvent currentEvent : currentEvents) { |
| 80 | currentEvent.setStreamEvent(currentState, event); |
| 81 | firstSimpleStreamProcessor.process(currentEvent); |
| 82 | if ( currentEvent.getEventState()<currentState) { |
| 83 | currentEvent.setStreamEvent(currentState, null); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public String getStreamId() { |
| 89 | return state.getSingleStream().getStreamId(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public SchedulerQueue<StreamEvent> getWindow() { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | public synchronized void addToNextEvents(StateEvent stateEvent) { |
| 98 | // //System.out.println("add to next ss"); |
| 99 | nextEvents.add(stateEvent); |
| 100 | } |
| 101 | |
| 102 | public synchronized void moveNextEventsToCurrentEvents() { |
| 103 | //todo need to check which is faster |
| 104 | // 1 |
| 105 | // currentEvents.clear(); |
| 106 | // currentEvents.addAll(nextEvents); |
| 107 | // nextEvents.clear(); |
| 108 | |
| 109 | // // 2 |
| 110 | currentEvents = nextEvents; |
| 111 | nextEvents = new LinkedList<StateEvent>(); |
| 112 | } |
| 113 | |
| 114 | } |