示例#1
0
 public void testFiveRules()
 {
     int objCount = 25000;
     Random ran = new Random();
     ArrayList facts = new ArrayList();
     // loop and create account and transaction objects
     for (int idx = 0; idx < objCount; idx++)
     {
         Account4 acc = new Account4();
         acc.AccountId = "acc" + idx;
         acc.AccountType = Convert.ToString(ran.Next(100000));
         acc.First = Convert.ToString(ran.Next(100000));
         acc.Last = Convert.ToString(ran.Next(100000));
         acc.Middle = Convert.ToString(ran.Next(100000));
         acc.Status = Convert.ToString(ran.Next(100000));
         acc.Title = Convert.ToString(ran.Next(100000));
         acc.Username = Convert.ToString(ran.Next(100000));
         acc.CountryCode = "US";
         acc.Cash = 1298.00;
         facts.Add(acc);
         Transaction tx = new Transaction();
         tx.AccountId = "acc" + idx;
         tx.Total = 1200000;
         facts.Add(tx);
     }
     Console.WriteLine("created " + objCount + " Accounts and Transactions");
     Rete engine = new Rete();
     engine.declareObject(typeof (Account4));
     engine.declareObject(typeof (Transaction));
     Console.WriteLine("delcare the objects");
     engine.close();
 }
示例#2
0
        public void ObjectBindingTest1()
        {
            long ts = DateTime.Now.Ticks;
            int fired = 0;
            int activations = 0;
            using (TextWriter writer = Console.Out)
            {
                Rete engine = new Rete();
                engine.Watch = WatchType.WATCH_ALL;
                engine.addPrintWriter("Console", writer);
                engine.declareObject(typeof (Account), "Account");
                engine.loadRuleset(getRule1());
                engine.assertObject(GetAcct0(), "Account", false, false);
                activations = engine.CurrentFocus.ActivationCount;
                fired = engine.fire();
                engine.printWorkingMemory(true, false);

            double endTime = DateTime.Now.Ticks - ts;
            Console.WriteLine(String.Format("ObjectBindingTest1 completed in {0} seconds.", (endTime/10000000).ToString("0.000000")));
                writer.Flush();
                writer.Close();
                engine.close();
            }

            Assert.IsTrue(fired == 1);
            Assert.IsTrue(activations == 1);
            //AppDomain.Unload(AppDomain.CurrentDomain);
        }
示例#3
0
        public void BasicObjectBindingTest1()
        {
            long ts = DateTime.Now.Ticks;
            int fired = 0;
            int activations = 0;
            Basic basic = new Basic("one", 1);
            using (TextWriter writer = Console.Out)
            {
                Rete engine = new Rete();
                engine.Watch = WatchType.WATCH_ALL;
                engine.addPrintWriter("Console", writer);
                engine.declareObject(typeof (Basic), "Basic");
                engine.loadRuleset(getRule3());

                foreach (Defrule rule in  engine.CurrentFocus.AllRules)
                {
                    Console.WriteLine(rule.toPPString());
                }

                engine.assertObject(basic, "Basic", false, false);
                activations = engine.CurrentFocus.ActivationCount;
                fired = engine.fire();
                engine.printWorkingMemory(true, false);
            double endTime = DateTime.Now.Ticks - ts;
            Console.WriteLine(String.Format("BasicObjectBindingTest1 completed in {0} seconds.", (endTime/10000000).ToString("0.000000")));
                writer.Flush();
                writer.Close();
                engine.close();
            }

            Assert.IsTrue(fired == 1);
            Assert.IsTrue(activations == 1);
            //AppDomain.Unload(AppDomain.CurrentDomain);
        }
示例#4
0
 public void testDeclareObject()
 {
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     engine.declareObject(typeof (Account));
     ICollection<object> templ = engine.CurrentFocus.Templates;
     Assert.AreEqual(2, templ.Count);
     engine.close();
 }
示例#5
0
 public void testInitModule()
 {
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     Assert.IsNotNull(engine.CurrentFocus);
     Assert.IsNotNull(engine.CurrentFocus.ModuleName);
     Assert.AreEqual(engine.CurrentFocus.ModuleName, Constants.MAIN_MODULE);
     Console.WriteLine("default module is " + engine.CurrentFocus.ModuleName);
     engine.close();
 }
示例#6
0
        //[TestMethod]
        public void mainTest()
        {
            String rulefile = getRoot("random_5_w_50Kdata.clp");
            //String datafile = getRoot("test.clp");
            // in case it's run within OptimizeIt and we want to keep the test running
            //bool keepopen = false;

            Console.WriteLine("Using file " + rulefile);

            MemoryBenchmark mb = new MemoryBenchmark();
            ArrayList facts = new ArrayList(50000);
            // Runtime rt = Runtime.getRuntime();

            long total1 = GC.GetTotalMemory(true);
            //long free1 = rt.freeMemory();
            //long used1 = total1 - free1;
            //int count = 100000;
            Console.WriteLine("Used memory before creating engine " + total1 + " bytes " +
                              (total1/1024) + " Kb");
            Rete engine = new Rete();

            long total2 = GC.GetTotalMemory(true);
            //long free2 = rt.freeMemory();
            //long used2 = total2 - free2;
            Console.WriteLine("Used memory after creating engine " + total2 + " bytes " +
                              (total2/1024) + " Kb");

            try
            {
                StreamReader freader = new StreamReader(rulefile);
                CLIPSParser parser = new CLIPSParser(engine, freader);
                long start = DateTime.Now.Ticks;
                mb.parse(engine, parser, facts);
                long end = DateTime.Now.Ticks;
                // parser.close();
                // rt.gc();

                long total3 = GC.GetTotalMemory(true);
                //long free3 = rt.freeMemory();
                //long used3 = total3 - free3;
                Console.WriteLine("Used memory after loading rules, data and asserting facts " +
                                  total3 + " bytes " + (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
                Console.WriteLine("elapsed time to parse and assert the data " +
                                  (end - start) + " ms");
                engine.printWorkingMemory(true, false);

                engine.close();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#7
0
 public void testLoadJoinSample13()
 {
     Rete engine = new Rete();
     engine.loadRuleset(getRoot("join_sample13.clp"));
     ICollection<object> rules = engine.CurrentFocus.AllRules;
     int count = rules.Count;
     IEnumerator itr = rules.GetEnumerator();
     while (itr.MoveNext())
     {
         Defrule r = (Defrule) itr.Current;
         Console.WriteLine(r.toPPString());
     }
     Assert.AreEqual(3, count);
     engine.close();
 }
示例#8
0
 public void testNodeId()
 {
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     Assert.AreEqual(3, engine.peakNextNodeId());
     Assert.AreEqual(3, engine.peakNextNodeId());
     Assert.AreEqual(3, engine.peakNextNodeId());
     Console.WriteLine("we call peakNextNodeId() 3 times and it should return 3");
     Assert.AreEqual(3, engine.nextNodeId());
     Assert.AreEqual(4, engine.nextNodeId());
     Assert.AreEqual(5, engine.nextNodeId());
     int id = engine.nextNodeId();
     Assert.AreEqual(6, id);
     Console.WriteLine("if the test passes, the last id should be 6. it is " + id);
     engine.close();
 }
示例#9
0
 public void manners64()
 {
     long ts = DateTime.Now.Ticks;
     using (TextWriter writer = Console.Out)
     {
         Rete engine = new Rete();
         engine.addPrintWriter("Console", writer);
         engine.loadRuleset(getRoot("manners64guests.clp"));
         engine.printWorkingMemory(false, false);
         writer.Flush();
         writer.Close();
         //engine.MessageRouter.ShutDown();
         engine.close();
     }
     double endTime = DateTime.Now.Ticks - ts;
     Console.WriteLine(String.Format("Manners 64 completed in {0} seconds.", (endTime/10000000).ToString("0.000000")));
     //AppDomain.Unload(AppDomain.CurrentDomain);
 }
示例#10
0
 public static double manners64()
 {
     long ts = DateTime.Now.Ticks;
     long totTime = 0;
     using (TextWriter writer = System.Console.Out)
     {
         Rete engine = new Rete();
         //engine.CurrentFocus.Lazy = true;
         engine.addPrintWriter("Console", writer);
         engine.loadRuleset(getRoot("manners64guests.clp"));
         engine.printWorkingMemory(false, false);
         totTime = DateTime.Now.Ticks - ts;
         writer.Flush();
         writer.Close();
         engine.close();
     }
     return totTime;
 }
示例#11
0
 public void testDeclareClass()
 {
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     engine.declareObject(typeof (Account));
     int count = engine.Defclasses.Count;
     Assert.AreEqual(1, count);
     Console.WriteLine("number of Defclass is " + count);
     ICollection<object> templates = engine.CurrentFocus.Templates;
     Assert.AreEqual(2, templates.Count);
     IEnumerator itr = templates.GetEnumerator();
     while (itr.MoveNext())
     {
         Deftemplate dtemp = (Deftemplate) itr.Current;
         Console.WriteLine(dtemp.toPPString());
     }
     Console.WriteLine("--------------------------------");
     engine.close();
 }
示例#12
0
        private void RunRules(string dir, string clpFile)
        {
            var outFile = Path.Combine(dir, String.Concat(Path.GetFileNameWithoutExtension(clpFile), ".out"));
            var refFile = Path.Combine(dir, String.Concat(Path.GetFileNameWithoutExtension(clpFile), ".ref"));

            using (TextWriter writer = new StreamWriter(outFile))
            {
                Rete engine = new Rete();
                engine.addPrintWriter("File", writer);
                engine.loadRuleset(clpFile);
                engine.printWorkingMemory(false, false);
                engine.close();
                writer.Flush();
                writer.Close();
            }

            var outTxt = File.ReadAllText(outFile);
            var refTxt = File.ReadAllText(refFile);
        }
示例#13
0
        public static void main(String[] args)
        {
            String rulefile = "./benchmark_files/4K_rules_50Kdata_sequential.clp";
            //String datafile = "./benchmark_files/test.clp";
            //bool keepopen = false;
            if (args != null && args.Length > 0)
            {
                rulefile = args[0];
            }
            //if (args.Length >= 2)
            //{
            //    if (args[1].Equals("true", StringComparison.InvariantCultureIgnoreCase))
            //    {
            //        keepopen = true;
            //    }
            //}
            Console.WriteLine("Using file " + rulefile);

            MemoryBenchmark2 mb = new MemoryBenchmark2();
            long begin = DateTime.Now.Ticks;
            long totalET = 0;
            long parseET = 0;
            ArrayList facts = new ArrayList(50000);
            // Runtime rt = Runtime.getRuntime();
            long total1 = GC.GetTotalMemory(true);
            //long free1 = rt.freeMemory();
            //long used1 = total1 - free1;
            int loopcount = 5;
            Console.WriteLine("Used memory before creating engine " + total1 + " bytes " +
                              (total1/1024) + " Kb");
            for (int loop = 0; loop < loopcount; loop++)
            {
                Console.WriteLine(" ---------------------------------- ");
                Rete engine = new Rete();
                facts.Clear();

                long total2 = GC.GetTotalMemory(true);
                //long free2 = rt.freeMemory();
                //long used2 = total2 - free2;
                Console.WriteLine("Used memory after creating engine " + total2 + " bytes " +
                                  (total2/1024) + " Kb");

                try
                {
                    StreamReader freader = new StreamReader(rulefile);
                    CLIPSParser parser = new CLIPSParser(engine, freader);
                    long start = DateTime.Now.Ticks;
                    mb.parse(engine, parser, facts);
                    long end = DateTime.Now.Ticks;
                    long el = end - start;
                    parser.close();
                    //rt.gc();
                    parseET += el;

                    long total3 = GC.GetTotalMemory(true);
                    //long free3 = rt.freeMemory();
                    //long used3 = total3 - free3;
                    Console.WriteLine("Used memory after loading rules, and parsing data " +
                                      (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
                    Console.WriteLine("elapsed time to parse the rules and data " +
                                      el + " ms");

                    Console.WriteLine("Number of rules: " +
                                      engine.CurrentFocus.RuleCount);
                    // now assert the facts
                    IEnumerator itr = facts.GetEnumerator();
                    long start2 = DateTime.Now.Ticks;
                    while (itr.MoveNext())
                    {
                        Deffact fct = (Deffact) itr.Current;
                        engine.assertFact(fct);
                    }
                    int actCount = engine.ActivationList.size();
                    long end2 = DateTime.Now.Ticks;
                    long et2 = end2 - start2;
                    totalET += et2;
                    // now fire the rules
                    long start3 = 0;
                    long end3 = 0;
                    int fired = 0;
                    try
                    {
                        start3 = DateTime.Now.Ticks;
                        fired = engine.fire();
                        end3 = DateTime.Now.Ticks;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    facts.Clear();

                    long total4 = GC.GetTotalMemory(true);
                    //long free4 = rt.freeMemory();
                    //long used4 = total4 - free4;

                    Console.WriteLine("");
                    Console.WriteLine("Number of facts - " + engine.DeffactCount);
                    Console.WriteLine("Time to assert facts " + et2 + " ms");
                    Console.WriteLine("Used memory after assert " +
                                      (total4/1024) + " Kb " + (total4/1024/1024) + " Mb");
                    engine.printWorkingMemory(true, false);
                    Console.WriteLine("number of activations " + actCount);
                    Console.WriteLine("rules fired " + fired);
                    Console.WriteLine("time to fire rules " + (end3 - start3) + " ms");
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
                engine.close();
                engine = null;
                //rt.gc();engine.close();
            }
            long finished = DateTime.Now.Ticks;
            Console.WriteLine("average parse ET - " + parseET/loopcount + " ms");
            Console.WriteLine("average assert ET - " + totalET/loopcount + " ms");
            Console.WriteLine("total run time " + (finished - begin) + " ms");
        }
示例#14
0
        public void testPropogateNoMatch()
        {
            Console.WriteLine("testPropogateNoMatch");
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin nj = new NotJoin(engine.nextNodeId());
            HashedEqBNode bn2 = new HashedEqBNode(engine.nextNodeId());
            Assert.IsNotNull(nj);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            Binding[] binds2 = new Binding[1];
            Binding b2 = new Binding();
            b2.LeftIndex = (1);
            b2.IsObjectVar = (false);
            b2.LeftRow = (0);
            b2.RightIndex = (1);
            b2.VarName = ("var2");
            binds2[0] = b2;

            // set the binding
            nj.Bindings = (binds);

            bn2.Bindings = (binds2);

            // now add the second Not to the first
            try
            {
                nj.addSuccessorNode(bn2, engine, engine.WorkingMemory);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }

            int count = 10;
            ArrayList data = new ArrayList();
            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random" + idx);
                bean.Attr2 = (101 + idx);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018 + idx;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }

            IEnumerator itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    nj.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory);
                    nj.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj);
            Assert.AreEqual(count, rbmem.Count);

            IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(nj);
            Assert.AreEqual(count, lbmem.Count);

            // now check the BetaMemory has matches
            Console.WriteLine(nj.toPPString());
            IEnumerator mitr = lbmem.Values.GetEnumerator();
            while (mitr.MoveNext())
            {
                IBetaMemory btm = (IBetaMemory) mitr.Current;
                Assert.AreEqual(0, btm.matchCount());
                Console.WriteLine("match count=" + btm.matchCount() +
                                  " - " + btm.toPPString());
            }

            IGenericMap<Object, Object> lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(count, lbmem2.Count);
            Console.WriteLine(bn2.toPPString());
            IEnumerator mitr2 = lbmem2.Values.GetEnumerator();
            engine.close();
            // TODO need to update the test to check the match count
            // by getting the right memory
        }
示例#15
0
        public void testPropogateChange()
        {
            Console.WriteLine("testPropogateChange");
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin nj = new NotJoin(engine.nextNodeId());
            HashedEqBNode bn2 = new HashedEqBNode(engine.nextNodeId());
            Assert.IsNotNull(nj);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            Binding[] binds2 = new Binding[1];
            Binding b2 = new Binding();
            b2.LeftIndex = (1);
            b2.IsObjectVar = (false);
            b2.LeftRow = (0);
            b2.RightIndex = (1);
            b2.VarName = ("var2");
            binds2[0] = b2;

            // set the binding
            nj.Bindings = (binds);

            bn2.Bindings = (binds2);

            // now add the second Not to the first
            try
            {
                nj.addSuccessorNode(bn2, engine, engine.WorkingMemory);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }

            int count = 2;
            ArrayList data = new ArrayList();
            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random");
                bean.Attr2 = (101 + idx);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018 + idx;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }

            IEnumerator itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    nj.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(nj);
            Assert.AreEqual(count, lbmem.Count);

            IGenericMap<Object, Object> lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(2, lbmem2.Count);

            itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    nj.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(nj);
            Assert.AreEqual(count, rbmem.Count);

            // once the facts are asserted to the right, there should be no
            // facts in successor. this makes sure that assertRight correctly
            // results in a retract.
            lbmem2 = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn2);
            Assert.AreEqual(0, lbmem2.Count);
            engine.close();
        }
示例#16
0
        public void testAssertAndRetract()
        {
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin bn = new NotJoin(engine.nextNodeId());
            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            // set the binding
            bn.Bindings = (binds);

            int count = 10;
            ArrayList data = new ArrayList();
            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random");
                bean.Attr2 = (101);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }

            IEnumerator itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    bn.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory);
                    bn.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<IFact, IFact> rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);
            Assert.AreEqual(count, rbmem.Count);

            IGenericMap<Object, Object> lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn);
            Assert.AreEqual(count, lbmem.Count);

            int retract = 5;
            try
            {
                for (int idx = 0; idx < retract; idx++)
                {
                    IFact f2 = (IFact) data[idx];
                    bn.retractRight(f2, engine, engine.WorkingMemory);
                }
            }
            catch (RetractException e)
            {
                Console.WriteLine(e.Message);
            }

            rbmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);
            Assert.AreEqual(retract, rbmem.Count);

            lbmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn);
            Assert.AreEqual(count, lbmem.Count);

            // now check the BetaMemory has matches
            Console.WriteLine(bn.toPPString());
            IEnumerator mitr = lbmem.Values.GetEnumerator();
            while (mitr.MoveNext())
            {
                IBetaMemory btm = (IBetaMemory) mitr.Current;
                Console.WriteLine("match count=" + btm.matchCount() +
                                  " - " + btm.toPPString());
            }
            engine.close();
        }
示例#17
0
        public void testCreateNode2()
        {
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin bn = new NotJoin(engine.nextNodeId());
            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            // set the binding
            bn.Bindings = (binds);
            engine.close();
        }
示例#18
0
 public void testCreateNode()
 {
     Rete engine = new Rete();
     NotJoin bn = new NotJoin(engine.nextNodeId());
     Assert.IsNotNull(bn);
     engine.close();
 }
示例#19
0
        public void MainTest()
        {
            String rulefile = getRoot("account_5.clp");

            ArrayList objects = new ArrayList();
            //AssertWRules awr = new AssertWRules();

            // Runtime rt = Runtime.getRuntime();
            long total1 = GC.GetTotalMemory(true);
            //long free1 = rt.freeMemory();
            //long used1 = total1 - free1;
            int count = 5000;
            Console.WriteLine("loading file " + rulefile);
            Console.WriteLine("Used memory before creating objects " + total1 + " bytes " +
                              (total1/1024) + " Kb");
            for (int idx = 0; idx < count; idx++)
            {
                Account acc = new Account();
                acc.AccountId = "acc" + idx;
                // acc.setAccountId("acc" + ran.Next(4));
                acc.AccountType = "standard";
                acc.First = Convert.ToString(ran.Next(100000));
                acc.Last = Convert.ToString(ran.Next(100000));
                acc.Middle = Convert.ToString(ran.Next(100000));
                acc.OfficeCode = Convert.ToString(ran.Next(100000));
                acc.RegionCode = Convert.ToString(ran.Next(100000));
                acc.Status = "active";
                acc.Title = "mr";
                acc.Username = Convert.ToString(ran.Next(100000));
                acc.AreaCode = Convert.ToString(ran.Next(999));
                acc.Exchange = Convert.ToString(ran.Next(999));
                acc.Number = Convert.ToString(ran.Next(999));
                acc.Ext = Convert.ToString(ran.Next(9999));
                objects.Add(acc);
            }
            long total2 = GC.GetTotalMemory(true);
            //long free2 = rt.freeMemory();
            //long used2 = total2 - free2;
            Console.WriteLine("Used memory after creating objects " + total2 + " bytes " +
                              (total2/1024) + " Kb " + (total2/1024/1024) + " Mb");
            int loop = 5;
            long ETTotal = 0;
            for (int idx = 0; idx < loop; idx++)
            {
                Rete engine = new Rete();
                engine.declareObject(typeof (Account), "Account");

                try
                {
                    StreamReader freader = new StreamReader(rulefile);
                    CLIPSParser parser = new CLIPSParser(engine, freader);
                    //Object item = null;
                    ArrayList list = new ArrayList();
                    long start = DateTime.Now.Ticks;
                    parse(engine, parser, list);
                    long end = DateTime.Now.Ticks;
                    long el = end - start;
                    // parser.close();
                    //rt.gc();
                    Console.WriteLine("time to parse rules " + el + " ms");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                IEnumerator itr = objects.GetEnumerator();
                long start2 = DateTime.Now.Ticks;
                try
                {
                    while (itr.MoveNext())
                    {
                        engine.assertObject(itr.Current, "Account", false, false);
                    }
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
                long end2 = DateTime.Now.Ticks;
                long start3 = DateTime.Now.Ticks;
                int fired = 0;
                try
                {
                    fired = engine.fire();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                long end3 = DateTime.Now.Ticks;
                long total3 = GC.GetTotalMemory(true);
                //long free3 = rt.freeMemory();
                //long used3 = total3 - free3;
                Console.WriteLine("Number of rules: " +
                                  engine.CurrentFocus.RuleCount);
                Console.WriteLine("rules fired " + fired);
                Console.WriteLine("Used memory after asserting objects " + total3 + " bytes " +
                                  (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
                Console.WriteLine("number of facts " + engine.ObjectCount);
                Console.WriteLine("memory used by facts " + (total3 - total3)/1024/1024 + " Mb");
                Console.WriteLine("elapsed time to assert " + (end2 - start2) + " ms");
                Console.WriteLine("elapsed time to fire " + (end3 - start3) + " ms");
                ETTotal += (end2 - start2);
                engine.printWorkingMemory(true, false);
                //engine.close();
                engine.clearAll();
                engine.close();
                //rt.gc();
            }
            Console.WriteLine("Average ET to assert " + (ETTotal/loop) + " ms");
        }
示例#20
0
        public void testAssertLeftOne()
        {
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin bn = new NotJoin(engine.nextNodeId());
            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            // set the binding
            bn.Bindings = (binds);

            TestBean2 bean = new TestBean2();
            bean.Attr1 = ("random1");
            bean.Attr2 = (101);
            short s = 10001;
            bean.Attr3 = (s);
            long l = 10101018;
            bean.Attr4 = (l);
            bean.Attr5 = (1010101);
            bean.Attr6 = (1001.1001);
            IFact f1 = dtemp.createFact(bean, dc, engine.nextFactId());
            try
            {
                bn.assertLeft(new Index(new IFact[] {f1}), engine, engine.WorkingMemory);
                IGenericMap<Object, Object> bmem = (IGenericMap<Object, Object>) engine.WorkingMemory.getBetaLeftMemory(bn);
                Assert.AreEqual(1, bmem.Count);
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }
            engine.close();
        }
示例#21
0
        public void mainTest()
        {
            String rulefile = getRoot("share_5nodes.clp");
            //bool keepopen = false;
            int fcount = 50000;

            Console.WriteLine("Using file " + rulefile);

            RulesetBenchmark2 mb = new RulesetBenchmark2();
            long begin = DateTime.Now.Ticks;
            long totalET = 0;
            long parseET = 0;
            ArrayList facts = new ArrayList(50000);
            // Runtime rt = Runtime.getRuntime();
            long total1 = GC.GetTotalMemory(true);
            //long free1 = rt.freeMemory();
            //long used1 = total1 - free1;
            int loopcount = 5;
            Console.WriteLine("Used memory before creating engine " + total1 + " bytes " +
                              (total1/1024) + " Kb");
            for (int loop = 0; loop < loopcount; loop++)
            {
                Console.WriteLine(" ---------------------------------- ");
                Rete engine = new Rete();
                facts.Clear();
                // declare the objects
                engine.declareObject(typeof (Account), "account");
                engine.declareObject(typeof (Transaction), "transaction");

                long total2 = GC.GetTotalMemory(true);
                //long free2 = rt.freeMemory();
                //long used2 = total2 - free2;
                Console.WriteLine("Used memory after creating engine " + total2 + " bytes " +
                                  (total2/1024) + " Kb");

                try
                {
                    StreamReader freader = new StreamReader(rulefile);
                    CLIPSParser parser = new CLIPSParser(engine, freader);
                    long start = DateTime.Now.Ticks;
                    mb.parse(engine, parser, facts);
                    long end = DateTime.Now.Ticks;
                    long el = end - start;
                    parser.close();
                    //rt.gc();
                    parseET += el;

                    long total3 = GC.GetTotalMemory(true);
                    //long free3 = rt.freeMemory();
                    //long used3 = total3 - free3;
                    Console.WriteLine("Used memory after loading rules, and parsing data " +
                                      (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
                    Console.WriteLine("elapsed time to parse the rules and data " +
                                      el + " ms");

                    Console.WriteLine("Number of rules: " +
                                      engine.CurrentFocus.RuleCount);
                    // now create the facts
                    Account acc = new Account();
                    acc.AccountType = "standard";
                    facts.Add(acc);
                    for (int i = 0; i < fcount; i++)
                    {
                        Transaction tx = new Transaction();
                        tx.AccountId = "acc" + i;
                        tx.Exchange = "NYSE";
                        tx.Issuer = "AAA";
                        tx.Shares = 100.00;
                        tx.SecurityType = "stocks";
                        tx.SubIndustryID = 25201010;
                        facts.Add(tx);
                    }
                    IEnumerator itr = facts.GetEnumerator();
                    long start2 = DateTime.Now.Ticks;
                    while (itr.MoveNext())
                    {
                        Object d = itr.Current;
                        if (d is Account)
                        {
                            engine.assertObject(d, "account", false, true);
                        }
                        else
                        {
                            engine.assertObject(d, "transaction", false, false);
                        }
                    }
                    int actCount = engine.ActivationList.size();
                    long end2 = DateTime.Now.Ticks;
                    long et2 = end2 - start2;
                    totalET += et2;
                    // now fire the rules
                    long start3 = 0;
                    long end3 = 0;
                    int fired = 0;
                    try
                    {
                        start3 = DateTime.Now.Ticks;
                        fired = engine.fire();
                        end3 = DateTime.Now.Ticks;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    facts.Clear();

                    long total4 = GC.GetTotalMemory(true);
                    //long free4 = rt.freeMemory();
                    //long used4 = total4 - free4;

                    Console.WriteLine("");
                    Console.WriteLine("Number of facts - " + engine.ObjectCount);
                    Console.WriteLine("Time to assert facts " + et2 + " ms");
                    Console.WriteLine("Used memory after assert " +
                                      (total4/1024) + " Kb " + (total4/1024/1024) + " Mb");
                    engine.printWorkingMemory(true, false);
                    Console.WriteLine("number of activations " + actCount);
                    Console.WriteLine("rules fired " + fired);
                    Console.WriteLine("time to fire rules " + (end3 - start3) + " ms");
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
                engine.close();
                engine = null;
                //rt.gc();
            }
            long finished = DateTime.Now.Ticks;
            Console.WriteLine("average parse ET - " + parseET/loopcount + " ms");
            Console.WriteLine("average assert ET - " + totalET/loopcount + " ms");
            Console.WriteLine("total run time " + (finished - begin) + " ms");
        }
示例#22
0
 public void testDeclareClassInheritance()
 {
     Console.WriteLine("\ntestDeclareClassInheritance");
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     engine.declareObject(typeof (Account));
     engine.declareObject(typeof (BackupAccount));
     int count = engine.Defclasses.Count;
     Assert.AreEqual(2, count);
     Console.WriteLine("number of Defclass is " + count);
     ITemplate acctemp = engine.CurrentFocus.getTemplate(typeof (Account).FullName);
     ITemplate bkacc = engine.CurrentFocus.getTemplate(typeof (BackupAccount).FullName);
     Slot[] accslots = acctemp.AllSlots;
     Slot[] bkslots = bkacc.AllSlots;
     for (int idx = 0; idx < accslots.Length; idx++)
     {
         Assert.IsTrue(accslots[idx].Name.Equals(bkslots[idx].Name));
         Console.WriteLine(accslots[idx].Name + "=" + bkslots[idx].Name);
     }
     engine.close();
 }
示例#23
0
        public void testRepeatedAssert()
        {
            Console.WriteLine("start testRepatedAssert");
            Rete engine = new Rete();
            engine.declareObject(typeof (Account));
            engine.declareObject(typeof (TestBean3));
            Assert.IsNotNull(engine);
            // create instance of Account
            Account acc1 = new Account();
            acc1.AccountId = "1234";
            acc1.AccountType = "new";
            acc1.First = "fName";
            acc1.Last = "lName";
            acc1.Middle = "m";
            acc1.OfficeCode = "MA";
            acc1.RegionCode = "NE";
            acc1.Status = "active";
            acc1.Title = "MR";
            acc1.Username = "******";
            // create instance of TestBean3
            TestBean3 b = new TestBean3();
            b.Count = 3;
            b.Float = 10000;
            try
            {
                long start = DateTime.Now.Ticks;
                for (int idx = 0; idx < 100; idx++)
                {
                    engine.assertObject(acc1, null, false, true);
                }
                int count = engine.ObjectCount;
                for (int idx = 0; idx < 100; idx++)
                {
                    engine.assertObject(b, null, false, true);
                }
                int count2 = engine.ObjectCount;
                long end = DateTime.Now.Ticks;

                Assert.IsTrue(true);
                Assert.IsTrue(true);
                Assert.AreEqual(1, count);
                Assert.AreEqual(2, count2);
                Console.WriteLine("Number of facts: " + count);
                Console.WriteLine("Number of facts: " + count2);
                Console.WriteLine("ET: " + (end - start) + " ns");
                double el = ((double) end - (double) start)/100000;
                Console.WriteLine("ET: " + el + " ms");
            }
            catch (AssertException e)
            {
                Console.WriteLine(e.Message);
            }
            engine.close();
        }
示例#24
0
 public void testDeftemplate()
 {
     Rete engine = new Rete();
     Assert.IsNotNull(engine);
     engine.declareObject(typeof (Account));
     Assert.IsNotNull(engine.CurrentFocus.Templates);
     int count = engine.CurrentFocus.TemplateCount;
     Assert.AreEqual(2, count);
     Console.WriteLine("number of Deftemplates is " + count);
     engine.close();
 }
示例#25
0
 public void MeasureDeffactTest()
 {
     ArrayList objects = new ArrayList();
     // Runtime rt = Runtime.getRuntime();
     long total1 = GC.GetTotalMemory(true);
     //long free1 = rt.freeMemory();
     //long used1 = total1 - free1;
     int count = 50000;
     Console.WriteLine("Used memory before creating objects " + total1 + " bytes " +
                       (total1/1024) + " Kb");
     for (int idx = 0; idx < count; idx++)
     {
         Account acc = new Account();
         acc.AccountId = Convert.ToString(ran.Next(100000));
         acc.AccountType = Convert.ToString(ran.Next(100000));
         acc.First = Convert.ToString(ran.Next(100000));
         acc.Last = Convert.ToString(ran.Next(100000));
         acc.Middle = Convert.ToString(ran.Next(100000));
         acc.OfficeCode = Convert.ToString(ran.Next(100000));
         acc.RegionCode = Convert.ToString(ran.Next(100000));
         acc.Status = Convert.ToString(ran.Next(100000));
         acc.Title = Convert.ToString(ran.Next(100000));
         acc.Username = Convert.ToString(ran.Next(100000));
         acc.AreaCode = Convert.ToString(ran.Next(999));
         acc.Exchange = Convert.ToString(ran.Next(999));
         acc.Number = Convert.ToString(ran.Next(999));
         acc.Ext = Convert.ToString(ran.Next(9999));
         objects.Add(acc);
     }
     long total2 = GC.GetTotalMemory(true);
     //long free2 = rt.freeMemory();
     //long used2 = total2 - free2;
     Console.WriteLine("Used memory after creating objects " + total2 + " bytes " +
                       (total2/1024) + " Kb " + (total2/1024/1024) + " Mb");
     Rete engine = new Rete();
     engine.declareObject(typeof (Account));
     IEnumerator itr = objects.GetEnumerator();
     long start = DateTime.Now.Ticks;
     try
     {
         while (itr.MoveNext())
         {
             engine.assertObject(itr.Current, null, false, false);
         }
     }
     catch (AssertException e)
     {
         Console.WriteLine(e.Message);
     }
     long end = DateTime.Now.Ticks;
     long total3 = GC.GetTotalMemory(true);
     //long free3 = rt.freeMemory();
     //long used3 = total3 - free3;
     //rt.gc();
     Console.WriteLine("Used memory after asserting objects " + total3 + " bytes " +
                       (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
     Console.WriteLine("number of facts " + engine.ObjectCount);
     Console.WriteLine("memory used by facts " + (total3 - total3)/1024/1024 + " Mb");
     Console.WriteLine("elapsed time is " + (end - start) + " ms");
     engine.close();
 }
示例#26
0
        public void testAssertRightMultiple()
        {
            // first create a rule engine instance
            Rete engine = new Rete();
            NotJoin bn = new NotJoin(engine.nextNodeId());
            Assert.IsNotNull(bn);

            // create a defclass
            Defclass dc = new Defclass(typeof (TestBean2));
            // create deftemplate
            Deftemplate dtemp = dc.createDeftemplate("testBean2");
            Assert.IsNotNull(dtemp);
            Binding[] binds = new Binding[1];
            Binding b1 = new Binding();
            b1.LeftIndex = (0);
            b1.IsObjectVar = (false);
            b1.LeftRow = (0);
            b1.RightIndex = (0);
            b1.VarName = ("var1");
            binds[0] = b1;

            // set the binding
            bn.Bindings = (binds);

            int count = 10;
            ArrayList data = new ArrayList();
            for (int idx = 0; idx < count; idx++)
            {
                TestBean2 bean = new TestBean2();
                bean.Attr1 = ("random" + (idx + 1));
                bean.Attr2 = (101);
                short s = 10001;
                bean.Attr3 = (s);
                long l = 10101018;
                bean.Attr4 = (l);
                bean.Attr5 = (1010101);
                bean.Attr6 = (1001.1001);
                IFact fact = dtemp.createFact(bean, dc, engine.nextFactId());
                data.Add(fact);
            }
            IEnumerator itr = data.GetEnumerator();
            while (itr.MoveNext())
            {
                try
                {
                    IFact f1 = (IFact) itr.Current;
                    bn.assertRight(f1, engine, engine.WorkingMemory);
                }
                catch (AssertException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            IGenericMap<IFact, IFact> bmem = (IGenericMap<IFact, IFact>)engine.WorkingMemory.getBetaRightMemory(bn);
            Assert.AreEqual(count, bmem.Count);
            engine.close();
        }
示例#27
0
 public void testRetractNoShadow()
 {
     Console.WriteLine("testRetractNoShadow");
     Random ran = new Random();
     ArrayList objects = new ArrayList();
     // Runtime rt = Runtime.getRuntime();
     long total1 = GC.GetTotalMemory(true);
     //long free1 = rt.freeMemory();
     //long used1 = total1 - free1;
     int count = 50000;
     Console.WriteLine("Used memory before creating objects " + total1 + " bytes " +
                       (total1/1024) + " Kb");
     for (int idx = 0; idx < count; idx++)
     {
         Account acc = new Account();
         acc.AccountId = Convert.ToString(ran.Next(100000));
         acc.AccountType = Convert.ToString(ran.Next(100000));
         acc.First = Convert.ToString(ran.Next(100000));
         acc.Last = Convert.ToString(ran.Next(100000));
         acc.Middle = Convert.ToString(ran.Next(100000));
         acc.OfficeCode = Convert.ToString(ran.Next(100000));
         acc.RegionCode = Convert.ToString(ran.Next(100000));
         acc.Status = Convert.ToString(ran.Next(100000));
         acc.Title = Convert.ToString(ran.Next(100000));
         acc.Username = Convert.ToString(ran.Next(100000));
         acc.AreaCode = Convert.ToString(ran.Next(999));
         acc.Exchange = Convert.ToString(ran.Next(999));
         acc.Number = Convert.ToString(ran.Next(999));
         acc.Ext = Convert.ToString(ran.Next(9999));
         objects.Add(acc);
     }
     long total2 = GC.GetTotalMemory(true);
     //long free2 = rt.freeMemory();
     //long used2 = total2 - free2;
     Console.WriteLine("Used memory after creating objects " + total2 + " bytes " +
                       (total2/1024) + " Kb " + (total2/1024/1024) + " Mb");
     Rete engine = new Rete();
     engine.declareObject(typeof (Account));
     IEnumerator itr = objects.GetEnumerator();
     long start = DateTime.Now.Ticks;
     try
     {
         while (itr.MoveNext())
         {
             engine.assertObject(itr.Current, null, false, false);
         }
     }
     catch (AssertException e)
     {
         Console.WriteLine(e.Message);
     }
     long end = DateTime.Now.Ticks;
     long assertET = end - start;
     long total3 = GC.GetTotalMemory(true);
     //long free3 = rt.freeMemory();
     //long used3 = total3 - free3;
     //rt.gc();
     Console.WriteLine("Used memory after asserting objects " + total3 + " bytes " +
                       (total3/1024) + " Kb " + (total3/1024/1024) + " Mb");
     Console.WriteLine("number of facts " + engine.ObjectCount);
     Console.WriteLine("memory used by facts " + (total3 - total3)/1024/1024 + " Mb");
     Console.WriteLine("elapsed time is assert " + assertET + " ms");
     // now retract
     IEnumerator itr2 = objects.GetEnumerator();
     long retstart = DateTime.Now.Ticks;
     try
     {
         while (itr2.MoveNext())
         {
             engine.retractObject(itr2.Current);
         }
     }
     catch (RetractException e)
     {
         Console.WriteLine(e.Message);
     }
     long retend = DateTime.Now.Ticks;
     long retractET = retend - retstart;
     long total4 = GC.GetTotalMemory(true);
     //long free4 = rt.freeMemory();
     //long used4 = total4 - free4;
     objects.Clear();
     engine.clearAll();
     engine.close();
     //rt.gc();
     Console.WriteLine("elapsed time to retract " + retractET + " ms");
     // the retract should be atleast 3 times shorter than the assert
     #if DEBUG
     Assert.IsTrue(retractET > 0);
     #else
     Assert.IsTrue((assertET > (retractET*3)));
     #endif
 }
示例#28
0
 public void testAssertWithInterface()
 {
     Console.WriteLine("-----------------------------");
     Console.WriteLine("start testAssertWithInterface");
     Rete engine = new Rete();
     engine.declareObject(typeof (IAccount), "account");
     Assert.IsNotNull(engine);
     Account acc1 = new Account();
     acc1.AccountId = "1234";
     acc1.AccountType = "new";
     acc1.First = "fName";
     acc1.Last = "lName";
     acc1.Middle = "m";
     acc1.OfficeCode = "MA";
     acc1.RegionCode = "NE";
     acc1.Status = "active";
     acc1.Title = "MR";
     acc1.Username = "******";
     try
     {
         engine.assertObject(acc1, "account", false, true);
         Assert.IsTrue(true);
         Assert.AreEqual(1, engine.ObjectCount);
         Console.WriteLine("Number of facts: " + engine.ObjectCount);
         engine.printWorkingMemory(true, true);
     }
     catch (AssertException e)
     {
         Console.WriteLine(e.Message);
     }
     engine.close();
 }