コード例 #1
0
ファイル: Program.cs プロジェクト: mgrucella/Sales-Samples
        static void ExecuteBusinessRules(string input)
        {
            //Option 1: Retreive rule application from the catalog
            // - URL is for the catalog service
            //var ruleAppRef = new CatalogRuleApplicationReference("http://localhost/InRuleCatalogService/Service.svc", "HelloWorld", "admin", "password");

            //Option 2: use a file based rule application
            // - Fully qualified path to the *.ruleappx file
            var ruleAppRef = new FileSystemRuleApplicationReference(@"HelloWorld.ruleappx");

            using (var session = new RuleSession(ruleAppRef))
            {
                //You cam set logging options. By default, all logging is turned off
                //session.Settings.LogOptions = EngineLogOptions.RuleTrace | EngineLogOptions.StateChanges;

                //You have 3 options for passing in data:
                // 1. Xml
                // 2. Json
                // 3. .NET object instance
                //In all 3 situations, it must map to the data structure (Entities) in the rule application.
                var entity = session.CreateEntity("Test", input);

                session.ApplyRules();

                //Post rule execution, you can get the output state:
                // 1. Xml
                // 2. Json
                //If you passed in a .NET object instance, you don't need to do anything as the rule engine will operate on it in memory.
                var outputXml = entity.GetXml();
                Console.WriteLine(outputXml);

                var outputJson = entity.GetJson();
                Console.WriteLine(outputJson);

                //You can get a list of Notifications...
                //session.GetNotifications();

                //You can get a list of Validations...
                //session.GetValidations();
            }
        }
コード例 #2
0
        public InRuleResponse PaymentAllocationRules(InRuleRequest request)
        {
            ServiceData myServiceData        = new ServiceData();
            var         response             = new InRuleResponse();
            var         ruleAppNotifications = string.Empty;

            try
            {
                if (Settings.Default.IsHardCoded)
                {
                    XmlSerializer reader = new XmlSerializer(typeof(InRuleResponse));
                    using (
                        StreamReader file = new StreamReader(Settings.Default.RuleAppDirectory + "InRuleResponse.xml"))
                    {
                        response = (InRuleResponse)reader.Deserialize(file);
                        file.Close();
                    }
                }
                else
                {
                    var ruleApp =
                        new FileSystemRuleApplicationReference(Settings.Default.RuleAppDirectory +
                                                               Settings.Default.RuleAppObject);
                    using (var session = new RuleSession(ruleApp))
                    {
                        var objectEntityState =
                            Util.DeserializeFromXml <PaymentAllocationRoot>("<" + Settings.Default.TopEntityName + ">" +
                                                                            request.RuleDataXML + "</" +
                                                                            Settings.Default.TopEntityName + ">");
                        objectEntityState.RuleEvaluationDate = request.RuleInfo.RuleEvaluationDate;
                        var rootEntity = session.CreateEntity(Settings.Default.TopEntityName, objectEntityState);

                        if (Settings.Default.UseLog)
                        {
                            session.Settings.LogOptions = EngineLogOptions.Execution | EngineLogOptions.RuleTrace;
                        }
                        else
                        {
                            session.Settings.LogOptions = EngineLogOptions.None;
                        }

                        RuleExecutionLog executionLog = rootEntity.ExecuteRuleSet(request.RuleInfo.RuleName,
                                                                                  Settings.Default.PerformPreValidation, Settings.Default.PerformPostValidation);

                        if (Settings.Default.UseLog)
                        {
                            string logMessages = string.Empty;
                            foreach (TextFeedbackLogMessage logMessage in executionLog.TextFeedbackMessages)
                            {
                                logMessages += logMessage.Description + "\r\n";
                            }

                            if (executionLog.HasNotifications)
                            {
                                foreach (var notification in session.GetNotifications())
                                {
                                    ruleAppNotifications += notification.Message + "\r\n";
                                }
                            }
                        }

                        response.RuleInfo = request.RuleInfo;
                        response.RuleInfo.ErrorDetails = objectEntityState.ErrorDetails;
                        response.RuleInfo.Status       = objectEntityState.ErrorDetails.Count > 0 ? "Failure" : "Success";
                        response.RuleResult            = Util.GetRuleResultXml(objectEntityState.Payments);
                    }
                }
                Util.SerializeToXmlFile(response, Settings.Default.RuleAppDirectory + @"\InRuleResponse.xml");
            }
            catch (System.Exception ex)
            {
                myServiceData.Result       = false;
                myServiceData.ErrorMessage = "PaymentAllocationRules call failed";
                myServiceData.ErrorDetails = ex.ToString();
                throw new FaultException <ServiceData>(myServiceData, ex.ToString());
            }
            return(response);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: ponkarthik87/Samples
        static async Task <int> Main(string[] args)
        {
            //Required Parameters
            bool   showHelp          = false;
            string irDistributionKey = null;
            string OutputFilePath    = null;
            //Option 1
            string RuleAppFilePath = null;
            //Option 2
            string CatalogUri          = null;
            string CatalogUsername     = null;
            string CatalogPassword     = null;
            string CatalogRuleAppName  = null;
            string CatalogRuleAppLabel = "LIVE";

            var clParams = new OptionSet {
                { "h|help", "Display Help.", k => showHelp = true },
                { "k|DistributionKey=", "The irDistributionKey for your account.", k => irDistributionKey = k },
                { "o|OutputPath=", "Desired Path for the compiled output library.", p => OutputFilePath = p },
                //Option 1
                { "r|RuleAppPath=", "Path to the Rule Application to be compiled.", p => RuleAppFilePath = p },
                //Option 2
                { "c|CatUri=", "Web URI for the IrCatalog Service endpoint.", p => CatalogUri = p },
                { "u|CatUsername="******"IrCatalog Username for authentication.", p => CatalogUsername = p },
                { "p|CatPassword="******"IrCatalog Password for authentication.", p => CatalogPassword = p },
                { "n|CatRuleAppName=", "Name of the Rule Application.", p => CatalogRuleAppName = p },
                { "l|CatLabel=", "Label of the Rule Application to retrieve (LIVE).", p => CatalogRuleAppLabel = p },
            };

            try
            {
                clParams.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("Failed parsing execution parameters: " + e.Message);
                showHelp = true;
            }

            RuleApplicationReference ruleApp = null;

            if (showHelp)
            {
                ShowHelp(clParams);
                return(1);
            }
            else if (string.IsNullOrEmpty(irDistributionKey))
            {
                Console.WriteLine("Error: Missing required parameter DistributionKey.");
                return(1);
            }
            else if (string.IsNullOrEmpty(OutputFilePath))
            {
                Console.WriteLine("Error: Missing required parameter OutputPath.");
                return(1);
            }

            if (!string.IsNullOrEmpty(RuleAppFilePath))
            {
                try
                {
                    ruleApp = new FileSystemRuleApplicationReference(RuleAppFilePath);
                }
                catch (IntegrationException ie)
                {
                    Console.WriteLine("Error creating reference to file-based Rule App: " + ie.Message); //Rule App file not found
                }
            }

            if (!string.IsNullOrEmpty(CatalogUri) &&
                !string.IsNullOrEmpty(CatalogUsername) &&
                !string.IsNullOrEmpty(CatalogPassword) &&
                !string.IsNullOrEmpty(CatalogRuleAppName))
            {
                if (ruleApp != null)
                {
                    Console.WriteLine("Error: Parameters were provided for both File- and Catalog-based Rule App; only one may be specified.");
                    return(1);
                }
                else
                {
                    ruleApp = new CatalogRuleApplicationReference(CatalogUri, CatalogRuleAppName, CatalogUsername, CatalogPassword, CatalogRuleAppLabel);
                }
            }

            if (ruleApp == null)
            {
                Console.WriteLine("You must provide either RuleAppPath or all of CatUri, CatUsername, CatPassword, and CatRuleAppName (with optional CatLabel)");
                return(1);
            }
            else
            {
                var success = await RetrieveAndWriteIrJSFromDistributionService(ruleApp, irDistributionKey, OutputFilePath);

                if (success)
                {
                    return(0);
                }
                else
                {
                    return(1);
                }
            }
        }