private void Validate_Click(object sender, RibbonControlEventArgs e) { try { var thisCell = Globals.ThisAddIn.GetActiveCell(); var currAttrData = Globals.ThisAddIn.GetCurrentAttributeData(); WonkaProduct currProduct = AssembleProduct(currAttrData); var report = rulesEngine.Validate(currProduct); if (report.GetRuleSetSevereFailureCount() == 0) { var postAttrData = DisassembleProduct(currProduct); Globals.ThisAddIn.SetCurrentAttributeData(postAttrData); MessageBox.Show("SUCCESS!"); } else { string sErrors = GetErrors(report); MessageBox.Show("ERROR! " + sErrors); } } catch (WonkaBizRuleException bizEx) { MessageBox.Show("ERROR! Wonka Exception: " + bizEx); } catch (Exception ex) { MessageBox.Show("ERROR! Exception: " + ex); } }
public void Execute() { // Using the metadata source, we create an instance of a defined data domain WonkaRefEnvironment RefEnv = WonkaRefEnvironment.GetInstance(); // To test whether the data domain has been created, we plan on retrieving the value // of the "AccountStatus" Attribute WonkaRefAttr AccountStsAttr = RefEnv.GetAttributeByAttrName("AccountStatus"); // Creating an instance of the rules engine using our rules and the metadata WonkaBizRulesEngine RulesEngine = new WonkaBizRulesEngine(new StringBuilder(msRulesContents), moMetadataSource); // Gets a predefined data record that will be our analog for new data coming into the system WonkaProduct NewProduct = GetNewProduct(); // Check that the data has been populated correctly on the "new" record - also, we will use // it later for comparison purposes string sStatusValueBefore = GetAttributeValue(NewProduct, AccountStsAttr); // Since the rules can reference values from different records (like O.Price for the existing // record's price and N.Price for the new record's price), we need to provide the delegate // that can pull the existing (i.e., old) record from the blockchain using a key RulesEngine.GetCurrentProductDelegate = GetOldProduct; // Validate the new record using our rules engine and its initialized RuleTree Wonka.BizRulesEngine.Reporting.WonkaBizRuleTreeReport Report = RulesEngine.Validate(NewProduct); // Now retrieve the AccountStatus value and see if the rules have altered it (which should // not be the case) string sStatusValueAfter = GetAttributeValue(NewProduct, AccountStsAttr); // We will evaluate whether or not any failures of the rules were detected during the engine's execution if (Report.OverallRuleTreeResult == ERR_CD.CD_SUCCESS) { // If successful, we will write the record back into the contract on the blockchain Serialize(NewProduct); } else if (Report.GetRuleSetFailureCount() > 0) { throw new Exception("Oh heavens to Betsy! Something bad happened!"); } else { throw new Exception("What in the world is happening?!"); } }
public void Execute() { // Using the metadata source, we create an instance of a defined data domain WonkaRefEnvironment RefEnv = WonkaRefEnvironment.CreateInstance(false, moMetadataSource); // To test whether the data domain has been created, we pull back one attribute WonkaRefAttr AccountStsAttr = RefEnv.GetAttributeByAttrName("AccountStatus"); // Creating an instance of the rules engine using our rules and the metadata WonkaBizRulesEngine RulesEngine = new WonkaBizRulesEngine(new StringBuilder(msRulesContents), moMetadataSource); // Gets a predefined data record that will be our analog for new data coming into the system WonkaProduct NewProduct = GetNewProduct(); // Check that the data has been populated correctly on the "new" record string sStatusValueBefore = GetAttributeValue(NewProduct, AccountStsAttr); // Since the rules can reference values from different records (like O.Price for the existing // record's price and N.Price for the new record's price), we need to provide the delegate // that can pull the existing (i.e., old) record using a key RulesEngine.GetCurrentProductDelegate = GetOldProduct; // Validate the new record using our rules engine and its initialized RuleTree Wonka.BizRulesEngine.Reporting.WonkaBizRuleTreeReport Report = RulesEngine.Validate(NewProduct); // Now retrieve the AccountStatus value and see if the rules have altered it (which should // not be the case) string sStatusValueAfter = GetAttributeValue(NewProduct, AccountStsAttr); if (Report.GetRuleSetFailureCount() > 0) { throw new Exception("Oh heavens to Betsy! Something bad happened!"); } }
public void Execute(string psOrchestrationTestAddress = null, bool pbValidateWithinTransaction = false) { WonkaRefEnvironment RefEnv = WonkaRefEnvironment.GetInstance(); Dictionary <string, WonkaBizSource> SourceMap = new Dictionary <string, WonkaBizSource>(); string sDefaultSourceId = "S"; string sContractSourceId = sDefaultSourceId; string sContractAddress = ""; string sContractAbi = ""; string sOrchGetterMethod = ""; string sOrchSetterMethod = ""; // These values indicate the Custom Operator "INVOKE_VAT_LOOKUP" which has been used in the markup - // its implementation can be found in the method "lookupVATDenominator" string sCustomOpId = "INVOKE_VAT_LOOKUP"; string sCustomOpMethod = "lookupVATDenominator"; // If a 'psOrchestrationTestAddress' value has been provided, it indicates that the user wishes // to use Orchestration if (!String.IsNullOrEmpty(psOrchestrationTestAddress)) { sContractAddress = psOrchestrationTestAddress; sContractAbi = msAbiOrchTest; sOrchGetterMethod = "getAttrValueBytes32"; sOrchSetterMethod = "setAttrValueBytes32"; } else { sContractAddress = msContractAddress; sContractAbi = msAbiWonka; sOrchGetterMethod = "getValueOnRecord"; sOrchSetterMethod = ""; } WonkaBizSource DefaultSource = new WonkaBizSource(sContractSourceId, msSenderAddress, msPassword, sContractAddress, sContractAbi, sOrchGetterMethod, sOrchSetterMethod, RetrieveValueMethod); // Here a mapping is created, where each Attribute points to a specific contract and its "accessor" methods // - the class that contains this information (contract, accessors, etc.) is of the WonkaBreSource type foreach (WonkaRefAttr TempAttr in moTargetAttrList) { SourceMap[TempAttr.AttrName] = DefaultSource; } Dictionary <string, WonkaBizSource> CustomOpSourceMap = new Dictionary <string, WonkaBizSource>(); // Here a mapping is created, where each Custom Operator points to a specific contract and its "implementation" method // - the class that contains this information (contract, accessors, etc.) is of the WonkaBreSource type WonkaBizSource CustomOpSource = new WonkaBizSource(sCustomOpId, msSenderAddress, msPassword, sContractAddress, sContractAbi, LookupVATDenominator, sCustomOpMethod); CustomOpSourceMap[sCustomOpId] = CustomOpSource; // Creating an instance of the rules engine using our rules and the metadata WonkaBizRulesEngine RulesEngine = new WonkaBizRulesEngine(new StringBuilder(msRulesContents), SourceMap, CustomOpSourceMap, moMetadataSource, false); RulesEngine.DefaultSource = sDefaultSourceId; // The contract dictates that the RuleTree (and its other info, like the Orchestration and CU metadata) // is serialized to the blockchain before interacting with it SerializeRulesEngineToBlockchain(RulesEngine); WonkaRefAttr NewSellTaxAmountAttr = RefEnv.GetAttributeByAttrName("NewSellTaxAmount"); WonkaRefAttr NewVATAmountForHMRCAttr = RefEnv.GetAttributeByAttrName("NewVATAmountForHMRC"); // Gets a predefined data record that will be our analog for new data coming into the system // We are only using this record to test the .NET implementation WonkaProduct NewProduct = GetNewProduct(); string sSellAmtBefore = GetAttributeValue(NewProduct, NewSellTaxAmountAttr); string sVATAmtBefore = GetAttributeValue(NewProduct, NewVATAmountForHMRCAttr); /** ** Test the .NET side */ Wonka.BizRulesEngine.Reporting.WonkaBizRuleTreeReport Report = RulesEngine.Validate(NewProduct); string sSellAmtAfter = GetAttributeValue(NewProduct, NewSellTaxAmountAttr); string sVATAmtAfter = GetAttributeValue(NewProduct, NewVATAmountForHMRCAttr); if (Report.OverallRuleTreeResult == ERR_CD.CD_SUCCESS) { // NOTE: This should only be used for further testing // Serialize(NewProduct); } else if (Report.GetRuleSetFailureCount() > 0) { System.Console.WriteLine(".NET Engine says \"Oh heavens to Betsy! Something bad happened!\""); } else { System.Console.WriteLine(".NET Engine says \"What in the world is happening?\""); } // If a 'psOrchestrationTestAddress' value has been provided, it indicates that the user wishes // to use Orchestration if (!String.IsNullOrEmpty(psOrchestrationTestAddress)) { /** ** Now execute the rules engine on the blockchain, using both Orchestration to call accessors ** on other contract(s) and Custom Operators to invoke the "INVOKE_VAT_LOOKUP" operator ** (i.e., the "lookupVATDenominator()" method) implemented on a contract. ** ** NOTE: Based on the value of the argument 'pbValidateWithinTransaction', we will act accordingly - ** If set to 'true', we issue a call() when we execute the rules engine, since we are only ** looking to validate here. However, if the value if 'false', we issue a sendTransaction() ** so that we can attempts to set values (i.e., change the blockchain) will take effect. ** In that case, we might want to pull back the record afterwards with a subsequent function ** call, in order to examine the record here. ** **/ var BlockchainReport = ExecuteWithReport(RulesEngine, pbValidateWithinTransaction, SourceMap[NewSellTaxAmountAttr.AttrName], psOrchestrationTestAddress); if (BlockchainReport.NumberOfRuleFailures == 0) { // Indication of a success } else if (BlockchainReport.NumberOfRuleFailures > 0) { throw new Exception("Oh heavens to Betsy! Something bad happened!"); } else { throw new Exception("Seriously, what in the world is happening?!"); } } }