1 | /* |
2 | * Copyright (c) 2005-2012, 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.projector.attibute.generator.groupby; |
19 | |
20 | import org.wso2.siddhi.core.event.AtomicEvent; |
21 | import org.wso2.siddhi.core.executor.expression.ExpressionExecutor; |
22 | import org.wso2.siddhi.core.projector.attibute.generator.AbstractAggregateAttributeGenerator; |
23 | import org.wso2.siddhi.core.util.parser.ExecutorParser; |
24 | import org.wso2.siddhi.query.api.definition.Attribute; |
25 | import org.wso2.siddhi.query.api.expression.Expression; |
26 | import org.wso2.siddhi.query.api.expression.Variable; |
27 | import org.wso2.siddhi.query.api.query.QueryEventStream; |
28 | |
29 | import java.util.List; |
30 | |
31 | public class GroupByOutputAttributeGenerator extends AbstractAggregateAttributeGenerator { |
32 | |
33 | private ExpressionExecutor[] groupByExecutors; |
34 | |
35 | //stateful |
36 | private GroupByAttributeGeneratorMap attributeGeneratorMap; |
37 | private AbstractAggregateAttributeGenerator attributeGenerator; |
38 | |
39 | public GroupByOutputAttributeGenerator(List<Variable> groupByList, |
40 | List<QueryEventStream> queryEventStreamList) { |
41 | groupByExecutors = new ExpressionExecutor[groupByList.size()]; |
42 | for (int i = 0, expressionsSize = groupByList.size(); i < expressionsSize; i++) { |
43 | groupByExecutors[i] = ExecutorParser.parseExpression(groupByList.get(i), queryEventStreamList, null); |
44 | } |
45 | |
46 | } |
47 | |
48 | private GroupByOutputAttributeGenerator(ExpressionExecutor[] groupByExecutors) { |
49 | this.groupByExecutors = groupByExecutors; |
50 | } |
51 | |
52 | public GroupByOutputAttributeGenerator assignAggregateAttributeGenerator( |
53 | AbstractAggregateAttributeGenerator attributeGenerator) { |
54 | this.attributeGeneratorMap = new GroupByAttributeGeneratorMap(groupByExecutors, attributeGenerator); |
55 | this.attributeGenerator = attributeGenerator; |
56 | return this; |
57 | } |
58 | |
59 | @Override |
60 | public void assignExpressions(Expression[] expressions, |
61 | List<QueryEventStream> queryEventStreamList) { |
62 | attributeGenerator.assignExpressions(expressions, queryEventStreamList); |
63 | } |
64 | |
65 | |
66 | @Override |
67 | public GroupByOutputAttributeGenerator createNewInstance() { |
68 | return new GroupByOutputAttributeGenerator(groupByExecutors); |
69 | |
70 | } |
71 | |
72 | @Override |
73 | public Attribute.Type getType() { |
74 | return attributeGenerator.getType(); |
75 | } |
76 | |
77 | @Override |
78 | public Object process(AtomicEvent event) { |
79 | return attributeGeneratorMap.get(event).process(event); |
80 | } |
81 | } |