See Also: irServer - Rule Execution Service
The following example demonstrates calling the irServer - Rule Execution Service REST interface with a JSON based Entity State.
static void Main(string[] args)
{
// The name of the ruleApp in the Catalog
string ruleApp = "MortgageCalculator";
// The address of the Rule Execution Service
string ruleExecutionServiceURI = "http://localhost/InRuleRuleEngineService_v5.0.16/HttpService.svc";
// The name of the Entity
string entityName = "Mortgage";
// The name of the RuleSet to execute, or use 'ApplyRules' if found to be null.
string ruleSetName = "PaymentSummaryRules";
// The entity state as a C# dynamic type
dynamic entityState = new
{
LoanInfo = new
{
Principal = 53000m, APR = 7.625f, TermInYears = 15
}
};
// A console application's main() entry point cannot be declared as async, so here
// we are creating an Async task to execute the Async method, then we will wait on
// the result.
Task<string> httpTask = AsyncRestJsonCatalog(ruleExecutionServiceURI, ruleApp,
entityName, entityState, ruleSetName);
// Calling the Result property of a Task object will block this thread until
// the Task has had a chance to complete in it's worker thread.
string httpTaskResult = httpTask.Result;
// After this call you can inspect the raw result (as JSON) in resultJson, but note
// that the EntityState will be serialized Json and must be decoded.
dynamic resultJson = JsonConvert.DeserializeObject<dynamic>(httpTaskResult);
string entityJsonString = resultJson.EntityState;
// Decode the serialized entity Json into a dynamic entity.
dynamic entity = JsonConvert.DeserializeObject<dynamic>(entityJsonString);
}
private async static Task<string> AsyncRestJsonCatalog(string ruleExecutionServiceURI, string ruleApp,
string ruleApplicationEntityName, dynamic entityStateObjectInput, string ruleSetName)
{
// We use Newtonsoft's Json.Net library to convert this dynamic C# object to a JSON string
string entityStateJsonInput = JsonConvert.SerializeObject(entityStateObjectInput);
// New create the full request, inserting the JSON string that represents EntityState.
dynamic requestData = new
{
RuleApp = new
{
RepositoryRuleAppRevisionSpec = new
{
RuleApplicationName = ruleApp
}
},
EntityName = ruleApplicationEntityName,
EntityState = entityStateJsonInput,
RuleSetName = ruleSetName
};
string requestDataString = JsonConvert.SerializeObject(requestData);
//If no ruleSetName exists, execute Auto rules, otherwise execute explicit rule.
string postUri;
if (string.IsNullOrEmpty(ruleSetName))
{ postUri = ruleExecutionServiceURI + "/ApplyRules"; }
else
{ postUri = ruleExecutionServiceURI + "/ExecuteRuleSet"; }
// Our EntityState is encoded as JSON document, not XML.
string mediaType = "application/json";
// Async call to HttpClient with an HttpContent and wait for result.
HttpContent content = new StringContent(requestDataString, Encoding.UTF8, mediaType);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(postUri, content);
// Async convert the HttpResponseMessage's content to a string.
string responsestring = await response.Content.ReadAsStringAsync();
// We're done, return result.
return responsestring;
}
Comments
0 comments
Please sign in to leave a comment.