org.drools.runtime.StatefulKnowledgeSession Java Examples

The following examples show how to use org.drools.runtime.StatefulKnowledgeSession. 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: RulesExecutorDataTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeTest() {
  // init
  data = spy( new RulesExecutorData() );
  ValueMetaInterface c1 = mock( ValueMetaInterface.class );
  when( c1.getName() ).thenReturn( "c1" );
  ValueMetaInterface c2 = mock( ValueMetaInterface.class );
  when( c2.getName() ).thenReturn( "c2" );
  List<ValueMetaInterface> initColumns = Arrays.asList( c1, c2 );

  Rules.Column fc1 = new Rules.Column();
  fc1.setName( "c1" );
  List<Rules.Column> fetchedColumns = new ArrayList<Rules.Column>( Arrays.asList( fc1 ) );
  List<Rules.Column> fetchedColumnsSpy = spy( fetchedColumns );

  StatefulKnowledgeSession session = mock( StatefulKnowledgeSession.class );
  doReturn( session ).when( data ).initNewKnowledgeSession();
  doReturn( fetchedColumnsSpy ).when( data ).fetchColumns( session );

  RowMetaInterface rowMeta = mock( RowMetaInterface.class );
  when( rowMeta.getValueMetaList() ).thenReturn( initColumns );
  data.initializeColumns( rowMeta );
}
 
Example #2
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 #3
Source File: RulesExecutorData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected Collection<Object> fetchColumns( StatefulKnowledgeSession session ) {
  Collection<Object> oList = session.getObjects( new ObjectFilter() {
    @Override
    public boolean accept( Object o ) {
      if ( o instanceof Column && !( (Column) o ).isExternalSource() ) {
        return true;
      }
      return false;
    }
  } );
  return oList;
}
 
Example #4
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 #5
Source File: RulesExecutorData.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void execute() {
  StatefulKnowledgeSession session = initNewKnowledgeSession();

  Collection<Object> oList = fetchColumns( session );
  for ( Object o : oList ) {
    resultMap.put( ( (Column) o ).getName(), (Column) o );
  }

  session.dispose();
}
 
Example #6
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 #7
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void setObsoleteCheckKnowledgeSession(
        StatefulKnowledgeSession obsoleteCheckKnowledgeSession) {
    this.obsoleteCheckKnowledgeSession = obsoleteCheckKnowledgeSession;
}
 
Example #8
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getScaleCheckKnowledgeSession() {
    return scaleCheckKnowledgeSession;
}
 
Example #9
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void setScaleCheckKnowledgeSession(
        StatefulKnowledgeSession scaleCheckKnowledgeSession) {
    this.scaleCheckKnowledgeSession = scaleCheckKnowledgeSession;
}
 
Example #10
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getDependentScaleCheckKnowledgeSession() {
    return dependentScaleCheckKnowledgeSession;
}
 
Example #11
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void setDependentScaleCheckKnowledgeSession(StatefulKnowledgeSession dependentScaleCheckKnowledgeSession) {
    this.dependentScaleCheckKnowledgeSession = dependentScaleCheckKnowledgeSession;
}
 
Example #12
Source File: AutoscalerRuleEvaluator.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getStatefulSession(String drlFileName) {
    StatefulKnowledgeSession ksession;
    ksession = knowledgeBases.get(drlFileName).newStatefulKnowledgeSession();
    ksession.setGlobal("log", RuleLog.getInstance());
    return ksession;
}
 
Example #13
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getObsoleteCheckKnowledgeSession() {
    return obsoleteCheckKnowledgeSession;
}
 
Example #14
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getMaxCheckKnowledgeSession() {
    return maxCheckKnowledgeSession;
}
 
Example #15
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public void setMinCheckKnowledgeSession(
        StatefulKnowledgeSession minCheckKnowledgeSession) {
    this.minCheckKnowledgeSession = minCheckKnowledgeSession;
}
 
Example #16
Source File: ClusterInstanceContext.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
public StatefulKnowledgeSession getMinCheckKnowledgeSession() {
    return minCheckKnowledgeSession;
}
 
Example #17
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);
}