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); }
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); }
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()); }
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; }
/// <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()); }
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); }
/// <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 <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()); }
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); }
/// <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); } }
public virtual IReturnVector executeFunction(Rete engine, IParameter[] params_Renamed) { DefaultReturnVector ret = new DefaultReturnVector(); engine.writeMessage(Constants.VERSION + Constants.LINEBREAK, Constants.DEFAULT_OUTPUT); return(ret); }
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); }
/// <summary> Add an activation to the agenda. /// </summary> /// <param name="">actv /// /// </param> public virtual void addActivation(IActivation actv) { // the implementation should Get the current focus from Rete // and then Add the activation to the Module. if (profAdd) { addActivationWProfile(actv); } else { if (watch_Renamed_Field) { engine.writeMessage("=> " + actv.toPPString() + "\r\n", "t"); } actv.Rule.Module.addActivation(actv); } }
/// <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); }
/// <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()); } }
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); } } }
/// <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()); }
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); }
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); }
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); }
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); }
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); }
public virtual void assertFact(IFact fact) { Deffact f = (Deffact)fact; if (!containsFact(f)) { deffactMap.Put(fact.equalityIndex(), f); f.setFactId = engine; if (profileAssert_Renamed_Field) { assertFactWProfile(f); } else { if (watchFact_Renamed_Field) { engine.writeMessage("==> " + fact.toFactString() + Constants.LINEBREAK, "t"); } root.assertObject(f, engine, this); } } else { f.resetID((Deffact)deffactMap.Get(fact.equalityIndex())); } }
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); }
/// <summary> Here is a description of the compilation algorithm. /// 1. iterate over the conditional elements /// i. generate the alpha nodes /// a. literal constraints generate alpha node /// b. predicate constaints that compare against a literal generate alpha node /// ii. calculate the bindings /// a. each binding has a rowId /// b. NOT and EXIST CE do not increment the rowId /// 2. iterate over the conditional elements /// i. generate the beta nodes /// ii. attach the Left Input adapater nodes /// iii. attach the join nodes to the alpha nodes /// 3. create the terminal node and attach to the last /// join node. /// /// This means the rule compiler takes a 2 pass approach to /// compiling rules. At the start of the method, it sets 3 /// attributes to null: prevCE, prevJoinNode, joinNode. /// Those attributes are used by the compile join methods, /// so it's important to set it to null at the start. If /// we don't the Current rule won't compile correctly. /// </summary> public virtual bool addRule(Rule.IRule rule) { rule.resolveTemplates(engine); if (!validate || (validate && tval.analyze(rule) == Analysis_Fields.VALIDATION_PASSED)) { // we have to set the attributes to null, before we start compiling a rule. // we've set the attributes to null, so we can compile now!! if (rule.Conditions != null && rule.Conditions.Length > 0) { // we check the name of the rule to see if it is for a specific // module. if it is, we have to Add it to that module Module = rule; try { ICondition[] conds = rule.Conditions; // first we create the constraints, before creating the Conditional // elements which include joins // we use a counter and only increment it to make sure the // row index of the bindings are accurate. this makes it simpler // for the rule compiler and compileJoins is cleaner and does // less work. int counter = 0; for (int idx = 0; idx < conds.Length; idx++) { ICondition con = conds[idx]; // compile object conditions //implement in the ObjectConditionCompiler.compile or ExistConditionCompiler.compile con.getCompiler(this).compile(con, counter, rule, rule.RememberMatch); if ((con is ObjectCondition) && (!((ObjectCondition)con).Negated)) { counter++; } } // now we compile the joins compileJoins(rule); BaseNode last = rule.LastNode; TerminalNode tnode = createTerminalNode(rule); attachTerminalNode(last, tnode); // compile the actions compileActions(rule, rule.Actions); // now we pass the bindings to the rule, so that actiosn can // resolve the bindings // now we Add the rule to the module currentMod.addRule(rule); CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.ADD_RULE_EVENT); ce.Rule = rule; notifyListener(ce); return(true); } catch (AssertException e) { CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.INVALID_RULE); ce.Message = Messages.getString("RuleCompiler.assert.error"); //$NON-NLS-1$ notifyListener(ce); TraceLogger.Instance.Debug(e); return(false); } } else if (rule.Conditions.Length == 0) { Module = rule; // the rule has no LHS, this means it only has actions BaseNode last = (BaseNode)inputnodes.Get(engine.initFact); TerminalNode tnode = createTerminalNode(rule); compileActions(rule, rule.Actions); attachTerminalNode(last, tnode); // now we Add the rule to the module currentMod.addRule(rule); CompileMessageEventArgs ce = new CompileMessageEventArgs(rule, EventType.ADD_RULE_EVENT); ce.Rule = rule; notifyListener(ce); return(true); } return(false); } else { // we need to print out a message saying the rule was not valid ISummary error = tval.Errors; engine.writeMessage("Rule " + rule.Name + " was not added. ", Constants.DEFAULT_OUTPUT); //$NON-NLS-1$ //$NON-NLS-2$ engine.writeMessage(error.Message, Constants.DEFAULT_OUTPUT); ISummary warn = tval.Warnings; engine.writeMessage(warn.Message, Constants.DEFAULT_OUTPUT); return(false); } }
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); }