《专家系统(开发)--表达式检测--与表达式模板一起使用》
生活随笔
收集整理的這篇文章主要介紹了
《专家系统(开发)--表达式检测--与表达式模板一起使用》
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
《專家系統破解篇 九、Simple Rule Engine規則推理引擎三(表達式分析)》
表達式模板化配置的思路
在用不用后綴表達式方面搖擺了一陣,實在是覺得不用的話相當麻煩,做了一半又改成用后綴的了。小看了先人的智慧啊。
先刪了。 測試的時候發現Regex.Split 方法 (String, Int32, Int32)
方法竟然返回一些空字符串,處理完再放上來。
| using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using FineUI; using System.Text.RegularExpressions; using System.Collections; namespace ExpertManager.Web.ALarmLogic { ??? public partial class testexpress : System.Web.UI.Page ??? { ??????? #region 正則表達式、自定義結構(事實list、類型、封裝類型對象)、中綴后綴表達式 ??????? public static Regex currencyRegex = new Regex(@"\bsensor\(\d{1,9},\d{0,9},\d{1,9}\)\b*|\balarm\(\d{1,9}\)\b*|\bsensoralarm\(\d{1,9},\d{0,9},\d{1,9}\)\b*", RegexOptions.IgnoreCase); ??????? //表達式中監控點,告警,監控點的告警相關的三種事實數據。 ??????? public static Regex repRegex = new Regex(@"\s*|\b\r\n\b+", RegexOptions.IgnoreCase); ??????? //替換表達式中的 空白與換行 字符 ??????? public static readonly string LogicalRegEx = @"(\x29|\x28|>=|<=|!=|==|<|>|AND|OR|NOT|Exists|NULL|True|False|Fact|ISNULL|XOR|\x2b|\x2d|\x2a|\x2f|\x25)"; ??????? //x29 ) x28 ( x2b + x2d - x2a * x2f / x25 % ??????? public static Regex spliRegex = new Regex(LogicalRegEx, RegexOptions.IgnoreCase); ??????? //切割表達式中的每一個操作符 ??????? public struct ExpressFact// 表達式中事實LIST ??????? { ??????????? public string Value; ??????????? public int Index; ??????????? public int Length; ??????? } ??????? //類型 ??????? public enum Type ??????? { ??????????? Fact,//--事實型都是value ??????????? Value,//布爾、浮點類型數字、字符型 ??????????? Operator,//各種操作符 ??????????? Function,//兩個方法 ISNULL 與Exists (都成立,不論是否有內容,還是內容是數字,字符,null 都成立。) ??????????? OpenBracket,//(開括號 ??????????? CloseBracket,//)關括號 ??????????? Null,//NULL ??????????? Invalid // 無效 ??????? } ??????? public struct Symbol//封裝類型對象 ??????? { ??????????? public string name; ??????????? public Type type; ??????????? public object Vaule;//與類型type對象,可空。枚舉type為Value時有值 ??????????? public ExpressFact Fact;//事實對象,可空 ??????? } ??????? protected List<Symbol> infix = new List<Symbol>();//中綴 ??????? public List<Symbol> Infix ??????? { ??????????? get ??????????? { ??????????????? return new List<Symbol>(infix); ??????????? } ??????????? set ??????????? { ??????????????? infix = value; ??????????? } ??????? } ??????? protected List<Symbol> postfix = new List<Symbol>();//后綴 ??????? /// <summary> ??????? /// 后綴 ??????? /// </summary> ??????? public List<Symbol> Postfix ??????? { ??????????? get ??????????? { ??????????????? return new List<Symbol>(postfix); ??????????? } ??????????? set ??????????? { ??????????????? postfix = value; ??????????? } ??????? } ??????? #endregion ??????? protected void Page_Load(object sender, EventArgs e) ??????? { ??????????? if (!IsPostBack) ??????????? { ??????????? } ??????? } ??????? #region 檢測表達式 ??????? protected void toolbarBtn_Click(object sender, EventArgs e) ??????? { ??????????? string resultStr = string.Empty; ??????????? try ??????????? { ??????????????? string input = ExpressValue.Text; ??????????????? input = repRegex.Replace(input, ""); ??????????????? string express = input; ??????????????? MatchCollection mc = currencyRegex.Matches(input);//得到事實(告警、監控點、監控點告警)位置,事實經過匹配的肯定都是符合規則的1-9位之間的數字 ??????????????? List<ExpressFact> efl = new List<ExpressFact>(); ??????????????? foreach (Match match in mc) ??????????????? { ??????????????????? GroupCollection groups = match.Groups; ??????????????????? foreach (Group g in groups) ??????????????????? { ??????????????????????? for (int i = 0; i < g.Captures.Count; i++) ??????????????????????? { ??????????????????????????? ExpressFact temp = new ExpressFact(); ??????????????????????????? temp.Value = g.Captures[i].Value; ??????????????????????????? temp.Index = g.Captures[i].Index; ??????????????????????????? temp.Length = g.Captures[i].Length; ??????????????????????????? efl.Add(temp); ??????????????????????? } ??????????????????? } ??????????????? } ??????????????? //構造表達式的類型 ??????????????? string replacestr = currencyRegex.Replace(input, "Fact");//替換掉事實(告警、監控點、監控點告警) ??????????????? string[] rawTokins = spliRegex.Split(replacestr);//中綴表達式切割 此方法會返回一下空字符串 ??????????????? for (int x = 0; x < rawTokins.Length; x++) ??????????????? { ??????????????????? string currentTokin = rawTokins[x].Trim(); ??????????????????? if (currentTokin == null || currentTokin == String.Empty) continue;//Split的字符串 ??????????????????? if (currentTokin == "Fact") ??????????????????? { ??????????????????????? ExpressFact indexitem = efl.ElementAt(0); ??????????????????????? efl.RemoveAt(0); ??????????????????????? if (!string.IsNullOrEmpty(indexitem.Value)) ??????????????????????? { ??????????????????????????? Symbol currentfact = new Symbol(); ??????????????????????????? currentfact.name = "fact"; ??????????????????????????? currentfact.type = Type.Fact; ??????????????????????????? currentfact.Fact = indexitem; ??????????????????????????? infix.Add(currentfact); ??????????????????????? } ??????????????????? } ??????????????????? else ??????????????????? { ??????????????????????? Symbol current = ParseToSymbol(currentTokin); ??????????????????????? if (current.type == Type.Invalid) ??????????????????????? { ??????????????????????????? resultStr = "警告:表達式出現無效字符!"; ??????????????????????????? return; ??????????????????????? } ??????????????????????? infix.Add(current); ??????????????????? } ??????????????? } ??????????????? //需要轉后綴表達式,只有轉成后綴才能得到某操作符需要的參數。 ??????????????? CheckOutExpress(ref resultStr); ??????????? } ??????????? catch { } ??????????? finally ??????????? { ??????????????? if (string.IsNullOrEmpty(resultStr)) ??????????????? { ??????????????????? //存儲表達式express ??????????????? } ??????????????? else ??????????????? { ??????????????????? Alert.ShowInTop(resultStr); ??????????????? } ??????????? } ??????? } ??????? /// <summary> ??????? /// 檢查表達式中綴 ??????? /// </summary> ??????? /// <param name="resultStr"></param> ??????? private void CheckOutExpress(ref string resultStr) ??????? { ??????????? #region 轉換成后綴表達式 --為了操作符參數、優先級 ??????????? Stack postfixStack = new Stack(); ??????????? foreach (Symbol s in infix) ??????????? { ??????????????? if (s.type == Type.Value || s.type == Type.Fact || s.type == Type.Null) ??????????????? { ??????????????????? postfix.Add(s);//后綴表達式 ??????????????? } ??????????????? else if (s.type == Type.Operator || s.type == Type.Function) ??????????????? { ??????????????????? //棧中是否有括號和函數、操作符-- 當前操作符優先級低則彈出原來高的(先計算原來的優先級高的) ??????????????????? while (postfixStack.Count > 0 && !DeterminePrecidence(s, (Symbol)postfixStack.Peek())) ??????????????????? { ??????????????????????? postfix.Add((Symbol)postfixStack.Pop()); ??????????????????? } ??????????????????? postfixStack.Push(s);//后綴棧 ??????????????? } ??????????????? else if (s.type == Type.OpenBracket)//開括號 ??????????????? { ??????????????????? postfixStack.Push(s); ??????????????? } ??????????????? else if (s.type == Type.CloseBracket)//關括號 ??????????????? { ??????????????????? while (((Symbol)postfixStack.Peek()).type != Type.OpenBracket)//循環括號中間操作。輸出,直到匹配開括號結束 ??????????????????? { ??????????????????????? postfix.Add((Symbol)postfixStack.Pop()); ??????????????????? } ??????????????????? postfixStack.Pop(); //彈出開括號。? 不符合規則的表達式))))這種要注意空引用異常。 ??????????????? } ??????????????? else ??????????????? { ??????????????????? resultStr = "非法 符號: " + s.name; ??????????????????? return; ??????????????? } ??????????? } ??????????? //輸出棧,補全操作符號 ??????????? while (postfixStack.Count > 0) ??????????? { ??????????????? postfix.Add((Symbol)postfixStack.Pop()); ??????????? } ??????????? #endregion ??????????? #region?? 后綴表達式檢查 ??????????? Stack operandStack = new Stack();//操作符號棧 ??????????? foreach (Symbol s in postfix) ??????????? { ??????????????? if (s.type == Type.Value || s.type == Type.Null) ??????????????? { ??????????????????? operandStack.Push(s); ??????????????? } ??????????????? else if (s.type == Type.Operator) ??????????????? { ??????????????????? try ??????????????????? { ??????????????????????? //操作符參數 ??????????????????????? Symbol opQ;//前 參數 ??????????????????????? Symbol opH;//后 參數 ??????????????????????? Symbol opR = new Symbol();//計算結果的類型 ??????????????????????? switch (s.name) ??????????????????????? { ??????????????????????????? case "+": ??????????????????????????????? opH = (Symbol)operandStack.Pop();//先彈出的是后 ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 加法運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Double"; ??????????????????????????????? opR.Vaule = 1.0; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "-": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 減法運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Double"; ??????????????????????????????? opR.Vaule = 1.0; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "*": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 乘法運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Double"; ??????????????????????????????? opR.Vaule = 1.0; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "/": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 除法運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Double"; ??????????????????????????????? opR.Vaule = 1.0; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "%": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 求百分比運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Double"; ??????????????????????????????? opR.Vaule = 1.0; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "==": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.type != Type.Null && opH.type != Type.Null && opQ.Vaule.GetType() != opH.Vaule.GetType()) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 等于 運算兩邊參數類型不相同 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "!=": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.type != Type.Null && opH.type != Type.Null && opQ.Vaule.GetType() != opH.Vaule.GetType()) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 不等于 運算兩邊參數類型不相同 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case ">": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 大于 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "<": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 小于 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case ">=": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 大于等于 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "<=": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Double) || opH.Vaule.GetType() != typeof(Double)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " 小于等于 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "AND": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Boolean) || opH.Vaule.GetType() != typeof(Boolean)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " AND 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "OR": ??????????????????????????????? opH = (Symbol)operandStack.Pop(); ??????????????????????????????? opQ = (Symbol)operandStack.Pop(); ??????????????????????????????? if (opQ.Vaule.GetType() != typeof(Boolean) || opH.Vaule.GetType() != typeof(Boolean)) ??????????????????????????????? { ??????????????????????????????????? resultStr = opQ.Vaule + " OR 運算參數異常 "; ??????????????????????????????????? return; ??????????????????????????????? } ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? case "NOT": ??????????????????????????????? opQ = (Symbol)operandStack.Pop();? //一個參數 ??????????????????????????????? opR.name = "Boolean"; ??????????????????????????????? opR.Vaule = true; ??????????????????????????????? opR.type = Type.Value; ??????????????????????????????? break; ??????????????????????????? default: ??????????????????????????????? break; ??????????????????????? } ??????????????????????? operandStack.Push(opR); ??????????????????? } ??????????????????? catch ??????????????????? { ??????????????????????? resultStr = " 表達式異常!請檢查! "; ??????????????????????? return; ??????????????????? } ??????????????? } ??????????????? else if (s.type == Type.Function) ??????????????? { ??????????????????? try ??????????????????? { ??????????????????????? operandStack.Pop(); ??????????????????? } ??????????????????? catch ??????????????????? { ??????????????????????? resultStr = " 表達式函數參數異常!請檢查! ";//函數內部值 ??????????????????????? return; ??????????????????? } ??????????????????? Symbol opR = new Symbol(); ??????????????????? opR.type = Type.Value; ??????????????????? opR.name = "Boolean"; ??????????????????? opR.Vaule = true; ??????????????????? operandStack.Push(opR); ??????????????????? //switch (s.name.ToLower()) ??????????????????? //{ ??????????????????? //??? case "isnull": ??????????????????? //??????????? opR.name = "Boolean"; ??????????????????? //??????????? opR.Vaule = true; ??????????????????? //??????? break; ??????????????????? //??? case "exists": ??????????????????? //???????????? opR.name = "Boolean"; ??????????????????? //??????????? opR.Vaule = true; ??????????????????? //??????? break; ??????????????????? //} ??????????????? } ??????????????? else if (s.type == Type.Fact) ??????????????? { ??????????????????? //改成浮點值存入, 方法時,用過做不同的操作。因為事實是作為整體存入的,沒有對應的參數。 ??????????????????? Symbol opR = new Symbol(); ??????????????????? opR.name = "Double"; ??????????????????? opR.Vaule = 1.0; ??????????????????? opR.type = Type.Value; ??????????????????? operandStack.Push(opR); ??????????????????? //??? currentfact.name = "fact"; ??????????????????? //??? currentfact.type = Type.Fact; ??????????????????? //??? currentfact.Fact = indexitem; ??????????????? } ??????????? } ??????????? Symbol returnValue = (Symbol)operandStack.Pop(); ??????????? if (operandStack.Count > 0 || returnValue.Vaule.GetType() != typeof(Boolean)) ??????????? { ??????????????? resultStr = " 表達式輸入有誤、有多余參數、或計算結果非布爾值(真、假)!請檢查! "; ??????????????? return; ??????????? } ??????????? #endregion ??????? } ??????? //操作符優先級 ??????? private bool DeterminePrecidence(Symbol higher, Symbol lower) ??????? { ??????????? int s1 = Precidence(higher); ??????????? int s2 = Precidence(lower); ??????????? if (s1 > s2) ??????????????? return true; ??????????? else ??????????????? return false; ??????? } ??????? private int Precidence(Symbol s) ??????? { ??????????? int result = 0; ??????????? switch (s.name) ??????????? { ??????????????? case "*": ??????????????? case "/": ??????????????? case "%": ??????????????????? result = 32; ??????????????????? break; ??????????????? case "+": ??????????????? case "-": ??????????????????? result = 16; ??????????????????? break; ??????????????? case ">": ??????????????? case "<": ??????????????? case ">=": ??????????????? case "<=": ??????????????????? result = 8; ??????????????????? break; ??????????????? case "==": ??????????????? case "!=": ??????????????????? result = 4; ??????????????????? break; ??????????????? case "NOT": ??????????????????? result = 3; ??????????????????? break; ??????????????? case "AND": ??????????????????? result = 2; ??????????????????? break; ??????????????? case "XOR": ??????????????? case "OR": ??????????????????? result = 1; ??????????????????? break; ??????????? } ??????????? //函數具有最高的優先級 ??????????? if (s.type == Type.Function) ??????????????? result = 64; ??????????? return result; ??????? } ??????? #region 字符串轉換成Symbol ??????? /// <summary> ??????? /// 字符串轉換成Symbol ??????? /// </summary> ??????? /// <param name="s"></param> ??????? /// <returns></returns> ??????? /// <remarks>表達式字符串的處理符號解析---- 按照優先級進行判斷。</remarks> ??????? private Symbol ParseToSymbol(string s) ??????? { ??????????? Symbol sym = new Symbol(); ??????????? if (IsOpenParanthesis(s))//開括號 ??????????? { ??????????????? sym.name = s; ??????????????? sym.type = Type.OpenBracket; ??????????? } ??????????? else if (IsCloseParanthesis(s))//閉括號 ??????????? { ??????????????? sym.name = s; ??????????????? sym.type = Type.CloseBracket; ??????????? } ??????????? else if (IsFunction(s)) //函數 ??????????? { ??????????????? sym.name = s; ??????????????? sym.type = Type.Function; ??????????? } ??????????? else if (IsOperator(s))//操作符 ??????????? { ??????????????? sym.name = s; ??????????????? sym.type = Type.Operator; ??????????? } ??????????? else if (IsBoolean(s))//布爾值 ??????????? { ??????????????? sym.name = s; ??????????????? sym.Vaule = Boolean.Parse(s); ??????????????? sym.type = Type.Value; ??????????? } ??????????? else if (IsNumber(s))//浮點值 ??????????? { ??????????????? sym.name = s; ??????????????? sym.Vaule = Double.Parse(s); ??????????????? sym.type = Type.Value; ??????????? } ??????????? else if (IsNUll(s))//null值 ??????????? { ??????????????? sym.name = s; ??????????????? sym.type = Type.Null; ??????????? } ??????????? else if (IsString(s))//字符串 ??????????? { ??????????????? sym.name = s; ??????????????? sym.Vaule = s;//.Substring(1, s.Length - 2); ??????????????? sym.type = Type.Value; ??????????? } ??????????? else//null或其他 ??????????? { ??????????????? sym.name = null; ??????????????? sym.Vaule = null;//.Substring(1, s.Length - 2); ??????????????? sym.type = Type.Invalid; ??????????? } ??????????? return sym; ??????? } ??????? //null值 ??????? private bool IsNUll(string s) ??????? { ??????????? if (s == null || s.ToLower() == "null") ??????????????? return true; ??????????? else ??????????????? return false; ??????? } ??????? //是否是布爾 ??????? private bool IsBoolean(string s) ??????? { ??????????? if (s != null && (s.ToLower() == "true" || s.ToLower() == "false")) ??????????????? return true; ??????????? else ??????????????? return false; ??????? } ??????? //是否是數字 ??????? private bool IsNumber(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? bool result = true; ??????????? foreach (char c in s.ToCharArray()) ??????????? { ??????????????? if (Char.IsNumber(c)) ??????????????????? continue; ??????????????? result = false; ??????????????? break; ??????????? } ??????????? return result; ??????? } ??????? //是否是字符串 ??????? private bool IsString(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? bool result = false; ??????????? if (s.StartsWith(@"""") && s.EndsWith(@"""")) ??????????????? result = true; ??????????? return result; ??????? } ??????? //是否是打開的小括號 ??????? private bool IsOpenParanthesis(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? // ??????????? bool result = false; ??????????? if (s == "(") ??????????????? result = true; ??????????? return result; ??????? } ??????? //是否是關閉的小括號 ??????? private bool IsCloseParanthesis(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? // ??????????? bool result = false; ??????????? if (s == ")") ??????????????? result = true; ??????????? return result; ??????? } ??????? //是否是操作符號 ??????? private bool IsOperator(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? bool result = false; ??????????? switch (s) ??????????? { ??????????????? case "+": ??????????????? case "-": ??????????????? case "/": ??????????????? case "*": ??????????????? case "%": ??????????????? case "==": ??????????????? case "!=": ??????????????? case ">=": ??????????????? case "<=": ??????????????? case ">": ??????????????? case "<": ??????????????? case "AND": ??????????????? case "OR": ??????????????? case "NOT": ??????????????????? result = true; ??????????????????? break; ??????????? } ??????????? return result; ??????? } ??????? //是否是函數 ??????? private bool IsFunction(string s) ??????? { ??????????? if (s == null) ??????????????? return false; ??????????? bool result = false; ??????????? switch (s.ToLower()) ??????????? { ??????????????? case "isnull": ??????????????????? result = true; ??????????????????? break; ??????????????? case "exists": ??????????????????? result = true; ??????????????????? break; ??????????? } ??????????? return result; ??????? } ??????? #endregion ??????? #endregion ??????? #region 模板生成方法 ??????? //1.檢測該選的是否選擇。 ??????? //2.檢測該填寫的是否填寫,填寫的是否符合規格。 ??????? //3.插入text中。并且定位位置。 統一的函數定位法,直接在最后一個)之后即可。 ??????? void funpos(string insertstr) ??????? { ??????????? int pos = 0; int returnpos = 0; ??????????? string s = scrollTop.Text;//滾動條位置 ??????????? try ??????????? { ??????????????? pos = int.Parse(leFTPos.Text); ??????????????? if (pos == -1) pos = 0; ??????????? } ??????????? catch { } ??????????? string expressvaluetext = ExpressValue.Text; ??????????? ExpressValue.Text = expressvaluetext.Insert(pos, insertstr); ??????????? int keypos = insertstr.LastIndexOf(')') + 1;//最后一個)后面 ??????????? returnpos = pos + keypos; ??????????? leFTPos.Text = returnpos.ToString(); ??????????? scrollTop.Text = s; ??????????? FineUI.PageContext.RegisterStartupScript("dingwei('" + returnpos + "', '" + s + "', '" + ExpressValue.ClientID + "');"); ??????? } ??????? protected void AlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "Alarm({0})"; ??????????? if (DDLAlarm.SelectedValue != "-1") ??????????? { ??????????????? insertstr = string.Format(insertstr, DDLAlarm.SelectedValue); ??????????????? funpos(insertstr); ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇告警信息!"); ??????????? } ??????? } ??????? protected void ExistsAlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "Exists(Alarm({0}))"; ??????????? if (DDLExAlarm.SelectedValue != "-1") ??????????? { ??????????????? insertstr = string.Format(insertstr, DDLExAlarm.SelectedValue); ??????????????? funpos(insertstr); ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇告警信息!"); ??????????? } ??????? } ??????? protected void PanAlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "(Alarm({0}){1}{2})"; ??????????? if (DDLPanAlarm.SelectedValue != "-1") ??????????? { ??????????????? if (SelectPanAlarm.SelectedValue != "-1") ??????????????? { ??????????????????? if (!string.IsNullOrEmpty(textBoxPanAlarm.Text)) ??????????????????? { ??????????????????????? insertstr = string.Format(insertstr, DDLPanAlarm.SelectedValue, SelectPanAlarm.SelectedValue, textBoxPanAlarm.Text); ??????????????????????? funpos(insertstr); ??????????????????? } ??????????????????? else ??????????????????? { ??????????????????????? Alert.ShowInTop("請輸入告警邏輯判斷的數值!"); ??????????????????? } ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇邏輯符號!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇告警信息!"); ??????????? } ??????? } ??????? protected void SensorBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "Sensor({0},{1},{2})"; ??????????? if (DDLSensorType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLSensor.SelectedValue != "-1") ??????????????? { ??????????????????? insertstr = string.Format(insertstr, DDLSensorType.SelectedValue, DDLSensorSubType.SelectedValue, DDLSensor.SelectedValue); ??????????????????? funpos(insertstr); ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? protected void ExistsSensorBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "Exists(Sensor({0},{1},{2}))"; ??????????? if (DDLExSensorType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLExSensor.SelectedValue != "-1") ??????????????? { ??????????????????? insertstr = string.Format(insertstr, DDLExSensorType.SelectedValue, DDLExSensorSubType.SelectedValue, DDLExSensor.SelectedValue); ??????????????????? funpos(insertstr); ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? protected void PanSensorBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "(Sensor({0},{1},{2}){3}{4})"; ??????????? if (DDLPanSensorType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLPanSensor.SelectedValue != "-1") ??????????????? { ??????????????????? if (SelectPanSensor.SelectedValue != "-1") ??????????????????? { ??????????????????????? if (!string.IsNullOrEmpty(textBoxPanSensor.Text)) ??????????????????????? { ??????????????????????????? insertstr = string.Format(insertstr, DDLPanSensorType.SelectedValue, DDLPanSensorSubType.SelectedValue, DDLPanSensor.SelectedValue, SelectPanSensor.SelectedValue, textBoxPanSensor.Text); ??????????????????????????? funpos(insertstr); ??????????????????????? } ??????????????????????? else ??????????????????????? { ??????????????????????????? Alert.ShowInTop("請輸入監控點的邏輯判斷的數值!"); ??????????????????????? } ??????????????????? } ??????????????????? else { Alert.ShowInTop("請選擇邏輯符號!"); } ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? protected void SensorAlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "SensorAlarm({0},{1},{2})"; ??????????? if (DDLSensorAlarmType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLSensorAlarm.SelectedValue != "-1") ??????????????? { ??????????????????? insertstr = string.Format(insertstr, DDLSensorAlarmType.SelectedValue, DDLSensorAlarmSubType.SelectedValue, DDLSensorAlarm.SelectedValue); ??????????????????? funpos(insertstr); ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? protected void PanSensorAlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "(SensorAlarm({0},{1},{2}){3}{4})"; ??????????? if (DDLPanSensorAlarmType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLPanSensorAlarm.SelectedValue != "-1") ??????????????? { ??????????????????? if (SelectPanSensorAlarm.SelectedValue != "-1") ??????????????????? { ??????????????????????? if (!string.IsNullOrEmpty(textBoxPanSensorAlarm.Text)) ??????????????????????? { ??????????????????????????? insertstr = string.Format(insertstr, DDLPanSensorAlarmType.SelectedValue, DDLPanSensorAlarmSubType.SelectedValue, DDLPanSensorAlarm.SelectedValue, SelectPanSensorAlarm.SelectedValue, textBoxPanSensorAlarm.Text); ??????????????????????????? funpos(insertstr); ??????????????????????? } ??????????????????????? else ??????????????????????? { ??????????????????????????? Alert.ShowInTop("請輸入監控點的告警的邏輯判斷的數值!"); ??????????????????????? } ??????????????????? } ??????????????????? else { Alert.ShowInTop("請選擇邏輯符號!"); } ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? protected void ExistsSensorAlarmBtn(object s, EventArgs e) ??????? { ??????????? string insertstr = "Exists(SensorAlarm({0},{1},{2}))"; ??????????? if (DDLExSensorAlarmType.SelectedValue != "-1") ??????????? { ??????????????? if (DDLExSensorAlarm.SelectedValue != "-1") ??????????????? { ??????????????????? insertstr = string.Format(insertstr, DDLExSensorAlarmType.SelectedValue, DDLExSensorAlarmSubType.SelectedValue, DDLExSensorAlarm.SelectedValue); ??????????????????? funpos(insertstr); ??????????????? } ??????????????? else { Alert.ShowInTop("請選擇監控點信息!"); } ??????????? } ??????????? else ??????????? { ??????????????? Alert.ShowInTop("請選擇設備類型!"); ??????????? } ??????? } ??????? #endregion ??????? #region 樹形表達式選擇方法 ??????? protected void TreeNodeSelected(object sender, EventArgs e) ??????? { ??????????? int pos = 0; int returnpos = 0; ??????????? string s = scrollTop.Text;//滾動條位置 ??????????? try ??????????? { ??????????????? pos = int.Parse(leFTPos.Text); ??????????????? if (pos == -1) pos = 0; ??????????? } ??????????? catch { } ??????????? //隱藏域 位置長度(用于插入,計算新的位置 +回傳)。 ??????????? switch (ExpressTree.SelectedNode.Value) ??????????? { ??????????????? case "NULL": ??????????????????? returnpos = pose(pos, "NULL"); ??????????????????? break; ??????????????? case "True": ??????????????????? returnpos = pose(pos, "True"); ??????????????????? break; ??????????????? case "False": ??????????????????? returnpos = pose(pos, "False"); ??????????????????? break; ??????????????? case "()": ??????????????????? returnpos = posm(pos, "( )"); ??????????????????? break; ??????????????? case "Alarm": ??????????????????? returnpos = posm(pos, "Alarm( )"); ??????????????????? break; ??????????????? case "Sensor": ??????????????????? returnpos = posm(pos, "Sensor( , , )"); ??????????????????? break; ??????????????? case "SensorAlarm": ??????????????????? returnpos = posm(pos, "SensorAlarm( , , )"); ??????????????????? break; ??????????????? case "ISNULL": ??????????????????? returnpos = posm(pos, "ISNULL( )"); ??????????????????? break; ??????????????? case "Exists": ??????????????????? returnpos = posm(pos, "Exists( )"); ??????????????????? break; ??????????????? case "NOT": ??????????????????? returnpos = posm(pos, "NOT( )"); ??????????????????? break; ??????????????? case "+": ??????????????????? returnpos = posf(pos, " + "); ??????????????????? break; ??????????????? case "-": ??????????????????? returnpos = posf(pos, " - "); ??????????????????? break; ??????????????? case "*": ??????????????????? returnpos = posf(pos, " * "); ??????????????????? break; ??????????????? case "/": ??????????????????? returnpos = posf(pos, " / "); ??????????????????? break; ??????????????? case "%": ??????????????????? returnpos = posf(pos, " % "); ??????????????????? break; ??????????????? case "==": ??????????????????? returnpos = posf(pos, " == "); ??????????????????? break; ??????????????? case "!=": ??????????????????? returnpos = posf(pos, " != "); ??????????????????? break; ??????????????? case ">": ??????????????????? returnpos = posf(pos, " > "); ??????????????????? break; ??????????????? case "<": ??????????????????? returnpos = posf(pos, " < "); ??????????????????? break; ??????????????? case ">=": ??????????????????? returnpos = posf(pos, " >= "); ??????????????????? break; ??????????????? case "<=": ??????????????????? returnpos = posf(pos, " <= "); ??????????????????? break; ??????????????? case "AND": ??????????????????? returnpos = posf(pos, " AND "); ??????????????????? break; ??????????????? case "OR": ??????????????????? returnpos = posf(pos, " OR "); ??????????????????? break; ??????????????? default: ??????????????????? break; ??????????? } ??????????? ExpressTree.SelectedNode.Selected = false;//因為只有選擇事件。為了模擬出點擊事件。 ??????????? leFTPos.Text = returnpos.ToString(); ??????????? scrollTop.Text = s; ??????????? FineUI.PageContext.RegisterStartupScript("dingwei('" + returnpos + "', '" + s + "', '" + ExpressValue.ClientID + "');"); ??????? } ??????? #region 三種指定光標位置方法 ??????? /// <summary> ??????? /// 字符串之前插入光標 ??????? /// </summary> ??????? /// <param name="pos"></param> ??????? /// <param name="insertstr"></param> ??????? /// <returns></returns> ??????? int posf(int pos, string insertstr) ??????? { ??????????? int position = pos; ??????????? string expressvaluetext = ExpressValue.Text; ??????????? ExpressValue.Text = expressvaluetext.Insert(pos, insertstr); ??????????? return position;//前插法,光標不變。 ??????? } ??????? /// <summary> ??????? /// 函數之中插入光標 ??????? /// </summary> ??????? /// <param name="pos"></param> ??????? /// <param name="insertstr"></param> ??????? /// <returns></returns> ??????? int posm(int pos, string insertstr) ??????? { ??????????? int position = pos; ??????????? string expressvaluetext = ExpressValue.Text; ??????????? ExpressValue.Text = expressvaluetext.Insert(pos, insertstr); ??????????? int keypos = insertstr.IndexOf(',');//兩個括號之間的第一個逗號 ??????????? if (keypos == -1) ??????????? { ??????????????? keypos = insertstr.LastIndexOf(')');//兩個括號之間的空 ??????????? } ??????????? return position + keypos; //中插法,光標在括號中間。 ??????? } ??????? /// <summary> ??????? /// 字符串之后插入光標 ??????? /// </summary> ??????? /// <param name="pos"></param> ??????? /// <param name="insertstr"></param> ??????? /// <returns></returns> ??????? int pose(int pos, string insertstr) ??????? { ??????????? int position = pos; ??????????? string expressvaluetext = ExpressValue.Text; ??????????? ExpressValue.Text = expressvaluetext.Insert(pos, insertstr); ??????????? int keypos = insertstr.Length;//兩個括號之間的空 ??????????? return position + keypos; ??????? } ??????? #endregion ??????? #endregion ??? } } |
現在是凌晨,發現如果精神好,集中注意力。這些東西都是可以一個小時內搞出來的,我的畏難情緒多來自身體不夠健康。
轉載于:https://my.oschina.net/mahaisong/blog/147046
總結
以上是生活随笔為你收集整理的《专家系统(开发)--表达式检测--与表达式模板一起使用》的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac下配置cocos2d-x开发环境(
- 下一篇: 用VS2005开发Sharepoint工