Communicate to host

From FlexRule Wiki
Jump to: navigation, search

Creating events

There are different ways to communicate to the host application that is running the rules. One of the is creating and raising event and let the host application handles event ActiveElementCallback via the ProcedureEngine.

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

How it works

This mechanism allows rules to communicate to outside word and send and received 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 will be able to response to the event.

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

Rule Sample

  1. <Procedure name="HostCallBackSample" enabled="true">
  3.   <Declaration>
  4.     <Define name="userName" direction="In"/>
  5.     <Define name="userRole" direction="local"/>
  6.   </Declaration>
  8.   <HostCallback tagValue="userName" key="askUserRole" responseRef="userRole"/>
  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. }