Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. {
  2. "exp": {
  3. "typ": "and",
  4. "sbe": [
  5. {
  6. "exp": {
  7. "typ": "or",
  8. "vtp": "sta",
  9. "key": "x",
  10. "vsa": [
  11. "ss",
  12. "0"
  13. ]
  14. }
  15. },
  16. {
  17. "exp": {
  18. "typ": "or",
  19. "vtp": "sta",
  20. "key": "y",
  21. "vsa": [
  22. "dd",
  23. "rr"
  24. ]
  25. }
  26. },
  27. {
  28. "exp": {
  29. "typ": "eq",
  30. "vtp": "str",
  31. "key": "z",
  32. "vsa": "1S"
  33. }
  34. }
  35. ]
  36. }
  37. }
  38.  
  39. public class Tester {
  40. public static void main(String[] args) throws IOException {
  41. String input = "(((x=ss)OR(x=0))AND((y=dd)OR(y=rr))AND(z=1S))";
  42. if (isExpressionBalanced(input)) {
  43. System.out.println("input = " + input);
  44. extractRecursive(input);
  45. } else {
  46. System.out.println("The expression is not balanced");
  47. }
  48. }
  49.  
  50. private static List<String> splitByOperator(String text) {
  51. Map<String, String > map = new HashMap<>();
  52. String bracketContents = getWhatsInsideBrackets(text);
  53. String operator = extractOperator(bracketContents);
  54. if (operator == null) {
  55. System.out.println(bracketContents);
  56. map.put(bracketContents.split("=")[0], bracketContents.split("=")[1]);
  57. return Collections.emptyList();
  58. }
  59. String[] splitTextArray = bracketContents.split(operator);
  60. for (String splitText : splitTextArray) {
  61.  
  62. System.out.println(operator);
  63.  
  64. List<String> list = splitByOperator(splitText);
  65. list.size();
  66. }
  67. return Arrays.asList(splitTextArray);
  68. }
  69.  
  70. private static void extractRecursive(String text) {
  71. List<String> splitTextArray = splitByOperator(text);
  72. for (String splitText : splitTextArray) {
  73. String bracketContents = getWhatsInsideBrackets(splitText);
  74. List<String> list = splitByOperator(bracketContents);
  75. list.size();
  76. }
  77. }
  78.  
  79. public static String getWhatsInsideBrackets(String stringWithBracket) {
  80. int firstBracketIndexStart = stringWithBracket.indexOf('(');
  81. int firstBracketIndexEnd = findClosingParen(stringWithBracket.toCharArray(), firstBracketIndexStart);
  82. String stringWIthinBrackets = stringWithBracket.substring(firstBracketIndexStart + 1, firstBracketIndexEnd);
  83. return stringWIthinBrackets;
  84. }
  85.  
  86. private static String extractOperator(String text) {
  87. String operator = null;
  88. int innerFirstBracketIndexStart = text.indexOf('(');
  89. if (innerFirstBracketIndexStart < 0) {
  90. return operator;
  91. }
  92. int innerFirstBracketIndexEnd = findClosingParen(text.toCharArray(), innerFirstBracketIndexStart);
  93. if (text.startsWith("AND", innerFirstBracketIndexEnd + 1)) {
  94. operator = "AND";
  95. } else if (text.startsWith("OR", innerFirstBracketIndexEnd + 1)) {
  96. operator = "OR";
  97. }
  98. return operator;
  99.  
  100. }
  101.  
  102. public static int findClosingParen(char[] text, int openPos) {
  103. int closePos = openPos;
  104. int counter = 1;
  105. while (counter > 0) {
  106. char c = text[++closePos];
  107. if (c == '(') {
  108. counter++;
  109. } else if (c == ')') {
  110. counter--;
  111. }
  112. }
  113. return closePos;
  114. }
  115.  
  116. static boolean isExpressionBalanced(String searchTerm) {
  117. Stack stack = new Stack();
  118. for (int i = 0; i < searchTerm.length(); i++) {
  119. if (searchTerm.charAt(i) == '(') {
  120. stack.push(searchTerm.charAt(i));
  121. }
  122. if (searchTerm.charAt(i) == ')') {
  123. if (stack.empty()) {
  124. return false;
  125. }
  126. char top_char = (char) stack.pop();
  127.  
  128. if ((top_char == '(' && searchTerm.charAt(i) != ')')) {
  129. return false;
  130. }
  131. }
  132. }
  133. return stack.empty();
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement