Example #1
0
        void LoadRulesCSV(string path)
        {
            Debug.Log("Loading Rules CSV: " + path);
            Dictionary <string, int> columnIDs = new Dictionary <string, int>();

            using (var streamRdr = new StreamReader(path)) {
                var csvReader = new CsvReader(streamRdr, ",");
                while (csvReader.Read())
                {
                    // load column names
                    if (columnIDs.Count == 0)
                    {
                        for (int i = 0; i < csvReader.FieldsCount; i++)
                        {
                            columnIDs.Add(csvReader[i], i);
                        }

                        // check that file is valid. if not, we die.
                        if (!(columnIDs.ContainsKey("name") && columnIDs.ContainsKey("criteria") && columnIDs.ContainsKey("responses") && columnIDs.ContainsKey("norepeat")))
                        {
                            throw new Exception("The Rules CSV file [" + path + "] does not contain all the columns we require");
                        }
                        continue;
                    }
                    // load in rule
                    string name           = csvReader[columnIDs["name"]];
                    string criteria       = csvReader[columnIDs["criteria"]];
                    string responses      = csvReader[columnIDs["responses"]];
                    bool   norepeat       = false;
                    string norepeatString = csvReader[columnIDs["norepeat"]];
                    if (norepeatString != "")
                    {
                        norepeat = Convert.ToBoolean(norepeatString);
                    }

                    if (name == "")
                    {
                        // skip empty rows
                        continue;
                    }
                    var rule = new RSRule(name, criteria, responses, norepeat);
                    this.lazyAllRules.Insert(rule);
                }
            }
            Debug.Log(this.lazyAllRules);
        }
        // rules are ordered in our bucket from:
        //  most criteria -> least criteria
        // this is described in the responsesystem talk, and is because if a rule
        // with more criteria (that's more specific) matches, we don't need to find
        // a rule that's less specific, we've already got the most specific one \o/
        //
        //TODO(dan): apply critereon weight when doing this as well.
        public void Insert(RSRule rule)
        {
            int c = rule.CriteriaCount();

            if (this.rules.Count == 0)
            {
                this.rules.Add(rule);
                return;
            }

            for (int i = 0; i <= c; i++)
            {
                if (i == c)
                {
                    this.rules.Add(rule);
                    break;
                }
                else if (this.rules[i].CriteriaCount() < c)
                {
                    this.rules.Insert(i, rule);
                    break;
                }
            }
        }