//public virtual void fireErrorEvent(Object reason) //{ //} /// <summary> /// basic method iterates over the listeners and passes the event, checking /// what kind of event it is and calling the appropriate method. /// </summary> /// <param name="eventArgs">The event_ renamed.</param> public virtual void notifyListener(CompileMessageEventArgs eventArgs) { switch (eventArgs.EventType) { case EventType.ADD_RULE_EVENT: if (RuleAdded != null) { RuleAdded(this, eventArgs); } break; case EventType.REMOVE_RULE_EVENT: if (RuleRemoved != null) { RuleRemoved(this, eventArgs); } break; default: if (CompileError != null) { CompileError(this, eventArgs); } break; } //IEnumerator itr = listener.GetEnumerator(); ////engine.writeMessage(event.getMessage()); //while (itr.MoveNext()) //{ // ICompilerListener listen = (ICompilerListener) itr.Current; // EventType etype = messageEventArgsRenamed.EventType; // if (etype == EventType.ADD_RULE_EVENT) // { // listen.ruleAdded(messageEventArgsRenamed); // } // else if (etype == EventType.REMOVE_RULE_EVENT) // { // listen.ruleRemoved(messageEventArgsRenamed); // } // else // { // listen.compileError(messageEventArgsRenamed); // } //} }
/// <summary> /// For now, this is not implemented /// </summary> /// <param name="messageEventArgsRenamed">The event_ renamed.</param> public virtual void compileError(object sender, CompileMessageEventArgs messageEventArgsRenamed) { TraceLogger.Instance.Warn(messageEventArgsRenamed.Message); }
/// <summary> /// For now, this is not implemented /// </summary> /// <param name="messageEventArgsRenamed">The event_ renamed.</param> public virtual void ruleRemoved(object sender, CompileMessageEventArgs messageEventArgsRenamed) { TraceLogger.Instance.Info("removed: " + messageEventArgsRenamed.Message); }
//public virtual void fireErrorEvent(Object reason) //{ //} /// <summary> /// basic method iterates over the listeners and passes the event, checking /// what kind of event it is and calling the appropriate method. /// </summary> /// <param name="eventArgs">The event_ renamed.</param> public virtual void notifyListener(CompileMessageEventArgs eventArgs) { switch(eventArgs.EventType) { case EventType.ADD_RULE_EVENT: if(RuleAdded != null) { RuleAdded(this, eventArgs); } break; case EventType.REMOVE_RULE_EVENT: if (RuleRemoved != null) { RuleRemoved(this, eventArgs); } break; default: if (CompileError != null) { CompileError(this, eventArgs); } break; } //IEnumerator itr = listener.GetEnumerator(); ////engine.writeMessage(event.getMessage()); //while (itr.MoveNext()) //{ // ICompilerListener listen = (ICompilerListener) itr.Current; // EventType etype = messageEventArgsRenamed.EventType; // if (etype == EventType.ADD_RULE_EVENT) // { // listen.ruleAdded(messageEventArgsRenamed); // } // else if (etype == EventType.REMOVE_RULE_EVENT) // { // listen.ruleRemoved(messageEventArgsRenamed); // } // else // { // listen.compileError(messageEventArgsRenamed); // } //} }
/// <summary> /// Compiles the constraint. /// </summary> /// <param name="cnstr">The CNSTR.</param> /// <param name="templ">The templ.</param> /// <param name="rule">The rule.</param> /// <param name="position">The position.</param> /// <returns></returns> public virtual BaseAlpha2 compileConstraint(PredicateConstraint cnstr, ITemplate templ, Rule.IRule rule, int position) { BaseAlpha2 current = null; // for now we expect the user to write the predicate in this // way (> ?bind value), where the binding is first. this // needs to be updated so that we look at the order of the // parameters and set the node appropriately // we only create an AlphaNode if the predicate isn't // joining 2 bindings. if (!cnstr.PredicateJoin) { if (ConversionUtils.isPredicateOperatorCode(cnstr.FunctionName)) { int oprCode = ConversionUtils.getOperatorCode(cnstr.FunctionName); Slot sl = (Slot) templ.getSlot(cnstr.Name).Clone(); Object sval = ConversionUtils.convert(sl.ValueType, cnstr.Value); sl.Value = sval; // create the alphaNode if (rule.RememberMatch) { current = new AlphaNode(engine.nextNodeId()); } else { current = new NoMemANode(engine.nextNodeId()); } current.Slot = sl; current.Operator = oprCode; current.incrementUseCount(); // we increment the node use count when when create a new // AlphaNode for the LiteralConstraint templ.getSlot(sl.Id).incrementNodeCount(); } else { // the function isn't a built in predicate function that // returns boolean true/false. We look up the function IFunction f = engine.findFunction(cnstr.FunctionName); if (f != null) { // we create the alphaNode if a function is found and // the return type is either boolean primitive or object if (f.ReturnType == Constants.BOOLEAN_PRIM_TYPE || f.ReturnType != Constants.BOOLEAN_OBJECT) { // TODO - need to implement it } else { // the function doesn't return boolean, so we have to notify // the listeners the condition is not valid CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_INVALID); ce.Message = INVALID_FUNCTION + " " + f.ReturnType; //$NON-NLS-1$ notifyListener(ce); } } else { // we need to notify listeners the function wasn't found CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_NOT_FOUND); ce.Message = FUNCTION_NOT_FOUND + " " + cnstr.FunctionName; notifyListener(ce); } } } Binding bind = new Binding(); bind.VarName = cnstr.VariableName; bind.LeftRow = position; bind.LeftIndex = templ.getSlot(cnstr.Name).Id; bind.RowDeclared = position; // we only Add the binding to the map if it doesn't already exist if (rule.getBinding(cnstr.VariableName) == null) { rule.addBinding(cnstr.VariableName, bind); } return current; }
/// <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; } }
/// <summary> /// Compiles the constraint. /// </summary> /// <param name="cnstr">The CNSTR.</param> /// <param name="templ">The templ.</param> /// <param name="rule">The rule.</param> /// <param name="position">The position.</param> /// <returns></returns> public virtual BaseAlpha2 compileConstraint(PredicateConstraint cnstr, ITemplate templ, Rule.IRule rule, int position) { BaseAlpha2 current = null; // for now we expect the user to write the predicate in this // way (> ?bind value), where the binding is first. this // needs to be updated so that we look at the order of the // parameters and set the node appropriately // we only create an AlphaNode if the predicate isn't // joining 2 bindings. if (!cnstr.PredicateJoin) { if (ConversionUtils.isPredicateOperatorCode(cnstr.FunctionName)) { int oprCode = ConversionUtils.getOperatorCode(cnstr.FunctionName); Slot sl = (Slot)templ.getSlot(cnstr.Name).Clone(); Object sval = ConversionUtils.convert(sl.ValueType, cnstr.Value); sl.Value = sval; // create the alphaNode if (rule.RememberMatch) { current = new AlphaNode(engine.nextNodeId()); } else { current = new NoMemANode(engine.nextNodeId()); } current.Slot = sl; current.Operator = oprCode; current.incrementUseCount(); // we increment the node use count when when create a new // AlphaNode for the LiteralConstraint templ.getSlot(sl.Id).incrementNodeCount(); } else { // the function isn't a built in predicate function that // returns boolean true/false. We look up the function IFunction f = engine.findFunction(cnstr.FunctionName); if (f != null) { // we create the alphaNode if a function is found and // the return type is either boolean primitive or object if (f.ReturnType == Constants.BOOLEAN_PRIM_TYPE || f.ReturnType != Constants.BOOLEAN_OBJECT) { // TODO - need to implement it } else { // the function doesn't return boolean, so we have to notify // the listeners the condition is not valid CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_INVALID); ce.Message = INVALID_FUNCTION + " " + f.ReturnType; //$NON-NLS-1$ notifyListener(ce); } } else { // we need to notify listeners the function wasn't found CompileMessageEventArgs ce = new CompileMessageEventArgs(this, EventType.FUNCTION_NOT_FOUND); ce.Message = FUNCTION_NOT_FOUND + " " + cnstr.FunctionName; notifyListener(ce); } } } Binding bind = new Binding(); bind.VarName = cnstr.VariableName; bind.LeftRow = position; bind.LeftIndex = templ.getSlot(cnstr.Name).Id; bind.RowDeclared = position; // we only Add the binding to the map if it doesn't already exist if (rule.getBinding(cnstr.VariableName) == null) { rule.addBinding(cnstr.VariableName, bind); } return(current); }
/// <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); } }