@Override
  public Object execute(Context context) {
    KieSession kieSession = ((KnowledgeCommandContext) context).getKieSession();
    TimerManager tm = getTimerManager(kieSession);

    RuleFlowProcessInstance wfp =
        (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId);

    for (NodeInstance nodeInstance : wfp.getNodeInstances()) {
      if (nodeInstance instanceof TimerNodeInstance) {
        TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
        if (tni.getNodeName().equals(timerName)) {
          TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());

          tm.cancelTimer(timer.getTimerId());
          TimerInstance newTimer = new TimerInstance();

          if (delay != 0) {
            long diff = System.currentTimeMillis() - timer.getActivated().getTime();
            newTimer.setDelay(delay * 1000 - diff);
          }
          newTimer.setPeriod(period);
          newTimer.setRepeatLimit(repeatLimit);
          newTimer.setTimerId(timer.getTimerId());
          tm.registerTimer(newTimer, wfp);

          tni.internalSetTimerId(newTimer.getId());

          break;
        }
      }
    }
    return null;
  }
Exemple #2
0
 public ProcessInstance getProcessInstance(long arg0, boolean readOnly) {
   RuleFlowProcessInstance pi = new RuleFlowProcessInstance();
   pi.setId(arg0);
   ProcessImpl processImpl = new ProcessImpl();
   processImpl.setId("" + arg0);
   pi.setProcess(processImpl);
   return pi;
 }
  @Test
  public void testSimpleReturnValueConstraintEvaluator() throws Exception {
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");

    ProcessDescr processDescr = new ProcessDescr();
    processDescr.setClassName("Process1");
    processDescr.setName("Process1");

    WorkflowProcessImpl process = new WorkflowProcessImpl();
    process.setName("Process1");
    process.setPackageName("pkg1");

    ReturnValueDescr descr = new ReturnValueDescr();
    descr.setText("return value;");

    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    DialectCompiletimeRegistry dialectRegistry =
        pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");

    ProcessBuildContext context =
        new ProcessBuildContext(
            pkgBuilder, pkg, process, processDescr, dialectRegistry, javaDialect);

    pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));

    ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();

    final JavaReturnValueEvaluatorBuilder builder = new JavaReturnValueEvaluatorBuilder();
    builder.build(context, node, descr, null);

    ProcessDialectRegistry.getDialect(JavaDialect.ID).addProcess(context);
    javaDialect.compileAll();
    assertEquals(0, javaDialect.getResults().size());

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    List<KnowledgePackage> packages = new ArrayList<KnowledgePackage>();
    packages.add(pkgBuilder.getPackage());
    kbase.addKnowledgePackages(packages);
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ksession.setGlobal("value", true);

    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
    processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);

    SplitInstance splitInstance = new SplitInstance();
    splitInstance.setProcessInstance(processInstance);

    assertTrue(node.evaluate(splitInstance, null, null));

    ksession.setGlobal("value", false);

    assertFalse(node.evaluate(splitInstance, null, null));
  }
  @Test
  public void testPersistenceSubProcess() {
    KieSession service = (KieSession) ctx.getBean("jpaSingleSessionCommandService2");

    int sessionId = service.getId();

    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.startProcess("org.drools.test.ProcessSubProcess");
    log.info("Started process instance {}", processInstance.getId());
    long processInstanceId = processInstance.getId();

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    final Environment env = (Environment) ctx.getBean("env");
    /*Environment env = KnowledgeBaseFactory.newEnvironment();
    env.set( EnvironmentName.ENTITY_MANAGER_FACTORY,
             ctx.getBean( "myEmf" ) );
    env.set( EnvironmentName.TRANSACTION_MANAGER,
             ctx.getBean( "txManager" ) );
    */
    KieStoreServices kstore = (KieStoreServices) ctx.getBean("kstore1");
    KieBase kbase1 = (KieBase) ctx.getBean("kb_persistence");
    service = kstore.loadKieSession(sessionId, kbase1, null, env);

    processInstance = (RuleFlowProcessInstance) service.getProcessInstance(processInstanceId);
    assertNotNull(processInstance);

    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.getProcessInstance(subProcessInstanceId);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = kstore.loadKieSession(sessionId, kbase1, null, env);
    service.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    service.dispose();

    service = kstore.loadKieSession(sessionId, kbase1, null, env);
    subProcessInstance = (RuleFlowProcessInstance) service.getProcessInstance(subProcessInstanceId);
    assertNull(subProcessInstance);

    processInstance = (RuleFlowProcessInstance) service.getProcessInstance(processInstanceId);
    assertNull(processInstance);
    service.dispose();
  }
  @Test
  public void testPersistenceSubProcess() {
    setUp();

    Properties properties = new Properties();
    properties.setProperty("drools.commandService", SingleSessionCommandService.class.getName());
    properties.setProperty(
        "drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty(
        "drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty(
        "drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = new SessionConfiguration(properties);

    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    Package pkg = getProcessSubProcess();
    ruleBase.addPackage(pkg);

    SingleSessionCommandService service = new SingleSessionCommandService(ruleBase, config, env);
    int sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance =
        (RuleFlowProcessInstance) service.execute(startProcessCommand);
    System.out.println("Started process instance " + processInstance.getId());
    long processInstanceId = processInstance.getId();

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);

    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance =
        (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance =
        (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();

    service = new SingleSessionCommandService(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);

    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
  }
Exemple #6
0
  @Test
  @Ignore
  public void testTimer() {
    //        AbstractRuleBase ruleBase = (AbstractRuleBase) RuleBaseFactory.newRuleBase();
    //        ExecutorService executorService = new DefaultExecutorService();
    //        final StatefulSession workingMemory = new ReteooStatefulSession(1, ruleBase,
    // executorService);
    //        executorService.setCommandExecutor( new CommandExecutor( workingMemory ) );
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final StatefulKnowledgeSession workingMemory = kbase.newStatefulKnowledgeSession();

    RuleFlowProcessInstance processInstance =
        new RuleFlowProcessInstance() {
          private static final long serialVersionUID = 510l;

          public void signalEvent(String type, Object event) {
            if ("timerTriggered".equals(type)) {
              TimerInstance timer = (TimerInstance) event;
              logger.info("Timer {} triggered", timer.getId());
              counter++;
            }
          }
        };
    processInstance.setKnowledgeRuntime(
        ((InternalWorkingMemory) workingMemory).getKnowledgeRuntime());
    processInstance.setId(1234);
    InternalProcessRuntime processRuntime =
        ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime());
    processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);

    new Thread(
            new Runnable() {
              public void run() {
                workingMemory.fireUntilHalt();
              }
            })
        .start();

    TimerManager timerManager =
        ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime())
            .getTimerManager();
    TimerInstance timer = new TimerInstance();
    timerManager.registerTimer(timer, processInstance);
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      // do nothing
    }
    assertEquals(1, counter);

    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      // do nothing
    }
    assertEquals(1, counter);

    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timer.setPeriod(300);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
      Thread.sleep(700);
    } catch (InterruptedException e) {
      // do nothing
    }
    assertEquals(1, counter);

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      // do nothing
    }
    // we can't know exactly how many times this will fire as timers are not precise, but should be
    // atleast 4
    assertTrue(counter >= 4);

    timerManager.cancelTimer(timer.getId());
    int lastCount = counter;
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      // do nothing
    }
    assertEquals(lastCount, counter);
  }