2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (C) 2018 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.core.sli;
26 import java.util.LinkedList;
27 import java.util.List;
29 import org.antlr.v4.runtime.tree.TerminalNode;
30 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AddExprContext;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AtomContext;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.CompareExprContext;
33 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ConstantContext;
34 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
35 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.FuncExprContext;
36 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.MultExprContext;
37 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ParenExprContext;
38 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.RelExprContext;
39 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableContext;
40 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableLeadContext;
41 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableTermContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class SvcLogicExprListener extends ExprGrammarBaseListener
51 private static final Logger LOG = LoggerFactory
52 .getLogger(SvcLogicExprListener.class);
54 private SvcLogicExpression curExpr;
55 //private SvcLogicExpression topExpr;
56 private LinkedList<SvcLogicExpression> exprStack;
58 public SvcLogicExprListener()
60 exprStack = new LinkedList<>();
63 public SvcLogicExpression getParsedExpr()
68 private void pushOperand(SvcLogicExpression operand)
76 curExpr.addOperand(operand);
80 private void pushExpr(SvcLogicExpression expr)
82 LOG.trace("Pushing expression ["+expr.getClass().getName()+"]");
85 exprStack.push(curExpr);
90 private void popExpr()
92 if (exprStack.isEmpty())
98 SvcLogicExpression lastExpr = curExpr;
99 curExpr = exprStack.pop();
100 curExpr.addOperand(lastExpr);
106 public void enterAtom(AtomContext ctx) {
107 String atomText = ctx.getText();
108 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
114 public void enterMultExpr(MultExprContext ctx) {
115 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
116 pushExpr(curBinExpr);
118 List<TerminalNode> opList = ctx.MULTOP();
120 for (TerminalNode nd : opList)
122 curBinExpr.addOperator(nd.getText());
128 public void exitMultExpr(MultExprContext ctx) {
133 public void exitAtom(AtomContext ctx) {
138 public void enterAddExpr(AddExprContext ctx) {
139 List<TerminalNode> opList = ctx.ADDOP();
142 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
143 pushExpr(curBinExpr);
146 for (TerminalNode nd : opList)
148 curBinExpr.addOperator(nd.getText());
154 public void exitAddExpr(AddExprContext ctx) {
159 public void enterFuncExpr(FuncExprContext ctx) {
160 LOG.trace("enterFuncExpr: text = "+ctx.getText());
161 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
163 for (ExprContext expr: ctx.expr())
165 LOG.trace("enterFuncExpr - expr = "+expr.getText());
169 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
173 public void exitFuncExpr(FuncExprContext ctx) {
178 public void enterParenExpr(ParenExprContext ctx) {
179 LOG.trace("enterParenExpr: text = "+ctx.getText());
180 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
184 public void exitParenExpr(ParenExprContext ctx) {
185 LOG.trace("exitParenExpr: text = "+ctx.getText());
189 public void enterRelExpr(RelExprContext ctx) {
190 List<TerminalNode> opList = ctx.RELOP();
193 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
194 pushExpr(curBinExpr);
197 for (TerminalNode nd : opList)
199 curBinExpr.addOperator(nd.getText());
205 public void exitRelExpr(RelExprContext ctx) {
210 public void enterCompareExpr(CompareExprContext ctx) {
212 TerminalNode nd = ctx.COMPAREOP();
214 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
215 pushExpr(curBinExpr);
217 curBinExpr.addOperator(nd.getText());
222 public void exitCompareExpr(CompareExprContext ctx) {
230 public void enterConstant(ConstantContext ctx) {
234 public void exitConstant(ConstantContext ctx) {
239 public void enterVariable(VariableContext ctx) {
243 public void exitVariable(VariableContext ctx) {
248 public void enterVariableLead(VariableLeadContext ctx) {
252 public void exitVariableLead(VariableLeadContext ctx) {
256 public void enterVariableTerm(VariableTermContext ctx) {
257 String name = ctx.getText();
259 int subscrStart = name.indexOf("[");
260 if (subscrStart > -1)
262 name = name.substring(0, subscrStart);
264 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
269 public void exitVariableTerm(VariableTermContext ctx) {