Exemplo n.º 1
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     IList<object> facts = engine.AllFacts;
     Object[] sorted = FactUtils.sortFacts(facts);
     for (int idx = 0; idx < sorted.Length; idx++)
     {
         IFact ft = (IFact) sorted[idx];
         engine.writeMessage(ft.toFactString() + Constants.LINEBREAK);
     }
     engine.writeMessage("for a total of " + sorted.Length + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
     return new DefaultReturnVector();
 }
Exemplo n.º 2
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     IList modules = (IList) engine.WorkingMemory.Modules;
     int count = modules.Count;
     IEnumerator itr = modules.GetEnumerator();
     while (itr.MoveNext())
     {
         IModule r = (IModule) itr.Current;
         engine.writeMessage(r.ModuleName + Constants.LINEBREAK, "t");
     }
     engine.writeMessage("for a total of " + count + Constants.LINEBREAK, "t");
     DefaultReturnVector rv = new DefaultReturnVector();
     return rv;
 }
Exemplo n.º 3
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     Process rt = Process.GetCurrentProcess();
     long total = GC.GetTotalMemory(false);
     engine.writeMessage("Total memory " + (total/1024).ToString() + "Kb | " + (total/1024/1024).ToString() + "Mb" + Constants.LINEBREAK, "t");
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 4
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     Process rt = Process.GetCurrentProcess();
     long free = GC.GetTotalMemory(false);
     long total = GC.GetTotalMemory(false);
     long used = total - free;
     used = used/1024/1024;
     total = total/1024;
     long mbtotal = total/1024;
     engine.writeMessage(used.ToString() + "Mb used of " + mbtotal.ToString() + "Mb " + Constants.LINEBREAK, "t");
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 5
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     if (params_Renamed != null && params_Renamed.Length > 0)
     {
         for (int idx = 0; idx < params_Renamed.Length; idx++)
         {
             IRule rls = engine.CurrentFocus.findRule(params_Renamed[idx].StringValue);
             engine.writeMessage(rls.toPPString(), "t");
         }
     }
     DefaultReturnVector rv = new DefaultReturnVector();
     return rv;
 }
Exemplo n.º 6
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     engine.writeMessage("fire ET=" + ProfileStats.fireTime + " ms" + Constants.LINEBREAK, "t");
     engine.writeMessage("assert ET=" + ProfileStats.assertTime + " ms" + Constants.LINEBREAK, "t");
     engine.writeMessage("retract ET=" + ProfileStats.retractTime + " ms" + Constants.LINEBREAK, "t");
     engine.writeMessage("Add Activation ET=" + ProfileStats.addActivation + " ms" + Constants.LINEBREAK, "t");
     engine.writeMessage("Remove Activation ET=" + ProfileStats.rmActivation + " ms" + Constants.LINEBREAK, "t");
     engine.writeMessage("Activation added=" + ProfileStats.addcount + Constants.LINEBREAK, "t");
     engine.writeMessage("Activation removed=" + ProfileStats.rmcount + Constants.LINEBREAK, "t");
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 7
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     List<Object> rules = (List<Object>) engine.CurrentFocus.AllRules;
     int count = rules.Count;
     IEnumerator itr = rules.GetEnumerator();
     while (itr.MoveNext())
     {
         IRule r = (IRule) itr.Current;
         engine.writeMessage(r.Name + " \"" + r.Comment + "\" salience:" + r.Salience + " version:" + r.Version + " no-agenda:" + r.NoAgenda + "\r\n", "t");
     }
     engine.writeMessage("for a total of " + count + "\r\n", "t");
     DefaultReturnVector rv = new DefaultReturnVector();
     return rv;
 }
Exemplo n.º 8
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     IList fgroups = engine.FunctionGroups;
     IEnumerator itr = fgroups.GetEnumerator();
     int counter = 0;
     while (itr.MoveNext())
     {
         // we iterate over the function groups and print out the
         // functions in each group
         IFunctionGroup fg = (IFunctionGroup) itr.Current;
         engine.writeMessage("++++ " + fg.Name + " ++++" + Constants.LINEBREAK, "t");
         IEnumerator listitr = fg.listFunctions().GetEnumerator();
         while (listitr.MoveNext())
         {
             IFunction f = (IFunction) listitr.Current;
             engine.writeMessage("  " + f.Name + Constants.LINEBREAK, "t");
             counter++;
         }
     }
     engine.writeMessage(counter + " functions" + Constants.LINEBREAK, "t");
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 9
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     List<Object> templates = (List<Object>) engine.CurrentFocus.Templates;
     int count = templates.Count;
     IEnumerator itr = templates.GetEnumerator();
     while (itr.MoveNext())
     {
         ITemplate r = (ITemplate) itr.Current;
         engine.writeMessage(r.Name + Constants.LINEBREAK, "t");
     }
     engine.writeMessage("for a total of " + count + Constants.LINEBREAK, "t");
     DefaultReturnVector rv = new DefaultReturnVector();
     return rv;
 }
Exemplo n.º 10
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     Process rt = Process.GetCurrentProcess();
     long free = GC.GetTotalMemory(false);
     long total = GC.GetTotalMemory(false);
     double percentfree = ((double) free/(double) total)*100;
     free = free/1024;
     total = total/1024;
     long mbtotal = total/1024;
     String freestr = percentfree.ToString().Substring(0, (4) - (0));
     engine.writeMessage(free.ToString() + "Kb - " + freestr + "% free of " + mbtotal.ToString() + "Mb / " + total.ToString() + "Kb " + Constants.LINEBREAK, "t");
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 11
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     bool add = true;
     if (params_Renamed.Length == 1)
     {
         engine.addModule(params_Renamed[0].StringValue);
         engine.writeMessage("true", Constants.DEFAULT_OUTPUT);
     }
     else
     {
         add = false;
     }
     DefaultReturnVector ret = new DefaultReturnVector();
     DefaultReturnValue rv = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, add);
     ret.addReturnValue(rv);
     DefaultReturnValue rv2 = new DefaultReturnValue(Constants.STRING_TYPE, params_Renamed[0].StringValue);
     ret.addReturnValue(rv2);
     return ret;
 }
Exemplo n.º 12
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     DefaultReturnVector ret = new DefaultReturnVector();
     engine.writeMessage(Constants.VERSION + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
     return ret;
 }
Exemplo n.º 13
0
 /// <summary> The current implementation will print out all the templates in
 /// no specific order. The function does basically the same thing
 /// as CLIPS (list-deftemplates)
 /// </summary>
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     List<Object> templ = (List<Object>) engine.CurrentFocus.Templates;
     IEnumerator itr = templ.GetEnumerator();
     while (itr.MoveNext())
     {
         ITemplate tp = (ITemplate) itr.Current;
         engine.writeMessage(tp.toPPString() + "\r\n", "t");
     }
     return new DefaultReturnVector();
 }
Exemplo n.º 14
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            bool exec = false;
            if (engine != null && params_Renamed != null && params_Renamed.Length >= 2 && params_Renamed[0].ObjectBinding)
            {
                BoundParam bp = (BoundParam) params_Renamed[0];
                Deffact fact = (Deffact) bp.Fact;
                try
                {
                    // first retract the fact
                    engine.retractFact(fact);
                    // now modify the fact
                    SlotParam[] sp = new SlotParam[params_Renamed.Length - 1];
                    for (int idx = 0; idx < sp.Length; idx++)
                    {
                        IParameter p = params_Renamed[idx + 1];
                        if (p is SlotParam)
                        {
                            sp[idx] = (SlotParam) p;
                        }
                    }
                    fact.updateSlots(engine, convertToSlots(sp, fact.Deftemplate));
                    if (fact.hasBinding())
                    {
                        fact.resolveValues(engine, triggerFacts);
                        fact = fact.cloneFact();
                    }
                    // now assert the fact using the same fact-id
                    engine.assertFact(fact);
                    exec = true;
                }
                catch (RetractException e)
                {
                    engine.writeMessage(e.Message);
                }
                catch (AssertException e)
                {
                    engine.writeMessage(e.Message);
                }
            }

            DefaultReturnVector rv = new DefaultReturnVector();
            DefaultReturnValue rval = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, exec);
            rv.addReturnValue(rval);
            return rv;
        }
Exemplo n.º 15
0
 /// <summary> the function will printout one or more templates. This implementation
 /// is slightly different than CLIPS in that it can take one or more
 /// template names. The definition in CLIPS beginners guide states the 
 /// function does the following: (ppdeftemplate &lt;deftemplate-name>)
 /// </summary>
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     GenericHashMap<object, object> filter = new GenericHashMap<object, object>();
     if (params_Renamed != null && params_Renamed.Length > 0)
     {
         for (int idx = 0; idx < params_Renamed.Length; idx++)
         {
             if (params_Renamed[idx] is ValueParam)
             {
                 Object df = ((ValueParam) params_Renamed[idx]).Value;
                 filter.Put(df, df);
             }
         }
     }
     List<Object> templ = (List<Object>) engine.CurrentFocus.Templates;
     IEnumerator itr = templ.GetEnumerator();
     while (itr.MoveNext())
     {
         ITemplate tp = (ITemplate) itr.Current;
         if (filter.Get(tp.Name) != null)
         {
             engine.writeMessage(tp.toPPString() + "\r\n", "t");
         }
     }
     return new DefaultReturnVector();
 }
Exemplo n.º 16
0
 public virtual void writeArray(Object[] arry, Rete engine, String output, bool linebreak)
 {
     for (int idz = 0; idz < arry.Length; idz++)
     {
         Object val = arry[idz];
         if (val is IFact)
         {
             IFact f = (IFact) val;
             engine.writeMessage(f.toFactString() + " ", output);
         }
         else
         {
             engine.writeMessage(arry[idz].ToString() + " ", output);
         }
         if (linebreak)
         {
             engine.writeMessage(Constants.LINEBREAK, output);
         }
     }
 }
Exemplo n.º 17
0
 /// <summary> method will attempt to load one or more files. If batch is called without
 /// any parameters, the function does nothing and just returns.
 /// </summary>
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     DefaultReturnVector rv = new DefaultReturnVector();
     if (params_Renamed != null && params_Renamed.Length > 0)
     {
         for (int idx = 0; idx < params_Renamed.Length; idx++)
         {
             try
             {
                 String input = params_Renamed[idx].StringValue;
                 Stream inStream;
                 // Check for a protocol indicator at the beginning of the
                 // String. If we have one use a URL.
                 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);
                 }
                 parse(engine, inStream, rv);
                 inStream.Close();
                 if (inStream is IDisposable)
                 {
                     inStream.Dispose();
                 }
             }
             catch (FileNotFoundException e)
             {
                 // we should report the error
                 rv.addReturnValue(new DefaultReturnValue(Constants.BOOLEAN_OBJECT, false));
                 engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
             }
             catch (IOException e)
             {
                 rv.addReturnValue(new DefaultReturnValue(Constants.BOOLEAN_OBJECT, false));
                 engine.writeMessage(e.Message + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT);
             }
         }
     }
     return rv;
 }
Exemplo n.º 18
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);
     }
 }
Exemplo n.º 19
0
        public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
        {
            DefaultReturnVector ret = new DefaultReturnVector();
            bool echo = false;
            IList<Object> facts = null;
            String output = null;
            if (params_Renamed != null && params_Renamed.Length >= 1)
            {
                Defrule r = (Defrule) engine.CurrentFocus.findRule(params_Renamed[0].StringValue);
                if (params_Renamed.Length == 2)
                {
                    if (params_Renamed[1].BooleanValue)
                    {
                        echo = true;
                    }
                }
                // if there's 3 parameters, it means we should save the fact
                // to a file
                if (params_Renamed.Length == 3)
                {
                    output = params_Renamed[2].StringValue;
                }
                facts = GenerateFacts.generateFacts(r, engine);
                if (facts.Count > 0)
                {
                    if (echo)
                    {
                        IEnumerator itr = facts.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            Object data = itr.Current;
                            if (data is Deffact)
                            {
                                Deffact f = (Deffact) data;
                                engine.writeMessage(f.toFactString());
                            }
                            else
                            {
                                engine.writeMessage(data.ToString());
                            }
                        }
                    }
                    if (output != null)
                    {
                        // we need to save facts to a file
                        IOUtilities.saveFacts(facts, output);
                    }

                    object[] ary = new object[facts.Count];
                    facts.CopyTo(ary,0);
                    DefaultReturnValue rv = new DefaultReturnValue(Constants.OBJECT_TYPE, ary);
                    ret.addReturnValue(rv);
                }
                else
                {
                    DefaultReturnValue rv = new DefaultReturnValue(Constants.BOOLEAN_OBJECT, false);
                    ret.addReturnValue(rv);
                }
            }
            return ret;
        }
Exemplo n.º 20
0
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     if (params_Renamed != null && params_Renamed.Length > 0)
     {
         FileInfo dir = new FileInfo(params_Renamed[0].StringValue);
         if (Directory.Exists(dir.FullName))
         {
             FileInfo[] files = dir.Directory.GetFiles();
             for (int idx = 0; idx < files.Length; idx++)
             {
                 if (Directory.Exists(files[idx].FullName))
                 {
                     engine.writeMessage("d " + files[idx] + Constants.LINEBREAK);
                 }
                 else
                 {
                     engine.writeMessage("- " + files[idx] + Constants.LINEBREAK);
                 }
             }
             engine.writeMessage(files.Length + " files in the directory" + Constants.LINEBREAK, "t");
         }
         else
         {
         }
     }
     DefaultReturnVector ret = new DefaultReturnVector();
     return ret;
 }
Exemplo n.º 21
0
 /// <summary> Convienance method for iterating over the entries in the HashMap
 /// and printing it out. The implementation prints the String key and
 /// calls Object.toString() for the value.
 /// </summary>
 /// <param name="">engine
 /// 
 /// </param>
 public virtual void printDefglobals(Rete engine)
 {
     IEnumerator itr = variables.Keys.GetEnumerator();
     while (itr.MoveNext())
     {
         String key = (String) itr.Current;
         Object val = variables.Get(key);
         engine.writeMessage(key + "=" + val.ToString());
     }
 }
Exemplo n.º 22
0
 /// <summary> The implementation will call Rete.writeMessage(). This means that
 /// if multiple output streams are set, the message will be printed to
 /// all of them.
 /// </summary>
 public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed)
 {
     // print out some stuff
     if (params_Renamed.Length > 0)
     {
         String output = params_Renamed[0].StringValue;
         for (int idx = 1; idx < params_Renamed.Length; idx++)
         {
             if (params_Renamed[idx] is BoundParam)
             {
                 BoundParam bp = (BoundParam) params_Renamed[idx];
                 Object v = engine.getBinding(bp.VariableName);
                 if (v.GetType().IsArray)
                 {
                     Object[] ary = (Object[]) v;
                     writeArray(ary, engine, output, false);
                 }
                 else
                 {
                     engine.writeMessage(v.ToString(), output);
                 }
             }
             else if (params_Renamed[idx].Value != null && params_Renamed[idx].Value.Equals(Constants.CRLF))
             {
                 engine.writeMessage(Constants.LINEBREAK, output);
             }
             else
             {
                 Object val = params_Renamed[idx].Value;
                 if (val is String)
                 {
                     engine.writeMessage((String) val, output);
                 }
                 else if (val.GetType().IsArray)
                 {
                     Object[] ary = (Object[]) val;
                     writeArray(ary, engine, output, true);
                 }
                 else
                 {
                     engine.writeMessage(val.ToString(), output);
                 }
             }
         }
     }
     // there's nothing to return, so just return a new DefaultReturnVector
     return new DefaultReturnVector();
 }
Exemplo n.º 23
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;
        }