示例#1
0
        static void Main(string[] args)
        {
            RuleInferenceEngine ruleInferenceEngine = new RuleInferenceEngine();

            Rule rule = new Rule("seo_hard_freelancer");

            rule.AddAntecedents(new GreaterEqualClause("experience", "4"));
            rule.AddAntecedents(new IsClause("type", "SEO"));
            rule.Consequent = new BaseClause("freelancer", "Oleg");
            ruleInferenceEngine.Rules.Add(rule);

            rule = new Rule("seo_easy_freelancer");
            rule.AddAntecedents(new LessClause("experience", "4"));
            rule.AddAntecedents(new IsClause("type", "SEO"));
            rule.Consequent = new BaseClause("freelancer", "Ivan");

            ruleInferenceEngine.Rules.Add(rule);

            List <BaseClause> unproved_conditions = new List <BaseClause>();

            Console.WriteLine("Write freencer experience:");

            ruleInferenceEngine.AddFact(new IsClause("type", Console.ReadLine()));
            ruleInferenceEngine.AddFact(new IsClause("experience", Console.ReadLine()));

            BaseClause conclusion = ruleInferenceEngine.Infer("freelancer", unproved_conditions);

            Console.WriteLine("Conclusion: " + conclusion);
        }
示例#2
0
        static public void DemoBackwardChainWithNullMemory()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            Console.WriteLine("Infer with All Facts Cleared:");
            rie.ClearFacts();

            List <Clause> unproved_conditions = new List <Clause>();

            Clause conclusion = null;

            while (conclusion == null)
            {
                conclusion = rie.Infer("vehicle", unproved_conditions);
                if (conclusion == null)
                {
                    if (unproved_conditions.Count == 0)
                    {
                        break;
                    }
                    Clause c = unproved_conditions[0];
                    Console.WriteLine("ask: " + c + "?");
                    unproved_conditions.Clear();
                    Console.WriteLine("What is " + c.Variable + "?");
                    String value = Console.ReadLine();
                    rie.AddFact(new ClauseIs(c.Variable, value));
                }
            }

            Console.WriteLine("Conclusion: " + conclusion);
            Console.WriteLine("Memory: ");
            Console.WriteLine("{0}", rie.Facts);
        }
示例#3
0
        static public void TestBackwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new ClauseIs("num_wheels", "4"));
            rie.AddFact(new ClauseIs("motor", "yes"));
            rie.AddFact(new ClauseIs("num_doors", "3"));
            rie.AddFact(new ClauseIs("size", "medium"));

            Console.WriteLine("Infer: vehicle");

            List <Clause> unproved_conditions = new List <Clause>();

            Clause conclusion = rie.Infer("vehicle", unproved_conditions);

            Console.WriteLine("Conclusion: " + conclusion);

            Debug.Assert(conclusion.Value == MiniVan);
        }
示例#4
0
        static public void TestForwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new ClauseIs("num_wheels", "4"));
            rie.AddFact(new ClauseIs("motor", "yes"));
            rie.AddFact(new ClauseIs("num_doors", "3"));
            rie.AddFact(new ClauseIs("size", "medium"));

            Console.WriteLine("before inference");
            Console.WriteLine("{0}", rie.Facts);
            Console.WriteLine("");

            rie.Infer();             //forward chain

            Console.WriteLine("after inference");
            Console.WriteLine("{0}", rie.Facts);
            Console.WriteLine("");

            Debug.Assert(rie.Facts.Count == 6);
        }
        public void TestForwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new IsClause("num_wheels", "4"));
            rie.AddFact(new IsClause("motor", "yes"));
            rie.AddFact(new IsClause("num_doors", "3"));
            rie.AddFact(new IsClause("size", "medium"));

            console.WriteLine("before inference");
            console.WriteLine("{0}", rie.Facts);
            console.WriteLine("");

            rie.Infer(); //forward chain

            console.WriteLine("after inference");
            console.WriteLine("{0}", rie.Facts);
            console.WriteLine("");

            Assert.Equal(6, rie.Facts.Count);
        }
示例#6
0
        private static void ReadStudentRecords()
        {
            var csv     = new CsvReader(File.OpenText("Intakes.csv"));
            var records = csv.GetRecords <StudentInfo>();

            foreach (StudentInfo record in records)
            {
                if (record.gewogen_gemiddelde == "")
                {
                    record.gewogen_gemiddelde = "0";
                }

                RuleInferenceEngine rie = GetInferenceEngine();
                rie.AddFact(new IsClause("gewogen_gemiddelde", record.gewogen_gemiddelde));
                rie.AddFact(new IsClause("competenties", record.competenties));
                rie.AddFact(new IsClause("capaciteiten", record.capaciteiten));
                rie.AddFact(new IsClause("intr. Motivatie", record.intr_motivatie));
                rie.AddFact(new IsClause("extr. Motivatie", record.extr_motivatie));
                rie.AddFact(new IsClause("is_mbo_deficient", record.is_mbo_deficient));
                rie.AddFact(new IsClause("Persoonlijk 'bijspijker'-advies", record.persoonlijk_bijspijker_advies));
                rie.AddFact(new IsClause("Aanmelden voor verkort opleidingstraject", record.Aanmelden_voor_verkort_opleidingstraject));
                rie.AddFact(new IsClause("reden_stoppen", record.reden_stoppen));

                List <Clause> unproved_conditions = new List <Clause>();
                var           conclusion          = rie.Infer("advies", unproved_conditions);

                if (conclusion != null)
                {
                    Console.WriteLine($"{record.studentnummer}: {conclusion.Value}");
                }
                else
                {
                    Console.WriteLine($"Could not generate advice for student {record.studentnummer}");
                }
            }
        }
示例#7
0
        private void ExpertCycle()
        {
            RuleInferenceEngine rie = AutoRules.getInferenceEngine();

            rie.ClearFacts();

            List <Clause> unproved_conditions = new List <Clause>();

            conclusion = null;
            while (conclusion == null)
            {
                conclusion = rie.Infer("vehicle", unproved_conditions);
                if (conclusion == null)
                {
                    if (unproved_conditions.Count == 0)
                    {
                        break;
                    }
                    Clause c = unproved_conditions[0];

                    number_value = false;

                    for (int i = 0; i < rie.m_rules.Count(); i++)
                    {
                        if (number_value == true)
                        {
                            break;
                        }
                        for (int j = 0; j < rie.m_rules[i].m_antecedents.Count(); j++)
                        {
                            if (rie.m_rules[i].m_antecedents[j].Variable == c.Variable)
                            {
                                if (rie.m_rules[i].m_antecedents[j].Condition != "=")
                                {
                                    number_value = true;
                                    break;
                                }
                                else
                                {
                                    if (!answers_list.Contains(rie.m_rules[i].m_antecedents[j].Value))
                                    {
                                        answers_list.Add(rie.m_rules[i].m_antecedents[j].Value);
                                    }
                                }
                            }
                        }
                    }
                    if (number_value)
                    {
                        question = "У данного ТС сколько " + c.Variable + "?";
                    }
                    else
                    {
                        question = "У данного ТС " + c.Variable + "?";
                    }
                    //Console.WriteLine("ask: " + c + "?");
                    //question = "What is " + c.Variable + "?";
                    readyEvent.Set();
                    nextEvent.Reset();
                    unproved_conditions.Clear();
                    nextEvent.WaitOne();
                    answers_list.Clear();
                    //Console.WriteLine("What is " + c.Variable + "?");
                    String value = answer;//Console.ReadLine();
                    rie.AddFact(new IsClause(c.Variable, value));
                }
            }
            readyEvent.Set();
            finished = true;
            //Console.WriteLine("Conclusion: " + conclusion);
            //Console.WriteLine("Memory: ");
            //Console.WriteLine("{0}", rie.Facts);
            //Console.ReadLine();
        }