Java Code Examples for org.drools.runtime.StatefulKnowledgeSession#fireAllRules()

The following examples show how to use org.drools.runtime.StatefulKnowledgeSession#fireAllRules() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BusinessRuleTaskActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void execute(DelegateExecution execution) {
  ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(execution.getProcessDefinitionId());
  String deploymentId = processDefinition.getDeploymentId();

  KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId);
  StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();

  if (variablesInputExpressions != null) {
    Iterator<Expression> itVariable = variablesInputExpressions.iterator();
    while (itVariable.hasNext()) {
      Expression variable = itVariable.next();
      ksession.insert(variable.getValue(execution));
    }
  }

  if (!rulesExpressions.isEmpty()) {
    RulesAgendaFilter filter = new RulesAgendaFilter();
    Iterator<Expression> itRuleNames = rulesExpressions.iterator();
    while (itRuleNames.hasNext()) {
      Expression ruleName = itRuleNames.next();
      filter.addSuffic(ruleName.getValue(execution).toString());
    }
    filter.setAccept(!exclude);
    ksession.fireAllRules(filter);

  } else {
    ksession.fireAllRules();
  }

  Collection<Object> ruleOutputObjects = ksession.getObjects();
  if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {
    Collection<Object> outputVariables = new ArrayList<Object>();
    for (Object object : ruleOutputObjects) {
      outputVariables.add(object);
    }
    execution.setVariable(resultVariable, outputVariables);
  }
  ksession.dispose();
  leave(execution);
}
 
Example 2
Source File: ClusterMonitor.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
private FactHandle evaluate(StatefulKnowledgeSession ksession, FactHandle handle, Object obj) {
    if (handle == null) {
        ksession.setGlobal("delegator", new RuleTasksDelegator());
        handle = ksession.insert(obj);
    } else {
        ksession.update(handle, obj);
    }
    ksession.fireAllRules();
    if (log.isDebugEnabled()) {
        log.debug(String.format("Rule executed for: %s ", obj));
    }
    return handle;
}
 
Example 3
Source File: RulesExecutorData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected StatefulKnowledgeSession initNewKnowledgeSession() {
  StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
  for ( int i = 0; i < columnList.length; i++ ) {
    session.insert( columnList[i] );
  }

  session.fireAllRules();
  return session;
}
 
Example 4
Source File: BusinessRuleTaskActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public void execute(DelegateExecution execution) {
  ActivityExecution activityExecution = (ActivityExecution) execution;
  PvmProcessDefinition processDefinition = activityExecution.getActivity().getProcessDefinition();
  String deploymentId = processDefinition.getDeploymentId();
  
  KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId); 
  StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
  
  if (variablesInputExpressions != null) {
    Iterator<Expression> itVariable = variablesInputExpressions.iterator();
    while (itVariable.hasNext()) {
      Expression variable = itVariable.next();
      ksession.insert(variable.getValue(execution));
    }
  }
  
  if (!rulesExpressions.isEmpty()) {
    RulesAgendaFilter filter = new RulesAgendaFilter();
    Iterator<Expression> itRuleNames = rulesExpressions.iterator();
    while (itRuleNames.hasNext()) {
      Expression ruleName = itRuleNames.next();
      filter.addSuffic(ruleName.getValue(execution).toString());
    }
    filter.setAccept(!exclude);
    ksession.fireAllRules(filter);
    
  } else {
    ksession.fireAllRules();
  }
  
  Collection<Object> ruleOutputObjects = ksession.getObjects();
  if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {
    Collection<Object> outputVariables = new ArrayList<Object>();
    for (Object object : ruleOutputObjects) {
      outputVariables.add(object);
    }
    execution.setVariable(resultVariable, outputVariables);
  }
  ksession.dispose();
  leave(activityExecution);
}