Exemplo n.º 1
0
        // This constructor will be called in the case that we wish to initialize the framework
        // with configuration files locally (embedded resources, local filesystem, etc.)
        public WonkaCQSOrchTest()
        {
            moAttrSourceMap = new Dictionary <string, WonkaBreSource>();
            moCustomOpMap   = new Dictionary <string, WonkaBreSource>();

            var TmpAssembly = Assembly.GetExecutingAssembly();

            // Using the metadata source, we create an instance of a defined data domain
            WonkaRefEnvironment RefEnv =
                WonkaRefEnvironment.CreateInstance(false, moMetadataSource);

            // Read the XML markup that lists the business rules (i.e., the RuleTree)
            using (var RulesReader = new StreamReader(TmpAssembly.GetManifestResourceStream("WonkaSystem.TestData.VATCalculationExample.xml")))
            {
                msRulesContents = RulesReader.ReadToEnd();
            }

            // Read the configuration file that contains all the initialization details regarding the rules engine
            // (like addresses of contracts, senders, passwords, etc.)
            using (var XmlReader = new System.IO.StreamReader(TmpAssembly.GetManifestResourceStream("WonkaSystem.TestData.VATCalculationExample.init.xml")))
            {
                string sInitXml = XmlReader.ReadToEnd();

                // We deserialize/parse the contents of the config file
                System.Xml.Serialization.XmlSerializer WonkaEthSerializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(WonkaEth.Init.WonkaEthInitialization),
                                                               new System.Xml.Serialization.XmlRootAttribute("WonkaEthInitialization"));

                WonkaEth.Init.WonkaEthInitialization WonkaInit =
                    WonkaEthSerializer.Deserialize(new System.IO.StringReader(sInitXml)) as WonkaEth.Init.WonkaEthInitialization;

                // Here, any embeddeded resources mentioned in the config file (instead of simple file URLs) are accessed here
                WonkaInit.RetrieveEmbeddedResources(TmpAssembly);

                // The initialization data is transformed into a structure used by the WonkaEth namespace
                moOrchInitData = WonkaInit.TransformIntoOrchestrationInit(moMetadataSource);

                System.Console.WriteLine("Number of custom operators: (" + WonkaInit.CustomOperatorList.Length + ").");
            }

            // Read the configuration file that contains all the initialization details regarding the rules registry
            // (like Ruletree info, Grove info, etc.) - this information will allow us to add our RuleTree to the
            // Registry so that it can be discovered by users and so it can be added to a Grove (where it can be executed
            // as a member of a collection)
            using (var XmlReader = new System.IO.StreamReader(TmpAssembly.GetManifestResourceStream("WonkaSystem.TestData.WonkaRegistry.init.xml")))
            {
                string sInitRegistryXml = XmlReader.ReadToEnd();

                // We deserialize/parse the contents of the config file
                System.Xml.Serialization.XmlSerializer WonkaEthSerializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(WonkaEth.Init.WonkaEthRegistryInitialization),
                                                               new System.Xml.Serialization.XmlRootAttribute("WonkaEthRegistryInitialization"));

                moWonkaRegistryInit =
                    WonkaEthSerializer.Deserialize(new System.IO.StringReader(sInitRegistryXml)) as WonkaEth.Init.WonkaEthRegistryInitialization;

                // Here, any embeddeded resources mentioned in the config file (instead of simple file URLs) are accessed here
                moWonkaRegistryInit.RetrieveEmbeddedResources(TmpAssembly);
            }

            // Here, we save all data from the config files to member properties
            // This region and the usage of member properties isn't necessary, but it's useful for debugging
            #region Set Class Member Variables
            msSenderAddress = moOrchInitData.BlockchainEngine.SenderAddress;
            msPassword      = moOrchInitData.BlockchainEngine.Password;

            if (moOrchInitData.BlockchainEngine.ContractAddress == null)
            {
                msWonkaContractAddress = DeployWonkaContract();
            }
            else
            {
                msWonkaContractAddress = moOrchInitData.BlockchainEngine.ContractAddress;
            }

            if (moOrchInitData.DefaultBlockchainDataSource.ContractAddress == null)
            {
                msOrchContractAddress = DeployOrchestrationContract();
            }
            else
            {
                msOrchContractAddress = moOrchInitData.DefaultBlockchainDataSource.ContractAddress;
            }

            msAbiWonka        = moOrchInitData.BlockchainEngine.ContractABI;
            msAbiOrchContract = moOrchInitData.DefaultBlockchainDataSource.ContractABI;

            moDefaultSource =
                new WonkaBreSource(moOrchInitData.DefaultBlockchainDataSource.SourceId,
                                   moOrchInitData.DefaultBlockchainDataSource.SenderAddress,
                                   moOrchInitData.DefaultBlockchainDataSource.Password,
                                   moOrchInitData.DefaultBlockchainDataSource.ContractAddress,
                                   moOrchInitData.DefaultBlockchainDataSource.ContractABI,
                                   moOrchInitData.DefaultBlockchainDataSource.MethodName,
                                   moOrchInitData.DefaultBlockchainDataSource.SetterMethodName,
                                   RetrieveValueMethod);

            foreach (WonkaRefAttr TempAttr in RefEnv.AttrCache)
            {
                moAttrSourceMap[TempAttr.AttrName] = moDefaultSource;
            }

            moCustomOpMap = moOrchInitData.BlockchainCustomOpFunctions;
            #endregion

            // We initialize the proxy that will be used to communicate with the Registry on the blockchain
            WonkaEth.Contracts.WonkaRuleTreeRegistry WonkaRegistry =
                WonkaEth.Contracts.WonkaRuleTreeRegistry.CreateInstance(moWonkaRegistryInit.BlockchainRegistry.ContractSender,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractPassword,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractAddress,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractABI,
                                                                        moWonkaRegistryInit.Web3HttpUrl);

            // Here, the data domain is serialized to the blockchain for use by the RuleTree(s)
            RefEnv.Serialize(moOrchInitData.BlockchainEngineOwner, msPassword, msSenderAddress, msWonkaContractAddress, msAbiWonka, moOrchInitData.Web3HttpUrl);
        }
Exemplo n.º 2
0
        // This constructor will be called in the case that we wish to initialize the framework
        // with configuration files that will be accessed through IPFS
        public WonkaCQSOrchTest(StringBuilder psPeerKeyId, string psRulesMarkupFile, string psRulesInitFile, string psRegistryInitFile)
        {
            moAttrSourceMap = new Dictionary <string, WonkaBreSource>();
            moCustomOpMap   = new Dictionary <string, WonkaBreSource>();

            var TmpAssembly = Assembly.GetExecutingAssembly();

            WonkaRefEnvironment RefEnv = WonkaRefEnvironment.CreateInstance(false, moMetadataSource);

            WonkaIpfs.WonkaIpfsEnvironment IpfsEnv = WonkaIpfs.WonkaIpfsEnvironment.CreateInstance();

            // Read the XML markup that lists the business rules
            msRulesContents = IpfsEnv.GetFile(psPeerKeyId.ToString(), psRulesMarkupFile);

            // Read the configuration file that contains all the initialization details regarding the rules engine
            // (like addresses of contracts, senders, passwords, etc.)
            string sInitXml = IpfsEnv.GetFile(psPeerKeyId.ToString(), psRulesInitFile);

            if (!String.IsNullOrEmpty(sInitXml))
            {
                System.Xml.Serialization.XmlSerializer WonkaEthSerializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(WonkaEth.Init.WonkaEthInitialization),
                                                               new System.Xml.Serialization.XmlRootAttribute("WonkaEthInitialization"));

                WonkaEth.Init.WonkaEthInitialization WonkaInit =
                    WonkaEthSerializer.Deserialize(new System.IO.StringReader(sInitXml)) as WonkaEth.Init.WonkaEthInitialization;

                WonkaInit.RetrieveEmbeddedResources(TmpAssembly);

                moOrchInitData = WonkaInit.TransformIntoOrchestrationInit(moMetadataSource);

                System.Console.WriteLine("Number of custom operators: (" + WonkaInit.CustomOperatorList.Length + ").");
            }

            // Read the configuration file that contains all the initialization details regarding the rules registry
            // (like Ruletree info, Grove info, etc.)
            string sInitRegistryXml = IpfsEnv.GetFile(psPeerKeyId.ToString(), psRegistryInitFile);

            if (!String.IsNullOrEmpty(sInitRegistryXml))
            {
                System.Xml.Serialization.XmlSerializer WonkaEthSerializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(WonkaEth.Init.WonkaEthRegistryInitialization),
                                                               new System.Xml.Serialization.XmlRootAttribute("WonkaEthRegistryInitialization"));

                moWonkaRegistryInit =
                    WonkaEthSerializer.Deserialize(new System.IO.StringReader(sInitRegistryXml)) as WonkaEth.Init.WonkaEthRegistryInitialization;

                moWonkaRegistryInit.RetrieveEmbeddedResources(TmpAssembly);
            }

            #region Set Class Member Variables
            msSenderAddress = moOrchInitData.BlockchainEngine.SenderAddress;
            msPassword      = moOrchInitData.BlockchainEngine.Password;

            if (moOrchInitData.BlockchainEngine.ContractAddress == null)
            {
                msWonkaContractAddress = DeployWonkaContract();
            }
            else
            {
                msWonkaContractAddress = moOrchInitData.BlockchainEngine.ContractAddress;
            }

            if (moOrchInitData.DefaultBlockchainDataSource.ContractAddress == null)
            {
                msOrchContractAddress = DeployOrchestrationContract();
            }
            else
            {
                msOrchContractAddress = moOrchInitData.DefaultBlockchainDataSource.ContractAddress;
            }

            msAbiWonka        = moOrchInitData.BlockchainEngine.ContractABI;
            msAbiOrchContract = moOrchInitData.DefaultBlockchainDataSource.ContractABI;

            moDefaultSource =
                new WonkaBreSource(moOrchInitData.DefaultBlockchainDataSource.SourceId,
                                   moOrchInitData.DefaultBlockchainDataSource.SenderAddress,
                                   moOrchInitData.DefaultBlockchainDataSource.Password,
                                   moOrchInitData.DefaultBlockchainDataSource.ContractAddress,
                                   moOrchInitData.DefaultBlockchainDataSource.ContractABI,
                                   moOrchInitData.DefaultBlockchainDataSource.MethodName,
                                   moOrchInitData.DefaultBlockchainDataSource.SetterMethodName,
                                   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 RefEnv.AttrCache)
            {
                moAttrSourceMap[TempAttr.AttrName] = moDefaultSource;
            }

            // 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
            moCustomOpMap = moOrchInitData.BlockchainCustomOpFunctions;

            #endregion

            WonkaEth.Contracts.WonkaRuleTreeRegistry WonkaRegistry =
                WonkaEth.Contracts.WonkaRuleTreeRegistry.CreateInstance(moWonkaRegistryInit.BlockchainRegistry.ContractSender,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractPassword,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractAddress,
                                                                        moWonkaRegistryInit.BlockchainRegistry.ContractABI,
                                                                        moWonkaRegistryInit.Web3HttpUrl);

            RefEnv.Serialize(moOrchInitData.BlockchainEngineOwner, msPassword, msSenderAddress, msWonkaContractAddress, msAbiWonka, moOrchInitData.Web3HttpUrl);
        }