QueryService.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package cn.cslg.pas.service.query;
  2. import cn.cslg.pas.common.utils.parseQueryToTree.*;
  3. import com.google.gson.Gson;
  4. import lombok.RequiredArgsConstructor;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.context.annotation.Lazy;
  7. import org.springframework.stereotype.Service;
  8. import java.io.*;
  9. import java.util.*;
  10. @Service
  11. @Slf4j
  12. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  13. public class QueryService {
  14. public String getText(String text) throws Exception {
  15. treeNode tree = expressManager.getInstance().Parse(text, false);
  16. String a = this.get(tree);
  17. System.out.print(this.ToString((operateNode) tree));
  18. return "";
  19. }
  20. public String get(treeNode node) {
  21. String a = "(";
  22. treeNode leftNode = node.getLeft();
  23. treeNode rightNode = node.getRight();
  24. if (leftNode instanceof valueNode) {
  25. a += ((valueNode) leftNode).getvalue() +" "+ ((operateNode)node).getoperate().getShowName()+" ";
  26. } else {
  27. a += this.get(leftNode)+" "+ ((operateNode)node).getoperate().getShowName()+" ";
  28. }
  29. if (rightNode instanceof valueNode) {
  30. a += ((valueNode) rightNode).ToString();
  31. } else {
  32. a += this.get(rightNode);
  33. }
  34. a+=")";
  35. return a;
  36. }
  37. public String ToString(operateNode node){
  38. operate operate1 =node.getoperate();
  39. treeNode Left =node.getLeft();
  40. treeNode Right =node.getRight();
  41. String strCode = "";
  42. if ((operate1.getShowName()!=null))
  43. {
  44. strCode = operate1.getShowName();
  45. }
  46. else
  47. {
  48. strCode = operate1.getCode();
  49. }
  50. if(Left != null)
  51. {
  52. if((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Left.getLeft() != null || Left.getRight() != null))
  53. {
  54. strCode = "(" + Left.ToString() +") " + strCode;
  55. }
  56. else
  57. {
  58. strCode = Left.ToString() +" " + strCode;
  59. }
  60. }
  61. if(Right!= null)
  62. {
  63. if ((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Right.getLeft() != null || Right.getRight() != null))
  64. {
  65. strCode = strCode +" (" + Right.ToString()+ ") ";
  66. }
  67. else
  68. {
  69. strCode = strCode + " " + Right.ToString();
  70. }
  71. }
  72. return strCode;
  73. }
  74. public void write() {
  75. // 创建一个Map对象并添加一些数据
  76. Map<String, String> map = new HashMap<>();
  77. map.put("key1", "value1");
  78. map.put("key2", "value2");
  79. map.put("key3", "value3");
  80. // 将Map存储到文件中
  81. try {
  82. FileOutputStream fileOut = new FileOutputStream("map.ser");
  83. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  84. out.writeObject(map);
  85. out.close();
  86. fileOut.close();
  87. System.out.println("Map已经成功存储到文件中");
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. public static Map<String, Object> readJsonFile(String fileName) {
  93. Gson gson = new Gson();
  94. String json = "";
  95. try {
  96. File file = new File("target/file/test.json");
  97. Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
  98. int ch = 0;
  99. StringBuffer buffer = new StringBuffer();
  100. while ((ch = reader.read()) != -1) {
  101. buffer.append((char) ch);
  102. }
  103. reader.close();
  104. json = buffer.toString();
  105. return gson.fromJson(json, Map.class);
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. return null;
  109. }
  110. }
  111. }