123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<string, int> dictPointClumns;
- private IDictionary<string, ArrayList> dictPoints;
- private IDictionary<string, string[]> dictPointArray;
- private IDictionary<string, ArrayList> dictModel;
- private IDictionary<string, CodeCompilerResults> 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<string, ArrayList>();
- 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<string, ArrayList>();
- if (dictPoints.ContainsKey(clsName))
- dictPoints[clsName] = al;
- else
- dictPoints.Add(clsName, al);
- string[] varArray = (string[])al[0];
- for(int i=1;i<al.Count;i++)
- {
- string[] arr = (string[])al[i];
- if (dictCodeCompilerResults == null)
- dictCodeCompilerResults = new Dictionary<string, CodeCompilerResults>();
- 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<string, int>();
- 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<string, string[]>();
- 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<al.Count;i++)
- {
- string[] strArr = (string[])al[i];
- for(int j=1;j<strArr.Length;j++)
- {
- result[index++] = strArr[j];
- }
- }
- dictPointArray.Add(clsName, result);
- }
- return dictPointArray[clsName];
- }
- }
- }
|