| 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.util.parser; |
| 19 | |
| 20 | import org.wso2.siddhi.core.exception.OperationNotSupportedException; |
| 21 | import org.wso2.siddhi.core.executor.conditon.AndConditionExecutor; |
| 22 | import org.wso2.siddhi.core.executor.conditon.BooleanConditionExecutor; |
| 23 | import org.wso2.siddhi.core.executor.conditon.ConditionExecutor; |
| 24 | import org.wso2.siddhi.core.executor.conditon.NotConditionExecutor; |
| 25 | import org.wso2.siddhi.core.executor.conditon.OrConditionExecutor; |
| 26 | import org.wso2.siddhi.core.executor.expression.ConstantExpressionExecutor; |
| 27 | import org.wso2.siddhi.core.executor.expression.ExpressionExecutor; |
| 28 | import org.wso2.siddhi.core.executor.expression.VariableExpressionExecutor; |
| 29 | import org.wso2.siddhi.query.api.condition.AndCondition; |
| 30 | import org.wso2.siddhi.query.api.condition.BooleanCondition; |
| 31 | import org.wso2.siddhi.query.api.condition.Compare; |
| 32 | import org.wso2.siddhi.query.api.condition.Condition; |
| 33 | import org.wso2.siddhi.query.api.condition.NotCondition; |
| 34 | import org.wso2.siddhi.query.api.condition.OrCondition; |
| 35 | import org.wso2.siddhi.query.api.definition.Attribute; |
| 36 | import org.wso2.siddhi.query.api.expression.Expression; |
| 37 | import org.wso2.siddhi.query.api.expression.Variable; |
| 38 | import org.wso2.siddhi.query.api.expression.constant.BoolConstant; |
| 39 | import org.wso2.siddhi.query.api.expression.constant.Constant; |
| 40 | import org.wso2.siddhi.query.api.expression.constant.DoubleConstant; |
| 41 | import org.wso2.siddhi.query.api.expression.constant.FloatConstant; |
| 42 | import org.wso2.siddhi.query.api.expression.constant.IntConstant; |
| 43 | import org.wso2.siddhi.query.api.expression.constant.LongConstant; |
| 44 | import org.wso2.siddhi.query.api.expression.constant.StringConstant; |
| 45 | import org.wso2.siddhi.query.api.query.QueryEventStream; |
| 46 | |
| 47 | import java.util.List; |
| 48 | |
| 49 | public class ExecutorParser { |
| 50 | |
| 51 | |
| 52 | public static ConditionExecutor parseCondition(Condition condition, |
| 53 | List<QueryEventStream> queryEventStreamList, |
| 54 | String currentStreamReference) { |
| 55 | if (condition instanceof AndCondition) { |
| 56 | return new AndConditionExecutor(parseCondition(((AndCondition) condition).getLeftCondition(), queryEventStreamList, currentStreamReference), |
| 57 | parseCondition(((AndCondition) condition).getRightCondition(), queryEventStreamList, currentStreamReference)); |
| 58 | } else if (condition instanceof OrCondition) { |
| 59 | return new OrConditionExecutor(parseCondition(((OrCondition) condition).getLeftCondition(), queryEventStreamList, currentStreamReference), |
| 60 | parseCondition(((OrCondition) condition).getRightCondition(), queryEventStreamList, currentStreamReference)); |
| 61 | } else if (condition instanceof NotCondition) { |
| 62 | return new NotConditionExecutor(parseCondition(((NotCondition) condition).getCondition(), queryEventStreamList, currentStreamReference)); |
| 63 | } else if (condition instanceof BooleanCondition) { |
| 64 | return new BooleanConditionExecutor(parseExpression(((BooleanCondition) condition).getExpression(), queryEventStreamList, currentStreamReference)); |
| 65 | } else if (condition instanceof Compare) { |
| 66 | if (((Compare) condition).getOperator() == Condition.Operator.EQUAL) { |
| 67 | return ComparatorParser.parseEqualCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 68 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 69 | } else if (((Compare) condition).getOperator() == Condition.Operator.NOT_EQUAL) { |
| 70 | return ComparatorParser.parseNotEqualCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 71 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 72 | } else if (((Compare) condition).getOperator() == Condition.Operator.GREATER_THAN) { |
| 73 | return ComparatorParser.parseGreaterThanCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 74 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 75 | } else if (((Compare) condition).getOperator() == Condition.Operator.GREATER_THAN_EQUAL) { |
| 76 | return ComparatorParser.parseGreaterThanEqualCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 77 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 78 | } else if (((Compare) condition).getOperator() == Condition.Operator.LESS_THAN) { |
| 79 | return ComparatorParser.parseLessThanCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 80 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 81 | } else if (((Compare) condition).getOperator() == Condition.Operator.LESS_THAN_EQUAL) { |
| 82 | return ComparatorParser.parseLessThanEqualCompare(parseExpression(((Compare) condition).getLeftExpression(), queryEventStreamList, currentStreamReference), |
| 83 | parseExpression(((Compare) condition).getRightExpression(), queryEventStreamList, currentStreamReference)); |
| 84 | |
| 85 | } |
| 86 | } |
| 87 | throw new OperationNotSupportedException(condition + " not supported !"); |
| 88 | } |
| 89 | |
| 90 | public static ExpressionExecutor parseExpression(Expression expression, |
| 91 | List<QueryEventStream> queryEventStreamList, |
| 92 | String currentStreamReference) { |
| 93 | if (expression instanceof Constant) { |
| 94 | if (expression instanceof BoolConstant) { |
| 95 | return new ConstantExpressionExecutor(((BoolConstant) expression).getValue(), Attribute.Type.BOOL); |
| 96 | } else if (expression instanceof StringConstant) { |
| 97 | return new ConstantExpressionExecutor(((StringConstant) expression).getValue(), Attribute.Type.STRING); |
| 98 | } else if (expression instanceof IntConstant) { |
| 99 | return new ConstantExpressionExecutor(((IntConstant) expression).getValue(), Attribute.Type.INT); |
| 100 | } else if (expression instanceof LongConstant) { |
| 101 | return new ConstantExpressionExecutor(((LongConstant) expression).getValue(), Attribute.Type.LONG); |
| 102 | } else if (expression instanceof FloatConstant) { |
| 103 | return new ConstantExpressionExecutor(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT); |
| 104 | } else if (expression instanceof DoubleConstant) { |
| 105 | return new ConstantExpressionExecutor(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE); |
| 106 | } |
| 107 | } else if (expression instanceof Variable) { |
| 108 | return new VariableExpressionExecutor(((Variable) expression).getStreamId(), ((Variable) expression).getAttributeName(), ((Variable) expression).getPosition(), queryEventStreamList, currentStreamReference); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | throw new UnsupportedOperationException(expression.toString() + " not supported!"); |
| 113 | |
| 114 | } |
| 115 | } |