可以使用 Halt 函数来停止当前规则引擎执行。 Halt 函数采用一个类型Boolean参数。 如果将参数的值指定为 true,规则引擎还将清除包含待处理候选规则的计划。

Policy.Execute 方法实际上是对 RuleEngine.Execute 方法的封装器。 它具有类似于以下代码的代码:

RuleEngine.Assert(facts);   
RuleEngine.Execute();   
RuleEngine.Retract(facts);  

如果使用 Policy.Execute 方法执行策略,则执行 Halt 函数时,规则引擎会将控制权返回到 Policy.Execute 方法。 Policy.Execute 方法收回事实,并将控制权返回到调用方。 因此,在这种情况下,无法恢复已停止的策略执行。 使用 “调用规则” 形状调用策略时,会发生同样的事情。

但是,如果使用 RuleEngine.Execute 方法直接执行策略,则只需再次调用 RuleEngine.Execute 即可恢复停止的策略执行,只需再次调用 RuleEngine.Execute (前提是未收回两次调用之间所需的任何对象)。 恢复停止的策略执行的示例代码如下所示:

//assert facts into working memory of the rule engine instance  
RuleEngine.Assert(facts);   
//execute the policy  
RuleEngine.Execute();   
//policy invokes the Halt method when executing actions.   
//control is returned to here when the Halt method is invoked  
  
//when engine is halted do the following  
//Add your code here  
  
//To resume the halted rule engine execution  
RuleEngine.Execute();  
//retract or remove facts frm the working memory of the rule engine  
RuleEngine.Retract(facts);  

请注意, Policy.Execute 方法缓存规则引擎实例以提高性能。 直接使用 RuleEngine.Execute 方法时,不会缓存规则引擎实例。

另请参阅

引擎控制函数