All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.activiti.crystalball.simulator.SimulationRunContext Maven / Gradle / Ivy

The newest version!
/* 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
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.runtime.Clock;

/**
 * Context in which simulation is run.
 * It contains references to process engine, event calendar.
 *
 * @author martin.grofcik
 */
public abstract class SimulationRunContext {
    
	//
	//  Process engine on which simulation will be executed
	//
	protected static ThreadLocal> processEngineThreadLocal = new ThreadLocal>();

  //
  // Simulation objects
  //
	protected static ThreadLocal> eventCalendarThreadLocal = new ThreadLocal>();

  //
  // Simulation run Id
  //
  protected static ThreadLocal> simulationRunIdThreadLocal = new ThreadLocal>();

  //
  // Variable scope used for getting/setting variables to the simulationManager
  //
  protected static ThreadLocal> executionThreadLocal = new ThreadLocal>();

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

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

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

	public static void removeProcessEngine() {
	  getStack(processEngineThreadLocal).pop();
	}

	public static TaskService getTaskService() {
		Stack stack = getStack(processEngineThreadLocal);
		if (stack.isEmpty()) {
			return null;
		}
		return stack.peek().getTaskService();
	}
	
	public static EventCalendar getEventCalendar() {
		Stack stack = getStack(eventCalendarThreadLocal);
		if (stack.isEmpty()) {
			return null;
		}
		return stack.peek();
	}

	public static void setEventCalendar(EventCalendar eventCalendar) {
	  getStack(eventCalendarThreadLocal).push(eventCalendar);
	}

  public static String getSimulationRunId() {
    Stack stack = getStack(simulationRunIdThreadLocal);
    if (stack.isEmpty()) {
      return null;
    }
    return stack.peek();
  }

  public static void setSimulationRunId(String simulationRunId) {
    getStack(simulationRunIdThreadLocal).push(simulationRunId);
  }

  public static void removeEventCalendar() {
	  getStack(eventCalendarThreadLocal).pop();
	}

	public static HistoryService getHistoryService() {
		Stack stack = getStack(processEngineThreadLocal);
		if (stack.isEmpty()) {
			return null;
		}
		return stack.peek().getHistoryService();
	}

	
  public static RepositoryService getRepositoryService() {
		Stack stack = getStack(processEngineThreadLocal);
		if (stack.isEmpty()) {
			return null;
		}
		return stack.peek().getRepositoryService();
	}

  public static VariableScope getExecution() {
    Stack stack = getStack(executionThreadLocal);
    if (stack.isEmpty()) {
      return null;
    }
    return stack.peek();
  }

  public static void setExecution(VariableScope execution) {
    getStack(executionThreadLocal).push(execution);
  }

  public static Clock getClock() {
    return getProcessEngine().getProcessEngineConfiguration().getClock();
  }

  protected static  Stack getStack(ThreadLocal> threadLocal) {
		Stack stack = threadLocal.get();
		if (stack == null) {
			stack = new Stack();
			threadLocal.set(stack);
		}
		return stack;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy