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
            WonkaBreRulesEngine RulesEngine =
                new WonkaBreRulesEngine(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
            WonkaBre.Reporting.WonkaBreRuleTreeReport 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?!");
            }
        }
Beispiel #2
0
        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
            WonkaBreRulesEngine RulesEngine =
                new WonkaBreRulesEngine(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
            WonkaBre.Reporting.WonkaBreRuleTreeReport 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!");
            }
        }
Beispiel #3
0
        public void Execute(string psOrchestrationTestAddress = null, bool pbValidateWithinTransaction = false)
        {
            WonkaRefEnvironment RefEnv = WonkaRefEnvironment.GetInstance();

            Dictionary <string, WonkaBre.RuleTree.WonkaBreSource> SourceMap =
                new Dictionary <string, WonkaBre.RuleTree.WonkaBreSource>();

            string sDefaultSource    = "S";
            string sContractSourceId = sDefaultSource;
            string sContractAddress  = "";
            string sContractAbi      = "";
            string sOrchGetterMethod = "";
            string sOrchSetterMethod = "";

            // If a 'psOrchestrationTestAddress' value has been provided, it indicates that the user wishes
            // to use Orchestration
            if (!String.IsNullOrEmpty(psOrchestrationTestAddress))
            {
                // Here we set the values for Orchestration (like the target contract) and the implemented
                // methods that have the expected function signatures for getting/setting Attribute values
                sContractAddress  = psOrchestrationTestAddress;
                sContractAbi      = msAbiOrchTest;
                sOrchGetterMethod = "getAttrValueBytes32";
                sOrchSetterMethod = "setAttrValueBytes32";
            }
            else
            {
                sContractAddress  = msContractAddress;
                sContractAbi      = msAbiWonka;
                sOrchGetterMethod = "getValueOnRecord";
                sOrchSetterMethod = "";
            }

            // 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)
            {
                WonkaBreSource TempSource =
                    new WonkaBreSource(sContractSourceId, msSenderAddress, msPassword, sContractAddress, sContractAbi, sOrchGetterMethod, sOrchSetterMethod, RetrieveValueMethod);

                SourceMap[TempAttr.AttrName] = TempSource;
            }

            // Creating an instance of the rules engine using our rules and the metadata
            WonkaBreRulesEngine RulesEngine = null;

            if (psOrchestrationTestAddress == null)
            {
                RulesEngine = new WonkaBreRulesEngine(new StringBuilder(msRulesContents), moMetadataSource);
            }
            else
            {
                RulesEngine = new WonkaBreRulesEngine(new StringBuilder(msRulesContents), SourceMap, moMetadataSource);
                RulesEngine.DefaultSource = sDefaultSource;
            }

            RulesEngine.SetDefaultStdOps(msPassword);

            // The contract dictates that the RuleTree (and its other info, like the Source mapping) is serialized
            // to the blockchain before interacting with it
            SerializeRulesEngineToBlockchain(RulesEngine);

            WonkaRefAttr AccountStsAttr = RefEnv.GetAttributeByAttrName("AccountStatus");
            WonkaRefAttr RvwFlagAttr    = RefEnv.GetAttributeByAttrName("AuditReviewFlag");

            // 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 sStatusValueBefore = GetAttributeValue(NewProduct, AccountStsAttr);
            string sFlagValueBefore   = GetAttributeValue(NewProduct, RvwFlagAttr);

            // SerializeProductToBlockchain(NewProduct);

            // Validate that the .NET implementation and the rules markup are both working properly
            WonkaBre.Reporting.WonkaBreRuleTreeReport Report = RulesEngine.Validate(NewProduct);

            string sStatusValueAfter = GetAttributeValue(NewProduct, AccountStsAttr);
            string sFlagValueAfter   = GetAttributeValue(NewProduct, RvwFlagAttr);

            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.
                **
                ** 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[RvwFlagAttr.AttrName]);

                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?!");
                }
            }
        }
        public async Task <bool> Execute(bool pbValidateTransaction = false)
        {
            bool bResult = false;

            var RefEnv      = WonkaRefEnvironment.GetInstance();
            var RulesEngine = moEthEngineInit.Engine.RulesEngine;
            var SourceMap   = moEthEngineInit.Engine.SourceMap;

            WonkaRefAttr AccountStsAttr = RefEnv.GetAttributeByAttrName("AccountStatus");
            WonkaRefAttr RvwFlagAttr    = RefEnv.GetAttributeByAttrName("AuditReviewFlag");

            // 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 = moProduct;

            string sStatusValueBefore = NewProduct.GetAttributeValue(AccountStsAttr);
            string sFlagValueBefore   = NewProduct.GetAttributeValue(RvwFlagAttr);

            // Validate that the .NET implementation and the rules markup are both working properly
            WonkaBre.Reporting.WonkaBreRuleTreeReport Report = RulesEngine.Validate(NewProduct);

            string sStatusValueAfter = NewProduct.GetAttributeValue(AccountStsAttr);
            string sFlagValueAfter   = NewProduct.GetAttributeValue(RvwFlagAttr);

            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(moEthEngineInit.StorageContractAddress))
            {
                /**
                ** Now execute the rules engine on the blockchain.
                **
                ** 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 = await ExecuteWithReportAsync(RulesEngine, pbValidateTransaction).ConfigureAwait(false);

                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?!");
                }
            }

            return(bResult);
        }
        public void Execute(string psOrchestrationTestAddress = null, bool pbValidateWithinTransaction = false)
        {
            WonkaRefEnvironment RefEnv = WonkaRefEnvironment.GetInstance();

            Dictionary <string, WonkaBre.RuleTree.WonkaBreSource> SourceMap =
                new Dictionary <string, WonkaBre.RuleTree.WonkaBreSource>();

            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 = "";
            }

            WonkaBreSource DefaultSource =
                new WonkaBreSource(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, WonkaBreSource> CustomOpSourceMap = new Dictionary <string, WonkaBreSource>();

            // 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
            WonkaBreSource CustomOpSource =
                new WonkaBreSource(sCustomOpId, msSenderAddress, msPassword, sContractAddress, sContractAbi, LookupVATDenominator, sCustomOpMethod);

            CustomOpSourceMap[sCustomOpId] = CustomOpSource;

            // Creating an instance of the rules engine using our rules and the metadata
            WonkaBreRulesEngine RulesEngine =
                new WonkaBreRulesEngine(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
            */
            WonkaBre.Reporting.WonkaBreRuleTreeReport 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?!");
                }
            }
        }