转载

Urule增加激活执行组并执行功能

为了满足支持多重的循环的要求,增加了循环组的概念。支持多重循环调用。

第一步:增加function

package com.huida.platform.rule.model.function.impl;


import com.huida.platform.rule.model.function.Argument;
import com.huida.platform.rule.model.function.FunctionDescriptor;
import com.huida.platform.rule.runtime.WorkingMemory;

public class ActiveAndExecutePendedFunctionDescriptor implements FunctionDescriptor {
  public Argument getArgument() {
    Argument argument = new Argument();
    argument.setName("执行组名");
    argument.setNeedProperty(false);
    return argument;
  }
  
  public Object doFunction(Object paramObject, String paramString, WorkingMemory paramWorkingMemory) {
    if (paramObject == null)
      return null; 
    String str = paramObject.toString();
    paramWorkingMemory.activePendedGroupAndExecute(str);
    return null;
  }
  
  public String getName() {
    return "ActivePended";
  }
  
  public String getLabel() {
    return "激活执行组并执行";
  }
  
  public boolean isDisabled() {
    return false;
  }
}

第二步:WorkingMemory增加接口

/**
 * 激活并执行执行组
 * @param paramString
 */
void activePendedGroupAndExecute(String paramString);

第三部:Agent增加实现方法

/**
 * 激活执行组并执行
 * @param groupname
 */
public void activePendedGroupAndExecute(String groupname, List<Object> facts, List<ReteInstance> reteInstanceList, EvaluationContextImpl evaluationContext) {
   /**
    * 1.获取执行组(agenda-group)
    * 2.执行[执行组里面的规则]
    */
   if(agendaGroupMap.containsKey(groupname)) {
      AgendaGroup agendaGroup =  agendaGroupMap.get(groupname);
      if (null != agendaGroup) {
         agendaGroup.setFocus(true);
         agendaGroup.setExecuted(false);
         List<ActionValue> actionValues=new ArrayList<ActionValue>();
        
         List<Activation>  activations =agendaGroup.getActivations();
         for(Activation activation:activations){
            activation.setProcessed(false);
         }

         List<ReteInstance>  list =  parentSession.getReteInstanceList();
         for(ReteInstance reteInstance:list){
            for(Object obj:facts) {
               reteInstance.resetForReevaluate(obj);
               Collection<FactTracker> trackers=reteInstance.enter(evaluationContext, obj);

            }
         }
         agendaGroup.execute(context,null,Integer.MAX_VALUE,actionValues);
      } else {
         System.out.println("AgendaGroup is empty");
      }
   }
}
实现重置rete网络,rete网络判定 LHS,执行action动作 RHS agendaGroupMap是AgendaGroupRuleBox里面的    
正文到此结束
Loading...