using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Text; using System.Text.RegularExpressions; using System.Linq; namespace GDNXFD.Alert.Interpreter { /// /// /// public class SustainModel { public string Expression { get; set; } public int Duration { get; set; } public int Interval { get; set; } public int Sequence { get; set; } public int StartIndex { get; set; } public int EndIndex { get; set; } public int DurationIndex { get; set; } public int IntervalIndex { get; set; } public IList VarList { get; set; } public IList SustainList { get; set; } public string ToMethodString() { if (VarList == null || VarList.Count == 0) { throw new ExpressionException("Sustain函数中至少包含一个测点!"); } StringBuilder sbVarDeclare = new StringBuilder(); foreach (string varStr in VarList) { if (varStr.StartsWith("AI")) sbVarDeclare.AppendLine(string.Format(CodeTemplate.AIsDeclare, varStr)); else sbVarDeclare.AppendLine(string.Format(CodeTemplate.DIsDeclare, varStr)); } if (SustainList != null && SustainList.Count > 0) { foreach (SustainModel sm in SustainList) { sbVarDeclare.AppendLine(string.Format(CodeTemplate.BoolSustainDeclare, sm.Sequence, sm.GetCallString())); //exps = exps.Replace(sm.Expression, sm.GetCallString()); } } string sustainExp = GetNewExpression(); string snapLength = VarList.First().Trim() + ".Length"; return string.Format(CodeTemplate.SustainMethod, Sequence, sbVarDeclare.ToString(), snapLength, sustainExp); } public string GetCallString() { return string.Format(CodeTemplate.SustainCallString, Sequence, Duration, Interval); } public string GetNewExpression() { string exps = Expression; exps = exps.Substring(0, DurationIndex - StartIndex - 1); exps = exps.Trim(); exps = exps.Substring(7); exps = exps.TrimStart(' ', '('); if (SustainList != null && SustainList.Count > 0) { foreach (SustainModel sm in SustainList) { exps = exps.Replace(sm.Expression, "bool" + sm.Sequence); } } if (VarList != null && VarList.Count > 0) { foreach (string strVar in VarList) { string pattern = "\\b" + strVar + "\\b"; string replacement = strVar + "[i]"; Regex rgx = new Regex(pattern); exps = rgx.Replace(exps, replacement); } } return exps; } private string GenerateRandomString(int codeCount) { string str = string.Empty; int seed = new Random().Next(); Random random = new Random(seed); for (int i = 0; i < codeCount; i++) { int num = random.Next(); str = str + ((char)(65 + ((ushort)(num % 26)))).ToString(); } return str; } } }