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.pattern; |
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.pattern.element.CountElement; |
25 | import org.wso2.siddhi.query.api.stream.pattern.element.FollowedByElement; |
26 | import org.wso2.siddhi.query.api.stream.pattern.element.LogicalElement; |
27 | import org.wso2.siddhi.query.api.stream.pattern.element.PatternElement; |
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 PatternStream implements Stream, PatternElement { |
35 | |
36 | private PatternElement patternElement; |
37 | private List<String> streamIdList; |
38 | |
39 | public PatternStream(PatternElement patternElement) { |
40 | this.patternElement = patternElement; |
41 | this.streamIdList = new ArrayList<String>(collectStreamIds(patternElement, new HashSet<String>())); |
42 | } |
43 | |
44 | @Override |
45 | public List<String> getStreamIds() { |
46 | return streamIdList; |
47 | } |
48 | |
49 | @Override |
50 | public List<QueryEventStream> constructQueryEventStreamList( |
51 | Map<String, StreamDefinition> streamDefinitionMap, |
52 | List<QueryEventStream> queryEventStreams) { |
53 | return constructEventStreamList(patternElement, streamDefinitionMap, queryEventStreams); |
54 | } |
55 | |
56 | public PatternElement getPatternElement() { |
57 | return patternElement; |
58 | } |
59 | |
60 | private HashSet<String> collectStreamIds(PatternElement patternElement, |
61 | HashSet<String> streamIds) { |
62 | if (patternElement instanceof PatternStream) { |
63 | streamIds.addAll(((PatternStream) patternElement).getStreamIds()); |
64 | } else if (patternElement instanceof SingleStream) { |
65 | streamIds.addAll(((SingleStream) patternElement).getStreamIds()); |
66 | } else if (patternElement instanceof LogicalElement) { |
67 | collectStreamIds(((LogicalElement) patternElement).getSingleStream1(), streamIds); |
68 | collectStreamIds(((LogicalElement) patternElement).getSingleStream2(), streamIds); |
69 | } else if (patternElement instanceof CountElement) { |
70 | collectStreamIds(((CountElement) patternElement).getSingleStream(), streamIds); |
71 | } else if (patternElement instanceof FollowedByElement) { |
72 | collectStreamIds(((FollowedByElement) patternElement).getPatternElement(), streamIds); |
73 | collectStreamIds(((FollowedByElement) patternElement).getFollowedByPatternElement(), streamIds); |
74 | } |
75 | return streamIds; |
76 | } |
77 | |
78 | public List<QueryEventStream> constructEventStreamList(PatternElement patternElement, |
79 | Map<String, StreamDefinition> streamDefinitionMap, |
80 | List<QueryEventStream> queryEventStreams) { |
81 | |
82 | |
83 | if (patternElement instanceof SingleStream) { |
84 | ((SingleStream) patternElement).constructQueryEventStreamList(streamDefinitionMap, queryEventStreams); |
85 | } else if (patternElement instanceof LogicalElement) { |
86 | constructEventStreamList(((LogicalElement) patternElement).getSingleStream1(), streamDefinitionMap, queryEventStreams); |
87 | constructEventStreamList(((LogicalElement) patternElement).getSingleStream2(), streamDefinitionMap, queryEventStreams); |
88 | } else if (patternElement instanceof CountElement) { |
89 | constructEventStreamList(((CountElement) patternElement).getSingleStream(), streamDefinitionMap, queryEventStreams); |
90 | } else if (patternElement instanceof FollowedByElement) { |
91 | constructEventStreamList(((FollowedByElement) patternElement).getPatternElement(), streamDefinitionMap, queryEventStreams); |
92 | constructEventStreamList(((FollowedByElement) patternElement).getFollowedByPatternElement(), streamDefinitionMap, queryEventStreams); |
93 | } else if (patternElement instanceof PatternStream) { |
94 | ((PatternStream) patternElement).constructQueryEventStreamList(streamDefinitionMap, queryEventStreams); |
95 | } |
96 | |
97 | return queryEventStreams; |
98 | } |
99 | } |