/** * @param smt1 code当前遍历到的语义 * @param smt2 semanSet已存在的语义 * @return */ public void solveTwoSemantic(Semantic smt1, Semantic smt2) { // 用新加入的语义对已存在的语义进行化简 for (int i = 0; i < smt1.getSemanSet().size(); i++) { Item item1 = smt1.getSemanSet().get(i); for (int j = 0; j < smt2.getSemanSet().size(); j++) { Item item2 = smt2.getSemanSet().get(j); // null, 已存在的语义删去 if (null == solveTwoItem(item1, item2)) { smt2.getSemanSet().remove(j); j--; } } } // 去掉三选一的情况 for (int i = 0; i < smt1.getSemanSet().size(); i++) { Item a = smt1.getSemanSet().get(i); if (null != a.getPremise()) { for (int j = 0; j < smt1.getSemanSet().size(); j++) { Item b = smt1.getSemanSet().get(j); if (null == b.getPremise() && a.getLeft().equals(b.getLeft())) { smt1.getSemanSet().remove(i); i--; } } } } }
public List<Semantic> verificationProcess(List<Semantic> semanSrc) { List<Semantic> semanSet = new ArrayList<Semantic>(); for (int i = 0; i < semanSrc.size(); i++) { Semantic smt1 = semanSrc.get(i); // code当前遍历到的语义 for (int j = 0; j < semanSet.size(); j++) { Semantic smt2 = semanSet.get(j); // 已存在semanSet中的语义 solveTwoSemantic(smt1, smt2); if (0 == smt2.getSemanSet().size()) { semanSet.remove(j); j--; } } if (0 != smt1.getSemanSet().size()) semanSet.add(smt1); // 增加 } // for(int i = 0; i < semanSet.size(); i++) { // if(semanSet.get(i).getSemanSet().size() == 1) { // if(semanSet.get(i).getSemanSet().get(0).getLeft().contains("GPR")) { // semanSet.remove(i); // i--; // } // } // } return semanSet; }
@Override public void execute() { Cache cache = Cache.getInstance(); SemanticDao dao_semantic = SemanticDao.getInstance(); JSONObject params = getJSONObject(); String text = params.optString("text"); if (text == null || "".equals(text)) { result.put("ret", "-1"); result.put("msg", "text is null"); response(result); return; } Semantic semantic = cache.get("Semantic_text" + text); if (semantic == null) { semantic = dao_semantic.getSemantic(text); } if (semantic == null) { result.put("ret", "1"); result.put("msg", "Semantic is null"); response(result); return; } else { // 缓存 1 天 cache.set("Semantic_text" + text, semantic, 0); } result.put("ret", "0"); result.put("Semantic", semantic.toString()); response(result); }
public List<Instruction> createCodeSet(String inputFile, Map<String, Semantic> axiomSet) throws Exception { BufferedReader reader = null; List<Instruction> instructionSet = new ArrayList<Instruction>(); String regex = "\t| |,|\\(|\\)"; /** 获得输入汇编代码文件 */ try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))); String str = null; while (null != (str = reader.readLine())) { str = str.trim(); if (0 == str.length()) continue; String[] lines = str.split(regex); // separator不作为任何数组元素的部分返回 lines = Tool.filterOtherSignal(lines); Instruction inst = null; if (1 == lines.length) { // 特殊处理单项 inst = new Instruction(lines[0]); List<Item> semanSet = new ArrayList<Item>(); Semantic seman = new Semantic(); Item item = new Item(lines[0]); // 单项语义放在Item.left中 // 若为逻辑表达式,则把表达式的结果放到r0中 if (lines[0].contains("LOG-EXP")) { item.setLeft("GPR[0]"); item.setRight(lines[0]); } semanSet.add(item); seman.setSemanSet(semanSet); inst.setSeman(seman); } else if (lines.length > 1) { inst = new Instruction(lines[0]); Map<String, String> paras = Helper.generateParas(lines); Semantic seman = Helper.generateSemantic(lines[0], paras, axiomSet); inst.setParas(paras); inst.setSeman(seman); } else { inst = null; } if (null != inst) instructionSet.add(inst); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != reader) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return instructionSet; }