using log4net; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; namespace IntelligentControlForsx.CodeGenerator { public class TableModelFactory { private ILog logger = LogManager.GetLogger("AppLog"); private TableModelFactory() { } public static TableModelFactory Instance { get { return SingletonCreator.instance; } } class SingletonCreator { internal static readonly TableModelFactory instance = new TableModelFactory(); } private IDictionary dictPointClumns; private IDictionary dictPoints; private IDictionary dictPointArray; private IDictionary dictModel; private IDictionary dictCodeCompilerResults; public int getPointsColumns(string clsName) { if (dictPointClumns == null || dictPointClumns.ContainsKey(clsName) == false) return 0; return dictPointClumns[clsName]; } public ArrayList getTableModel(string clsName) { if (dictModel == null) dictModel = new Dictionary(); if (dictModel.ContainsKey(clsName) == false) { string fileName = System.Environment.CurrentDirectory + "\\conf\\mainparameters\\" + clsName + ".csv"; ArrayList tm = CreateTableModel(clsName, fileName); dictModel.Add(clsName, tm); } return dictModel[clsName]; } private ArrayList CreateTableModel(string clsName, string fileFullName) { ArrayList result = new ArrayList(); try { ArrayList al = readCsv(fileFullName); if (dictPoints == null) dictPoints = new Dictionary(); if (dictPoints.ContainsKey(clsName)) dictPoints[clsName] = al; else dictPoints.Add(clsName, al); string[] varArray = (string[])al[0]; for(int i=1;i(); if (dictCodeCompilerResults.ContainsKey(clsName) == false) { CodeCompilerResults er = CodeCompiler.CompileModelCode(clsName, varArray); if (er.HasErrors) { logger.ErrorFormat("生成失败!clsName={0}", clsName); break; } dictCodeCompilerResults.Add(clsName, er); } CodeCompilerResults cr = dictCodeCompilerResults[clsName]; result.Add(cr.CreateTableModel(arr[0])); } if (dictPointClumns == null) dictPointClumns = new Dictionary(); if (dictPointClumns.ContainsKey(clsName)) dictPointClumns[clsName] = varArray.Length -1; else dictPointClumns.Add(clsName, varArray.Length - 1); } catch (Exception ex) { logger.ErrorFormat("代码生成失败!", ex.Message); } return result; } private ArrayList readCsv(string fileFullName) { ArrayList result = new ArrayList(); using (StreamReader sr = new StreamReader(fileFullName, Encoding.Default)) { string strLine = sr.ReadLine(); string[] titles = strLine.Split(','); result.Add(titles); while (strLine != null) { Console.WriteLine(strLine); strLine = sr.ReadLine(); if (String.IsNullOrWhiteSpace(strLine)) continue; string[] points = strLine.Split(','); if (points.Length == titles.Length) { result.Add(points); } } } return result; } public ArrayList getTablePoints(string clsName) { if (dictPoints == null) getTableModel(clsName); if (dictPoints.ContainsKey(clsName)) return dictPoints[clsName]; return null; } public string[] getTablePointArray(string clsName) { if (dictPointArray == null) dictPointArray = new Dictionary(); if (dictPointArray.ContainsKey(clsName) == false) { ArrayList al = getTablePoints(clsName); int rows = al.Count -1; int columns = dictPointClumns[clsName]; string[] result = new string[rows * columns]; int index = 0; for (int i=1;i