Search in sources :

Example 1 with SimulationEvent

use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.

the class EventOverviewPanel method getTransformers.

protected List<Function<EventLogEntry, SimulationEvent>> getTransformers() {
    List<Function<EventLogEntry, SimulationEvent>> transformers = new ArrayList<Function<EventLogEntry, SimulationEvent>>();
    transformers.add(new EventLogProcessInstanceCreateTransformer(PROCESS_INSTANCE_START_EVENT_TYPE, PROCESS_DEFINITION_ID_KEY, SIMULATION_BUSINESS_KEY, VARIABLES_KEY));
    transformers.add(new EventLogUserTaskCompleteTransformer(USER_TASK_COMPLETED_EVENT_TYPE));
    return transformers;
}
Also used : Function(org.activiti.crystalball.simulator.delegate.event.Function) EventLogEntry(org.activiti.engine.event.EventLogEntry) EventLogProcessInstanceCreateTransformer(org.activiti.crystalball.simulator.delegate.event.impl.EventLogProcessInstanceCreateTransformer) ArrayList(java.util.ArrayList) EventLogUserTaskCompleteTransformer(org.activiti.crystalball.simulator.delegate.event.impl.EventLogUserTaskCompleteTransformer) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent)

Example 2 with SimulationEvent

use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.

the class ReplayEventLogTest method testProcessInstanceStartEvents.

@Test
public void testProcessInstanceStartEvents() throws Exception {
    ProcessEngineImpl processEngine = initProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    ManagementService managementService = processEngine.getManagementService();
    HistoryService historyService = processEngine.getHistoryService();
    // record events
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put(TEST_VARIABLE, TEST_VALUE);
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(USERTASK_PROCESS, BUSINESS_KEY, variables);
    Task task = taskService.createTaskQuery().taskDefinitionKey("userTask").singleResult();
    TimeUnit.MILLISECONDS.sleep(50);
    variables = new HashMap<String, Object>();
    variables.put(TASK_TEST_VARIABLE, TASK_TEST_VALUE);
    taskService.complete(task.getId(), variables);
    // transform log events
    List<EventLogEntry> eventLogEntries = managementService.getEventLogEntries(null, null);
    EventLogTransformer transformer = new EventLogTransformer(getTransformers());
    List<SimulationEvent> simulationEvents = transformer.transform(eventLogEntries);
    SimpleEventCalendar eventCalendar = new SimpleEventCalendar(processEngine.getProcessEngineConfiguration().getClock(), new SimulationEventComparator());
    eventCalendar.addEvents(simulationEvents);
    // replay process instance run
    final SimulationDebugger simRun = new ReplaySimulationRun(processEngine, eventCalendar, getReplayHandlers(processInstance.getId()));
    simRun.init(new NoExecutionVariableScope());
    // original process is finished - there should not be any running process instance/task
    assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
    assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
    simRun.step();
    // replay process was started
    ProcessInstance replayProcessInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).singleResult();
    assertNotNull(replayProcessInstance);
    assertTrue(replayProcessInstance.getId().equals(processInstance.getId()) == false);
    assertEquals(TEST_VALUE, runtimeService.getVariable(replayProcessInstance.getId(), TEST_VARIABLE));
    // there should be one task
    assertEquals(1, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
    simRun.step();
    // userTask was completed - replay process was finished
    assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(USERTASK_PROCESS).count());
    assertEquals(0, taskService.createTaskQuery().taskDefinitionKey("userTask").count());
    HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery().processInstanceId(replayProcessInstance.getId()).variableName(TASK_TEST_VARIABLE).singleResult();
    assertNotNull(variableInstance);
    assertEquals(TASK_TEST_VALUE, variableInstance.getValue());
    // close simulation
    simRun.close();
    processEngine.close();
    ProcessEngines.destroy();
}
Also used : ReplaySimulationRun(org.activiti.crystalball.simulator.ReplaySimulationRun) Task(org.activiti.engine.task.Task) RuntimeService(org.activiti.engine.RuntimeService) HashMap(java.util.HashMap) TaskService(org.activiti.engine.TaskService) EventLogEntry(org.activiti.engine.event.EventLogEntry) SimpleEventCalendar(org.activiti.crystalball.simulator.SimpleEventCalendar) NoExecutionVariableScope(org.activiti.engine.impl.el.NoExecutionVariableScope) HistoryService(org.activiti.engine.HistoryService) SimulationEventComparator(org.activiti.crystalball.simulator.SimulationEventComparator) EventLogTransformer(org.activiti.crystalball.simulator.delegate.event.impl.EventLogTransformer) ManagementService(org.activiti.engine.ManagementService) SimulationDebugger(org.activiti.crystalball.simulator.SimulationDebugger) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent) ProcessEngineImpl(org.activiti.engine.impl.ProcessEngineImpl) Test(org.junit.Test)

Example 3 with SimulationEvent

use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.

the class SimulationAcquireJobsRunnable method run.

public synchronized void run() {
    //		    if (log.isLoggable(Level.INFO)) {
    //		      log.info(jobExecutor.getName() + " starting to acquire jobs");
    //		    }
    final CommandExecutor commandExecutor = jobExecutor.getCommandExecutor();
    // while is not needed - repetition is done by event scheduling 
    //		    while (!isInterrupted) {
    isWaiting.set(false);
    int maxJobsPerAcquisition = jobExecutor.getMaxJobsPerAcquisition();
    try {
        AcquiredJobs acquiredJobs = commandExecutor.execute(jobExecutor.getAcquireJobsCmd());
        for (List<String> jobIds : acquiredJobs.getJobIdBatches()) {
            jobExecutor.executeJobs(jobIds);
        }
        // if all jobs were executed
        millisToWait = jobExecutor.getWaitTimeInMillis();
        int jobsAcquired = acquiredJobs.getJobIdBatches().size();
        if (jobsAcquired < maxJobsPerAcquisition) {
            isJobAdded = false;
            // check if the next timer should fire before the normal sleep time is over
            Date duedate = new Date(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait);
            List<TimerEntity> nextTimers = commandExecutor.execute(new GetUnlockedTimersByDuedateCmd(duedate, new Page(0, 1)));
            if (!nextTimers.isEmpty()) {
                long millisTillNextTimer = nextTimers.get(0).getDuedate().getTime() - SimulationRunContext.getClock().getCurrentTime().getTime();
                if (millisTillNextTimer < millisToWait && millisTillNextTimer != 0) {
                    millisToWait = millisTillNextTimer;
                }
            }
        } else {
            millisToWait = 0;
        }
    } catch (ActivitiOptimisticLockingException optimisticLockingException) {
        // See https://activiti.atlassian.net/browse/ACT-1390
        log.trace("Optimistic locking exception during job acquisition. If you have multiple job executors running against the same database, " + "this exception means that this thread tried to acquire a job, which already was acquired by another job executor acquisition thread." + "This is expected behavior in a clustered environment. " + "You can ignore this message if you indeed have multiple job executor acquisition threads running against the same database. " + "Exception message: " + optimisticLockingException.getMessage());
    } catch (Exception e) {
        log.error("exception during job acquisition: " + e.getMessage(), e);
        millisToWait = jobExecutor.getWaitTimeInMillis();
    }
    if ((millisToWait > 0) && (!isJobAdded)) {
        try {
            log.trace("job acquisition thread sleeping for " + millisToWait + " millis");
            synchronized (MONITOR) {
                if (!isInterrupted) {
                    isWaiting.set(true);
                    SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime() + millisToWait).property(this).build();
                    SimulationRunContext.getEventCalendar().addEvent(event);
                // do not need to wait. - event scheduling is enough
                //MONITOR.wait(millisToWait);
                }
            }
            log.trace("job acquisition thread woke up");
        } finally {
        //		          isWaiting.set(false);
        }
    } else {
        // schedule run now
        SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
        SimulationRunContext.getEventCalendar().addEvent(event);
    }
//		    }
//		    if (log.isLoggable(Level.INFO)) {
//		      log.info(jobExecutor.getName() + " stopped job acquisition");
//		    }
}
Also used : TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) Page(org.activiti.engine.impl.Page) Date(java.util.Date) ActivitiOptimisticLockingException(org.activiti.engine.ActivitiOptimisticLockingException) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent)

Example 4 with SimulationEvent

use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.

the class SimulationAcquireJobsRunnable method jobWasAdded.

public void jobWasAdded() {
    isJobAdded = true;
    if (isWaiting.compareAndSet(true, false)) {
        // I am OK with the race condition      
        synchronized (MONITOR) {
            //		        MONITOR.notifyAll();
            //Notify is not needed - event is enough
            SimulationEvent event = new SimulationEvent.Builder(SimulationEvent.TYPE_ACQUIRE_JOB_NOTIFICATION_EVENT).simulationTime(SimulationRunContext.getClock().getCurrentTime().getTime()).property(this).build();
            SimulationRunContext.getEventCalendar().addEvent(event);
        }
    }
}
Also used : SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent)

Example 5 with SimulationEvent

use of org.activiti.crystalball.simulator.SimulationEvent in project Activiti by Activiti.

the class ProcessInstanceCreateTransformer method apply.

@Override
public SimulationEvent apply(ActivitiEvent event) {
    if (ActivitiEventType.ENTITY_INITIALIZED.equals(event.getType()) && (event instanceof ActivitiEntityEvent) && ((ActivitiEntityEvent) event).getEntity() instanceof ProcessInstance && ((ExecutionEntity) ((ActivitiEntityEvent) event).getEntity()).isProcessInstanceType()) {
        ProcessInstance processInstance = (ProcessInstance) ((ActivitiEntityEvent) event).getEntity();
        ExecutionEntity executionEntity = (ExecutionEntity) ((ActivitiEntityEvent) event).getEntity();
        Map<String, Object> simEventProperties = new HashMap<String, Object>();
        simEventProperties.put(processDefinitionIdKey, processInstance.getProcessDefinitionId());
        simEventProperties.put(businessKey, processInstance.getBusinessKey());
        simEventProperties.put(variablesKey, executionEntity.getVariables());
        simEventProperties.put(PROCESS_INSTANCE_ID, executionEntity.getProcessInstanceId());
        return new SimulationEvent.Builder(simulationEventType).simulationTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime().getTime()).properties(simEventProperties).build();
    }
    return null;
}
Also used : ExecutionEntity(org.activiti.engine.impl.persistence.entity.ExecutionEntity) HashMap(java.util.HashMap) ActivitiEntityEvent(org.activiti.engine.delegate.event.ActivitiEntityEvent) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) SimulationEvent(org.activiti.crystalball.simulator.SimulationEvent)

Aggregations

SimulationEvent (org.activiti.crystalball.simulator.SimulationEvent)16 HashMap (java.util.HashMap)7 Function (org.activiti.crystalball.simulator.delegate.event.Function)3 EventLogEntry (org.activiti.engine.event.EventLogEntry)3 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CrystalballException (org.activiti.crystalball.simulator.CrystalballException)2 EventLogProcessInstanceCreateTransformer (org.activiti.crystalball.simulator.delegate.event.impl.EventLogProcessInstanceCreateTransformer)2 EventLogUserTaskCompleteTransformer (org.activiti.crystalball.simulator.delegate.event.impl.EventLogUserTaskCompleteTransformer)2 ActivitiEntityEvent (org.activiti.engine.delegate.event.ActivitiEntityEvent)2 Task (org.activiti.engine.task.Task)2 Date (java.util.Date)1 Map (java.util.Map)1 ReplaySimulationRun (org.activiti.crystalball.simulator.ReplaySimulationRun)1 SimpleEventCalendar (org.activiti.crystalball.simulator.SimpleEventCalendar)1 SimulationDebugger (org.activiti.crystalball.simulator.SimulationDebugger)1 SimulationEventComparator (org.activiti.crystalball.simulator.SimulationEventComparator)1