Пример #1
0
        public void HRFLoadRuleBase()
        {
            IInferenceEngine ie = new IEImpl();

            ie.LoadRuleBase(new HRF086Adapter(ruleFilesFolder + "gedcom-relations.hrf", FileAccess.Read));
            Assert.AreEqual("forward", ie.Direction, "Gedcom Direction");
            Assert.AreEqual("gedcom-relations", ie.Label, "Gedcom Label");
            Assert.AreEqual(0, ie.FactsCount, "Gedcom Facts");

            ie.LoadRuleBase(new HRF086Adapter(ruleFilesFolder + "fraudcontrol.hrf", FileAccess.Read));
            Assert.AreEqual(3, ie.FactsCount, "FraudControl Facts");

            ie.LoadRuleBase(new HRF086Adapter(ruleFilesFolder + "telco-rating.hrf", FileAccess.Read));
            Assert.AreEqual(3, ie.FactsCount, "Telco-Rating Facts");
        }
Пример #2
0
        public void BeforeAfterFlowEngineBinder()
        {
            IInferenceEngine ie = new IEImpl(new FlowEngineBinder(ruleFilesFolder + "testbinder1.ruleml.xbre",
                                   											BindingTypes.BeforeAfter));

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "testbinder.ruleml",
                                                       FileAccess.Read));

            ie.NewFactHandler += new NewFactEvent(ShowAllNewFacts);

            Hashtable bo = new Hashtable();
            bo.Add("THEDUKE", new Character("The Duke", "hello world"));
            bo.Add("BOBBYBOB", new Character("Bobby Bob", "what the?"));
            bo.Add("JOHNQDOE", new Character("John Q. Doe", "hello, who am i?"));
            bo.Add("DANNYDAN", new Character("Danny Dan", "get out of my world"));

            ie.Process(bo);

            IList<IList<Fact>> qrs = ie.RunQuery("all polite");
            Assert.AreEqual(2, qrs.Count, "polite Count");

            // here, we should have got one result (Danny Dan), but the politness of The Duke has
            // mutexed the "mundane" implication.
            qrs = ie.RunQuery("all mundane");
            Assert.AreEqual(0, qrs.Count, "mundane Count");
        }
Пример #3
0
        public void PerformProcess(IBinder binder)
        {
            // generate dummy business objects
            IDictionary businessObjects = DummyData.GetInstance().GetBusinessObjects(nbDecaCustomers);

            // instantiate an inference engine, bind my data and process the rules
            IInferenceEngine ie = new IEImpl(binder);
            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleBaseFile, System.IO.FileAccess.Read));
            ie.Process(businessObjects);

            // processing is done, let's analyze the results
            IList<IList<Fact>> qrs = ie.RunQuery("Fraudulent Customers");
            Console.WriteLine("\nDetected {0} fraudulent customers.", qrs.Count);
            if (qrs.Count != 2 * nbDecaCustomers)
                Console.WriteLine("\nError! " + 2* nbDecaCustomers + " was expected.");

            // check if the customer objects have been flagged correctly
            int flaggedCount = 0;
            foreach(Customer customer in (ArrayList)businessObjects["CUSTOMERS"])
                if (customer.Fraudulent)
                    flaggedCount++;

            if (flaggedCount != 2 * nbDecaCustomers)
                throw new Exception("\nError! " + 2* nbDecaCustomers + " flagged Customer objects were expected.\n");
            else
                Console.WriteLine("\nCustomer objects were correctly flagged\n");
        }
Пример #4
0
        public void AfterBinderRetraction()
        {
            Character theDuke = new Character("The Duke", "hello world");
            Hashtable bo = new Hashtable();
            bo.Add("THEDUKE", theDuke);

            // first test to confirm that a pre-existing fact is not rededucted
            IInferenceEngine ie = new IEImpl(new FlowEngineBinder(ruleFilesFolder + "testbinder3.ruleml.xbre",
                                                                  BindingTypes.BeforeAfter));

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "testbinder.ruleml",
                                                       		FileAccess.Read));

            ie.Assert(new Fact("retract_target", "education", new Individual(theDuke), new Individual("polite")));
            ie.NewFactHandler += new NewFactEvent(ShowAllNewFacts);
            deducted = 0;
            ie.Process(bo);
            Assert.AreEqual(0, deducted, "The Duke was already polite!");

            // second test to confirm that retracting the fact in the after binder restarts inference
            ie = new IEImpl(new FlowEngineBinder(ruleFilesFolder + "testbinder4.ruleml.xbre",
                                   							 BindingTypes.BeforeAfter));

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "testbinder.ruleml", FileAccess.Read));

            ie.Assert(new Fact("retract_target", "education", new Individual(theDuke), new Individual("polite")));
            ie.NewFactHandler += new NewFactEvent(ShowAllNewFacts);
            deducted = 0;
            ie.Process(bo);
            Assert.AreEqual(1, deducted, "The Duke was re-deducted polite!");
        }
Пример #5
0
		private static void ProcessRuleBase(DslAdapter adapter) {
			IEImpl ie = new IEImpl();
			ie.LoadRuleBase(adapter);
			ie.Process();
			
			for(IEnumerator<Fact> e = ie.Facts; e.MoveNext();) {
				Console.WriteLine(e.Current);
			}			
		}
Пример #6
0
        public void Setup()
        {
            ie = new IEImpl();

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "events-test.ruleml",
                                                       FileAccess.Read));

            newFactEventCount = 0;
            deleteFactEventCount = 0;
            modifyFactEventCount = 0;
            factEventCount = 0;
        }
        protected string GenerateRuleFile(string inFile, SaveFormatAttributes sfa)
        {
            string outFile = outFilesFolder + sfa.ToString().ToLower() + "-" + inFile;

            FileInfo fi = new FileInfo(outFile);
            if (fi.Exists) fi.Delete();

            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + inFile, FileAccess.Read));
            ie.SaveRuleBase(new RuleML09NafDatalogAdapter(outFile, FileAccess.Write, sfa));

            Console.Out.WriteLine("Generated: {0}", outFile);

            return outFile;
        }
        public void IsolatedThreadSafeMemoryCloning()
        {
            //Regression test for bug #1340799
            IInferenceEngine ie1 = new IEImpl(ThreadingModelTypes.Multi);
            ie1.LoadRuleBase(NewTestAdapter());
            int ie1facts = ie1.FactsCount;

            IInferenceEngine ie2 = new IEImpl(ThreadingModelTypes.Multi);
            ie2.LoadRuleBase(NewGedcomAdapter());
            ie2.Assert(new Fact("sex", new Individual("dad"), new Individual("m")));
            ie2.NewWorkingMemory(WorkingMemoryTypes.Isolated);

            Assert.AreEqual(ie1facts, ie1.FactsCount, "FactsCount has wrongly changed");
        }
Пример #9
0
        public void HRFSaveRuleBase()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new HRF086Adapter(ruleFilesFolder + "gedcom-relations.hrf", FileAccess.Read));
            ie.SaveRuleBase(new HRF086Adapter(outFileHRF, FileAccess.Write));

            IInferenceEngine ie2 = new IEImpl();
            ie2.LoadRuleBase(new HRF086Adapter(outFileHRF, FileAccess.Read));

            Assert.AreEqual(ie.Direction, ie2.Direction, "Same Direction");
            Assert.AreEqual(ie.Label, ie2.Label, "Same Label");
        }
Пример #10
0
        public void ControlFlowEngineBinder()
        {
            IInferenceEngine ie = new IEImpl(new FlowEngineBinder(ruleFilesFolder + "testbinder2.ruleml.xbre",
                                   											BindingTypes.Control));

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "testbinder.ruleml",
                                                       FileAccess.Read));

            ie.NewFactHandler += new NewFactEvent(ShowAllNewFacts);

            Character theDuke = new Character("The Duke", "hello world");
            Character bobbyBob = new Character("Bobby Bob", "what the?");
            Character johnQDoe = new Character("John Q. Doe", "hello, who am i?");
            Character dannyDan = new Character("Danny Dan", "get out of my world");

            Hashtable bo = new Hashtable();
            ArrayList al = new ArrayList();
            al.Add(theDuke);
            al.Add(bobbyBob);
            al.Add(johnQDoe);
            al.Add(dannyDan);
            bo.Add("CHARACTERS", al);
            ie.Process(bo);

            // the binder uses isolated memory for each character,
            // so we will not run the queries but check the objects directly
            Assert.AreEqual("polite", theDuke.Education, "theDuke");
            Assert.AreEqual(String.Empty, bobbyBob.Education, "bobbyBob");
            Assert.AreEqual("polite", johnQDoe.Education, "johnQDoe");
            Assert.AreEqual("mundane", dannyDan.Education, "dannyDan");
        }
Пример #11
0
        public void RuleMLNaf09SaveRuleBase()
        {
            string inFile = ruleFilesFolder + "test-0_9.ruleml";

            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(inFile, FileAccess.Read));
            ie.SaveRuleBase(new RuleML09NafDatalogAdapter(outFile, FileAccess.Write));

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(outFile, FileAccess.Read));

            Assert.IsTrue(AreSameXml(inFile, outFile), "Same XML: " + inFile + " and " + outFile);
        }
Пример #12
0
        public void RuleML08SaveRuleBase()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML08DatalogAdapter(ruleFilesFolder + "discount.ruleml", FileAccess.Read));
            ie.SaveRuleBase(new RuleML08DatalogAdapter(outFile, FileAccess.Write));

            // for now, let's only reload the rulebase
            ie.LoadRuleBase(new RuleML08DatalogAdapter(outFile, FileAccess.Read));
        }
Пример #13
0
        public void RuleML08LoadFacts()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML08DatalogAdapter(ruleFilesFolder + "discount.ruleml", FileAccess.Read));

            ie.Process();
            Assert.AreEqual(6, ie.FactsCount, "Initial process");

            ie.LoadFacts(new RuleML086DatalogAdapter(ruleFilesFolder + "facts.ruleml", FileAccess.Read));
            Assert.AreEqual(8, ie.FactsCount, "Loaded facts");

            ie.Process();
            Assert.AreEqual(10, ie.FactsCount, "Subsequent process");
        }
Пример #14
0
        public void RuleML086SaveRuleBase()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML086DatalogAdapter(ruleFilesFolder + "endlessloop.ruleml", FileAccess.Read));
            ie.SaveRuleBase(new RuleML086DatalogAdapter(outFile, FileAccess.Write));

            ie.LoadRuleBase(new RuleML086DatalogAdapter(outFile, FileAccess.Read));

            Assert.IsTrue(AreSameXml(ruleFilesFolder + "endlessloop.ruleml", outFile), "Same XML");
        }
Пример #15
0
        public void RuleML086SaveFacts()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML086NafDatalogAdapter(ruleFilesFolder + "test-0_86.ruleml", FileAccess.Read));

            ie.SaveFacts(new RuleML086NafDatalogAdapter(outFile, FileAccess.Write));

            IInferenceEngine ie2 = new IEImpl();
            ie2.LoadRuleBase(new RuleML086NafDatalogAdapter(outFile, FileAccess.Read));
            Assert.AreEqual(ie.Label, ie2.Label, "Label");
            Assert.AreEqual(ie.FactsCount, ie2.FactsCount, "FactsCount");
        }
Пример #16
0
        public void PerfomativeLoadProcessing()
        {
            string inFile = ruleFilesFolder + "discount-0_9.ruleml";

            IInferenceEngine ie = new IEImpl();

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(inFile, FileAccess.Read), false);
            Assert.AreEqual(0, ie.FactsCount, "No fact should have been loaded");

            ie.Process(RuleTypes.ConnectivesOnly);
            Assert.AreEqual(0, ie.FactsCount, "No fact should have been deducted");

            ie.Process(RuleTypes.PerformativesOnly);
            Assert.AreEqual(3, ie.FactsCount, "Only fact assertion should have happened");

            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(inFile, FileAccess.Read), false);
            Assert.AreEqual(0, ie.FactsCount, "Memory should be empty at this point");

            ie.Process(RuleTypes.All);
            Assert.AreEqual(6, ie.FactsCount, "Fact assertion and deduction should have happened");
        }
        public void RuleBaseLabel()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(NewTestAdapter());

            Assert.IsTrue(ie.Label.StartsWith("NxBRE"));
        }
Пример #18
0
 public void RunEngine()
 {
     IInferenceEngine ie = new IEImpl();
     ie.LoadRuleBase(m_CompositeAdapter);
     Trace.WriteLine("Facts Before RunEngine:" + ie.FactsCount);
     ie.Process();
     Trace.WriteLine("Facts After RunEngine:" + ie.FactsCount);
 }
Пример #19
0
 public void ErrorProcessBO()
 {
     IInferenceEngine ie = new IEImpl();
     ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "test-0_9.ruleml", FileAccess.Read));
     ie.Process(new Hashtable());
 }
Пример #20
0
 public void EndlessLoopException()
 {
     ie = new IEImpl();
     ie.LoadRuleBase(new RuleML086DatalogAdapter(ruleFilesFolder + "endlessloop.ruleml", FileAccess.Read));
     // Processing should reach iteration limit and throw a BREexception
     ie.Process();
     Assert.Fail("Should never reach me!");
 }
        private void PerformSaveTest(string ruleFile, SaveFormatAttributes sfa)
        {
            string inFile = ruleFilesFolder + ruleFile;

            string outFile = outFilesFolder + "outtest.ruleml";
            FileInfo fi = new FileInfo(outFile);
            if (fi.Exists) fi.Delete();

            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML09NafDatalogAdapter(inFile, FileAccess.Read));
            ie.SaveRuleBase(new RuleML09NafDatalogAdapter(outFile, FileAccess.Write, sfa));

            Assert.IsTrue(TestAdapter.AreSameXml(inFile, outFile), "Same XML file lengths");
        }
Пример #22
0
        private void RunTestBoundFormulas(IBinder binder, IRuleBaseAdapter rba)
        {
            // regression test for RFE 1504353 and BUG 1815223
            IInferenceEngine ie = new IEImpl(binder);
            ie.LoadRuleBase(rba);

            Assert.IsTrue(ie.Assert(new Fact("operandA", new Individual(23))));
            Assert.IsTrue(ie.Assert(new Fact("operandB", new Individual(7))));

            ie.Process();

            Assert.AreEqual(5, ie.FactsCount);
            Assert.IsTrue(ie.FactExists(new Fact("resultA-B", new Individual(16))), "resultA-B is wrong");
            Assert.IsTrue(ie.FactExists(new Fact("resultB-A", new Individual(-16))), "resultB-A is wrong");
            Assert.IsTrue(ie.FactExists(new Fact("resultBoth", new Individual(16), new Individual(-16))), "resultBoth is wrong");
        }