Beispiel #1
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);
            }
        }
Beispiel #2
0
 protected void parse(Rete engine, CLIPSParser parser, IList factlist)
 {
     Object itm = null;
     try
     {
         while ((itm = parser.basicExpr()) != null)
         {
             // System.Console.WriteLine("obj is " + itm.getClass().Name);
             if (itm is Defrule)
             {
                 Defrule rule = (Defrule) itm;
                 engine.RuleCompiler.addRule(rule);
             }
             else if (itm is Deftemplate)
             {
                 Deftemplate dt = (Deftemplate) itm;
                 Console.WriteLine("template=" + dt.Name);
                 engine.declareTemplate(dt);
             }
             else if (itm is FunctionAction)
             {
                 FunctionAction fa = (FunctionAction) itm;
             }
             else if (itm is IFunction)
             {
                 IReturnVector rv = ((IFunction) itm).executeFunction(engine, null);
                 IEnumerator itr = rv.Iterator;
                 while (itr.MoveNext())
                 {
                     IReturnValue rval = (IReturnValue) itr.Current;
                     Console.WriteLine(rval.StringValue);
                 }
             }
         }
     }
     catch
     {
         // Console.WriteLine(e.Message);
         parser.ReInit(Console.OpenStandardInput());
     }
 }
Beispiel #3
0
 public StringChannelImpl(String channelId, MessageRouter router, InterestType interest)
     : base(channelId, router, interest)
 {
     InitBlock();
     parser = new CLIPSParser(router.ReteEngine, (StreamReader) null);
 }
Beispiel #4
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");
        }
Beispiel #5
0
 /// <summary>
 /// method does the actual work of creating a CLIPSParser and parsing
 /// the file.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="ins">The ins.</param>
 /// <param name="rv">The rv.</param>
 public virtual void parse(Rete engine, Stream ins, DefaultReturnVector rv)
 {
     try
     {
         CLIPSParser parser = new CLIPSParser(engine, ins);
         Object expr = null;
         while ((expr = parser.basicExpr()) != null)
         {
             if (expr is Defrule)
             {
                 Defrule rl = (Defrule) expr;
                 engine.RuleCompiler.addRule(rl);
             }
             else if (expr is Deftemplate)
             {
                 Deftemplate dft = (Deftemplate) expr;
                 engine.CurrentFocus.addTemplate(dft, engine, engine.WorkingMemory);
             }
             else if (expr is IFunction)
             {
                 IFunction fnc = (IFunction) expr;
                 fnc.executeFunction(engine, null);
             }
         }
         if (rv != null)
         {
             rv.addReturnValue(new DefaultReturnValue(Constants.BOOLEAN_OBJECT, true));
         }
     }
     catch (ParseException e)
     {
         engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
     }
 }
Beispiel #6
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");
        }
Beispiel #7
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");
        }
Beispiel #8
0
 public virtual IReturnVector eval(Rete engine, String command)
 {
     IReturnVector result = null;
     try
     {
         CLIPSParser parser = new CLIPSParser(engine, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(command)));
         CLIPSInterpreter interpreter = new CLIPSInterpreter(engine);
         Object expr = null;
         while ((expr = parser.basicExpr()) != null)
         {
             result = interpreter.executeCommand(expr);
         }
     }
     catch (ParseException e)
     {
         // we should report the error
         Trace.WriteLine(e.Message);
     }
     return result;
 }
Beispiel #9
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            DefaultReturnVector rv = new DefaultReturnVector();
            bool loaded = true;
            if (params_Renamed != null && params_Renamed.Length > 0)
            {
                for (int idx = 0; idx < params_Renamed.Length; idx++)
                {
                    String input = null;
                    if (params_Renamed[idx] is ValueParam)
                    {
                        input = ((ValueParam) params_Renamed[idx]).StringValue;
                    }
                    else if (params_Renamed[idx] is BoundParam)
                    {
                    }
                    if (input.IndexOf((Char) '\\') > - 1)
                    {
                        input.Replace("\\", "/");
                    }
                    // check to see if the path is an absolute windows path
                    // or absolute unix path
                    if (input.IndexOf(":") < 0 && !input.StartsWith("/") && !input.StartsWith("./"))
                    {
                        input = "./" + input;
                    }
                    try
                    {
                        Stream inStream = null;
                        if (Regex.IsMatch(input, "^[a-zA-Z]+://.*"))
                        {
                            Uri url = new Uri(input);
                            inStream = ((HttpWebRequest) WebRequest.Create(url)).GetResponse().GetResponseStream();
                            // Otherwise treat it as normal file on the Filesystem
                        }
                        else
                        {
                            inStream = new FileStream(new FileInfo(input).FullName, FileMode.Open, FileAccess.Read);
                        }
                        CLIPSParser parser = new CLIPSParser(inStream);
                        IList data = parser.loadExpr();
                        IEnumerator itr = data.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            Object val = itr.Current;
                            ValueParam[] vp = (ValueParam[]) val;
                            Deftemplate tmpl = (Deftemplate) engine.CurrentFocus.getTemplate(vp[0].StringValue);
                            Deffact fact = (Deffact) tmpl.createFact((Object[]) vp[1].Value, - 1);

                            engine.assertFact(fact);
                        }
                    }
                    catch (FileNotFoundException e)
                    {
                        loaded = false;
                        engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                    catch (ParseException e)
                    {
                        loaded = false;
                        engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                    catch (AssertException e)
                    {
                        loaded = false;
                        engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                    catch (IOException e)
                    {
                        loaded = false;
                        engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
                    }
                }
            }
            else
            {
                loaded = false;
            }
            DefaultReturnValue drv = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, loaded);
            rv.addReturnValue(drv);
            return rv;
        }