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.query.api.stream.sequence; |
19 | |
20 | import org.wso2.siddhi.query.api.query.QueryEventStream; |
21 | import org.wso2.siddhi.query.api.stream.SingleStream; |
22 | import org.wso2.siddhi.query.api.stream.Stream; |
23 | import org.wso2.siddhi.query.api.definition.StreamDefinition; |
24 | import org.wso2.siddhi.query.api.stream.sequence.element.RegexElement; |
25 | import org.wso2.siddhi.query.api.stream.sequence.element.NextElement; |
26 | import org.wso2.siddhi.query.api.stream.sequence.element.OrElement; |
27 | import org.wso2.siddhi.query.api.stream.sequence.element.SequenceElement; |
28 | |
29 | import java.util.ArrayList; |
30 | import java.util.HashSet; |
31 | import java.util.List; |
32 | import java.util.Map; |
33 | |
34 | public class SequenceStream implements Stream, SequenceElement { |
35 | private SequenceElement sequenceElement; |
36 | private List<String> streamIdList; |
37 | |
38 | public SequenceStream(SequenceElement sequenceElement) { |
39 | this.sequenceElement = sequenceElement; |
40 | this.streamIdList = new ArrayList<String>(collectStreamIds(sequenceElement, new HashSet<String>())); |
41 | } |
42 | |
43 | public SequenceElement getSequenceElement() { |
44 | return sequenceElement; |
45 | } |
46 | |
47 | @Override |
48 | public List<String> getStreamIds() { |
49 | return streamIdList; |
50 | } |
51 | |
52 | @Override |
53 | public List<QueryEventStream> constructQueryEventStreamList( |
54 | Map<String, StreamDefinition> streamDefinitionMap, |
55 | List<QueryEventStream> queryEventStreams) { |
56 | return constructEventStreamList(getSequenceElement(), streamDefinitionMap, queryEventStreams); |
57 | } |
58 | private HashSet<String> collectStreamIds(SequenceElement sequenceElement, |
59 | HashSet<String> streamIds) { |
60 | if (sequenceElement instanceof SequenceStream) { |
61 | streamIds.addAll(((SequenceStream) sequenceElement).getStreamIds()); |
62 | } else if (sequenceElement instanceof SingleStream) { |
63 | streamIds.addAll(((SingleStream) sequenceElement).getStreamIds()); |
64 | } else if (sequenceElement instanceof OrElement) { |
65 | collectStreamIds(((OrElement) sequenceElement).getSingleStream1(), streamIds); |
66 | collectStreamIds(((OrElement) sequenceElement).getSingleStream2(), streamIds); |
67 | } else if (sequenceElement instanceof RegexElement) { |
68 | collectStreamIds(((RegexElement) sequenceElement).getSingleStream(), streamIds); |
69 | } else if (sequenceElement instanceof NextElement) { |
70 | collectStreamIds(((NextElement) sequenceElement).getSequenceElement(), streamIds); |
71 | collectStreamIds(((NextElement) sequenceElement).getNextSequenceElement(), streamIds); |
72 | } |
73 | return streamIds; |
74 | } |
75 | |
76 | public List<QueryEventStream> constructEventStreamList(SequenceElement sequenceElement, |
77 | Map<String, StreamDefinition> streamDefinitionMap, |
78 | List<QueryEventStream> queryEventStreams) { |
79 | |
80 | |
81 | if (sequenceElement instanceof SingleStream) { |
82 | ((SingleStream) sequenceElement).constructQueryEventStreamList(streamDefinitionMap, queryEventStreams); |
83 | } else if (sequenceElement instanceof OrElement) { |
84 | constructEventStreamList(((OrElement) sequenceElement).getSingleStream1(), streamDefinitionMap, queryEventStreams); |
85 | constructEventStreamList(((OrElement) sequenceElement).getSingleStream2(), streamDefinitionMap, queryEventStreams); |
86 | } else if (sequenceElement instanceof RegexElement) { |
87 | constructEventStreamList(((RegexElement) sequenceElement).getSingleStream(), streamDefinitionMap, queryEventStreams); |
88 | } else if (sequenceElement instanceof NextElement) { |
89 | constructEventStreamList(((NextElement) sequenceElement).getSequenceElement(), streamDefinitionMap, queryEventStreams); |
90 | constructEventStreamList(((NextElement) sequenceElement).getNextSequenceElement(), streamDefinitionMap, queryEventStreams); |
91 | } else if (sequenceElement instanceof SequenceStream) { |
92 | ((SequenceStream) sequenceElement).constructQueryEventStreamList(streamDefinitionMap, queryEventStreams); |
93 | } |
94 | |
95 | return queryEventStreams; |
96 | } |
97 | } |