Skip to content

Commit

Permalink
First version of crystalball added to Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsrademakers committed Jul 21, 2014
1 parent ad69603 commit f2eb4b7
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
*/


import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.delegate.VariableScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class implements all methods for Simulation run
*
Expand All @@ -37,7 +37,7 @@ public abstract class AbstractSimulationRun implements SimulationRun, Simulation
* Map for eventType -> event handlers to execute events on simulation engine
*/
protected Map<String, SimulationEventHandler> eventHandlerMap = new HashMap<String, SimulationEventHandler>();
protected ProcessEngineImpl processEngine;
protected ProcessEngine processEngine;

public AbstractSimulationRun(Map<String, SimulationEventHandler> eventHandlers) {
if (eventHandlers != null && !eventHandlers.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.activiti.crystalball.simulator;

import java.util.List;

/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -27,6 +29,8 @@ public interface EventCalendar {
SimulationEvent removeFirstEvent();

void addEvent(SimulationEvent event);

List<SimulationEvent> getEvents();

void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
*/


import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.ProcessEngineImpl;

import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;

/**
* This class provides simulation run for replay purposes
* replay uses real time and running engine to execute simulation events.
Expand All @@ -29,11 +30,11 @@ public class ReplaySimulationRun extends AbstractSimulationRun {

private final EventCalendar eventCalendar;

public ReplaySimulationRun(ProcessEngineImpl processEngine, Map<String, SimulationEventHandler> customEventHandlerMap) {
public ReplaySimulationRun(ProcessEngine processEngine, Map<String, SimulationEventHandler> customEventHandlerMap) {
this(processEngine, new SimpleEventCalendar(processEngine.getProcessEngineConfiguration().getClock(), new SimulationEventComparator()), customEventHandlerMap);
}

public ReplaySimulationRun(ProcessEngineImpl processEngine, EventCalendar eventCalendar, Map<String, SimulationEventHandler> customEventHandlerMap) {
public ReplaySimulationRun(ProcessEngine processEngine, EventCalendar eventCalendar, Map<String, SimulationEventHandler> customEventHandlerMap) {
super(customEventHandlerMap);
this.processEngine = processEngine;
this.eventCalendar = eventCalendar;
Expand All @@ -43,7 +44,8 @@ public ReplaySimulationRun(ProcessEngineImpl processEngine, EventCalendar eventC
protected void initSimulationRunContext(VariableScope execution) {
SimulationRunContext.setEventCalendar(eventCalendar);
SimulationRunContext.setProcessEngine(processEngine);
SimulationRunContext.setSimulationRunId(processEngine.getProcessEngineConfiguration().getIdGenerator().getNextId());
ProcessEngineConfigurationImpl configuration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
SimulationRunContext.setSimulationRunId(configuration.getIdGenerator().getNextId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
*/


import org.activiti.engine.runtime.ClockReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.runtime.ClockReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author martin.grofcik
*/
Expand Down Expand Up @@ -63,8 +64,9 @@ public SimulationEvent removeFirstEvent() {

SimulationEvent minEvent = eventList.remove( minIndex );

if (minEvent.hasSimulationTime() && minEvent.getSimulationTime() < this.clockReader.getCurrentTime().getTime())
throw new RuntimeException("Unable to execute event from the past");
if (minEvent.hasSimulationTime() && minEvent.getSimulationTime() < this.clockReader.getCurrentTime().getTime()) {
throw new ActivitiException("Unable to execute event from the past");
}

if (eventList.isEmpty()) {
minIndex = NULL;
Expand All @@ -80,6 +82,11 @@ public SimulationEvent removeFirstEvent() {
}
return minEvent;
}

@Override
public List<SimulationEvent> getEvents() {
return eventList;
}

@Override
public void addEvent(SimulationEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
*/
package org.activiti.crystalball.simulator;

import java.util.Stack;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.runtime.Clock;

import java.util.Stack;

/**
* Context in which simulation is run.
* It contains references to process engine, event calendar.
Expand All @@ -33,7 +33,7 @@ public abstract class SimulationRunContext {
//
// Process engine on which simulation will be executed
//
protected static ThreadLocal<Stack<ProcessEngineImpl>> processEngineThreadLocal = new ThreadLocal<Stack<ProcessEngineImpl>>();
protected static ThreadLocal<Stack<ProcessEngine>> processEngineThreadLocal = new ThreadLocal<Stack<ProcessEngine>>();

//
// Simulation objects
Expand All @@ -51,18 +51,18 @@ public abstract class SimulationRunContext {
protected static ThreadLocal<Stack<VariableScope>> executionThreadLocal = new ThreadLocal<Stack<VariableScope>>();

public static RuntimeService getRuntimeService() {
Stack<ProcessEngineImpl> stack = getStack(processEngineThreadLocal);
Stack<ProcessEngine> stack = getStack(processEngineThreadLocal);
if (stack.isEmpty()) {
return null;
}
return stack.peek().getRuntimeService();
}

public static void setProcessEngine(ProcessEngineImpl processEngine) {
public static void setProcessEngine(ProcessEngine processEngine) {
getStack(processEngineThreadLocal).push(processEngine);
}

public static ProcessEngineImpl getProcessEngine() {
public static ProcessEngine getProcessEngine() {
return getStack(processEngineThreadLocal).peek();
}

Expand All @@ -71,7 +71,7 @@ public static void removeProcessEngine() {
}

public static TaskService getTaskService() {
Stack<ProcessEngineImpl> stack = getStack(processEngineThreadLocal);
Stack<ProcessEngine> stack = getStack(processEngineThreadLocal);
if (stack.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void removeEventCalendar() {
}

public static HistoryService getHistoryService() {
Stack<ProcessEngineImpl> stack = getStack(processEngineThreadLocal);
Stack<ProcessEngine> stack = getStack(processEngineThreadLocal);
if (stack.isEmpty()) {
return null;
}
Expand All @@ -116,7 +116,7 @@ public static HistoryService getHistoryService() {


public static RepositoryService getRepositoryService() {
Stack<ProcessEngineImpl> stack = getStack(processEngineThreadLocal);
Stack<ProcessEngine> stack = getStack(processEngineThreadLocal);
if (stack.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public SimulationEvent apply(EventLogEntry event) {
simEventProperties.put(variablesKey, variableMap);
simEventProperties.put(PROCESS_INSTANCE_ID, processInstanceId);

return new SimulationEvent.Builder(simulationEventType).
priority((int) event.getLogNumber()).
properties(simEventProperties).
build();
return new SimulationEvent.Builder(simulationEventType)
.priority((int) event.getLogNumber())
.properties(simEventProperties)
.build();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ public SimulationEvent apply(EventLogEntry event) {
properties.put(VARIABLES_LOCAL_SCOPE, localScope);
}

return
new SimulationEvent.Builder(this.simulationEventType).
priority((int) event.getLogNumber()).
properties(properties).
build();
return new SimulationEvent.Builder(this.simulationEventType)
.priority((int) event.getLogNumber())
.properties(properties)
.build();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void init() {
public void handle(SimulationEvent event) {
// start process now
String processDefinitionId = (String) event.getProperty(processToStartIdKey);
String businessKey = (String) event.getProperty(this.businessKey);
String eventBusinessKey = (String) event.getProperty(this.businessKey);
Map<String, Object> variables = new HashMap<String, Object>();
Map<String, Object> processVariables = (Map<String, Object>) event.getProperty(variablesKey);
if (processVariables != null) {
Expand All @@ -68,7 +68,13 @@ public void handle(SimulationEvent event) {
variables.put(PROCESS_INSTANCE_ID, processInstanceId);
variables.put(SIMULATION_RUN_ID, SimulationRunContext.getSimulationRunId());

log.debug("Starting new processDefId[{}] businessKey[{}] with variables[{}]", processDefinitionId, businessKey, variables);
SimulationRunContext.getRuntimeService().startProcessInstanceById(processDefinitionId, businessKey, variables);
String startBusinessKey = null;
if (eventBusinessKey != null) {
startBusinessKey = eventBusinessKey;
} else {
startBusinessKey = this.businessKey;
}
log.debug("Starting new processDefId[{}] businessKey[{}] with variables[{}]", processDefinitionId, startBusinessKey, variables);
SimulationRunContext.getRuntimeService().startProcessInstanceById(processDefinitionId, startBusinessKey, variables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
ProcessEngineConfigurationImpl configuration = new org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration();
configuration.
setHistory("full").
setDatabaseSchemaUpdate("create-drop").
setDatabaseSchemaUpdate("drop-create").
setJobExecutorActivate(false);
configuration.setCustomDefaultBpmnParseHandlers(Arrays.<BpmnParseHandler>asList(new AddListenerUserTaskParseHandler(TaskListener.EVENTNAME_CREATE, new UserTaskExecutionListener(USER_TASK_COMPLETED_EVENT_TYPE, USER_TASK_COMPLETED_EVENT_TYPE, listener.getSimulationEvents()))));
configuration.setEventListeners(Arrays.<ActivitiEventListener>asList(listener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ public interface ManagementService {
*/
List<EventLogEntry> getEventLogEntries(Long startLogNr, Long pageSize);

/**
* [EXPERIMENTAL]
*
* Returns a list of event log entries for a specific process instance id.
* Note that the event logging must specifically must be enabled in the process engine configuration.
*
* Passing null as arguments will effectively fetch ALL event log entries.
* Be careful, as this list might be huge!
*/
List<EventLogEntry> getEventLogEntriesByProcessInstanceId(String processInstanceId);

/**
* Delete a EventLogEntry.
* Typically only used in testing, as deleting log entries defeats the whole purpose of keeping a log.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,25 @@
*/
package org.activiti.engine.impl;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ManagementService;
import org.activiti.engine.event.EventLogEntry;
import org.activiti.engine.impl.cmd.*;
import org.activiti.engine.impl.cmd.CancelJobCmd;
import org.activiti.engine.impl.cmd.CustomSqlExecution;
import org.activiti.engine.impl.cmd.DeleteEventLogEntry;
import org.activiti.engine.impl.cmd.ExecuteCustomSqlCmd;
import org.activiti.engine.impl.cmd.ExecuteJobsCmd;
import org.activiti.engine.impl.cmd.GetEventLogEntriesCmd;
import org.activiti.engine.impl.cmd.GetJobExceptionStacktraceCmd;
import org.activiti.engine.impl.cmd.GetPropertiesCmd;
import org.activiti.engine.impl.cmd.GetTableCountCmd;
import org.activiti.engine.impl.cmd.GetTableMetaDataCmd;
import org.activiti.engine.impl.cmd.GetTableNameCmd;
import org.activiti.engine.impl.cmd.SetJobRetriesCmd;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.db.DbSqlSessionFactory;
import org.activiti.engine.impl.interceptor.Command;
Expand All @@ -25,10 +40,6 @@
import org.activiti.engine.management.TablePageQuery;
import org.activiti.engine.runtime.JobQuery;

import java.sql.Connection;
import java.util.List;
import java.util.Map;


/**
* @author Tom Baeyens
Expand Down Expand Up @@ -118,6 +129,11 @@ public List<EventLogEntry> getEventLogEntries(Long startLogNr, Long pageSize) {
return commandExecutor.execute(new GetEventLogEntriesCmd(startLogNr, pageSize));
}

@Override
public List<EventLogEntry> getEventLogEntriesByProcessInstanceId(String processInstanceId) {
return commandExecutor.execute(new GetEventLogEntriesCmd(processInstanceId));
}

@Override
public void deleteEventLogEntry(long logNr) {
commandExecutor.execute(new DeleteEventLogEntry(logNr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@
*/
public class GetEventLogEntriesCmd implements Command<List<EventLogEntry>> {

protected String processInstanceId = null;
protected Long startLogNr = null;
protected Long pageSize = null;

public GetEventLogEntriesCmd() {

}

public GetEventLogEntriesCmd(String processInstanceId) {
this.processInstanceId = processInstanceId;
}

public GetEventLogEntriesCmd(Long startLogNr, Long pageSize) {
this.startLogNr = startLogNr;
this.pageSize = pageSize;
}

@Override
public List<EventLogEntry> execute(CommandContext commandContext) {
if (startLogNr == null) {
return commandContext.getEventLogEntryEntityManager().findAllEventLogEntries();
}
return commandContext.getEventLogEntryEntityManager().findEventLogEntries(
startLogNr,
pageSize != null ? pageSize : -1);
if (processInstanceId != null) {
return commandContext.getEventLogEntryEntityManager().findEventLogEntriesByProcessInstanceId(processInstanceId);

} else if (startLogNr != null) {
return commandContext.getEventLogEntryEntityManager().findEventLogEntries(
startLogNr,
pageSize != null ? pageSize : -1);

} else {
return commandContext.getEventLogEntryEntityManager().findAllEventLogEntries();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class DbSqlSessionFactory implements SessionFactory {
addDatabaseSpecificStatement("postgres", "insertEventLogEntry", "insertEventLogEntry_postgres");
addDatabaseSpecificStatement("postgres", "selectAllEventLogEntries", "selectAllEventLogEntries_postgres");
addDatabaseSpecificStatement("postgres", "selectEventLogEntries", "selectEventLogEntries_postgres");
addDatabaseSpecificStatement("postgres", "selectEventLogEntriesByProcessInstanceId", "selectEventLogEntriesByProcessInstanceId_postgres");

// oracle
databaseSpecificLimitBeforeStatements.put("oracle", "select * from ( select a.*, ROWNUM rnum from (");
Expand Down
Loading

0 comments on commit f2eb4b7

Please sign in to comment.