private void SetRuleValues(WonkaBizRule poTargetRule, string psRuleExpression) { char[] acRuleValuesDelim = new char[1] { ',' }; int nValueStartIdx = psRuleExpression.LastIndexOf(CONST_RULE_TOKEN_START_DELIM); if (nValueStartIdx >= 0) { int nValueEndIdx = psRuleExpression.IndexOf(CONST_RULE_TOKEN_END_DELIM, nValueStartIdx + 1); if (nValueEndIdx > 0) { string sValues = psRuleExpression.Substring(nValueStartIdx + 1, (nValueEndIdx - nValueStartIdx - 1)); string[] asValueSet = sValues.Split(acRuleValuesDelim); if (poTargetRule.RuleType == RULE_TYPE.RT_DOMAIN) { DomainRule Rule = (DomainRule)poTargetRule; Rule.SetDomain(asValueSet); } else if (poTargetRule.RuleType == RULE_TYPE.RT_ARITHMETIC) { ArithmeticRule Rule = (ArithmeticRule)poTargetRule; Rule.SetDomain(asValueSet); } else if (poTargetRule.RuleType == RULE_TYPE.RT_ARITH_LIMIT) { ArithmeticLimitRule Rule = (ArithmeticLimitRule)poTargetRule; Rule.SetMinAndMax(psRuleExpression, asValueSet); } else if (poTargetRule.RuleType == RULE_TYPE.RT_ASSIGNMENT) { AssignmentRule Rule = (AssignmentRule)poTargetRule; Rule.SetAssignValue(asValueSet); } else if (poTargetRule.RuleType == RULE_TYPE.RT_DATE_LIMIT) { DateLimitRule Rule = (DateLimitRule)poTargetRule; Rule.SetMinAndMax(psRuleExpression, asValueSet); } else if (poTargetRule.RuleType == RULE_TYPE.RT_CUSTOM_OP) { CustomOperatorRule Rule = (CustomOperatorRule)poTargetRule; string sCustomOpKey = CustomOpSources.Keys.Where(s => psRuleExpression.Contains(s)).FirstOrDefault(); if (!string.IsNullOrEmpty(sCustomOpKey)) { Rule.SetDomain(asValueSet); Rule.CustomOpName = sCustomOpKey; Rule.CustomOpSource = CustomOpSources[sCustomOpKey]; } } } } }
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); } }
/// /// <summary> /// /// This method will write the XML (i.e., Wonka rules markup) of a Rule. /// /// 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. /// /// <returns>Returns the XML payload that represents a Rule</returns> /// </summary> public string ExportXmlString(WonkaBizRule poRule, StringBuilder poSpaces) { string sOpName = string.Empty; string sRuleValue = ""; string sDelim = WonkaBizRulesXmlReader.CONST_RULE_TOKEN_VAL_DELIM; string sSingleQuote = "'"; string sRuleTagFormat = "{0}<" + WonkaBizRulesXmlReader.CONST_RULE_TAG + " " + WonkaBizRulesXmlReader.CONST_RULE_ID_ATTR + "=\"{1}\">(N.{2}) {3} {4}</eval>\n"; if (poRule.RuleType == RULE_TYPE.RT_ARITH_LIMIT) { ArithmeticLimitRule ArithLimitRule = (ArithmeticLimitRule)poRule; if (ArithLimitRule.MinValue == Double.MinValue) { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_AL_LT : WonkaBizRulesXmlReader.CONST_AL_NOT_LT; sRuleValue = Convert.ToString(ArithLimitRule.MaxValue); } else if (ArithLimitRule.MaxValue == Double.MaxValue) { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_AL_GT : WonkaBizRulesXmlReader.CONST_AL_NOT_GT; sRuleValue = Convert.ToString(ArithLimitRule.MinValue); } else { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_AL_EQ : WonkaBizRulesXmlReader.CONST_AL_NOT_EQ; sRuleValue = Convert.ToString(ArithLimitRule.MinValue); } } else if (poRule.RuleType == RULE_TYPE.RT_DATE_LIMIT) { DateLimitRule DtLimitRule = (DateLimitRule)poRule; if (DtLimitRule.MinValue == DateTime.MinValue) { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_DL_IA : WonkaBizRulesXmlReader.CONST_DL_NOT_IA; sRuleValue = DtLimitRule.MaxValue.ToString("MM/dd/yyyy"); } else if (DtLimitRule.MaxValue == DateTime.MaxValue) { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_DL_IB : WonkaBizRulesXmlReader.CONST_DL_NOT_IB; sRuleValue = DtLimitRule.MinValue.ToString("MM/dd/yyyy"); } else { sOpName = WonkaBizRulesXmlReader.CONST_DL_AROUND; sRuleValue = DtLimitRule.MinValue.ToString("MM/dd/yyyy"); } } else if (poRule.RuleType == RULE_TYPE.RT_POPULATED) { sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_BASIC_OP_POP : WonkaBizRulesXmlReader.CONST_BASIC_OP_NOT_POP; } else if (poRule.RuleType == RULE_TYPE.RT_DOMAIN) { StringBuilder DomainVals = new StringBuilder(); DomainRule DmnRule = (DomainRule)poRule; sOpName = !poRule.NotOperator ? WonkaBizRulesXmlReader.CONST_BASIC_OP_IN : WonkaBizRulesXmlReader.CONST_BASIC_OP_NOT_IN; sRuleValue = BuildDomainValues(DmnRule.DomainValueProps, sDelim, sSingleQuote); } else if (poRule.RuleType == RULE_TYPE.RT_ASSIGNMENT) { StringBuilder DomainVals = new StringBuilder(); AssignmentRule AssignRule = (AssignmentRule)poRule; sOpName = WonkaBizRulesXmlReader.CONST_BASIC_OP_ASSIGN; var ValueProps = new Dictionary <string, WonkaBizRuleValueProps>(); ValueProps[AssignRule.AssignValue] = AssignRule.AssignValueProps; sRuleValue = BuildDomainValues(ValueProps, sDelim, sSingleQuote); } else if (poRule.RuleType == RULE_TYPE.RT_ARITHMETIC) { ArithmeticRule ArithRule = (ArithmeticRule)poRule; if (ArithRule.OpType == ARITH_OP_TYPE.AOT_SUM) { sOpName = WonkaBizRulesXmlReader.CONST_BASIC_OP_ASSIGN_SUM; } else if (ArithRule.OpType == ARITH_OP_TYPE.AOT_DIFF) { sOpName = WonkaBizRulesXmlReader.CONST_BASIC_OP_ASSIGN_DIFF; } else if (ArithRule.OpType == ARITH_OP_TYPE.AOT_PROD) { sOpName = WonkaBizRulesXmlReader.CONST_BASIC_OP_ASSIGN_PROD; } else if (ArithRule.OpType == ARITH_OP_TYPE.AOT_QUOT) { sOpName = WonkaBizRulesXmlReader.CONST_BASIC_OP_ASSIGN_QUOT; } sRuleValue = BuildDomainValues(ArithRule.DomainValueProps, sDelim, sSingleQuote); } else if (poRule.RuleType == RULE_TYPE.RT_CUSTOM_OP) { CustomOperatorRule CustomOpRule = (CustomOperatorRule)poRule; sOpName = CustomOpRule.CustomOpName; sRuleValue = BuildDomainValues(CustomOpRule.DomainValueProps, sDelim, sSingleQuote); } else { throw new WonkaBizRuleException("ERROR! Unsupported Rule Type when writing out the Wonka RuleTree."); } if (!String.IsNullOrEmpty(sRuleValue)) { sRuleValue = "(" + sRuleValue + ")"; } return(String.Format(sRuleTagFormat, poSpaces.ToString(), poRule.DescRuleId, poRule.TargetAttribute.AttrName, sOpName, sRuleValue)); }
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); } }
private void ParseSingleRule(XmlNode poRuleXmlNode, WonkaBreRuleSet poTargetRuleSet) { int nNewRuleId = ++(this.RuleIdCounter); string sRuleExpression = poRuleXmlNode.InnerText; WonkaBreRule NewRule = null; if (this.CustomOpSources.Keys.Any(s => sRuleExpression.Contains(s))) { 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 (NewRule != null) { poTargetRuleSet.AddRule(NewRule); } }