Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "exp": {
- "typ": "and",
- "sbe": [
- {
- "exp": {
- "typ": "or",
- "vtp": "sta",
- "key": "x",
- "vsa": [
- "ss",
- "0"
- ]
- }
- },
- {
- "exp": {
- "typ": "or",
- "vtp": "sta",
- "key": "y",
- "vsa": [
- "dd",
- "rr"
- ]
- }
- },
- {
- "exp": {
- "typ": "eq",
- "vtp": "str",
- "key": "z",
- "vsa": "1S"
- }
- }
- ]
- }
- }
- public class Tester {
- public static void main(String[] args) throws IOException {
- String input = "(((x=ss)OR(x=0))AND((y=dd)OR(y=rr))AND(z=1S))";
- if (isExpressionBalanced(input)) {
- System.out.println("input = " + input);
- extractRecursive(input);
- } else {
- System.out.println("The expression is not balanced");
- }
- }
- private static List<String> splitByOperator(String text) {
- Map<String, String > map = new HashMap<>();
- String bracketContents = getWhatsInsideBrackets(text);
- String operator = extractOperator(bracketContents);
- if (operator == null) {
- System.out.println(bracketContents);
- map.put(bracketContents.split("=")[0], bracketContents.split("=")[1]);
- return Collections.emptyList();
- }
- String[] splitTextArray = bracketContents.split(operator);
- for (String splitText : splitTextArray) {
- System.out.println(operator);
- List<String> list = splitByOperator(splitText);
- list.size();
- }
- return Arrays.asList(splitTextArray);
- }
- private static void extractRecursive(String text) {
- List<String> splitTextArray = splitByOperator(text);
- for (String splitText : splitTextArray) {
- String bracketContents = getWhatsInsideBrackets(splitText);
- List<String> list = splitByOperator(bracketContents);
- list.size();
- }
- }
- public static String getWhatsInsideBrackets(String stringWithBracket) {
- int firstBracketIndexStart = stringWithBracket.indexOf('(');
- int firstBracketIndexEnd = findClosingParen(stringWithBracket.toCharArray(), firstBracketIndexStart);
- String stringWIthinBrackets = stringWithBracket.substring(firstBracketIndexStart + 1, firstBracketIndexEnd);
- return stringWIthinBrackets;
- }
- private static String extractOperator(String text) {
- String operator = null;
- int innerFirstBracketIndexStart = text.indexOf('(');
- if (innerFirstBracketIndexStart < 0) {
- return operator;
- }
- int innerFirstBracketIndexEnd = findClosingParen(text.toCharArray(), innerFirstBracketIndexStart);
- if (text.startsWith("AND", innerFirstBracketIndexEnd + 1)) {
- operator = "AND";
- } else if (text.startsWith("OR", innerFirstBracketIndexEnd + 1)) {
- operator = "OR";
- }
- return operator;
- }
- public static int findClosingParen(char[] text, int openPos) {
- int closePos = openPos;
- int counter = 1;
- while (counter > 0) {
- char c = text[++closePos];
- if (c == '(') {
- counter++;
- } else if (c == ')') {
- counter--;
- }
- }
- return closePos;
- }
- static boolean isExpressionBalanced(String searchTerm) {
- Stack stack = new Stack();
- for (int i = 0; i < searchTerm.length(); i++) {
- if (searchTerm.charAt(i) == '(') {
- stack.push(searchTerm.charAt(i));
- }
- if (searchTerm.charAt(i) == ')') {
- if (stack.empty()) {
- return false;
- }
- char top_char = (char) stack.pop();
- if ((top_char == '(' && searchTerm.charAt(i) != ')')) {
- return false;
- }
- }
- }
- return stack.empty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement