/// <summary> /// 解析 /// </summary> /// <param name="text"></param> /// <param name="splitterOfMatrixItems">分隔矩阵元素的分隔符</param> /// <returns></returns> public static AdjustObsMatrix Parse(string text, string[] splitterOfMatrixItems = null) { AdjustObsMatrix obs = new AdjustObsMatrix(); if (splitterOfMatrixItems == null) { splitterOfMatrixItems = new string[] { ",", ";", "\t", " " }; } string[] lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.None); //保留空格以分隔 //解析本对象属性 ParseHeader(obs, lines); int len = lines.Length; var typeNames = Enum.GetNames(typeof(ObsMatrixType)); StringBuilder currentBlockSb = new StringBuilder(); ObsMatrixType currentType = ObsMatrixType.Apriori; ObsMatrixType nextType = ObsMatrixType.Apriori; for (int i = 0; i < len; i++) { string line = lines[i].Trim(); //当前行 if (typeNames.Contains(line)) //新区 { currentType = nextType; // nextType = Geo.Utils.EnumUtil.Parse <ObsMatrixType>(line); //这是解析的下一个区的名称 if (currentBlockSb.Length > 2) { ParseBufferText(splitterOfMatrixItems, obs, currentBlockSb, currentType); } continue; } currentBlockSb.AppendLine(line); } if (currentBlockSb.Length > 2) { currentType = nextType;// //解析最后一个 ParseBufferText(splitterOfMatrixItems, obs, currentBlockSb, currentType); } return(obs); }
/// <summary> /// 解析为对象 /// </summary> /// <param name="splitterOfMatrixItems"></param> /// <param name="obs"></param> /// <param name="currentBlockSb"></param> /// <param name="currentType"></param> private static void ParseBufferText(string[] splitterOfMatrixItems, AdjustObsMatrix obs, StringBuilder currentBlockSb, ObsMatrixType currentType) { switch (currentType) { case ObsMatrixType.Apriori: obs.Apriori = WeightedVector.Parse(currentBlockSb.ToString()); break; case ObsMatrixType.Observation: obs.Observation = WeightedVector.Parse(currentBlockSb.ToString(), splitterOfMatrixItems); break; case ObsMatrixType.Coefficient: obs.Coefficient = Matrix.Parse(currentBlockSb.ToString(), splitterOfMatrixItems); break; case ObsMatrixType.SecondCoefficient: obs.SecondCoefficient = Matrix.Parse(currentBlockSb.ToString(), splitterOfMatrixItems); break; case ObsMatrixType.Transfer: obs.Transfer = WeightedMatrix.Parse(currentBlockSb.ToString(), splitterOfMatrixItems); break; case ObsMatrixType.ApproxVector: obs.ApproxVector = Vector.Parse(currentBlockSb.ToString()); break; case ObsMatrixType.SecondApproxVector: obs.SecondApproxVector = Vector.Parse(currentBlockSb.ToString()); break; case ObsMatrixType.FreeVector: obs.FreeVector = Vector.Parse(currentBlockSb.ToString()); break; case ObsMatrixType.SecondFreeVector: obs.SecondFreeVector = Vector.Parse(currentBlockSb.ToString()); break; default: break; } currentBlockSb.Clear(); }