日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

《专家系统(开发)--表达式检测--与表达式模板一起使用》

發(fā)布時間:2025/6/15 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《专家系统(开发)--表达式检测--与表达式模板一起使用》 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

《專家系統(tǒng)破解篇 九、Simple Rule Engine規(guī)則推理引擎三(表達(dá)式分析)》

表達(dá)式模板化配置的思路

在用不用后綴表達(dá)式方面搖擺了一陣,實在是覺得不用的話相當(dāng)麻煩,做了一半又改成用后綴的了。小看了先人的智慧啊。

先刪了。 測試的時候發(fā)現(xiàn)

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 正則表達(dá)式、自定義結(jié)構(gòu)(事實list、類型、封裝類型對象)、中綴后綴表達(dá)式

??????? 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);
??????? //表達(dá)式中監(jiān)控點,告警,監(jiān)控點的告警相關(guān)的三種事實數(shù)據(jù)。
??????? public static Regex repRegex = new Regex(@"\s*|\b\r\n\b+", RegexOptions.IgnoreCase);
??????? //替換表達(dá)式中的 空白與換行 字符
??????? 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);



??????? //切割表達(dá)式中的每一個操作符
??????? public struct ExpressFact// 表達(dá)式中事實LIST
??????? {
??????????? public string Value;
??????????? public int Index;
??????????? public int Length;
??????? }

??????? //類型
??????? public enum Type
??????? {
??????????? Fact,//--事實型都是value
??????????? Value,//布爾、浮點類型數(shù)字、字符型
??????????? Operator,//各種操作符
??????????? Function,//兩個方法 ISNULL 與Exists (都成立,不論是否有內(nèi)容,還是內(nèi)容是數(shù)字,字符,null 都成立。)
??????????? OpenBracket,//(開括號
??????????? CloseBracket,//)關(guān)括號
??????????? 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 檢測表達(dá)式

??????? 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);//得到事實(告警、監(jiān)控點、監(jiān)控點告警)位置,事實經(jīng)過匹配的肯定都是符合規(guī)則的1-9位之間的數(shù)字
??????????????? 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);
??????????????????????? }
??????????????????? }
??????????????? }

??????????????? //構(gòu)造表達(dá)式的類型
??????????????? string replacestr = currencyRegex.Replace(input, "Fact");//替換掉事實(告警、監(jiān)控點、監(jiān)控點告警)

??????????????? string[] rawTokins = spliRegex.Split(replacestr);//中綴表達(dá)式切割 此方法會返回一下空字符串
??????????????? 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 = "警告:表達(dá)式出現(xiàn)無效字符!";
??????????????????????????? return;
??????????????????????? }
??????????????????????? infix.Add(current);
??????????????????? }
??????????????? }
??????????????? //需要轉(zhuǎn)后綴表達(dá)式,只有轉(zhuǎn)成后綴才能得到某操作符需要的參數(shù)。
??????????????? CheckOutExpress(ref resultStr);
??????????? }
??????????? catch { }
??????????? finally
??????????? {
??????????????? if (string.IsNullOrEmpty(resultStr))
??????????????? {
??????????????????? //存儲表達(dá)式express

??????????????? }
??????????????? else
??????????????? {
??????????????????? Alert.ShowInTop(resultStr);
??????????????? }
??????????? }
??????? }

??????? /// <summary>
??????? /// 檢查表達(dá)式中綴
??????? /// </summary>
??????? /// <param name="resultStr"></param>
??????? private void CheckOutExpress(ref string resultStr)
??????? {

??????????? #region 轉(zhuǎn)換成后綴表達(dá)式 --為了操作符參數(shù)、優(yōu)先級

??????????? 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);//后綴表達(dá)式
??????????????? }
??????????????? else if (s.type == Type.Operator || s.type == Type.Function)
??????????????? {
??????????????????? //棧中是否有括號和函數(shù)、操作符-- 當(dāng)前操作符優(yōu)先級低則彈出原來高的(先計算原來的優(yōu)先級高的)
??????????????????? 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)//關(guān)括號
??????????????? {
??????????????????? while (((Symbol)postfixStack.Peek()).type != Type.OpenBracket)//循環(huán)括號中間操作。輸出,直到匹配開括號結(jié)束
??????????????????? {
??????????????????????? postfix.Add((Symbol)postfixStack.Pop());
??????????????????? }
??????????????????? postfixStack.Pop(); //彈出開括號。? 不符合規(guī)則的表達(dá)式))))這種要注意空引用異常。
??????????????? }
??????????????? else
??????????????? {
??????????????????? resultStr = "非法 符號: " + s.name;
??????????????????? return;
??????????????? }
??????????? }
??????????? //輸出棧,補全操作符號
??????????? while (postfixStack.Count > 0)
??????????? {
??????????????? postfix.Add((Symbol)postfixStack.Pop());
??????????? }

??????????? #endregion

??????????? #region?? 后綴表達(dá)式檢查

??????????? 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
??????????????????? {
??????????????????????? //操作符參數(shù)
??????????????????????? Symbol opQ;//前 參數(shù)
??????????????????????? Symbol opH;//后 參數(shù)

??????????????????????? Symbol opR = new Symbol();//計算結(jié)果的類型
??????????????????????? 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 + " 加法運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 減法運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 乘法運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 除法運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 求百分比運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 等于 運算兩邊參數(shù)類型不相同 ";
??????????????????????????????????? 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 + " 不等于 運算兩邊參數(shù)類型不相同 ";
??????????????????????????????????? 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 + " 大于 運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 小于 運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 大于等于 運算參數(shù)異常 ";
??????????????????????????????????? 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 + " 小于等于 運算參數(shù)異常 ";
??????????????????????????????????? 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 運算參數(shù)異常 ";
??????????????????????????????????? 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 運算參數(shù)異常 ";
??????????????????????????????????? return;
??????????????????????????????? }
??????????????????????????????? opR.name = "Boolean";
??????????????????????????????? opR.Vaule = true;
??????????????????????????????? opR.type = Type.Value;

??????????????????????????????? break;
??????????????????????????? case "NOT":
??????????????????????????????? opQ = (Symbol)operandStack.Pop();? //一個參數(shù)

??????????????????????????????? opR.name = "Boolean";
??????????????????????????????? opR.Vaule = true;
??????????????????????????????? opR.type = Type.Value;
??????????????????????????????? break;
??????????????????????????? default:
??????????????????????????????? break;
??????????????????????? }
??????????????????????? operandStack.Push(opR);

??????????????????? }
??????????????????? catch
??????????????????? {
??????????????????????? resultStr = " 表達(dá)式異常!請檢查! ";
??????????????????????? return;
??????????????????? }

??????????????? }
??????????????? else if (s.type == Type.Function)
??????????????? {
??????????????????? try
??????????????????? {
??????????????????????? operandStack.Pop();
??????????????????? }
??????????????????? catch
??????????????????? {
??????????????????????? resultStr = " 表達(dá)式函數(shù)參數(shù)異常!請檢查! ";//函數(shù)內(nèi)部值
??????????????????????? 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)
??????????????? {

??????????????????? //改成浮點值存入, 方法時,用過做不同的操作。因為事實是作為整體存入的,沒有對應(yīng)的參數(shù)。
??????????????????? 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 = " 表達(dá)式輸入有誤、有多余參數(shù)、或計算結(jié)果非布爾值(真、假)!請檢查! ";
??????????????? return;
??????????? }
??????????? #endregion

??????? }

??????? //操作符優(yōu)先級
??????? 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;
??????????? }

??????????? //函數(shù)具有最高的優(yōu)先級
??????????? if (s.type == Type.Function)
??????????????? result = 64;

??????????? return result;
??????? }


??????? #region 字符串轉(zhuǎn)換成Symbol

??????? /// <summary>
??????? /// 字符串轉(zhuǎn)換成Symbol
??????? /// </summary>
??????? /// <param name="s"></param>
??????? /// <returns></returns>
??????? /// <remarks>表達(dá)式字符串的處理符號解析---- 按照優(yōu)先級進(jìn)行判斷。</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)) //函數(shù)
??????????? {
??????????????? 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;
??????? }
??????? //是否是數(shù)字
??????? 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;
??????? }

??????? //是否是關(guān)閉的小括號
??????? 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;
??????? }

??????? //是否是函數(shù)
??????? 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.檢測該填寫的是否填寫,填寫的是否符合規(guī)格。
??????? //3.插入text中。并且定位位置。 統(tǒng)一的函數(shù)定位法,直接在最后一個)之后即可。
??????? 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("請輸入告警邏輯判斷的數(shù)值!");
??????????????????? }
??????????????? }
??????????????? 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("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }

??????? }

??????? 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("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }

??????? }

??????? 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("請輸入監(jiān)控點的邏輯判斷的數(shù)值!");
??????????????????????? }
??????????????????? }
??????????????????? else { Alert.ShowInTop("請選擇邏輯符號!"); }
??????????????? }
??????????????? else { Alert.ShowInTop("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }
??????? }

??????? 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("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }
??????? }

??????? 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("請輸入監(jiān)控點的告警的邏輯判斷的數(shù)值!");
??????????????????????? }
??????????????????? }
??????????????????? else { Alert.ShowInTop("請選擇邏輯符號!"); }
??????????????? }
??????????????? else { Alert.ShowInTop("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }

??????? }
??????? 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("請選擇監(jiān)控點信息!"); }
??????????? }
??????????? else
??????????? {
??????????????? Alert.ShowInTop("請選擇設(shè)備類型!");
??????????? }
??????? }

??????? #endregion

??????? #region 樹形表達(dá)式選擇方法

??????? 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 三種指定光標(biāo)位置方法
??????? /// <summary>
??????? /// 字符串之前插入光標(biāo)
??????? /// </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;//前插法,光標(biāo)不變。
??????? }
??????? /// <summary>
??????? /// 函數(shù)之中插入光標(biāo)
??????? /// </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; //中插法,光標(biāo)在括號中間。
??????? }
??????? /// <summary>
??????? /// 字符串之后插入光標(biāo)
??????? /// </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

??? }
}


現(xiàn)在是凌晨,發(fā)現(xiàn)如果精神好,集中注意力。這些東西都是可以一個小時內(nèi)搞出來的,我的畏難情緒多來自身體不夠健康。


本人聲明:沐海(http://my.oschina.net/mahaisong) 以上文章是經(jīng)過本人設(shè)計實踐和閱讀其他文檔得出。如果需要探討或指教可以留言!歡迎交流!

轉(zhuǎn)載于:https://my.oschina.net/mahaisong/blog/147046

總結(jié)

以上是生活随笔為你收集整理的《专家系统(开发)--表达式检测--与表达式模板一起使用》的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。