2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.core.sli;
23 import java.util.LinkedList;
24 import java.util.List;
26 import org.antlr.v4.runtime.tree.TerminalNode;
27 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AddExprContext;
28 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AtomContext;
29 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.CompareExprContext;
30 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ConstantContext;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.FuncExprContext;
33 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.MultExprContext;
34 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ParenExprContext;
35 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.RelExprContext;
36 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableContext;
37 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableLeadContext;
38 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableTermContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class SvcLogicExprListener extends ExprGrammarBaseListener
48 private static final Logger LOG = LoggerFactory
49 .getLogger(SvcLogicExprListener.class);
51 private SvcLogicExpression curExpr;
52 private SvcLogicExpression topExpr;
53 private LinkedList<SvcLogicExpression> exprStack;
55 public SvcLogicExprListener()
57 exprStack = new LinkedList<SvcLogicExpression>();
60 public SvcLogicExpression getParsedExpr()
65 private void pushOperand(SvcLogicExpression operand)
73 curExpr.addOperand(operand);
77 private void pushExpr(SvcLogicExpression expr)
79 LOG.trace("Pushing expression ["+expr.getClass().getName()+"]");
82 exprStack.push(curExpr);
87 private void popExpr()
89 if (exprStack.isEmpty())
91 LOG.trace("Popping last expression");
96 SvcLogicExpression lastExpr = curExpr;
97 curExpr = exprStack.pop();
98 curExpr.addOperand(lastExpr);
99 LOG.trace("New curExpr is ["+curExpr.getClass().getName()+"]");
105 public void enterAtom(AtomContext ctx) {
107 String atomText = ctx.getText();
109 LOG.trace("enterAtom: text = "+atomText);
112 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
119 public void enterMultExpr(MultExprContext ctx) {
120 LOG.trace("enterMultExpr: text = "+ctx.getText());
122 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
123 pushExpr(curBinExpr);
125 List<TerminalNode> opList = ctx.MULTOP();
127 for (TerminalNode nd : opList)
129 LOG.trace("enterMultExpr: operator - "+nd.getText());
130 curBinExpr.addOperator(nd.getText());
136 public void exitMultExpr(MultExprContext ctx) {
138 LOG.trace("exitMultExpr: text = "+ctx.getText());
145 public void exitAtom(AtomContext ctx) {
146 LOG.trace("exitAtom: text = "+ctx.getText());
151 public void enterAddExpr(AddExprContext ctx) {
152 LOG.trace("enterAddExpr: text = "+ctx.getText());
153 List<TerminalNode> opList = ctx.ADDOP();
156 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
157 pushExpr(curBinExpr);
160 for (TerminalNode nd : opList)
162 LOG.trace("enterAddExpr: operator - "+nd.getText());
163 curBinExpr.addOperator(nd.getText());
169 public void exitAddExpr(AddExprContext ctx) {
170 LOG.trace("exitAddExpr: text = "+ctx.getText());
176 public void enterFuncExpr(FuncExprContext ctx) {
177 LOG.trace("enterFuncExpr: text = "+ctx.getText());
178 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
180 for (ExprContext expr: ctx.expr())
182 LOG.trace("enterFuncExpr - expr = "+expr.getText());
186 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
190 public void exitFuncExpr(FuncExprContext ctx) {
191 LOG.trace("exitFuncExpr: text = "+ctx.getText());
197 public void enterParenExpr(ParenExprContext ctx) {
198 LOG.trace("enterParenExpr: text = "+ctx.getText());
199 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
203 public void exitParenExpr(ParenExprContext ctx) {
204 LOG.trace("exitParenExpr: text = "+ctx.getText());
208 public void enterRelExpr(RelExprContext ctx) {
209 LOG.trace("enterRelExpr: text = "+ctx.getText());
211 List<TerminalNode> opList = ctx.RELOP();
214 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
215 pushExpr(curBinExpr);
218 for (TerminalNode nd : opList)
220 LOG.trace("enterRelExpr: operator - "+nd.getText());
221 curBinExpr.addOperator(nd.getText());
227 public void exitRelExpr(RelExprContext ctx) {
228 LOG.trace("exitRelExpr: text = "+ctx.getText());
234 public void enterCompareExpr(CompareExprContext ctx) {
235 LOG.trace("enterCompareExpr: text = "+ctx.getText());
237 TerminalNode nd = ctx.COMPAREOP();
239 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
240 pushExpr(curBinExpr);
242 LOG.trace("enterCompareExpr: operator - "+nd.getText());
243 curBinExpr.addOperator(nd.getText());
248 public void exitCompareExpr(CompareExprContext ctx) {
249 LOG.trace("exitCompareExpr : text = "+ctx.getText());
257 public void enterConstant(ConstantContext ctx) {
258 LOG.trace("enterConstant: text = "+ctx.getText());
262 public void exitConstant(ConstantContext ctx) {
263 LOG.trace("exitConstant: text = "+ctx.getText());
268 public void enterVariable(VariableContext ctx) {
269 LOG.trace("enterVariable: text = "+ctx.getText());
275 public void exitVariable(VariableContext ctx) {
276 LOG.debug("exitVariable: text ="+ctx.getText());
282 public void enterVariableLead(VariableLeadContext ctx) {
284 LOG.debug("enterVariableLead: text ="+ctx.getText());
290 public void exitVariableLead(VariableLeadContext ctx) {
292 LOG.trace("exitVariableLead: text ="+ctx.getText());
296 public void enterVariableTerm(VariableTermContext ctx) {
297 LOG.trace("enterVariableTerm: text ="+ctx.getText());
299 String name = ctx.getText();
301 int subscrStart = name.indexOf("[");
302 if (subscrStart > -1)
304 name = name.substring(0, subscrStart);
306 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
311 public void exitVariableTerm(VariableTermContext ctx) {
312 LOG.trace("exitVariableTerm: text="+ctx.getText());