public static string GetJavaScriptRules(RuleApplicationDef ruleAppDef)
        {
            var subscriptionKey = Properties.Settings.Default.InRuleDistKey;

            string returnContent;

            try
            {
                var client = new HttpClient();
                client.BaseAddress = new Uri("https://api.distribution.inrule.com");
                var uri = new Uri("https://api.distribution.inrule.com/package?logOption=None&subscription-key=" + subscriptionKey);

                using (var mpfdc = new MultipartFormDataContent())
                {
                    var byteArray = System.Text.Encoding.UTF8.GetBytes(ruleAppDef.GetXml());
                    var stream    = new MemoryStream(byteArray);
                    var content   = new StreamContent(stream);
                    mpfdc.Add(content, "ruleApplication", ruleAppDef.Name + ".ruleapp");
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("multipart/form-data"));

                    var     result         = client.PostAsync(uri, mpfdc).Result;
                    dynamic resource       = result.Content.ReadAsAsync <JObject>().Result;
                    var     resultDownload = client.GetAsync((string)resource.PackagedApplicationDownloadUrl + "?subscription-key=" + subscriptionKey).Result;

                    returnContent = resultDownload.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception e)
            {
                returnContent = e.Message;
            }

            return(returnContent);
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            const string tokenUrl     = "https://auth.inrulecloud.com/oauth/token";
            const string publishUrl   = "https://[your-decision-runtime-hostname]/api/decisions";                // Change this to match the URL of your Decision Runtime
            const string clientId     = "xxxxxxxxxxxxxxxxxxxxxxx";                                               // Contact inrule for the client_id for your application
            const string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Contact inrule for the client_secret for your application

            try
            {
                var ruleApplication = new RuleApplicationDef("InterestRateApp");

                // Define 'CalculateLoanPayment' decision
                var calculateLoanPayment = ruleApplication.Decisions.Add(new DecisionDef("CalculateLoanPayment"));
                {
                    var presentValue = calculateLoanPayment.Fields.Add(new DecisionFieldDef("PresentValue", DecisionFieldType.Input, DataType.Number));
                    var interestRate = calculateLoanPayment.Fields.Add(new DecisionFieldDef("InterestRate", DecisionFieldType.Input, DataType.Number));
                    var periods      = calculateLoanPayment.Fields.Add(new DecisionFieldDef("Periods", DecisionFieldType.Input, DataType.Integer));
                    var payment      = calculateLoanPayment.Fields.Add(new DecisionFieldDef("Payment", DecisionFieldType.Output, DataType.Number));
                    calculateLoanPayment.DecisionStart.Rules.Add(new SetValueActionDef(payment.Name, $"{interestRate.Name} * {presentValue.Name} / (1 - ((1 + {interestRate.Name}) ^ -{periods.Name}))"));
                }

                // Define 'CalculateFutureValue' decision
                var calculateFutureValue = ruleApplication.Decisions.Add(new DecisionDef("CalculateFutureValue"));
                {
                    var presentValue = calculateFutureValue.Fields.Add(new DecisionFieldDef("PresentValue", DecisionFieldType.Input, DataType.Number));
                    var interestRate = calculateFutureValue.Fields.Add(new DecisionFieldDef("InterestRate", DecisionFieldType.Input, DataType.Number));
                    var periods      = calculateFutureValue.Fields.Add(new DecisionFieldDef("Periods", DecisionFieldType.Input, DataType.Integer));
                    var futureValue  = calculateFutureValue.Fields.Add(new DecisionFieldDef("FutureValue", DecisionFieldType.Output, DataType.Number));
                    calculateFutureValue.DecisionStart.Rules.Add(new SetValueActionDef(futureValue.Name, $"{presentValue.Name} * (1 + {interestRate.Name}) ^ {periods.Name}"));
                }

                // Get bearer token from authentication service
                string bearerToken = await GetBearerToken(tokenUrl, clientId, clientSecret);

                if (bearerToken == null)
                {
                    return;
                }
                Console.WriteLine();

                string ruleApplicationXml = ruleApplication.GetXml();

                // Publish CalculateLoanPayment Decision
                if (!await PublishDecision(publishUrl, calculateLoanPayment.Name, ruleApplicationXml, bearerToken))
                {
                    return;
                }

                // Publish CalculateFutureValue Decision
                if (!await PublishDecision(publishUrl, calculateFutureValue.Name, ruleApplicationXml, bearerToken))
                {
                    return;
                }

                string inputJson = JsonConvert.SerializeObject(new
                {
                    PresentValue = 20000.00m,
                    InterestRate = 0.05m,
                    Periods      = 5
                });

                // Execute CalculateLoanPayment Decision Service
                Console.WriteLine();
                if (!await ExecuteDecisionService(publishUrl, calculateLoanPayment.Name, inputJson, bearerToken))
                {
                    return;
                }

                // Execute CalculateFutureValue Decision Service
                Console.WriteLine();
                if (!await ExecuteDecisionService(publishUrl, calculateFutureValue.Name, inputJson, bearerToken))
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("An unexpected error occurred: " + ex);
            }
            finally
            {
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }