Loading models

From FlexRule Wiki
Jump to: navigation, search

Rules and logic are modeled in different languages (e.g., XML, SXP, Natural language, your custom DSL, etc.). All are stored in files, and those model files will be structured and stored into a physical container (i.e., File System Folders, Database tables, Cloud storages, etc.).

Physical-InMemory.PNG

Physical address

All those models located in a physical container have some address associated with them. For example, if a file is located on your hard drive in a folder, then you have an address to access the file. For example:

  1. C:\InventorySystem\Rules\Common\Common.xml
  2. C:\InventorySystem\Rules\Dispatch.xml
  3. C:\InventorySystem\Rules\Address.xml

Or you may store those rules in a file server or web server. Then there would be a physical address associated with them. For example:

  1. ftp://InventorySystem/Rules/Common/Common.xml
  2. ftp://InventorySystem/Rules/Dispatch.xml
  3. ftp://InventorySystem/Rules/Address.xml

Depending on how you are hosting those models, you will have different protocol and address associated with each one.

Out-of-the-box, the following containers are supported:

  • File system
  • Network location
  • FTP
  • HTTP

In some scenarios there is no direct and ready-to-use physical address for those models stored in a physical container (e.g., Storing models in cloud blob storage). What you need to do in such cases is to create a custom mechanism to access those containers.

Custom Physical Container

In order to access a custom physical container, you can implement an interface to create a connection and access to those models.

  1. namespace FlexRule.Core.Model.SourceConnections
  2. {
  3.     /// <summary>
  4.     /// An interface to a stream of source data is required to read a model.
  5.     /// This interface would give the functionality required to access the source of a model
  6.     /// </summary>
  7.     public interface ISourceConnection
  8.     {
  9.         /// <summary>
  10.         /// Opens a link to a physical location
  11.         /// </summary>
  12.         void Open();
  13.  
  14.         /// <summary>
  15.         /// Retrieve a stream to read model content
  16.         /// </summary>
  17.         /// <returns></returns>
  18.         Stream ReadSource();
  19.  
  20.         /// <summary>
  21.         /// Close the stream and cleans up the resources
  22.         /// </summary>
  23.         void Close();
  24.     }
  25. }

Sample database repository

For example, let's consider that we have a simple database with one table that stores the content of the rules. If you have implemented a DbSourceConnection like this:

  1. class DbSourceConnection : ISourceConnection
  2. {
  3.     private readonly string _connectionString;
  4.     private readonly string _modelName;
  5.  
  6.     /// <summary>
  7.     /// Creates a database source connection
  8.     /// </summary>
  9.     /// <param name="connectionString">Databse connection string</param>
  10.     /// <param name="modelName">name of model to be loaded from database</param>
  11.     public DbSourceConnection(string connectionString, string modelName)
  12.     {
  13.         _connectionString = connectionString;
  14.         _modelName = modelName;
  15.     }
  16.  
  17.     public void Open()
  18.     {
  19.         // Opens database using connection string
  20.         throw new NotImplementedException();
  21.     }
  22.  
  23.     public Stream ReadSource()
  24.     {
  25.         // Reads model from table using field: _modelName 
  26.         throw new NotImplementedException();
  27.     }
  28.  
  29.     public void Close()
  30.     {
  31.         // Closes database 
  32.         throw new NotImplementedException();
  33.     }
  34. }

Then you can use SourceConnectionReader to read the model like this:

  1. var sourceConnection = new DbSourceConnection("YOUR DATABASE CONNECTION STRING", "Discount");
  2. var sourceContent = new SourceConnectionReader(sourceConnection)
  3.                     .ReadAllBytes(); // reads all the content as binary
  4. if (sourceContent == null)
  5.     throw new DeploymentPackageException("Error in reading source, source can not be empty or null");
  6.  
  7. // Loads model container using LoadAdapterUtility class
  8. var modelContainer = LoadAdapterUtility.LoadNavigableSource(sourceContent);

In-memory address

When models are loaded into memory and stored in a IQueryableSet container like RuleSet, then each model can be accessed using a logical address. Those addresses start with a protocol named ruleset:// and locate a model in memory.

RuleSet is a programmatic group of multiple model containers already loaded into the application. RuleSet provides uniform resource locator (URL)–based addressing of each model. In each RuleSet, multiple ModelContainers can be stored, each able to have multiple rule and logic models. This grouping allows your engines (e.g., procedural, flow, and validation) to load logic and rules based on virtual addressing of a model inside a rule set. Therefore, in instantiating an engine, instead of passing an IElementModel, you can pass the RuleSet object and an address to load the model.

More Details

Loading

Loading a model is a process of converting models stored in physical location to an in-memory and logical container that can be used to access them during Execution plan creation and logic execution.

Simple version

The simple version of loading is using LoadAdapterUtility to do all the plumbing and then fill or create your container with models.

Loading model directly from source file or binary:

public static IElementModel LoadModel(string ruleUri)
public static IElementModel LoadModel(byte[] ruleContent)
Sample 1

Loading from string content:

IElementModel model = LoadAdapterUtility.LoadModel(Encoding.UTF8.GetBytes(rule));
Sample 2

Loading from file address:

IElementModel model = LoadAdapterUtility.LoadModel(@"C:\InventorySystem\Rules\Common\Common.xml");

Loading ModelContainer

A model container is a collection of IElementModels.

Loading model into ModelContainer:

public static void FillNavigableSource(ModelContainer container, string rule, string sectionName = null, string user = null, string userPassword = null)
public static ModelContainer LoadNavigableSource(string rule, string sectionName = null, string user = null, string userPassword = null)

When you have your custom source connection, you can use method ReadAllBytes() of class SourceConnectionReader to read your source and the use the following overload to create a model container:

public static ModelContainer LoadNavigableSource(byte[] rule, string sectionName = null)

Advanced Version

If you're defining your own language or source connection you may need to consider a more advanced version of the loading process. This version includes connecting all the components you see in the diagram below.

LoadingSteps.png
  • In order to load a model from a physical container you need to first create a connection using SourceConnectionBuilder.
var cnnBuilder = new SourceConnectionBuilder();
var sourceConnection = cnnBuilder.Build(rulePath);
  • Then you need to create a source provider and a load adapter
var sourceProvider = new CustomSourceProvider(sourceConnection);
var loadAdapter = new NavigableSourceLoadAdapter(sourceProvider);
  • And last is creating a model container and use the load adapter to fill the container with models
var mc = new ModelContainer();
loadAdapter.Load(mc, sectionName);