/**
 * Copyright (c) 2012 committers of YAKINDU and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */

package org.yakindu.sct.simulation.core.sexec.container;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.emf.common.util.WrappedException;
import org.yakindu.sct.model.sexec.ExecutionFlow;
import org.yakindu.sct.model.sexec.transformation.IModelSequencer;
import org.yakindu.sct.model.sgraph.Statechart;
import org.yakindu.sct.simulation.core.Activator;
import org.yakindu.sct.simulation.core.engine.IExecutionControl;
import org.yakindu.sct.simulation.core.engine.ISimulationEngine;
import org.yakindu.sct.simulation.core.sexec.interpreter.IExecutionFlowInterpreter;
import org.yakindu.sct.simulation.core.sruntime.ExecutionContext;

import com.google.inject.Inject;

/**
 * Abstract implementation if the {@link ISimulationEngine} interface.
 * transforms a {@link Statechart} to an {@link ExecutionFlow} and holds an
 * {@link ExecutionContext}
 * 
 * @author andreas muelder - Initial contribution and API
 * 
 */
public abstract class AbstractExecutionFlowSimulationEngine implements ISimulationEngine, IExecutionControl {

	public static final int ERROR_DURING_SIMULATION = 765;

	@Inject
	protected ExecutionContext context;
	@Inject
	private IModelSequencer sequencer;
	@Inject
	protected IExecutionFlowInterpreter interpreter;
	@Inject
	private IExecutionContextInitializer contextInitializer;

	protected boolean terminated = false;
	protected boolean suspended = false;

	private Statechart statechart;

	public AbstractExecutionFlowSimulationEngine(Statechart statechart) {
		this.statechart = statechart;
	}

	protected void runCycle() {
		try {
			interpreter.runCycle();
		} catch (WrappedException ex) {
			handleWrappedException(ex);
		}
	}

	private void handleWrappedException(WrappedException ex) {
		Status errorStatus = new Status(Status.ERROR, Activator.PLUGIN_ID, ERROR_DURING_SIMULATION, ex.getCause()
				.getMessage(), ex.getCause());
		IStatusHandler statusHandler = DebugPlugin.getDefault().getStatusHandler(errorStatus);
		try {
			statusHandler.handleStatus(errorStatus, getDebugTarget());
			interpreter.suspend();
			interpreter.tearDown();
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	protected Object getDebugTarget() {
		IDebugTarget[] debugTargets = DebugPlugin.getDefault().getLaunchManager().getDebugTargets();
		for (IDebugTarget iDebugTarget : debugTargets) {
			if (iDebugTarget.isTerminated())
				continue;
			if (iDebugTarget.getAdapter(ISimulationEngine.class) == this)
				return iDebugTarget;
		}
		return null;
	}

	@Override
	public void init() {
		ExecutionFlow flow = sequencer.transform(statechart);
		if (!context.isSnapshot()) {
			contextInitializer.initialize(context, flow);
		}
		interpreter.initialize(flow, context);
	}

	public void start() {
		try {
			interpreter.enter();
		} catch (WrappedException ex) {
			handleWrappedException(ex);
		}
	}

	public void suspend() {
		suspended = true;
	}

	public void resume() {
		try {
			suspended = false;
			interpreter.resume();
		} catch (WrappedException ex) {
			handleWrappedException(ex);
		}
	}

	public void terminate() {
		terminated = true;
		interpreter.tearDown();
	}

	public void stepForward() {
		try {
			interpreter.resume();
			interpreter.runCycle();
			interpreter.suspend();
		} catch (WrappedException ex) {
			handleWrappedException(ex);
		}
	}

	public void stepBackward() {

	}

	@Override
	public ExecutionContext getExecutionContext() {
		return context;
	}

	public void setExecutionContext(ExecutionContext context) {
		this.context = context;
	}

	@Override
	public IExecutionControl getExecutionControl() {
		return this;
	}

}

@@ -732,32 +732,226 @@
 ppedException;%0D%0A
+import org.yakindu.base.types.validation.IValidationIssueAcceptor.ListBasedValidationIssueAcceptor;%0D%0Aimport org.yakindu.base.types.validation.IValidationIssueAcceptor.ValidationIssue.Severity;%0D%0A
 import org.yakin
@@ -3391,61 +3391,594 @@
 %0D%0A%09%09
-ExecutionFlow flow = sequencer.transform(statechart);
+ListBasedValidationIssueAcceptor acceptor = new ListBasedValidationIssueAcceptor();%0D%0A%09%09ExecutionFlow flow = sequencer.transform(statechart, acceptor);%0D%0A%09%09if (acceptor.getTraces(Severity.ERROR).size() %3E 0) %7B%0D%0A%09%09%09Status errorStatus = new Status(Status.ERROR, Activator.PLUGIN_ID, acceptor.getTraces(Severity.ERROR)%0D%0A%09%09%09%09%09.iterator().next().toString());%0D%0A%09%09%09IStatusHandler statusHandler = DebugPlugin.getDefault().getStatusHandler(errorStatus);%0D%0A%09%09%09try %7B%0D%0A%09%09%09%09statusHandler.handleStatus(errorStatus, getDebugTarget());%0D%0A%09%09%09%7D catch (CoreException e) %7B%0D%0A%09%09%09%09e.printStackTrace();%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A
 %0D%0A%09%09

Handle Errors during transformation