停止

可以使用 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 ,即可在下一个挂起的规则触发中恢复已停止的策略执行。 恢复已停止的策略执行的示例代码如下:

//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 方法时,不会缓存规则引擎实例。

另请参阅

引擎控制函数