void Start()
        {
            autoWidthToggle.isOn = treeBuilder.lTreeAutoWidth;
            widthInput.text      = treeBuilder.lTreeWidth.ToString();
            genInput.text        = treeBuilder.lTreeGrowthCount.ToString();
            lengthInput.text     = treeBuilder.lTreeBranchLength.ToString();
            angleInput.text      = treeBuilder.lTreeAngle.ToString();
            axiomInput.text      = treeBuilder.lTreeAxiom;

            for (int i = 0; i < treeBuilder.lTreeRules.Length; i++)
            {
                ruleInputs [i].text = RuleToString(treeBuilder.lTreeRules [i]);
            }

            if (treeBuilder.lTreeRules.Length < ruleInputs.Length)
            {
                LRule[] tempRules = new LRule[ruleInputs.Length];

                for (int i = 0; i < tempRules.Length; i++)
                {
                    if (i < treeBuilder.lTreeRules.Length)
                    {
                        tempRules [i] = treeBuilder.lTreeRules [i];
                    }
                    else
                    {
                        tempRules[i] = new LRule('\0', string.Empty);
                    }
                }

                treeBuilder.lTreeRules = tempRules;
            }

            AddListeners();
        }
Example #2
0
    /// <summary>
    /// 对产生式进行分类,使相同类型的产生式归为一类
    /// </summary>
    /// <returns></returns>
    private List <List <LRule> > ProductionClassification()
    {
        List <List <LRule> > classificationList = new List <List <LRule> >();

        for (int i = 0; i < m_listProductionGroup.Count; i++)
        {
            bool  isFound    = false;
            LRule production = m_listProductionGroup[i];
            //对每个分类结果进行查询
            for (int j = 0; j < classificationList.Count; j++)
            {
                //如果在该分类结果中存在与当前的产生式相同的前驱以及条件,则将该产生式插入该分类结果中
                if (classificationList[j].Exists(
                        (rule) => rule.Predecessor.Symbol.Equals(production.Predecessor.Symbol) &&
                        rule.Predecessor.Params.Count == production.Predecessor.Params.Count &&
                        rule.Condition.Equals(production.Condition)))
                {
                    classificationList[j].Add(production);
                    isFound = true;
                    break;
                }
            }

            //未能找到符合当前产生式的分类结果,说明第一次出现该类产生式,因此新建一个分类结果用于存储该类产生式
            if (!isFound)
            {
                List <LRule> classification = new List <LRule>();
                classification.Add(production);
                classificationList.Add(classification);
            }
        }

        return(classificationList);
    }
Example #3
0
    /// <summary>
    /// 添加产生式至产生式集合,并自动归类相同前驱的产生式
    /// </summary>
    /// <param name="production">待添加的产生式</param>
    public void AddProduction(LRule production)
    {
        production.Validate();                                           //验证该产生式是否可用

        LRule SameTypeProduction = FindSameTypeOfProduction(production); //寻找相同类型的产生式

        if (SameTypeProduction == null)                                  //没有相同类型的产生式
        {
            production.Belong = this;
            m_listProductionGroup.Add(production); //插入该产生式
        }
        else                                       //有相同类型的产生式
        {
            for (int i = 0; i < production.Successors.Count; i++)
            {
                SameTypeProduction.AddSuccessor(production.Successors[i].Probability.Value, production.Successors[i].Result); //在该类型中添加后续
            }
        }
    }
Example #4
0
    /// <summary>
    /// 根据输入的产生式在产生式的集合中寻找与其相同类型的产生式。相同类型的主要特征是前驱一样(相同的符号、参数以及条件)。
    /// </summary>
    /// <param name="production">根据该产生式进行寻找</param>
    /// <returns>返回找到的相同类型的产生式</returns>
    private LRule FindSameTypeOfProduction(LRule production)
    {
        List <LRule> productionList = m_listProductionGroup.FindAll((rule) =>
                                                                    rule.Predecessor.Symbol.Equals(production.Predecessor.Symbol) /*相同的符号*/ &&
                                                                    rule.Predecessor.Params.Count == production.Predecessor.Params.Count /*相同的参数个数*/ &&
                                                                    ((rule.Condition == null && production.Condition == null) ||                                             /*均无条件或*/
                                                                     (rule.Condition != null && production.Condition != null && rule.Condition.Equals(production.Condition)) /*均有条件,且条件相同*/
                                                                    ));

        if (productionList.Count > 1)   //如果存在多个相同的类型,则说明产生式集合存在一定问题,调整该集合并重新寻找
        {
            AdjustProductionGroup();
            return(FindSameTypeOfProduction(production));
        }

        //当有且只有一个相同类型的产生式时,返回该产生式
        if (productionList.Count == 1)
        {
            return(productionList[0]);
        }

        return(null); //没有相同类型的产生式,返回NULL
    }
Example #5
0
        public void AddRule(string propertyName, RuleType ruleType)
        {
            var propertiesWithDuplicatesAllowed = new HashSet <ItemProperty>
            {
                ItemProperty.ElementalDamage,
                ItemProperty.SkillDamage
            };

            Func <ItemProperty, bool> allowedToAdd = p => Rules.All(r => r.ItemProperty != p) || propertiesWithDuplicatesAllowed.Contains(p);

            ItemProperty property;

            Logger.Log("Attempting to Add {0} Type={1}", propertyName, ruleType);

            if (Enum.TryParse(propertyName, out property) && property != ItemProperty.Unknown && allowedToAdd(property))
            {
                var statRange = GetItemStatRange(property);
                if (statRange != null)
                {
                    Logger.LogVerbose(string.Format("Stats Min = {0} Max = {1} Step = {2}",
                                                    statRange.AbsMin.ToString(), statRange.AbsMax.ToString(), statRange.AbsStep.ToString()));
                }

                Rules.Add(new LRule
                {
                    Id              = (int)property,
                    ItemStatRange   = statRange,
                    TrinityItemType = TrinityItemType,
                    RuleType        = ruleType,
                    Value           = LRule.GetDefaultValue(property)
                });

                OnPropertyChanged("Rules");
                OnPropertyChanged(ruleType + "Rules");
            }
        }
Example #6
0
    /// <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);
        }
    }
 private string RuleToString(LRule rule)
 {
     return(string.Format("{0}={1}", rule.from, rule.to));
 }
Example #8
0
 internal static bool EvaluateRule(LRule itemRule, CachedACDItem cItem)
 {
     return(EvaluateRule(cItem, itemRule.ItemProperty, itemRule.Value, itemRule.Variant, itemRule.RuleType));
 }
Example #9
0
    public void GenerateRules()
    {/*
        // First Rule
        LRule rule1 = new LRule();
        rule1.name = "First";
        rule1.axiom = "ONN";
        rule1.rules.Add("O", "O[--N][+N]");
        rule1.rules.Add("N", "N[-N+N][+N++N]");
        rule1.expandingIterations = 2;
        rule1.delta = Mathf.PI / 7.2f;
        ruleCollection.Add(rule1);
        
        // Second Rule *STAR FORMATION*
        LRule rule2 = new LRule();
        rule2.name = "Star";
        rule2.axiom = "N--N--N";
        rule2.rules.Add("N", "N+N--N+N");
        rule2.expandingIterations = 3;
        rule2.delta = Mathf.PI / 3;
        ruleCollection.Add(rule2);
        
        // Third Rule *Grass Straw*
        LRule rule3 = new LRule();
        rule3.name = "Grass Straw";
        rule3.axiom = "N";
        rule3.rules.Add("N", "O[+N][-N]ON");
        rule3.rules.Add("O", "O");
        rule3.expandingIterations = 3;
        rule3.delta = Mathf.PI/ 7.0039f;
        ruleCollection.Add(rule3);

        // Fourth Rule *Box Fractal*
        LRule rule4 = new LRule();
        rule4.name = "Box Fractal";
        rule4.axiom = "N-N-N-N";
        rule4.rules.Add("N", "N-N+N+N-N");
        rule4.expandingIterations = 2;
        rule4.delta = Mathf.PI / 2;
        ruleCollection.Add(rule4);

        // Fifth Rule *Seaweed*
        LRule rule5 = new LRule();
        rule5.name = "Seaweed";
        rule5.axiom = "N";
        rule5.rules.Add("N", "NN-[-N+N+N]+[+N-N-N]");
        rule5.expandingIterations = 2;
        rule5.delta = Mathf.PI / 8.1818f;
        ruleCollection.Add(rule5);

        // Sixth Rule *Rings*
        LRule rule6 = new LRule();
        rule6.name = "Rings";
        rule6.axiom = "N";
        rule6.rules.Add("N", "NN+N+N+N+N+N-N");
        rule6.expandingIterations = 2;
        rule6.delta = Mathf.PI / 2;
        ruleCollection.Add(rule6);
        */
        // Seventh Rule *Test*
        LRule rule7 = new LRule();
        rule7.name = "Test";
        rule7.axiom = "[N][+++N][---N][------N]";
        rule7.rules.Add("N", "N[-N][+N][NN]");
        rule7.expandingIterations = 3;
        rule7.delta = Mathf.PI / 6; //30deg
        ruleCollection.Add(rule7);

        
    }