/// <summary> /// 采用非多线程的方式迭代 /// </summary> private void ReplaceFinalList() { LLinkedListNode <LTerm> node = m_listFinal.First; //从头节点开始 LLinkedListNode <LTerm> head = m_listFinal.First; //头节点 LLinkedList <LTerm> FinalList = new LLinkedList <LTerm>(); do { LLinkedListNode <LTerm> NodeClone = new LLinkedListNode <LTerm>(node.Value); //防止替换后原先的Node出现错误 FinalList.Add(NodeClone); if (node.Value != null) { LLinkedList <LTerm> resultList = GetResultListFrom(node); if (resultList != null) { FinalList.Replace(NodeClone, resultList); } } node = node.Next; } while (node != head); m_listFinal = FinalList; }
/// <summary> /// 根据字符串获取其链表 /// </summary> /// <param name="subRule">需要解析的字符串</param> /// <returns>通过字符串解析得到的链表</returns> private static LLinkedList <LTerm> GetTermList(string subRule) { LLinkedList <LTerm> result = new LLinkedList <LTerm>(); string[] termArray = SplitTerm(subRule); //将字符串分解成各个代表Term的字符串 for (int i = 0; i < termArray.Length; i++) { result.Add(new LTerm(termArray[i])); //逐个添加到链表中 } return(result); }
/// <summary> /// 采用多线程的方式快速迭代 /// </summary> private void ReplaceFinalListFast() { LLinkedListNode <LTerm> node = m_listFinal.First; //从头节点开始 LLinkedListNode <LTerm> head = m_listFinal.First; //头节点 LLinkedListNode <LTerm>[] nodes = new LLinkedListNode <LTerm> [m_listFinal.Count]; LLinkedList <LTerm>[] resultLists = new LLinkedList <LTerm> [m_listFinal.Count]; for (int i = 0; i < m_listFinal.Count; i++) { nodes[i] = new LLinkedListNode <LTerm>(node.Value); node = node.Next; } Parallel.For(0, m_listFinal.Count, i => { resultLists[i] = GetResultListFrom(nodes[i]); }); LLinkedList <LTerm> FinalList = new LLinkedList <LTerm>(); for (int i = 0; i < resultLists.Length; i++) { if (resultLists[i] == null) { FinalList.Add(nodes[i]); } else { FinalList.Add(resultLists[i]); } } m_listFinal = FinalList; }
/// <summary> /// 将当前链表克隆 /// </summary> /// <returns></returns> public LLinkedList <T> Clone() { LLinkedList <T> resultList = new LLinkedList <T>(); LLinkedListNode <T> srcNode = m_head; //源节点从该链表的头结点开始 do { LLinkedListNode <T> destNode = new LLinkedListNode <T>(srcNode.Value); //复制源节点的值,不复制其前驱和后继 resultList.Add(destNode); //插入复制后的节点 srcNode = srcNode.Next; //移动到下一个节点 } while (srcNode != m_head); //当源节点重新回归到头结点时,停止复制 return(resultList); }
/// <summary> /// 根据输入的参数输出该产生式产生的结果链表。 /// 如该产生式为 F(length)-->F(length+1)A(length),输入的参数为1,则输出的结果为F(2)A(1) /// </summary> /// <param name="node">当前的节点,用于确定其上下文</param> /// <param name="inputParams">输入的参数,用于计算,且个数一定要与前驱中的形参的个数相同</param> /// <returns>返回最终产生的结果链表</returns> public LLinkedList <LTerm> GetResultList(LLinkedListNode <LTerm> node, params float[] inputParams) { Validate(); ValidateInputParams(inputParams); List <LTerm> FinalLeftContext, FinalRightLeftContext; if (!isContextual(node, out FinalLeftContext, out FinalRightLeftContext)) //不满足上下文的条件 { return(null); } LLinkedList <LTerm> result = new LLinkedList <LTerm>(); //存储最终产生的结果 DataTable table = new DataTable(); //用于计算各参数 参考代码:https://www.cnblogs.com/luther/p/3863274.html //添加形参及公式 table.BeginLoadData(); //添加全局变量形参 List <VariableEntry> GlobalVariablesList = m_RuleDataEntry.GlobalVariablesList; //获取全局变量的链表 for (int i = 0; i < GlobalVariablesList.Count; i++) { GlobalVariablesList[i].Validate(); //验证该全局变量是否可用 table.Columns.Add(GlobalVariablesList[i].Name, typeof(float)); //添加全局变量列 } table.Columns.Add("RANDOM", typeof(float)); //添加随机变量列 //添加Mesh形参 List <Mesh> Meshs = (m_RuleDataEntry.Belong).Meshes; //获取存放所有Mesh的链表 for (int i = 0; i < Meshs.Count; i++) { table.Columns.Add(Meshs[i].Name, typeof(int)); } //添加输入参数的形参 for (int i = 0; i < m_Predecessor.Term.Params.Count; i++) { table.Columns.Add(m_Predecessor.Term.Params[i], typeof(float)); //添加形参列 } //添加上下文参数的形参 foreach (LTerm term in m_Predecessor.LeftContext) //添加左侧上下文的形参 { for (int i = 0; i < term.Params.Count; i++) { table.Columns.Add(term.Params[i], typeof(float)); } } foreach (LTerm term in m_Predecessor.RightContext) //添加右侧上下文的形参 { for (int i = 0; i < term.Params.Count; i++) { table.Columns.Add(term.Params[i], typeof(float)); } } //添加公式列 table.Columns.Add("expression", typeof(float)); table.Columns["expression"].Expression = ""; //添加实参 DataRow row = table.Rows.Add(); for (int i = 0; i < GlobalVariablesList.Count; i++) { row[GlobalVariablesList[i].Name] = GlobalVariablesList[i].Value; //添加全局变量的实参 } for (int i = 0; i < Meshs.Count; i++) { row[Meshs[i].Name] = Meshs[i].NameValue; } for (int i = 0; i < m_Predecessor.Term.Params.Count; i++) { row[m_Predecessor.Term.Params[i]] = inputParams[i]; //添加输入参数的实参 } //添加上下文的实参 for (int i = 0; i < FinalLeftContext.Count; i++) { for (int j = 0; j < FinalLeftContext[i].Params.Count; j++) { row[m_Predecessor.LeftContext[i].Params[j]] = FinalLeftContext[i].Params[j]; } } for (int i = 0; i < FinalRightLeftContext.Count; i++) { for (int j = 0; j < FinalRightLeftContext[i].Params.Count; j++) { row[m_Predecessor.RightContext[i].Params[j]] = FinalRightLeftContext[i].Params[j]; } } //判断是否满足条件 if (!MeetTheConditions(table)) { return(null); } if (m_listSuccessor.Count > 1) //有多个不同概率的后续 { //根据概率选择一个后续 m_iSuccessorIndex = RandomlySelectResultList(); } //根据选择的后续,逐个计算term,输出单一的term,并添加到结果链表中 LLinkedListNode <LTerm> srcNode = m_listSuccessor[m_iSuccessorIndex].Result.First; do { LTerm destTerm = GetResultTerm(srcNode.Value, table); //获取最终的单个term result.Add(destTerm); //插入Term srcNode = srcNode.Next; //移动到下一个节点 } while (srcNode != m_listSuccessor[m_iSuccessorIndex].Result.First); table.EndLoadData(); table.Clear(); return(result); }