aboutsummaryrefslogtreecommitdiffstats
path: root/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/expr/ExpressionEvaluator.java
blob: dfb7d879599322f0344c07cce3f19859e0907ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 							reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.util.expr;

import java.util.Map;

public class ExpressionEvaluator {

	public static long evalLong(String expr, Map<String, Object> vars) {
		return (long) evalFloat(expr, vars);
	}

	public static float evalFloat(String expr, Map<String, Object> vars) {
		expr = expr.trim();
		int sl = expr.length();
		if (sl == 0)
			throw new IllegalArgumentException("Cannot interpret empty string.");

		// Remove parentheses if any
		if (expr.charAt(0) == '(' && expr.charAt(sl - 1) == ')')
			return evalFloat(expr.substring(1, sl - 1), vars);

		// Look for operators in the order of least priority
		String[] sss = findOperator(expr, "-", true);
		if (sss != null)
			return evalFloat(sss[0], vars) - evalFloat(sss[1], vars);

		sss = findOperator(expr, "+", true);
		if (sss != null)
			return evalFloat(sss[0], vars) + evalFloat(sss[1], vars);

		sss = findOperator(expr, "/", true);
		if (sss != null)
			return evalFloat(sss[0], vars) / evalFloat(sss[1], vars);

		sss = findOperator(expr, "*", true);
		if (sss != null)
			return evalFloat(sss[0], vars) * evalFloat(sss[1], vars);

		// Check if expr is a number
		try {
			return Float.valueOf(expr);
		} catch (Exception e) {
		}

		// Must be a variable
		Object v = vars.get(expr);
		if (v != null) {
			if (v instanceof Float)
				return (Float) v;
			if (v instanceof Long)
				return (Long) v;
			if (v instanceof Integer)
				return (Integer) v;
		}
		return 0;
	}

	public static boolean evalBoolean(String expr, Map<String, Object> vars) {
		expr = expr.trim();
		int sl = expr.length();
		if (sl == 0)
			throw new IllegalArgumentException("Cannot interpret empty string.");

		if (expr.equalsIgnoreCase("true"))
			return true;

		if (expr.equalsIgnoreCase("false"))
			return false;

		// Remove parentheses if any
		if (expr.charAt(0) == '(' && expr.charAt(sl - 1) == ')')
			return evalBoolean(expr.substring(1, sl - 1), vars);

		// Look for operators in the order of least priority
		String[] sss = findOperator(expr, "or", true);
		if (sss != null)
			return evalBoolean(sss[0], vars) || evalBoolean(sss[1], vars);

		sss = findOperator(expr, "and", true);
		if (sss != null)
			return evalBoolean(sss[0], vars) && evalBoolean(sss[1], vars);

		sss = findOperator(expr, "not", true);
		if (sss != null)
			return !evalBoolean(sss[1], vars);

		sss = findOperator(expr, "!=", false);
		if (sss == null)
			sss = findOperator(expr, "<>", false);
		if (sss != null)
			return evalLong(sss[0], vars) != evalLong(sss[1], vars);

		sss = findOperator(expr, "==", false);
		if (sss == null)
			sss = findOperator(expr, "=", false);
		if (sss != null)
			return evalLong(sss[0], vars) == evalLong(sss[1], vars);

		sss = findOperator(expr, ">=", false);
		if (sss != null)
			return evalLong(sss[0], vars) >= evalLong(sss[1], vars);

		sss = findOperator(expr, ">", false);
		if (sss != null)
			return evalLong(sss[0], vars) > evalLong(sss[1], vars);

		sss = findOperator(expr, "<=", false);
		if (sss != null)
			return evalLong(sss[0], vars) <= evalLong(sss[1], vars);

		sss = findOperator(expr, "<", false);
		if (sss != null)
			return evalLong(sss[0], vars) < evalLong(sss[1], vars);

		throw new IllegalArgumentException("Cannot interpret '" + expr + "': Invalid expression.");
	}

	private static String[] findOperator(String s, String op, boolean delimiterRequired) {
		int opl = op.length();
		int sl = s.length();
		String delimiters = " \0\t\r\n()";
		int pcount = 0, qcount = 0;
		for (int i = 0; i < sl; i++) {
			char c = s.charAt(i);
			if (c == '(' && qcount == 0)
				pcount++;
			else if (c == ')' && qcount == 0) {
				pcount--;
				if (pcount < 0)
					throw new IllegalArgumentException("Cannot interpret '" + s + "': Parentheses do not match.");
			} else if (c == '\'')
				qcount = (qcount + 1) % 2;
			else if (i <= sl - opl && pcount == 0 && qcount == 0) {
				String ss = s.substring(i, i + opl);
				if (ss.equalsIgnoreCase(op)) {
					boolean found = true;
					if (delimiterRequired) {
						// Check for delimiter before and after to make sure it is not part of another word
						char chbefore = '\0';
						if (i > 0)
							chbefore = s.charAt(i - 1);
						char chafter = '\0';
						if (i < sl - opl)
							chafter = s.charAt(i + opl);
						found = delimiters.indexOf(chbefore) >= 0 && delimiters.indexOf(chafter) >= 0;
					}
					if (found) {
						// We've found the operator, split the string
						String[] sss = new String[2];
						sss[0] = s.substring(0, i);
						sss[1] = s.substring(i + opl);
						return sss;
					}
				}
			}
		}
		if (pcount > 0)
			throw new IllegalArgumentException("Cannot interpret '" + s + "': Parentheses do not match.");
		if (qcount > 0)
			throw new IllegalArgumentException("Cannot interpret '" + s + "': No closing '.");
		return null;
	}

	private static Object parseObject(String s) {
		s = s.trim();
		int sl = s.length();
		if (sl == 0)
			throw new IllegalArgumentException("Cannot interpret empty string.");
		if (s.equalsIgnoreCase("null"))
			return null;
		if (s.charAt(0) == '\'') {
			if (sl < 2 || s.charAt(sl - 1) != '\'')
				throw new IllegalArgumentException("Cannot interpret '" + s + "': No closing '.");
			return s.substring(1, sl - 1);
		}
		// Not in quotes - must be a number
		try {
			return Long.valueOf(s);
		} catch (Exception e) {
		}
		try {
			return Double.valueOf(s);
		} catch (Exception e) {
			throw new IllegalArgumentException("Cannot interpret '" + s + "': Invalid number.");
		}
	}
}