Esempio n. 1
0
		public void PerformProcess(IBinder binder)	{
			// generate dummy business objects
			Hashtable businessObjects = DummyData.GetInstance().GetBusinessObjects(nbDecaCustomers);


			// instantiate an inference engine, bind my data and process the rules
			IEImpl ie = new IEImpl(binder);
			ie.LogHandlers += new DispatchLog(HandleLogEvent);
			ie.LoadRuleBase(new RuleML08DatalogAdapter(ruleBaseFile, System.IO.FileAccess.Read));		
			ie.Process(businessObjects);
			
			// processing is done, let's analyze the results
			QueryResultSet 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");
		}
Esempio n. 2
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!");
        }
Esempio n. 3
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);

            QueryResultSet 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");
        }
Esempio n. 4
0
		public RatingEngine(int verbosityLevel, int nbDodecaCalls, string ruleBaseFile) {
			this.nbDodecaCalls = nbDodecaCalls;

			ie = new IEImpl(CSharpBinderFactory.LoadFromFile("org.nxbre.examples.TelcoRatingBinder",
			                       																	ruleBaseFile + ".ccb"));

			if (verbosityLevel >= 2) ie.LogHandlers += new DispatchLog(HandleLogEvent);
			if (verbosityLevel >= 1) ie.NewFactHandler += new NewFactEvent(HandleNewFactEvent);
			
			ie.LoadRuleBase(new RuleML086DatalogAdapter(ruleBaseFile, System.IO.FileAccess.Read));

		}
        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;
        }
Esempio n. 6
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");
        }
Esempio n. 7
0
 public void RuleML086LoadFactsWithNoRuleBase()
 {
     IInferenceEngine ie = new IEImpl();
     ie.LoadFacts(new RuleML086DatalogAdapter(ruleFilesFolder + "facts.ruleml", FileAccess.Read));
     Assert.Fail("Should never reach me!");
 }
Esempio n. 8
0
 public void ErrorProcessBO()
 {
     IInferenceEngine ie = new IEImpl();
     ie.LoadRuleBase(new RuleML09NafDatalogAdapter(ruleFilesFolder + "test-0_9.ruleml", FileAccess.Read));
     ie.Process(new Hashtable());
 }
Esempio n. 9
0
        private void RunTestBoundFormulas(IBinder binder, IRuleBaseAdapter rba)
        {
            // regression test for RFE 1504353
            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(4, 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");
        }
Esempio n. 10
0
 public void BinderEventsFCE()
 {
     IInferenceEngine ie = new IEImpl(new FlowEngineBinder(ruleFilesFolder + "events-test.ruleml.xbre",
                        											BindingTypes.BeforeAfter));
       RunTestBinderEvents(ie);
 }
Esempio n. 11
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");
        }
Esempio n. 12
0
 public void RuleML086SaveFactsWithNoRuleBase()
 {
     IInferenceEngine ie = new IEImpl();
     ie.SaveFacts(new RuleML086DatalogAdapter(Parameter.GetString("unittest.outputfolder") + "/_outfacts.ruleml",
                                              FileAccess.Write));
     Assert.Fail("Should never reach me!");
 }
Esempio n. 13
0
 public void BinderEventsCCB()
 {
     // use this binder load method to overcome loading problems because
     // the ccb file is not in the same folder as NxBRE.dll
     using (StreamReader sr = File.OpenText(ruleFilesFolder + "events-test.ruleml.ccb")) {
       IInferenceEngine ie = new IEImpl(CSharpBinderFactory.LoadFromString("org.nxbre.test.ie.EventTestBinder", sr.ReadToEnd()));
       RunTestBinderEvents(ie);
     }
 }
Esempio n. 14
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");
        }
        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.AreXmlOfSameLength(inFile, outFile), "Same XML file lengths");
        }
Esempio n. 16
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));

            // for now, compare only the size
            Assert.IsTrue(AreXmlOfSameLength(inFile, outFile), "Same XML size");
        }
        public void RuleBaseLabel()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(NewTestAdapter());

            Assert.IsTrue(ie.Label.StartsWith("NxBRE"));
        }
Esempio n. 18
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 reload the rulebase
            ie.LoadRuleBase(new RuleML08DatalogAdapter(outFile, FileAccess.Read));
        }
Esempio n. 19
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 RuleML08DatalogAdapter(ruleFilesFolder + "facts.ruleml", FileAccess.Read));
            Assert.AreEqual(8, ie.FactsCount, "Loaded facts");

            ie.Process();
            Assert.AreEqual(10, ie.FactsCount, "Subsequent process");
        }
Esempio n. 20
0
        public void RuleML086SaveRuleBase()
        {
            IInferenceEngine ie = new IEImpl();
            ie.LoadRuleBase(new RuleML086DatalogAdapter(ruleFilesFolder + "endlessloop.ruleml", FileAccess.Read));
            ie.SaveRuleBase(new RuleML086DatalogAdapter(outFile, FileAccess.Write));

            // for now, compare only the size
            Assert.IsTrue(AreXmlOfSameLength(ruleFilesFolder + "endlessloop.ruleml", outFile), "Same XML size");
        }
        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");
        }
Esempio n. 22
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!");
 }
		protected override void NewIEImpl(IBinder bob) {
			if (bob != null) ie = new IEImpl(bob, ThreadingModelTypes.Multi);
			else ie = new IEImpl(ThreadingModelTypes.Multi);
		}
Esempio n. 24
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");
        }