Using Runner CommandHandler

◷ Reading Time: 4 minutes

FlexRule Runtime SDK provides a very simple, plug-and-run library for loading and executing models. This approach covers many different options out of the box to support a wide range of scenarios:

  • Hosting in Cloud Services
  • Command Line Consoles
  • Web API hosting
  • and much more…

Architecture and Design

Runner Command Handler is built on top of RuntimeEngine to provide ease of integration for complex scenarios. It provides the ability to:

  • Load models from Package or Individual files
  • Run models with extensions
  • Reuse (caching) of Runtime Engine
  • Write outputs of Notifications and Logs
  • Bind to a specific version of FlexRule Runtime
  • Load license files based on location or content
  • Query models signature
Runner Command Handler provides an abstraction layer on top of many components to make loading and executing models simpler.

An alternative approach is to create and use RuntimeEngine instances directly and handle everything related to instance management in your application’s code.

Commands

There are multiple different sets of commands that are available in Runner CommandHandler. These commands should be configured by your application code. For different tasks, your application should be using different commands.

License Initialization

Your licenses file will be required for executing a model. Therefore, the license’s content can be set using InitLicenseCommand .

Run a Model

Command RunLogicCommand allows you to load and run a model in different ways:

  • Individual files on computer file system
  • Package file on the computer file system
  • Package content (loaded from anywhere!)

Below are some basic properties that can be configured:

  • LogicContent: Packages content to load
  • Logic: package or model’s file path to load
  • Home: base folder and home when the list of logic documents are stored in a location
  • Extension: list of extensions to be registered and loaded during the execution. Each extension in this list is the name of the assembly.
  • InputValues: The values for input parameters
  • Runtime: the folder location that FlexRule Runtime assemblies can be found

Inspecting the Model Signature

Command LogicSignatureQueryloads the list of parameters from the model. The result will include:

  • Name of parameters
  • Type of each parameter
  • The direction of each parameter

Inspecting Packages Assemblies

In some scenarios when a package is a reference for executing a model, it might be needed to understand what assemblies are included in the package. Command AssemblyListQuery will return the name and content of the assemblies included in the package as IDictionary<string, byte[]>.

Removing Cache

A RuntimeEngineinstance is created for each request based on the Logic setting in RunLogicCommand. Each engine is checked to see if its performance can be improved. To remove an instance of RuntimeEnginefrom the cache, use RemoveRuntimeEngineCacheCommand.

Example of Use

var package = File.ReadAllBytes(@"Data\BlobReadXmlSample.frdp"); // load the content of the package from file, database, blob, etc.
var licenseContent = "CONTENT OF YOUR LICENSE GOES HERE";

var runCommand = new RunLogicCommand
{
    Runtime = @"d:\FlexRule\v8.1.45",
    LogicContent = package,

    // Key-Value of input parameters
    InputValues = null,

    Cache = true,

    // If your package uses any extentions, here they should be provided
    Extension = new[] { "FlexRule.Extensions.Storage.AzureStorage.dll" }, 

    // to manage the cache key outside
    // SessionId = ComputeCacheSessionId(package)
};

var logger = new StringBuilder();
var output = new StringBuilder();
var handler = new CommandHandler(OutputFactory.CreateString(output), OutputFactory.CreateString(logger));

// Initializing license
handler.Execute(new InitLicenseCommand(licenseContent));

// Executing a logic
var result = handler.Execute(runCommand);

Updated on May 18, 2020

Was this article helpful?

Related Articles