TableModelFactory.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using log4net;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. namespace IntelligentControlForsx.CodeGenerator
  8. {
  9. public class TableModelFactory
  10. {
  11. private ILog logger = LogManager.GetLogger("AppLog");
  12. private TableModelFactory()
  13. {
  14. }
  15. public static TableModelFactory Instance
  16. {
  17. get { return SingletonCreator.instance; }
  18. }
  19. class SingletonCreator
  20. {
  21. internal static readonly TableModelFactory instance = new TableModelFactory();
  22. }
  23. private IDictionary<string, int> dictPointClumns;
  24. private IDictionary<string, ArrayList> dictPoints;
  25. private IDictionary<string, string[]> dictPointArray;
  26. private IDictionary<string, ArrayList> dictModel;
  27. private IDictionary<string, CodeCompilerResults> dictCodeCompilerResults;
  28. public int getPointsColumns(string clsName)
  29. {
  30. if (dictPointClumns == null || dictPointClumns.ContainsKey(clsName) == false)
  31. return 0;
  32. return dictPointClumns[clsName];
  33. }
  34. public ArrayList getTableModel(string clsName)
  35. {
  36. if (dictModel == null)
  37. dictModel = new Dictionary<string, ArrayList>();
  38. if (dictModel.ContainsKey(clsName) == false)
  39. {
  40. string fileName = System.Environment.CurrentDirectory + "\\conf\\mainparameters\\" + clsName + ".csv";
  41. ArrayList tm = CreateTableModel(clsName, fileName);
  42. dictModel.Add(clsName, tm);
  43. }
  44. return dictModel[clsName];
  45. }
  46. private ArrayList CreateTableModel(string clsName, string fileFullName)
  47. {
  48. ArrayList result = new ArrayList();
  49. try
  50. {
  51. ArrayList al = readCsv(fileFullName);
  52. if (dictPoints == null)
  53. dictPoints = new Dictionary<string, ArrayList>();
  54. if (dictPoints.ContainsKey(clsName))
  55. dictPoints[clsName] = al;
  56. else
  57. dictPoints.Add(clsName, al);
  58. string[] varArray = (string[])al[0];
  59. for(int i=1;i<al.Count;i++)
  60. {
  61. string[] arr = (string[])al[i];
  62. if (dictCodeCompilerResults == null)
  63. dictCodeCompilerResults = new Dictionary<string, CodeCompilerResults>();
  64. if (dictCodeCompilerResults.ContainsKey(clsName) == false)
  65. {
  66. CodeCompilerResults er = CodeCompiler.CompileModelCode(clsName, varArray);
  67. if (er.HasErrors)
  68. {
  69. logger.ErrorFormat("生成失败!clsName={0}", clsName);
  70. break;
  71. }
  72. dictCodeCompilerResults.Add(clsName, er);
  73. }
  74. CodeCompilerResults cr = dictCodeCompilerResults[clsName];
  75. result.Add(cr.CreateTableModel(arr[0]));
  76. }
  77. if (dictPointClumns == null)
  78. dictPointClumns = new Dictionary<string, int>();
  79. if (dictPointClumns.ContainsKey(clsName))
  80. dictPointClumns[clsName] = varArray.Length -1;
  81. else
  82. dictPointClumns.Add(clsName, varArray.Length - 1);
  83. }
  84. catch (Exception ex)
  85. {
  86. logger.ErrorFormat("代码生成失败!", ex.Message);
  87. }
  88. return result;
  89. }
  90. private ArrayList readCsv(string fileFullName)
  91. {
  92. ArrayList result = new ArrayList();
  93. using (StreamReader sr = new StreamReader(fileFullName, Encoding.Default))
  94. {
  95. string strLine = sr.ReadLine();
  96. string[] titles = strLine.Split(',');
  97. result.Add(titles);
  98. while (strLine != null)
  99. {
  100. Console.WriteLine(strLine);
  101. strLine = sr.ReadLine();
  102. if (String.IsNullOrWhiteSpace(strLine))
  103. continue;
  104. string[] points = strLine.Split(',');
  105. if (points.Length == titles.Length)
  106. {
  107. result.Add(points);
  108. }
  109. }
  110. }
  111. return result;
  112. }
  113. public ArrayList getTablePoints(string clsName)
  114. {
  115. if (dictPoints == null)
  116. getTableModel(clsName);
  117. if (dictPoints.ContainsKey(clsName))
  118. return dictPoints[clsName];
  119. return null;
  120. }
  121. public string[] getTablePointArray(string clsName)
  122. {
  123. if (dictPointArray == null)
  124. dictPointArray = new Dictionary<string, string[]>();
  125. if (dictPointArray.ContainsKey(clsName) == false)
  126. {
  127. ArrayList al = getTablePoints(clsName);
  128. int rows = al.Count -1;
  129. int columns = dictPointClumns[clsName];
  130. string[] result = new string[rows * columns];
  131. int index = 0;
  132. for (int i=1;i<al.Count;i++)
  133. {
  134. string[] strArr = (string[])al[i];
  135. for(int j=1;j<strArr.Length;j++)
  136. {
  137. result[index++] = strArr[j];
  138. }
  139. }
  140. dictPointArray.Add(clsName, result);
  141. }
  142. return dictPointArray[clsName];
  143. }
  144. }
  145. }