package cn.cslg.pas.service.query; import cn.cslg.pas.common.utils.parseQueryToTree.*; import com.google.gson.Gson; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import java.io.*; import java.util.*; @Service @Slf4j @RequiredArgsConstructor(onConstructor_ = {@Lazy}) public class QueryService { public String getText(String text) throws Exception { treeNode tree = expressManager.getInstance().Parse(text, false); String a = this.get(tree); System.out.print(this.ToString((operateNode) tree)); return ""; } public String get(treeNode node) { String a = "("; treeNode leftNode = node.getLeft(); treeNode rightNode = node.getRight(); if (leftNode instanceof valueNode) { a += ((valueNode) leftNode).getvalue() +" "+ ((operateNode)node).getoperate().getShowName()+" "; } else { a += this.get(leftNode)+" "+ ((operateNode)node).getoperate().getShowName()+" "; } if (rightNode instanceof valueNode) { a += ((valueNode) rightNode).ToString(); } else { a += this.get(rightNode); } a+=")"; return a; } public String ToString(operateNode node){ operate operate1 =node.getoperate(); treeNode Left =node.getLeft(); treeNode Right =node.getRight(); String strCode = ""; if ((operate1.getShowName()!=null)) { strCode = operate1.getShowName(); } else { strCode = operate1.getCode(); } if(Left != null) { if((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Left.getLeft() != null || Left.getRight() != null)) { strCode = "(" + Left.ToString() +") " + strCode; } else { strCode = Left.ToString() +" " + strCode; } } if(Right!= null) { if ((operate1.gettype() == enuType.Logic || operate1.gettype() == enuType.Assignment ) && (Right.getLeft() != null || Right.getRight() != null)) { strCode = strCode +" (" + Right.ToString()+ ") "; } else { strCode = strCode + " " + Right.ToString(); } } return strCode; } public void write() { // 创建一个Map对象并添加一些数据 Map map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); // 将Map存储到文件中 try { FileOutputStream fileOut = new FileOutputStream("map.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(map); out.close(); fileOut.close(); System.out.println("Map已经成功存储到文件中"); } catch (IOException e) { e.printStackTrace(); } } public static Map readJsonFile(String fileName) { Gson gson = new Gson(); String json = ""; try { File file = new File("target/file/test.json"); Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8"); int ch = 0; StringBuffer buffer = new StringBuffer(); while ((ch = reader.read()) != -1) { buffer.append((char) ch); } reader.close(); json = buffer.toString(); return gson.fromJson(json, Map.class); } catch (IOException e) { e.printStackTrace(); return null; } } }