| 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.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.pattern.PatternState; |
| 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 PatternSingleStreamReceiver implements StreamReceiver, StreamElement { |
| 32 | protected int complexEventSize; |
| 33 | protected PatternState state; |
| 34 | protected PatternState nextState; |
| 35 | protected PatternState nextEveryState; |
| 36 | protected StreamProcessor firstSimpleStreamProcessor; |
| 37 | protected List<StateEvent> currentEvents = new LinkedList<StateEvent>(); |
| 38 | protected List<StateEvent> nextEvents = new LinkedList<StateEvent>(); |
| 39 | // private final boolean first; |
| 40 | protected final int currentState; |
| 41 | |
| 42 | |
| 43 | public PatternSingleStreamReceiver(PatternState state, |
| 44 | StreamProcessor firstSimpleStreamProcessor, |
| 45 | int complexEventSize) { |
| 46 | this.state = state; |
| 47 | this.nextState = state.getNextState(); |
| 48 | this.nextEveryState = state.getNextEveryState(); |
| 49 | this.currentState = state.getStateNumber(); |
| 50 | this.complexEventSize = complexEventSize; |
| 51 | this.firstSimpleStreamProcessor = firstSimpleStreamProcessor; |
| 52 | this.firstSimpleStreamProcessor.setPrevious(this); |
| 53 | // init(state, complexEventSize); |
| 54 | } |
| 55 | |
| 56 | public void init() { |
| 57 | if (state.isFirst()) { |
| 58 | //first event |
| 59 | addToNextEvents(new StateEvent(new StreamEvent[complexEventSize])); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void receive(StreamEvent event) { |
| 65 | //System.out.println("pr state=" +currentState+" event="+ event+" ||currentEvents="+currentEvents); |
| 66 | for (StateEvent currentEvent : currentEvents) { |
| 67 | |
| 68 | currentEvent.setStreamEvent(currentState, event); |
| 69 | firstSimpleStreamProcessor.process(currentEvent); |
| 70 | if ( currentEvent.getEventState()<currentState) { |
| 71 | currentEvent.setStreamEvent(currentState, null); |
| 72 | addToNextEvents(currentEvent); |
| 73 | } |
| 74 | } |
| 75 | // currentEvents.clear(); |
| 76 | // } |
| 77 | } |
| 78 | |
| 79 | public String getStreamId() { |
| 80 | return state.getSingleStream().getStreamId(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public SchedulerQueue<StreamEvent> getWindow() { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | public synchronized void addToNextEvents(StateEvent stateEvent) { |
| 89 | // //System.out.println("add to next ss"); |
| 90 | nextEvents.add(stateEvent); |
| 91 | } |
| 92 | |
| 93 | public synchronized void moveNextEventsToCurrentEvents() { |
| 94 | //todo need to check which is faster |
| 95 | // 1 |
| 96 | // currentEvents.clear(); |
| 97 | // currentEvents.addAll(nextEvents); |
| 98 | // nextEvents.clear(); |
| 99 | |
| 100 | // // 2 |
| 101 | currentEvents = nextEvents; |
| 102 | nextEvents = new LinkedList<StateEvent>(); |
| 103 | } |
| 104 | |
| 105 | } |