Ejemplo n.º 1
0
        public static WonkaBizRuleSet FindRuleSet(this WonkaBizRulesEngine rulesEngine, int pnTargetRuleSetId)
        {
            WonkaBizRuleSet FoundRuleSet = new WonkaBizRuleSet();

            if (rulesEngine.RuleTreeRoot.RuleSetId == pnTargetRuleSetId)
            {
                return(rulesEngine.RuleTreeRoot);
            }

            foreach (var TmpChildSet in rulesEngine.RuleTreeRoot.ChildRuleSets)
            {
                if (TmpChildSet.RuleSetId == pnTargetRuleSetId)
                {
                    return(TmpChildSet);
                }
                else
                {
                    FoundRuleSet = FindRuleSet(TmpChildSet, pnTargetRuleSetId);
                    if (IsValidRuleSet(FoundRuleSet))
                    {
                        break;
                    }
                }
            }

            return(FoundRuleSet);
        }
        public static void AddNethereumERC20GetBalanceRule(this WonkaBizRuleSet poRuleSet,
                                                           WonkaRefEnvironment poRefEnv,
                                                           Web3 poWeb3,
                                                           WonkaRefAttr poTargetAttr,
                                                           string psAddRuleDesc,
                                                           string psTargetOwner,
                                                           string psERC20ContractAddress)
        {
            WonkaBizRule NewRule = null;

            WonkaBizSource DummySource =
                new WonkaBizSource("ERC20", psERC20ContractAddress, "", "", "", "", "", null);

            WonkaEthCustomOpRule CustomOpRule =
                new WonkaEthCustomOpRule(mnRuleCounter++,
                                         TARGET_RECORD.TRID_NEW_RECORD,
                                         poTargetAttr.AttrId,
                                         CONST_ERC20_GET_BALANCE_OP,
                                         null,
                                         DummySource);

            CustomOpRule.OwnerWeb3 = poWeb3;
            CustomOpRule.PrimaryContractAddress = psERC20ContractAddress;
            CustomOpRule.CustomOpDelegate       = CustomOpRule.InvokeERC20GetBalance;

            CustomOpRule.AddDomainValue(psTargetOwner, true, TARGET_RECORD.TRID_NONE);

            if (!String.IsNullOrEmpty(psAddRuleDesc))
            {
                NewRule.DescRuleId = psAddRuleDesc;
            }

            poRuleSet.AddRule(NewRule);
        }
Ejemplo n.º 3
0
        private WonkaBizRuleSet ParseRuleSet(XmlNode poRuleSetXmlNode, bool pbLeafNode = false)
        {
            WonkaBizRuleSet currentRuleSet = new WonkaBizRuleSet(++(this.RuleSetIdCounter));

            AllParsedRuleSets.Add(currentRuleSet);

            var AttrDesc = poRuleSetXmlNode.Attributes.GetNamedItem(CONST_RS_FLOW_DESC_ATTR);

            if (AttrDesc != null)
            {
                currentRuleSet.Description = AttrDesc.Value;
            }

            XmlNodeList ChildNodeList = poRuleSetXmlNode.ChildNodes;

            foreach (XmlNode TempChildXmlNode in ChildNodeList)
            {
                if ((TempChildXmlNode.LocalName == CONST_RS_FLOW_TAG) ||
                    (TempChildXmlNode.LocalName == CONST_RS_VALID_TAG))
                {
                    bool bIsLeafNode = (TempChildXmlNode.LocalName == CONST_RS_VALID_TAG);

                    WonkaBizRuleSet NewChildRuleSet = ParseRuleSet(TempChildXmlNode, bIsLeafNode);

                    if (bIsLeafNode)
                    {
                        var AttrErrLevel = TempChildXmlNode.Attributes.GetNamedItem(CONST_RS_VALID_ERR_ATTR);
                        if (AttrErrLevel != null)
                        {
                            if (AttrErrLevel.Value.ToLower() == CONST_RS_VALID_ERR_WARNING)
                            {
                                NewChildRuleSet.ErrorSeverity = RULE_SET_ERR_LVL.ERR_LVL_WARNING;
                            }
                            else if (AttrErrLevel.Value.ToLower() == CONST_RS_VALID_ERR_SEVERE)
                            {
                                NewChildRuleSet.ErrorSeverity = RULE_SET_ERR_LVL.ERR_LVL_SEVERE;
                            }
                        }
                    }

                    NewChildRuleSet.ParentRuleSetId = currentRuleSet.RuleSetId;

                    currentRuleSet.AddChildRuleSet(NewChildRuleSet);
                }
                else if (TempChildXmlNode.LocalName == CONST_RULES_TAG)
                {
                    ParseRules(TempChildXmlNode, currentRuleSet);
                }
                else if (pbLeafNode && (TempChildXmlNode.LocalName == CONST_RS_FAIL_MSG_TAG))
                {
                    currentRuleSet.CustomFailureMsg = TempChildXmlNode.InnerText;
                }
                else if (pbLeafNode && (TempChildXmlNode.LocalName == CONST_RS_CUSTOM_ID_TAG))
                {
                    currentRuleSet.CustomId = TempChildXmlNode.InnerText;
                }
            }

            return(currentRuleSet);
        }
Ejemplo n.º 4
0
        public static WonkaBizRuleSet FindRuleSet(this WonkaBizRulesEngine rulesEngine, string psSoughtDescName)
        {
            WonkaBizRuleSet FoundRuleSet = new WonkaBizRuleSet();

            if (rulesEngine.RuleTreeRoot.Description == psSoughtDescName)
            {
                return(rulesEngine.RuleTreeRoot);
            }

            foreach (var TmpChildSet in rulesEngine.RuleTreeRoot.ChildRuleSets)
            {
                if (TmpChildSet.Description == psSoughtDescName)
                {
                    return(TmpChildSet);
                }
                else
                {
                    FoundRuleSet = FindRuleSet(TmpChildSet, psSoughtDescName);
                    if (IsValidRuleSet(FoundRuleSet))
                    {
                        break;
                    }
                }
            }

            return(FoundRuleSet);
        }
Ejemplo n.º 5
0
        public static WonkaBizRule FindRuleById(this WonkaBizRuleSet poTargetSet, string psSoughtId)
        {
            WonkaBizRule FoundRule = new ArithmeticRule();

            foreach (var TmpChildSet in poTargetSet.ChildRuleSets)
            {
                foreach (var TmpRule in TmpChildSet.EvaluativeRules)
                {
                    if (TmpRule.DescRuleId == psSoughtId)
                    {
                        return(TmpRule);
                    }
                }

                foreach (var TmpRule in TmpChildSet.AssertiveRules)
                {
                    if (TmpRule.DescRuleId == psSoughtId)
                    {
                        return(TmpRule);
                    }
                }

                FoundRule = FindRuleById(TmpChildSet, psSoughtId);
                if (FoundRule.RuleId > 0)
                {
                    return(FoundRule);
                }
            }

            return(FoundRule);
        }
Ejemplo n.º 6
0
        private void ParseRules(XmlNode poTargetXmlNode, WonkaBizRuleSet poTargetRuleSet)
        {
            var OpDesc = poTargetXmlNode.Attributes.GetNamedItem(CONST_RULES_OP_ATTR);

            if (OpDesc != null)
            {
                if (OpDesc.Value.ToLower() == "and")
                {
                    poTargetRuleSet.RulesEvalOperator = RULE_OP.OP_AND;
                }
                else if (OpDesc.Value.ToLower() == "or")
                {
                    poTargetRuleSet.RulesEvalOperator = RULE_OP.OP_OR;
                }
            }

            XmlNodeList ChildNodeList = poTargetXmlNode.ChildNodes;

            foreach (XmlNode TempChildXmlNode in ChildNodeList)
            {
                if (TempChildXmlNode.LocalName == CONST_RULE_TAG)
                {
                    ParseSingleRule(TempChildXmlNode, poTargetRuleSet);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// This method will begin the evaluation of a RuleTree when applied to both the incoming and current Product
        /// records.
        ///
        /// <param name="poRootRuleSet">The root RuleSet of the RuleTree whose rules are being applied against the provided records</param>
        /// <param name="poIncomingProduct">The incoming Product record</param>
        /// <param name="poCurrentProduct">The current Product record (i.e., in the database)</param>
        /// <param name="poRuleTreeReport">The report that will contain all evaluations of RuleSets in the RuleTree</param>
        /// <returns>Indicates whether or not the RuleSet evaluated to a success</returns>
        /// </summary>
        public static bool MediateRuleTreeExecution(WonkaBizRuleSet poRootRuleSet,
                                                    WonkaProduct poIncomingProduct,
                                                    WonkaProduct poCurrentProduct,
                                                    WonkaBizRuleTreeReport poRuleTreeReport)
        {
            bool bRuleTreeResult = true;

            bRuleTreeResult =
                MediateRuleSetExecution(poRootRuleSet, poIncomingProduct, poCurrentProduct, poRuleTreeReport);

            // NOTE: Should we do anything else here?

            return(bRuleTreeResult);
        }
Ejemplo n.º 8
0
        public static WonkaBizRuleSet AddNewRuleSet(this WonkaBizRuleSet poRuleSet,
                                                    string psAddRuleSetDesc,
                                                    string psAddRuleSetTypeNum,
                                                    string psAddRuleSetErrorLvlNum)
        {
            int nRuleSetTypeNum   = Int32.Parse(psAddRuleSetTypeNum);
            int nRuleSetErrLvlNum = Int32.Parse(psAddRuleSetErrorLvlNum);

            WonkaBizRuleSet NewRuleSet = null;

            RULE_OP          rulesOp  = RULE_OP.OP_NONE;
            RULE_SET_ERR_LVL errLevel = RULE_SET_ERR_LVL.ERR_LVL_NONE;

            if (String.IsNullOrEmpty(psAddRuleSetDesc))
            {
                throw new DataException("ERROR!  Cannot add RuleSet without a description.");
            }

            if (nRuleSetTypeNum == 1)
            {
                rulesOp = RULE_OP.OP_AND;
            }
            else
            {
                rulesOp = RULE_OP.OP_OR;
            }

            if (nRuleSetErrLvlNum == 1)
            {
                errLevel = RULE_SET_ERR_LVL.ERR_LVL_WARNING;
            }
            else
            {
                errLevel = RULE_SET_ERR_LVL.ERR_LVL_SEVERE;
            }

            NewRuleSet = new WonkaBizRuleSet(mnRuleSetCounter++)
            {
                Description = psAddRuleSetDesc
            };

            NewRuleSet.RulesEvalOperator = rulesOp;
            NewRuleSet.ErrorSeverity     = errLevel;

            poRuleSet.AddChildRuleSet(NewRuleSet);

            return(NewRuleSet);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// This method will parse the XML contents (provided via the constructor) and create a RuleTree
        /// based on the parsed data.
        ///
        /// <returns>The RuleTree that is represented via the provided XML markup</returns>
        /// </summary>
        public WonkaBizRuleSet ParseRuleTree()
        {
            WonkaBizRuleSet newRootRuleSet = new WonkaBizRuleSet();

            newRootRuleSet.RuleSetId   = ++this.RuleSetIdCounter;
            newRootRuleSet.Description = "Root";

            this.RootRuleSet = newRootRuleSet;
            this.AllParsedRuleSets.Add(newRootRuleSet);

            XmlDocument xmlDoc = new XmlDocument();

            if (this.BreXmlFilepath != null)
            {
                xmlDoc.Load(this.BreXmlFilepath);
            }
            else
            {
                using (var ruleReader = new StringReader(this.BreXmlContents))
                {
                    xmlDoc.Load(ruleReader);
                }
            }

            XmlNode rootNode = xmlDoc.LastChild;

            XmlNodeList firstTierList = rootNode.ChildNodes;

            foreach (XmlNode firstTierNode in firstTierList)
            {
                if (firstTierNode.LocalName == CONST_RS_FLOW_TAG)
                {
                    WonkaBizRuleSet newChildRuleSet = this.ParseRuleSet(firstTierNode);

                    newChildRuleSet.ParentRuleSetId = newRootRuleSet.RuleSetId;

                    newRootRuleSet.AddChildRuleSet(newChildRuleSet);
                }
            }

            return(newRootRuleSet);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///
        /// This constructor should only be used when we call Splinter() to break up a current RuleTree
        /// and form a new Grove with the main child branches under the root.  Several assumptions are then
        /// assumed here, like an instantiation of the WonkaRefEnvironment taking place already.
        ///
        ///
        /// </summary>
        public WonkaBizRulesEngine(WonkaBizRuleSet poRootRuleSet, WonkaBizRulesEngine poRefEngine)
        {
            UsingOrchestrationMode = true;
            AddToRegistry          = false;

            RefEnvHandle = Init(null);

            AllRuleSets  = new List <WonkaBizRuleSet>();
            RuleTreeRoot = poRootRuleSet;
            SourceMap    = poRefEngine.SourceMap;
            CustomOpMap  = poRefEngine.CustomOpMap;

            AllRuleSets.Add(poRootRuleSet);
            foreach (WonkaBizRuleSet TmpBizRuleSet in poRootRuleSet.ChildRuleSets)
            {
                AddRuleSets(TmpBizRuleSet);
            }

            this.RetrieveCurrRecord = AssembleOtherProduct;
        }
Ejemplo n.º 11
0
        public static WonkaBizRuleSet FindRuleSet(this WonkaBizRuleSet poTargetSet, string psSoughtDescName)
        {
            WonkaBizRuleSet FoundRuleSet = new WonkaBizRuleSet();

            foreach (var TmpChildSet in poTargetSet.ChildRuleSets)
            {
                if (TmpChildSet.Description == psSoughtDescName)
                {
                    return(TmpChildSet);
                }
                else
                {
                    FoundRuleSet = FindRuleSet(TmpChildSet, psSoughtDescName);
                    if (IsValidRuleSet(FoundRuleSet))
                    {
                        break;
                    }
                }
            }

            return(FoundRuleSet);
        }
Ejemplo n.º 12
0
        public static WonkaBizRuleSet FindRuleSet(this WonkaBizRuleSet poTargetSet, int pnTargetRuleSetId)
        {
            WonkaBizRuleSet FoundRuleSet = new WonkaBizRuleSet();

            foreach (var TmpChildSet in poTargetSet.ChildRuleSets)
            {
                if (TmpChildSet.RuleSetId == pnTargetRuleSetId)
                {
                    return(TmpChildSet);
                }
                else
                {
                    FoundRuleSet = FindRuleSet(TmpChildSet, pnTargetRuleSetId);
                    if (IsValidRuleSet(FoundRuleSet))
                    {
                        break;
                    }
                }
            }

            return(FoundRuleSet);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///
        /// This method will flag the failure of a particular RuleSet by
        ///
        /// 1.) Setting the error properties on the corresponding RuleSetReport
        /// 2.) Adding the RuleSetReport to the Failures list
        /// 3.) Marking the RuleTree as a failure (since the failure of only one RuleSet flags the whole tree)
        ///
        /// <param name="poTargetRuleSet">The target RuleSet which will be marked as a failure</param>
        /// <param name="peRuleSetErrCd">The type of failure for the RuleSet</param>
        /// <param name="psRuleSetErrMsg">The error message that will provide details about the RuleSet's failure</param>
        /// <returns>The list of RuleReports that describes the execution of a specific RuleSet</returns>
        /// </summary>
        public bool AddResultSetFailure(WonkaBizRuleSet poTargetRuleSet, ERR_CD peRuleSetErrCd, string psRuleSetErrMsg)
        {
            bool bResult = true;

            WonkaBizRuleSetReportNode RuleSetReportNode = FindRuleSetReport(poTargetRuleSet.RuleSetId, false);

            if (RuleSetReportNode != null)
            {
                RuleSetReportNode.ErrorSeverity    = poTargetRuleSet.ErrorSeverity;
                RuleSetReportNode.ErrorCode        = peRuleSetErrCd;
                RuleSetReportNode.ErrorDescription = psRuleSetErrMsg;

                if ((poTargetRuleSet.ErrorSeverity == RULE_SET_ERR_LVL.ERR_LVL_WARNING) ||
                    (poTargetRuleSet.ErrorSeverity == RULE_SET_ERR_LVL.ERR_LVL_SEVERE))
                {
                    RuleSetFailures.Add(RuleSetReportNode);

                    OverallRuleTreeResult = ERR_CD.CD_FAILURE;
                }
            }

            return(bResult);
        }
Ejemplo n.º 14
0
        private void ParseSingleRule(XmlNode poRuleXmlNode, WonkaBizRuleSet poTargetRuleSet)
        {
            int    nNewRuleId      = ++(this.RuleIdCounter);
            string sRuleExpression = poRuleXmlNode.InnerText;

            WonkaBizRule NewRule = null;

            if (this.CustomOpSources.Keys.Any(s => sRuleExpression.Contains(s)))
            {
                string sFoundKey = this.CustomOpSources.Keys.FirstOrDefault(s => sRuleExpression.Contains(s));

                if (!String.IsNullOrEmpty(sFoundKey) && (this.CustomOpSources[sFoundKey].CustomOpRuleBuilder != null))
                {
                    WonkaBizSource CustomOpSource = this.CustomOpSources[sFoundKey];

                    NewRule = CustomOpSource.CustomOpRuleBuilder.Invoke(CustomOpSource, nNewRuleId);
                }
                else
                {
                    NewRule = new CustomOperatorRule()
                    {
                        RuleId = nNewRuleId
                    };
                }
            }
            else if (this.ArithmeticLimitOps.Any(s => sRuleExpression.Contains(s)))
            {
                NewRule = new ArithmeticLimitRule()
                {
                    RuleId = nNewRuleId
                };
            }
            else if (this.DateLimitOps.Any(s => sRuleExpression.Contains(s)))
            {
                NewRule = new DateLimitRule()
                {
                    RuleId = nNewRuleId
                };
            }
            else if (sRuleExpression.Contains("NOT POPULATED"))
            {
                NewRule = new PopulatedRule()
                {
                    RuleId = nNewRuleId, NotOperator = true
                };
            }
            else if (sRuleExpression.Contains("POPULATED"))
            {
                NewRule = new PopulatedRule()
                {
                    RuleId = nNewRuleId, NotOperator = false
                };
            }
            else if (sRuleExpression.Contains("!="))
            {
                NewRule = new DomainRule()
                {
                    RuleId = nNewRuleId, NotOperator = true
                };
            }
            else if (sRuleExpression.Contains("=="))
            {
                NewRule = new DomainRule()
                {
                    RuleId = nNewRuleId, NotOperator = false
                };
            }
            else if (sRuleExpression.Contains("NOT IN"))
            {
                NewRule = new DomainRule()
                {
                    RuleId = nNewRuleId, NotOperator = true
                };
            }
            else if (sRuleExpression.Contains("IN"))
            {
                NewRule = new DomainRule()
                {
                    RuleId = nNewRuleId, NotOperator = false
                };
            }
            else if (sRuleExpression.Contains("EXISTS AS"))
            {
                NewRule = new DomainRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, SearchAllDataRows = true
                };
            }
            else if (sRuleExpression.Contains("DEFAULT"))
            {
                NewRule = new AssignmentRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, DefaultAssignment = true
                };
            }
            else if (sRuleExpression.Contains("ASSIGN_SUM"))
            {
                NewRule = new ArithmeticRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, OpType = ARITH_OP_TYPE.AOT_SUM
                };
            }
            else if (sRuleExpression.Contains("ASSIGN_DIFF"))
            {
                NewRule = new ArithmeticRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, OpType = ARITH_OP_TYPE.AOT_DIFF
                };
            }
            else if (sRuleExpression.Contains("ASSIGN_PROD"))
            {
                NewRule = new ArithmeticRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, OpType = ARITH_OP_TYPE.AOT_PROD
                };
            }
            else if (sRuleExpression.Contains("ASSIGN_QUOT"))
            {
                NewRule = new ArithmeticRule()
                {
                    RuleId = nNewRuleId, NotOperator = false, OpType = ARITH_OP_TYPE.AOT_QUOT
                };
            }
            else if (sRuleExpression.Contains("ASSIGN"))
            {
                NewRule = new AssignmentRule()
                {
                    RuleId = nNewRuleId, NotOperator = false
                };
            }

            if (NewRule != null)
            {
                var RuleId = poRuleXmlNode.Attributes.GetNamedItem(CONST_RULE_ID_ATTR);
                if (RuleId != null)
                {
                    NewRule.DescRuleId = RuleId.Value;
                }

                NewRule.ParentRuleSetId = poTargetRuleSet.RuleSetId;

                SetTargetAttribute(NewRule, sRuleExpression);

                if (NewRule.RuleType != RULE_TYPE.RT_POPULATED)
                {
                    SetRuleValues(NewRule, sRuleExpression);
                }

                if (RulesHostEngine != null)
                {
                    NewRule.RulesHostEngine = RulesHostEngine;

                    if (RulesHostEngine.StdOpMap != null)
                    {
                        if ((NewRule is ArithmeticLimitRule) && RulesHostEngine.StdOpMap.ContainsKey(STD_OP_TYPE.STD_OP_BLOCK_NUM))
                        {
                            ((ArithmeticLimitRule)NewRule).BlockNumDelegate = RulesHostEngine.StdOpMap[STD_OP_TYPE.STD_OP_BLOCK_NUM];
                        }
                    }
                }
            }

            if (NewRule != null)
            {
                poTargetRuleSet.AddRule(NewRule);
            }
        }
Ejemplo n.º 15
0
        private void AddRuleSets(WonkaBizRuleSet poTargetRuleSet)
        {
            AllRuleSets.Add(poTargetRuleSet);

            poTargetRuleSet.ChildRuleSets.ForEach(x => AddRuleSets(x));
        }
Ejemplo n.º 16
0
        public void HandleEvents(WonkaBizRulesEngine poRulesEngine, RuleTreeReport poRuleTreeReport)
        {
            var ruleTreeLog   = RuleTreeEvents.GetFilterChanges <CallRuleTreeEvent>(RuleTreeEventFilter).Result;
            var ruleSetLog    = RuleSetEvents.GetFilterChanges <CallRuleSetEvent>(RuleSetEventFilter).Result;
            var ruleLog       = RuleEvents.GetFilterChanges <CallRuleEvent>(RuleEventFilter).Result;
            var ruleSetErrLog = RuleSetErrorEvents.GetFilterChanges <RuleSetErrorEvent>(RuleSetErrorEventFilter).Result;

            /**
             * if (ruleTreeLog.Count > 0)
             *  System.Console.WriteLine("RuleTree Called that Belongs to : (" + ruleTreeLog[0].Event.TreeOwner + ")");
             **/

            if (ruleSetLog.Count > 0)
            {
                foreach (EventLog <CallRuleSetEvent> TmpRuleSetEvent in ruleSetLog)
                {
                    if (TmpRuleSetEvent.Event != null)
                    {
                        poRuleTreeReport.RuleSetIds.Add(TmpRuleSetEvent.Event.RuleSetId);
                    }
                }
            }

            if (ruleLog.Count > 0)
            {
                foreach (EventLog <CallRuleEvent> TmpRuleEvent in ruleLog)
                {
                    if (TmpRuleEvent.Event != null)
                    {
                        poRuleTreeReport.RuleIds.Add(TmpRuleEvent.Event.RuleId); // TmpRuleEvent.Event.RuleType
                    }
                }
            }

            if (ruleSetErrLog.Count > 0)
            {
                foreach (EventLog <RuleSetErrorEvent> TmpRuleSetError in ruleSetErrLog)
                {
                    if (TmpRuleSetError.Event != null)
                    {
                        if (TmpRuleSetError.Event.SevereFailure)
                        {
                            poRuleTreeReport.RuleSetFailures.Add(TmpRuleSetError.Event.RuleSetId);
                        }
                        else
                        {
                            poRuleTreeReport.RuleSetWarnings.Add(TmpRuleSetError.Event.RuleSetId);
                        }
                    }
                }
            }

            if (poRulesEngine.AllRuleSets != null)
            {
                foreach (string sTmpCustomId in poRuleTreeReport.RuleSetFailures)
                {
                    WonkaBizRuleSet FoundRuleSet =
                        poRulesEngine.AllRuleSets.Where(x => x.CustomId == sTmpCustomId).FirstOrDefault();

                    if (!String.IsNullOrEmpty(FoundRuleSet.CustomId))
                    {
                        poRuleTreeReport.RuleSetFailMessages[FoundRuleSet.CustomId] = FoundRuleSet.CustomFailureMsg;
                    }
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        ///
        /// This method will apply the Rules of the RuleSet to either one or both of the incoming Product record
        /// and the current Product record.  The collective result of those applied Rules will then determine
        /// whether or not the RuleSet was evaluated as a success.
        ///
        /// <param name="poTargetRuleSet">The RuleSet whose rules are being applied against the provided records</param>
        /// <param name="poIncomingProduct">The incoming Product record</param>
        /// <param name="poCurrentProduct">The current Product record (i.e., in the database)</param>
        /// <param name="poRuleTreeReport">The report that will contain all evaluations of RuleSets in the RuleTree</param>
        /// <param name="poRuleSetErrorMessage">The buffer that will contain an error message if the 'poTargetRuleSet' fails</param>
        /// <returns>Indicates whether or not the RuleSet evaluated to a success</returns>
        /// </summary>
        private static bool MediateRulesExecution(WonkaBizRuleSet poTargetRuleSet,
                                                  WonkaProduct poIncomingProduct,
                                                  WonkaProduct poCurrentProduct,
                                                  WonkaBizRuleTreeReport poRuleTreeReport,
                                                  StringBuilder poRuleSetErrorMessage)
        {
            bool bRuleSetResult = true;

            StringBuilder RuleErrorMsgBuilder = new StringBuilder();
            List <bool>   RuleResultList      = new List <bool>();

            foreach (WonkaBizRule TempRule in poTargetRuleSet.EvaluativeRules)
            {
                bool   bRuleResult      = true;
                string sRuleResult      = string.Empty;
                string sFinalRuleErrMsg = string.Empty;

                RuleErrorMsgBuilder.Clear();
                bRuleResult = TempRule.Execute(poIncomingProduct, poCurrentProduct, RuleErrorMsgBuilder);

                if (TempRule.IsPassive)
                {
                    if (TempRule.NotOperator)
                    {
                        bRuleResult = !bRuleResult;
                    }
                }

                RuleResultList.Add(bRuleResult);

                if (bRuleResult)
                {
                    poRuleSetErrorMessage.Append("SUCCESS");
                }
                else
                {
                    var RuleSetReport =
                        poRuleTreeReport.FindRuleSetReport(poTargetRuleSet.RuleSetId, true);

                    if (RuleSetReport != null)
                    {
                        if (poTargetRuleSet.ErrorSeverity == RULE_SET_ERR_LVL.ERR_LVL_SEVERE)
                        {
                            sRuleResult = "SEVERE";
                            RuleSetReport.SevereFailureCount++;
                        }
                        else
                        {
                            sRuleResult = "WARNING";
                            RuleSetReport.WarningFailureCount++;
                        }
                    }

                    sFinalRuleErrMsg = RuleErrorMsgBuilder.ToString() + " / " + poTargetRuleSet.CustomFailureMsg;
                }

                poRuleTreeReport.ArchiveRuleExecution(TempRule,
                                                      bRuleResult ? ERR_CD.CD_SUCCESS : ERR_CD.CD_FAILURE,
                                                      sRuleResult,
                                                      sFinalRuleErrMsg);
            }

            // Calculate the final outcome of the RuleSet
            if (RuleResultList.Count > 0)
            {
                bRuleSetResult = RuleResultList[0];

                foreach (bool bTempRuleResult in RuleResultList)
                {
                    if (poTargetRuleSet.RulesEvalOperator == RULE_OP.OP_AND)
                    {
                        bRuleSetResult = bRuleSetResult && bTempRuleResult;
                    }
                    else if (poTargetRuleSet.RulesEvalOperator == RULE_OP.OP_OR)
                    {
                        bRuleSetResult = bRuleSetResult || bTempRuleResult;
                    }
                    else
                    {
                        bRuleSetResult = bRuleSetResult && bTempRuleResult;
                    }
                }
            }

            // Only apply the assertive rules if the evaluative rules are applied successfully
            if (bRuleSetResult && (poTargetRuleSet.AssertiveRules.Count() > 0))
            {
                foreach (WonkaBizRule TempRule in poTargetRuleSet.AssertiveRules)
                {
                    bool bRuleResult = true;

                    string sRuleResult      = string.Empty;
                    string sFinalRuleErrMsg = string.Empty;

                    RuleErrorMsgBuilder.Clear();
                    bRuleResult = TempRule.Execute(poIncomingProduct, poCurrentProduct, RuleErrorMsgBuilder);

                    RuleResultList.Add(bRuleResult);

                    poRuleSetErrorMessage.Append("SUCCESS");

                    poRuleTreeReport.ArchiveRuleExecution(TempRule,
                                                          bRuleResult ? ERR_CD.CD_SUCCESS : ERR_CD.CD_FAILURE,
                                                          sRuleResult,
                                                          sFinalRuleErrMsg);
                }
            }

            return(bRuleSetResult);
        }
Ejemplo n.º 18
0
 public static bool IsValidRuleSet(this WonkaBizRuleSet poTargetRuleSet)
 {
     return((poTargetRuleSet.ChildRuleSets.Count > 0) || (poTargetRuleSet.AssertiveRules.Count > 0) || (poTargetRuleSet.EvaluativeRules.Count > 0));
 }
Ejemplo n.º 19
0
        public static void RemoveRuleById(this WonkaBizRuleSet poTargetSet, string psSoughtId)
        {
            poTargetSet.EvaluativeRules.RemoveAll(x => x.DescRuleId == psSoughtId);

            poTargetSet.AssertiveRules.RemoveAll(x => x.DescRuleId == psSoughtId);
        }
Ejemplo n.º 20
0
        public static string GetRuleSetDescription(this WonkaBizRuleSet targetRuleSet)
        {
            StringBuilder RuleSetDesc = new StringBuilder(targetRuleSet.Description);

            return(RuleSetDesc.ToString());
        }
Ejemplo n.º 21
0
        ///
        /// <summary>
        ///
        /// This method will write the XML (i.e., Wonka rules markup) of a RuleSet.
        ///
        /// NOTE: Currently, we use a StringBuilder class to build the XML Document.  In the future, we should transition to
        /// using a XmlDocument and a XmlWriter.
        ///
        /// <param name="poRuleSet">The rules engine which holds the RuleTree of interest</param>
        /// <param name="pnStepLevel">The level of the RuleTree</param>
        /// <returns>Returns the XML payload that represents a RuleSet</returns>
        /// </summary>
        private string ExportXmlString(WonkaBizRuleSet poRuleSet, uint pnStepLevel)
        {
            var RSNodeTag   = WonkaBizRulesXmlReader.CONST_RS_FLOW_TAG;
            var RSNodeDesc  = WonkaBizRulesXmlReader.CONST_RS_FLOW_DESC_ATTR;
            var RSLeafTag   = WonkaBizRulesXmlReader.CONST_RS_VALID_TAG;
            var RSLeafMode  = WonkaBizRulesXmlReader.CONST_RS_VALID_ERR_ATTR;
            var RuleCollTag = WonkaBizRulesXmlReader.CONST_RULES_TAG;
            var LogicOp     = WonkaBizRulesXmlReader.CONST_RULES_OP_ATTR;

            StringBuilder sbExportXmlString = new StringBuilder();
            StringBuilder sbTabSpaces       = new StringBuilder();
            StringBuilder sbCritSpaces      = new StringBuilder();
            StringBuilder sbRuleSpaces      = new StringBuilder();

            for (uint x = 0; x < pnStepLevel; x++)
            {
                sbTabSpaces.Append("    ");
            }

            sbCritSpaces.Append(sbTabSpaces.ToString()).Append("    ");
            sbRuleSpaces.Append(sbCritSpaces.ToString()).Append("    ");

            if (!poRuleSet.Description.StartsWith("Root", StringComparison.CurrentCultureIgnoreCase))
            {
                if (poRuleSet.ChildRuleSets.Count > 0)
                {
                    sbExportXmlString.Append(sbTabSpaces.ToString()).Append("<" + RSNodeTag + " " + RSNodeDesc + "=\"" + poRuleSet.Description + "\" >\n");
                }
                else
                {
                    string sMode =
                        (poRuleSet.ErrorSeverity == RULE_SET_ERR_LVL.ERR_LVL_SEVERE) ? WonkaBizRulesXmlReader.CONST_RS_VALID_ERR_SEVERE : WonkaBizRulesXmlReader.CONST_RS_VALID_ERR_WARNING;

                    sbExportXmlString.Append(sbTabSpaces.ToString()).Append("<" + RSLeafTag + " " + RSLeafMode + "=\"" + sMode + "\" >\n");
                }

                sbExportXmlString.Append(sbCritSpaces.ToString());
                sbExportXmlString.Append("<" + RuleCollTag + " " + LogicOp + "=\"" + ((poRuleSet.RulesEvalOperator == RULE_OP.OP_AND) ? "AND" : "OR") + "\" >\n");

                if (poRuleSet.EvaluativeRules.Count > 0)
                {
                    StringBuilder sbRulesBody = new StringBuilder();
                    sbRulesBody.Append(sbTabSpaces.ToString()).Append(sbTabSpaces.ToString()).Append(sbTabSpaces.ToString());

                    for (int idx = 0; idx < poRuleSet.EvaluativeRules.Count; idx++)
                    {
                        sbExportXmlString.Append(ExportXmlString(poRuleSet.EvaluativeRules[idx], sbRuleSpaces));
                    }
                }

                if (poRuleSet.AssertiveRules.Count > 0)
                {
                    StringBuilder sbRulesBody = new StringBuilder();
                    sbRulesBody.Append(sbTabSpaces.ToString()).Append(sbTabSpaces.ToString()).Append(sbTabSpaces.ToString());

                    for (int idx = 0; idx < poRuleSet.AssertiveRules.Count; idx++)
                    {
                        sbExportXmlString.Append(ExportXmlString(poRuleSet.AssertiveRules[idx], sbRuleSpaces));
                    }
                }

                sbExportXmlString.Append(sbCritSpaces.ToString());
                sbExportXmlString.Append("</" + RuleCollTag + ">\n");
            }

            // Now invoke the rulesets
            for (int childIdx = 0; childIdx < poRuleSet.ChildRuleSets.Count; childIdx++)
            {
                sbExportXmlString.Append(ExportXmlString(poRuleSet.ChildRuleSets[childIdx], pnStepLevel + 1));
            }

            if (!poRuleSet.Description.StartsWith("Root", StringComparison.CurrentCultureIgnoreCase))
            {
                if (poRuleSet.ChildRuleSets.Count > 0)
                {
                    sbExportXmlString.Append(sbTabSpaces.ToString()).Append("</" + RSNodeTag + ">\n");
                }
                else
                {
                    sbExportXmlString.Append(sbTabSpaces.ToString()).Append("</" + RSLeafTag + ">\n");
                }
            }

            return(sbExportXmlString.ToString());
        }
Ejemplo n.º 22
0
        public static void AddNewNethereumRule(this WonkaBizRuleSet poRuleSet,
                                               WonkaRefEnvironment poRefEnv,
                                               string psAddRuleDesc,
                                               string psAddRuleTargetAttr,
                                               string psAddRuleTypeNum,
                                               string psAddRuleEthAddress,
                                               string psAddRuleValue1,
                                               string psAddRuleValue2)
        {
            if (String.IsNullOrEmpty(psAddRuleTypeNum))
            {
                psAddRuleTypeNum = "1";
            }

            int nRuleTypeNum = Int32.Parse(psAddRuleTypeNum);

            WonkaBizRule NewRule = null;

            WonkaRefAttr targetAttr = poRefEnv.GetAttributeByAttrName(psAddRuleTargetAttr);

            if ((nRuleTypeNum == 1) | (nRuleTypeNum == 2) || (nRuleTypeNum == 5))
            {
                if (targetAttr.IsNumeric || targetAttr.IsDecimal)
                {
                    throw new DataException("ERROR!  Cannot perform offered Nethereum rules on a numeric value.");
                }

                if (String.IsNullOrEmpty(psAddRuleEthAddress))
                {
                    psAddRuleEthAddress = BlazorAppNethereumExtensions.CONST_ETH_FNDTN_EOA_ADDRESS;
                }
            }

            if (nRuleTypeNum == 1)
            {
                if (targetAttr.AttrName != "AccountStatus")
                {
                    throw new Exception("ERROR!  Cannot add BALANCE_WITHIN_RANGE rule with any attribute target other than AccountStatus.");
                }

                WonkaBizSource DummySource =
                    new WonkaBizSource("EF_EOA", psAddRuleEthAddress, "", "", "", "", "", null);

                CustomOperatorRule CustomOpRule =
                    new CustomOperatorRule(mnRuleCounter++,
                                           TARGET_RECORD.TRID_NEW_RECORD,
                                           targetAttr.AttrId,
                                           "BALANCE_WITHIN_RANGE",
                                           BlazorAppNethereumExtensions.CheckBalanceIsWithinRange,
                                           DummySource);

                CustomOpRule.AddDomainValue(psAddRuleEthAddress, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(psAddRuleValue1, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(psAddRuleValue2, true, TARGET_RECORD.TRID_NONE);

                NewRule = CustomOpRule;
            }
            else if (nRuleTypeNum == 2)
            {
                if (targetAttr.AttrName != "AuditReviewFlag")
                {
                    throw new Exception("ERROR!  Cannot add ANY_EVENTS_IN_BLOCK_RANGE rule with any attribute target other than AuditReviewFlag.");
                }

                WonkaBizSource DummySource =
                    new WonkaBizSource("ERC20", psAddRuleEthAddress, "", "", "", "", "", null);

                CustomOperatorRule CustomOpRule =
                    new CustomOperatorRule(mnRuleCounter++,
                                           TARGET_RECORD.TRID_NEW_RECORD,
                                           targetAttr.AttrId,
                                           "ANY_EVENTS_IN_BLOCK_RANGE",
                                           BlazorAppNethereumExtensions.AnyEventsInBlockRange,
                                           DummySource);

                if (String.IsNullOrEmpty(psAddRuleValue1))
                {
                    psAddRuleValue1 = "8450678";
                }

                if (String.IsNullOrEmpty(psAddRuleValue2))
                {
                    psAddRuleValue2 = "8450698";
                }

                CustomOpRule.AddDomainValue(psAddRuleEthAddress, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(psAddRuleValue1, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(psAddRuleValue2, true, TARGET_RECORD.TRID_NONE);

                NewRule = CustomOpRule;
            }
            else if (nRuleTypeNum == 3)
            {
                if (!targetAttr.IsNumeric && !targetAttr.IsDecimal)
                {
                    throw new DataException("ERROR!  Cannot perform arithmetic limit on a non-numeric value.");
                }

                WonkaBizSource DummySource =
                    new WonkaBizSource("ERC20", psAddRuleEthAddress, "", "", "", "", "", null);

                CustomOperatorRule CustomOpRule =
                    new CustomOperatorRule(mnRuleCounter++,
                                           TARGET_RECORD.TRID_NEW_RECORD,
                                           targetAttr.AttrId,
                                           "GET_ERC20_BALANCE",
                                           BlazorAppNethereumExtensions.GetERC20Balance,
                                           DummySource);

                CustomOpRule.AddDomainValue(psAddRuleEthAddress, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(psAddRuleValue1, true, TARGET_RECORD.TRID_NONE);

                NewRule = CustomOpRule;
            }
            else if (nRuleTypeNum == 4)
            {
                if (!targetAttr.IsNumeric && !targetAttr.IsDecimal)
                {
                    throw new DataException("ERROR!  Cannot perform arithmetic limit on a non-numeric value.");
                }

                WonkaBizSource DummySource =
                    new WonkaBizSource("ContractName", psAddRuleEthAddress, "", "", "", "", "", null);

                CustomOperatorRule CustomOpRule =
                    new CustomOperatorRule(mnRuleCounter++,
                                           TARGET_RECORD.TRID_NEW_RECORD,
                                           targetAttr.AttrId,
                                           "CALL_SIMPLE_CONTRACT_METHOD",
                                           BlazorAppNethereumExtensions.GetContractSimpleMethodValue,
                                           DummySource);

                var sAltConfigIpfsUrl = psAddRuleValue1;
                var sFunctionName     = psAddRuleValue2;

                if (String.IsNullOrEmpty(sAltConfigIpfsUrl))
                {
                    sAltConfigIpfsUrl = BlazorAppNethereumExtensions.CONST_INFURA_IPFS_GATEWAY_URL + "/QmYDp4ocbF1AVSuY1zBhXa6P4c2oaPkHi2jaSE3HU6bQnQ";
                }

                using (var client = new System.Net.Http.HttpClient())
                {
                    var sConfigDataXml = client.GetStringAsync(sAltConfigIpfsUrl).Result;

                    var configData = ReadConfigXml(sConfigDataXml);

                    if (!String.IsNullOrEmpty(configData.HostUrl))
                    {
                        CustomOpRule.AddDomainValue(configData.HostUrl, true, TARGET_RECORD.TRID_NONE);
                    }
                    else
                    {
                        CustomOpRule.AddDomainValue(BlazorAppNethereumExtensions.CONST_TEST_INFURA_URL, true, TARGET_RECORD.TRID_NONE);
                    }

                    CustomOpRule.AddDomainValue(configData.ContractABI, true, TARGET_RECORD.TRID_NONE);

                    if (!String.IsNullOrEmpty(psAddRuleEthAddress))
                    {
                        CustomOpRule.AddDomainValue(psAddRuleEthAddress, true, TARGET_RECORD.TRID_NONE);
                    }
                    else
                    {
                        CustomOpRule.AddDomainValue(configData.ContractAddress, true, TARGET_RECORD.TRID_NONE);
                    }

                    CustomOpRule.AddDomainValue(sFunctionName, true, TARGET_RECORD.TRID_NONE);

                    NewRule = CustomOpRule;
                }
            }
            else if (nRuleTypeNum == 5)
            {
                if (targetAttr.AttrName != "AccountStatus")
                {
                    throw new Exception("ERROR!  Cannot add V rule with any attribute target other than AccountStatus.");
                }

                WonkaBizSource DummySource =
                    new WonkaBizSource("ContractName", psAddRuleEthAddress, "", "", "", "", "", null);

                CustomOperatorRule CustomOpRule =
                    new CustomOperatorRule(mnRuleCounter++,
                                           TARGET_RECORD.TRID_NEW_RECORD,
                                           targetAttr.AttrId,
                                           "VALIDATE_SIGNATURE",
                                           BlazorAppNethereumExtensions.DetermineStatusByValidatingSignature,
                                           DummySource);

                var sAttrTarget = psAddRuleValue1;
                var sSignature  = psAddRuleValue2;

                var TargetAttr = poRefEnv.GetAttributeByAttrName(sAttrTarget);
                if (TargetAttr == null)
                {
                    throw new Exception("ERROR!  Cannot add rule since attribute(" + sAttrTarget + ") does not exist!");
                }

                CustomOpRule.AddDomainValue(psAddRuleEthAddress, true, TARGET_RECORD.TRID_NONE);
                CustomOpRule.AddDomainValue(sAttrTarget, false, TARGET_RECORD.TRID_NEW_RECORD);
                CustomOpRule.AddDomainValue(sSignature, true, TARGET_RECORD.TRID_NONE);

                NewRule = CustomOpRule;
            }

            if (NewRule != null)
            {
                if (!String.IsNullOrEmpty(psAddRuleDesc))
                {
                    NewRule.DescRuleId = psAddRuleDesc;
                }

                poRuleSet.AddRule(NewRule);
            }
        }
Ejemplo n.º 23
0
        public static void AddNewRule(this WonkaBizRuleSet poRuleSet,
                                      WonkaRefEnvironment poRefEnv,
                                      string psAddRuleDesc,
                                      string psAddRuleTargetAttr,
                                      string psAddRuleTypeNum,
                                      string psAddRuleValue1,
                                      string psAddRuleValue2,
                                      bool pbAddRuleNotOp = false)
        {
            int nRuleTypeNum = Int32.Parse(psAddRuleTypeNum);

            WonkaBizRule NewRule = null;

            WonkaRefAttr targetAttr = poRefEnv.GetAttributeByAttrName(psAddRuleTargetAttr);

            if (nRuleTypeNum == 1)
            {
                if (!targetAttr.IsNumeric && !targetAttr.IsDecimal)
                {
                    throw new DataException("ERROR!  Cannot perform arithmetic limit on a non-numeric value.");
                }

                double dMinVal = 0;
                double dMaxVal = 0;

                Double.TryParse(psAddRuleValue1, out dMinVal);
                Double.TryParse(psAddRuleValue2, out dMaxVal);

                NewRule =
                    new ArithmeticLimitRule(mnRuleCounter++, TARGET_RECORD.TRID_NEW_RECORD, targetAttr.AttrId, dMinVal, dMaxVal);
            }
            else if (nRuleTypeNum == 2)
            {
                /*
                 * NOTE: Will handle ArithmeticRule later
                 * string[] asParamArray = new string[0];
                 */
            }
            else if (nRuleTypeNum == 3)
            {
                if (!String.IsNullOrEmpty(psAddRuleValue1))
                {
                    NewRule =
                        new AssignmentRule(mnRuleCounter++, TARGET_RECORD.TRID_NEW_RECORD, targetAttr.AttrId, psAddRuleValue1);
                }
            }
            else if (nRuleTypeNum == 4)
            {
                /*
                 * NOTE: Will handle CustomOperatorRule later
                 */
            }
            else if (nRuleTypeNum == 5)
            {
                if (!targetAttr.IsDate)
                {
                    throw new DataException("ERROR!  Cannot perform date limit on a non-date value.");
                }

                if ((!String.IsNullOrEmpty(psAddRuleValue1)) || (!String.IsNullOrEmpty(psAddRuleValue2)))
                {
                    DateTime dtMinTime = DateTime.MinValue;
                    DateTime dtMaxTime = DateTime.MaxValue;

                    if (!String.IsNullOrEmpty(psAddRuleValue1))
                    {
                        DateTime.TryParse(psAddRuleValue1, out dtMinTime);
                    }

                    if (!String.IsNullOrEmpty(psAddRuleValue2))
                    {
                        DateTime.TryParse(psAddRuleValue2, out dtMaxTime);
                    }

                    var DtLimitRule =
                        new DateLimitRule(mnRuleCounter++)
                    {
                        MinValue = dtMinTime, MaxValue = dtMaxTime, TargetAttribute = targetAttr
                    };

                    NewRule = DtLimitRule;
                }
            }
            else if (nRuleTypeNum == 6)
            {
                if ((!String.IsNullOrEmpty(psAddRuleValue1)) || (!String.IsNullOrEmpty(psAddRuleValue2)))
                {
                    HashSet <string> RuleDomain = new HashSet <string>();

                    if (!String.IsNullOrEmpty(psAddRuleValue1))
                    {
                        if (psAddRuleValue1.Contains(","))
                        {
                            var DomainVals = psAddRuleValue1.Split(new char[1] {
                                ','
                            });
                            DomainVals.ToList().ForEach(x => DomainVals.Append(x));
                        }
                        else
                        {
                            RuleDomain.Add(psAddRuleValue1);
                        }
                    }

                    if (!String.IsNullOrEmpty(psAddRuleValue2))
                    {
                        if (psAddRuleValue2.Contains(","))
                        {
                            var DomainVals = psAddRuleValue2.Split(new char[1] {
                                ','
                            });
                            DomainVals.ToList().ForEach(x => DomainVals.Append(x));
                        }
                        else
                        {
                            RuleDomain.Add(psAddRuleValue2);
                        }
                    }

                    var DmnRule =
                        new DomainRule(mnRuleCounter++, TARGET_RECORD.TRID_NEW_RECORD, targetAttr.AttrId, false);

                    DmnRule.DomainCache = RuleDomain;

                    foreach (string sTmpValue in RuleDomain)
                    {
                        DmnRule.DomainValueProps.Add(sTmpValue, new WonkaBizRuleValueProps()
                        {
                            IsLiteralValue = true
                        });
                    }

                    NewRule = DmnRule;
                }
            }
            else if (nRuleTypeNum == 7)
            {
                NewRule = new PopulatedRule(mnRuleCounter++, TARGET_RECORD.TRID_NEW_RECORD, targetAttr.AttrId);
            }

            if (NewRule != null)
            {
                if (!String.IsNullOrEmpty(psAddRuleDesc))
                {
                    NewRule.DescRuleId = psAddRuleDesc;
                }

                NewRule.ParentRuleSetId = poRuleSet.RuleSetId;
                NewRule.NotOperator     = pbAddRuleNotOp;

                poRuleSet.AddRule(NewRule);
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        ///
        /// This method helps to direct the navigation of a branch within the RuleTree.  If the rules of 'poTargetRuleSet'
        /// evaluate to a success when applied to the records, then we will continue to traverse the branch by enumerating
        /// through the child branches that sprout from this node (i.e., by calling this method again recursively).  If not,
        /// we will stop the traversal here and return back to the parent node of 'poTargetRuleSet'.
        ///
        /// <param name="poTargetRuleSet">The RuleSet whose rules are being applied against the provided records</param>
        /// <param name="poIncomingProduct">The incoming Product record</param>
        /// <param name="poCurrentProduct">The current Product record (i.e., in the database)</param>
        /// <param name="poRuleTreeReport">The report that will contain all evaluations of RuleSets in the RuleTree</param>
        /// <returns>Indicates whether or not the RuleSet evaluated to a success</returns>
        /// </summary>
        private static bool MediateRuleSetExecution(WonkaBizRuleSet poTargetRuleSet,
                                                    WonkaProduct poIncomingProduct,
                                                    WonkaProduct poCurrentProduct,
                                                    WonkaBizRuleTreeReport poRuleTreeReport)
        {
            bool   bTotalRuleSetResult  = true;
            bool   bTempRulesResult     = true;
            bool   bTempRuleSetResult   = true;
            bool   bTraverseChildren    = true;
            string sGeneralRuleSetError = "ERROR!  One of the rules failed.";

            StringBuilder RuleSetErrorBuilder = new StringBuilder();

            poRuleTreeReport.LastRuleSetExecuted = poTargetRuleSet;

            /*
             * GOOD PLACE FOR TESTING
             *
             * if (!String.IsNullOrEmpty(poTargetRuleSet.Description))
             * {
             *  if (poTargetRuleSet.Description.Contains("Test RuleSet"))
             *  {
             *      int x = 1;
             *  }
             * }
             */

            bTempRulesResult =
                MediateRulesExecution(poTargetRuleSet,
                                      poIncomingProduct,
                                      poCurrentProduct,
                                      poRuleTreeReport,
                                      RuleSetErrorBuilder);

            if (bTempRulesResult)
            {
                poRuleTreeReport.SetRuleSetStatus(poTargetRuleSet.RuleSetId,
                                                  poTargetRuleSet.Description,
                                                  poTargetRuleSet.CustomId,
                                                  ERR_CD.CD_SUCCESS);
            }
            else
            {
                poRuleTreeReport.SetRuleSetStatus(poTargetRuleSet.RuleSetId,
                                                  poTargetRuleSet.Description,
                                                  poTargetRuleSet.CustomId,
                                                  ERR_CD.CD_FAILURE);

                var TargetRuleSetReport = poRuleTreeReport.FindRuleSetReport(poTargetRuleSet.RuleSetId);

                if ((TargetRuleSetReport.SevereFailureCount > 0) || (TargetRuleSetReport.WarningFailureCount > 0))
                {
                    poRuleTreeReport.AddResultSetFailure(poTargetRuleSet, ERR_CD.CD_FAILURE, sGeneralRuleSetError);
                }

                bTraverseChildren = false;
            }

            if (!bTraverseChildren)
            {
                // We should return here, preventing the recursion that will further traverse the branches of the RuleTree
                return(bTotalRuleSetResult);
            }

            foreach (WonkaBizRuleSet ChildRuleSet in poTargetRuleSet.ChildRuleSets)
            {
                bTempRuleSetResult =
                    MediateRuleSetExecution(ChildRuleSet, poIncomingProduct, poCurrentProduct, poRuleTreeReport);

                if (!bTempRuleSetResult)
                {
                    // NOTE: Currently, this condition will never happen...but at some point, should it?
                }
            }

            return(bTotalRuleSetResult);
        }