123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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<string> VarList { get; set; }
- public IList<SustainModel> 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()));
-
- }
- }
- 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;
- }
- }
- }
|