From 71b18e14ff515d76fbde8a51682c2a88837c796a Mon Sep 17 00:00:00 2001 From: Duncan Jones Date: Tue, 12 Jan 2021 11:29:25 -0600 Subject: [PATCH] SEAL-3015: Ability to execute decisions via SDK [documentation] SEAL-3015: Ability to execute decisions via SDK [documentation] --- .../ExecuteDecisionSample.csproj | 14 -- .../ExecuteDecisionSample.sln | 25 --- .../ExecuteDecisionSample/Program.cs | 169 ------------------ .../ExecuteDecisionSample/README.md | 4 - 4 files changed, 212 deletions(-) delete mode 100644 Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.csproj delete mode 100644 Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.sln delete mode 100644 Developer Samples/ExecuteDecisionSample/Program.cs delete mode 100644 Developer Samples/ExecuteDecisionSample/README.md diff --git a/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.csproj b/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.csproj deleted file mode 100644 index 1a0bf65..0000000 --- a/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Exe - netcoreapp2.2 - 7.1 - - - - - - - - diff --git a/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.sln b/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.sln deleted file mode 100644 index 171f84c..0000000 --- a/Developer Samples/ExecuteDecisionSample/ExecuteDecisionSample.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.902 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecuteDecisionSample", "ExecuteDecisionSample.csproj", "{91D81DF5-E03E-4BD8-B9E7-EEECA56ACCB7}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {91D81DF5-E03E-4BD8-B9E7-EEECA56ACCB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {91D81DF5-E03E-4BD8-B9E7-EEECA56ACCB7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {91D81DF5-E03E-4BD8-B9E7-EEECA56ACCB7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {91D81DF5-E03E-4BD8-B9E7-EEECA56ACCB7}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {64E4F60D-7E50-463B-BCCE-B736CC8D61C3} - EndGlobalSection -EndGlobal diff --git a/Developer Samples/ExecuteDecisionSample/Program.cs b/Developer Samples/ExecuteDecisionSample/Program.cs deleted file mode 100644 index b8eb0b3..0000000 --- a/Developer Samples/ExecuteDecisionSample/Program.cs +++ /dev/null @@ -1,169 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using InRule.Repository; -using InRule.Repository.Decisions; -using InRule.Repository.RuleElements; -using Newtonsoft.Json; - -namespace ExecuteDecisionSample -{ - class Program - { - 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(); - } - } - - private static async Task PublishDecision(string publishUrl, string decisionName, string ruleApplicationXml, string bearerToken) - { - Console.Write($"Publishing Decision '{decisionName}'..."); - - using (HttpClient client = new HttpClient()) - { - string decisionJson = JsonConvert.SerializeObject(new - { - name = decisionName, - ruleApplication = ruleApplicationXml - }); - - var request = new HttpRequestMessage(HttpMethod.Post, publishUrl); - request.Headers.Add("Authorization", $"Bearer {bearerToken}"); - request.Content = new StringContent(decisionJson, Encoding.UTF8, "application/json"); - - var response = await client.SendAsync(request); - if (!response.IsSuccessStatusCode) - { - string reason = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"Error {(int) response.StatusCode} ({response.StatusCode.ToString()}): {reason}"); - return false; - } - } - - Console.WriteLine("Success."); - return true; - } - - private static async Task ExecuteDecisionService(string publishUrl, string decisionName, string inputJson, string bearerToken) - { - Console.WriteLine($"Executing '{decisionName}' Decision Service..."); - Console.WriteLine($"{decisionName} inputs: {inputJson}"); - - using (HttpClient client = new HttpClient()) - { - var request = new HttpRequestMessage(HttpMethod.Post, $"{publishUrl}/{decisionName}"); - request.Headers.Add("Authorization", $"Bearer {bearerToken}"); - request.Content = new StringContent(inputJson, Encoding.UTF8, "application/json"); - - var response = await client.SendAsync(request); - if (!response.IsSuccessStatusCode) - { - string reason = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"Error {(int) response.StatusCode} ({response.StatusCode.ToString()}): {reason}"); - return false; - } - - string outputJson = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"{decisionName} output: {outputJson}"); - } - - return true; - } - - private static async Task GetBearerToken(string tokenUrl, string clientId, string clientSecret) - { - Console.Write("Retrieving bearer token from authentication service..."); - using (HttpClient client = new HttpClient()) - { - var request = new FormUrlEncodedContent(new[] - { - new KeyValuePair("grant_type", "client_credentials"), - new KeyValuePair("client_id", clientId), - new KeyValuePair("client_secret", clientSecret), - new KeyValuePair("audience", "master_service"), - }); - - var response = await client.PostAsync(tokenUrl, request); - if (!response.IsSuccessStatusCode) - { - string reason = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"Error retreiving token: {(int) response.StatusCode} ({response.StatusCode.ToString()}): {reason}"); - return null; - } - - string contentJson = await response.Content.ReadAsStringAsync(); - dynamic content = JsonConvert.DeserializeObject(contentJson); - Console.WriteLine("Success."); - return content.access_token; - } - } - } -} diff --git a/Developer Samples/ExecuteDecisionSample/README.md b/Developer Samples/ExecuteDecisionSample/README.md deleted file mode 100644 index b06ce01..0000000 --- a/Developer Samples/ExecuteDecisionSample/README.md +++ /dev/null @@ -1,4 +0,0 @@ -Execute Decision Sample -==== - -An example application that programmatically authors, publishes, and executes a Decision Service. For more information, please see the [Decision Services](../../Authoring%Samples/Decisions) documentation. \ No newline at end of file