Communicate to host

From FlexRule Wiki
Jump to: navigation, search

Creating events

There are different ways to communicate with the host application that is running the rules. One of these is to create and raise an event and let the host application handle it ActiveElementCallback via the ProcedureEngine.

HostCallBack allows you to create an event and propagate it to the host application. The host application is the code that is running the rules. It can either be on the client or server side.

How it works

This mechanism allows rules to communicate with the outside world by sending and receiving the required information and data for processing. When an execution reaches the command, its engine will raise an event (ActiveElementCallback). If the host application has handled the event, it will be able to respond to the event.

While raising the event inside of the rule, Tag setting of the command may carry more information to the host application. Then, the host application Response property of the CallbackEventArgs can be set. After event handling is finished, the execution of the rule continues. And the rule will be able to retrieve data by setting responseRef to some local parameters for later use. The key setting of the command allows the rule to send a decision key to the host application. Then the application will be able to respond to the event based on the set key.

Rule Sample

  1. <Procedure name="HostCallBackSample" enabled="true">
  2.  
  3.   <Declaration>
  4.     <Define name="userName" direction="In"/>
  5.     <Define name="userRole" direction="local"/>
  6.   </Declaration>
  7.  
  8.   <HostCallback tagValue="userName" key="askUserRole" responseRef="userRole"/>
  9.  
  10.   <If condition='userRole=="Admin"'>
  11.     <!--
  12.     Do something
  13.     -->
  14.   </If>
  15. </Procedure>

Code Sample

  1. // Create your engine from a procedure execution plan named 'proc'
  2. ProcedureEngine engine = CreateEngine(proc);
  3. // Handles callback event of engine
  4. engine.ActiveElementCallback += engine_ActiveElementCallback;

And later on you have the event handler:

  1. void engine_ActiveElementCallback(object sender, CallbackEventArgs e)
  2. {
  3.     switch (e.Key)
  4.     {
  5.         case "askUser":
  6.             // you can decide based a tag value
  7.             if (e.Tag != null)
  8.             {
  9.                 // e.g. loopup for user role 
  10.                 // set the response, then the rules will store the value in a processing context 
  11.                 e.Response = "Admin";
  12.             }
  13.             break;
  14.     }
  15. }