//解析XML文件中Variable元素的内容 private static void ParseVariableElement(XmlTextReader variableElement, TreeModel treeModel) { LSTree ruleData = treeModel.RuleData; string name = variableElement.GetAttribute("VName"); string strValue = variableElement.GetAttribute("VValue"); if (strValue == null || strValue.Length == 0) //确保value的值存在 { throw new ArgumentNullException("No VValue Attribute in Variable Element."); } float value = Convert.ToSingle(variableElement.GetAttribute("VValue")); string strMax = variableElement.GetAttribute("VMax"); string strMin = variableElement.GetAttribute("VMin"); string description = variableElement.GetAttribute("VDesc"); if (strMax != null && strMax.Length > 0 && strMin != null && strMin.Length > 0) //有最大最小值 { float max = Convert.ToSingle(strMax); float min = Convert.ToSingle(strMin); ruleData.AddGlobalVariable(name, value, max, min, description); } else //无最大最小值 { ruleData.AddGlobalVariable(name, value); } }
/// <summary> /// 解析XML文件中Model元素的Rule属性 /// </summary> /// <param name="ruleAttribute">Rule属性</param> /// <param name="ruleData">将Rule属性的内容存放在ruleData中</param> private static void ParseRuleAttribute(string ruleAttribute, LSTree ruleData) { ruleAttribute = ruleAttribute.Replace("\t", "").Replace("\n", "").Replace("\r", "").Replace(" ", ""); //清除空格等符号 string[] separtor = { "%%**" }; string[] ruleArray = ruleAttribute.Split(separtor, StringSplitOptions.RemoveEmptyEntries); //将字符串分类 for (int i = 0; i < ruleArray.Length; i++) { ParseRule(ruleArray[i], ruleData); } }
public void Initialize() { m_strName = ""; m_iCurrentStep = 0; m_iMaxStep = 0; m_RuleData = new LSTree(); m_Texture = null; AccumulatedTemperature = 0.0; InitialMeshGroup(); InitialActiveModel(); ClearParentModel(); ClearChildModels(); }
/// <summary> /// 清除当前的数据 /// </summary> public void Clear() { this.m_strName = null; this.m_iCurrentStep = 0; this.m_iMaxStep = 0; this.m_RuleData.Clear(); this.m_RuleData = null; this.m_Texture = null; this.m_MeshGroup.Clear(); this.LAI = 0; ClearParentModel(); ClearChildModels(); ClearAllWormholeTex(); ClearActiveModel(); //清除已经渲染出来的模型 }
void LoadModelInfo(TreeModel treeModel) { if (treeModel == null || treeModel.IsEmpty()) { return; //为空 } Init(); //基本信息 Name = treeModel.Name; InitStep = treeModel.InitStep; MaxStep = treeModel.MaxStep; //规则的输入 LSTree lstree = treeModel.RuleData; //公理 LRule += "@" + lstree.Axiom.ToString() + "\n"; //产生式 foreach (LRule production in lstree.ProductionGroup) { LRule += production.ToString() + "\n"; } //全局变量 GlobalVariables = lstree.GlobalVariablesList; //模型集合 Meshes = treeModel.Meshes; //枝干纹理 BranchTexturePath = treeModel.TexturePath; //用于判断是否为空,为空则不显示任何内容 isEmpty = false; }
/// <summary> /// 对规则进行分析,并存入对应的模型中 /// </summary> /// <param name="rule">规则</param> /// <param name="ruleData">将解析出的内容存放在ruleData中</param> private static void ParseRule(string rule, LSTree ruleData) { if (rule == null || rule.Length == 0) { throw new ArgumentNullException("Empty rule."); } if (rule[0] == '#' && rule.Substring(0, 7).ToLower().Equals("#ignore")) //上下文的无视条件 { ruleData.AddIgnore(GetTermList(rule.Substring(8)).ToList()); } else if (rule[0] == '@') //公理 { if (ruleData.Axiom.Count > 0) { throw new InvalidOperationException("Too much axiom"); } ruleData.AddAxiom(GetTermList(rule.Substring(1))); } else //产生式 { string[] separtor = { "-->" }; string[] subRules = rule.Split(separtor, StringSplitOptions.RemoveEmptyEntries);//将产生式分解成前半部分(包括前驱和条件)和后半部分(包括后续,即概率和结果) if (subRules.Length != 2) { throw new InvalidOperationException("Invalid Production"); } LRule production = new LRule(); //前半部分 string[] PredecessorAndConditions = subRules[0].Split(':'); string Predecessor; //解析右侧上下文 string[] PredecessorAndRightContext = PredecessorAndConditions[0].Split('>'); if (PredecessorAndRightContext.Length == 2) //存在右侧上下文 { string RigthContext = PredecessorAndRightContext[1]; production.AddRightContext(GetTermList(RigthContext).ToList()); } //解析左侧上下文 string[] LeftContextAndPredecessor = PredecessorAndRightContext[0].Split('<'); if (LeftContextAndPredecessor.Length == 2) //存在左侧上下文 { string LeftContext = LeftContextAndPredecessor[0]; Predecessor = LeftContextAndPredecessor[1]; production.AddLeftContext(GetTermList(LeftContext).ToList()); } else { Predecessor = LeftContextAndPredecessor[0]; } production.AddPredecessor(Predecessor); //添加前驱 if (PredecessorAndConditions.Length == 2) { production.AddCondition(PredecessorAndConditions[1].Remove(PredecessorAndConditions[1].Length - 1, 1).Remove(0, 1)); //添加去掉头尾的括号的条件 } //后半部分 string ProbabilityAndResult = subRules[1]; if (ProbabilityAndResult[0] == '(') //有概率 { int iIndexOfProbability = ProbabilityAndResult.IndexOf(')'); production.AddProbability(Convert.ToSingle(ProbabilityAndResult.Substring(1, iIndexOfProbability - 1))); //添加概率 production.AddResult(GetTermList(ProbabilityAndResult.Substring(iIndexOfProbability + 1))); //添加结果 } else //无概率 { production.AddResult(GetTermList(ProbabilityAndResult)); } ruleData.AddProduction(production); } }
public void SetBelong(LSTree ruleData) { this.m_RuleDataEntry = ruleData; }