import { VarEntity } from "../common/CommonEntity"; import InputStream from 'antlr4/InputStream'; import CommonTokenStream from 'antlr4/CommonTokenStream'; import JavaScriptLexer from 'JavaScriptLexer'; export class CommonUtils { //变量函数的key public static variableFuncKeys: string[] = ["GetContextParameter", "GetSessionValue"]; /** * Antlr4分词:获取tokens * @param expressionText 表达式文本 * @returns tokens:any[] */ public static getTokens(expressionText: any): any[] { let tokens = []; let inputStream = new InputStream.InputStream(expressionText); let lexer = new JavaScriptLexer.JavaScriptLexer(inputStream); let antlrtokens = new CommonTokenStream.CommonTokenStream(lexer); antlrtokens.fill(); for (let index = 0; index < antlrtokens.tokens.length - 1; index++) { tokens.push(antlrtokens.tokens[index].text); } return tokens; } /** * 将变量转化为映射字典 * 解决:变量名称重复问题 * @param contexts 上下文变量 * @param sessions 会话变量 * @param type 映射表类型 Key2Name || Name2Key * @returns 变量key和name的map映射表 */ public static getVariableMap(contexts: VarEntity[], sessions: VarEntity[], type: string): Map { //合并变量到lists let lists = []; contexts != null && contexts.forEach(entity => { lists.push(entity); }) sessions != null && sessions.forEach(entity => { lists.push(entity); }) //定义返回值并开始转化 let map = new Map; if (lists != null && lists.length > 0) { let tempLists = lists; while (tempLists.length > 0) { let filterTempLists = tempLists.filter(i => i.name == tempLists[0].name); if (filterTempLists.length > 1) { tempLists.filter(i => i.name == tempLists[0].name).forEach(varEntity => { if (type == "Key2Name") { map.set(varEntity.key, varEntity.key + '_' + varEntity.name); } else if (type == "Name2Key") { map.set(varEntity.key + '_' + varEntity.name, varEntity.key); } }) } else { if (type == "Key2Name") { map.set(filterTempLists[0].key, filterTempLists[0].name); } else if (type == "Name2Key") { map.set(filterTempLists[0].name, filterTempLists[0].key); } } //重置tempLists tempLists = tempLists.filter(i => i.name != tempLists[0].name); } } return map; } /** * 表达式串解析,返回语义化文本 * @param tokens 分词后token * @param allFunctions 函数 * @param variableMap 变量映射map * @param functionSuffixs 特殊函数后缀 * @returns 语义化文本 */ public static toSemantic(tokens: any, allFunctions: any[], variableMap: Map, functionSuffixs: string[]): string { let result = ""; for (let index = 0; index < tokens.length; index++) { if (functionSuffixs.includes(tokens[index]) && (tokens[index + 1] == ".")) { continue; } else if (tokens[index] == "." && functionSuffixs.includes(tokens[index - 1])) { continue; } else { var num = 0; //遍历会话变量和上下文变量 if (num == 0) { if (tokens[index].length > 2 && tokens[index][0] == "\"" && tokens[index][tokens[index].length - 1] == "\"") { //校验第0位和第length-1位是否是" let temp = tokens[index].substring(1, tokens[index].length - 1); //此处需要GetContextParameter、GetSessionValue if (variableMap.has(temp) && tokens[index - 1] == "(" && tokens[index + 1] == ")" && this.variableFuncKeys.includes(tokens[index - 2])) { result = result + "\"" + variableMap.get(temp) + "\""; num = 1; } } } //计数器num:如果遍历allFunctions没找到对应字段,则直接拼接(num=0);否则替换(num=1) if (num == 0) { for (let i = 0; i < allFunctions.length; i++) { if (tokens[index] == allFunctions[i].key) { if (allFunctions[i].type != "Advanced") { result = result + allFunctions[i].name; num = 1; break; } } } } //如果计数器为0,证明没有语义化匹配。显示原始数据 if (num == 0) { result = result + tokens[index]; } } } return result; } }