SustainModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Linq;
  7. namespace GDNXFD.Alert.Interpreter
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public class SustainModel
  13. {
  14. public string Expression { get; set; }
  15. public int Duration { get; set; }
  16. public int Interval { get; set; }
  17. public int Sequence { get; set; }
  18. public int StartIndex { get; set; }
  19. public int EndIndex { get; set; }
  20. public int DurationIndex { get; set; }
  21. public int IntervalIndex { get; set; }
  22. public IList<string> VarList { get; set; }
  23. public IList<SustainModel> SustainList { get; set; }
  24. public string ToMethodString()
  25. {
  26. if (VarList == null || VarList.Count == 0)
  27. {
  28. throw new ExpressionException("Sustain函数中至少包含一个测点!");
  29. }
  30. StringBuilder sbVarDeclare = new StringBuilder();
  31. foreach (string varStr in VarList)
  32. {
  33. if (varStr.StartsWith("AI"))
  34. sbVarDeclare.AppendLine(string.Format(CodeTemplate.AIsDeclare, varStr));
  35. else
  36. sbVarDeclare.AppendLine(string.Format(CodeTemplate.DIsDeclare, varStr));
  37. }
  38. if (SustainList != null && SustainList.Count > 0)
  39. {
  40. foreach (SustainModel sm in SustainList)
  41. {
  42. sbVarDeclare.AppendLine(string.Format(CodeTemplate.BoolSustainDeclare, sm.Sequence, sm.GetCallString()));
  43. //exps = exps.Replace(sm.Expression, sm.GetCallString());
  44. }
  45. }
  46. string sustainExp = GetNewExpression();
  47. string snapLength = VarList.First().Trim() + ".Length";
  48. return string.Format(CodeTemplate.SustainMethod, Sequence, sbVarDeclare.ToString(), snapLength, sustainExp);
  49. }
  50. public string GetCallString()
  51. {
  52. return string.Format(CodeTemplate.SustainCallString, Sequence, Duration, Interval);
  53. }
  54. public string GetNewExpression()
  55. {
  56. string exps = Expression;
  57. exps = exps.Substring(0, DurationIndex - StartIndex - 1);
  58. exps = exps.Trim();
  59. exps = exps.Substring(7);
  60. exps = exps.TrimStart(' ', '(');
  61. if (SustainList != null && SustainList.Count > 0)
  62. {
  63. foreach (SustainModel sm in SustainList)
  64. {
  65. exps = exps.Replace(sm.Expression, "bool" + sm.Sequence);
  66. }
  67. }
  68. if (VarList != null && VarList.Count > 0)
  69. {
  70. foreach (string strVar in VarList)
  71. {
  72. string pattern = "\\b" + strVar + "\\b";
  73. string replacement = strVar + "[i]";
  74. Regex rgx = new Regex(pattern);
  75. exps = rgx.Replace(exps, replacement);
  76. }
  77. }
  78. return exps;
  79. }
  80. private string GenerateRandomString(int codeCount)
  81. {
  82. string str = string.Empty;
  83. int seed = new Random().Next();
  84. Random random = new Random(seed);
  85. for (int i = 0; i < codeCount; i++)
  86. {
  87. int num = random.Next();
  88. str = str + ((char)(65 + ((ushort)(num % 26)))).ToString();
  89. }
  90. return str;
  91. }
  92. }
  93. }