<commit_before>package hu.eltesoft.modelexecution.ide.debug.model;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.papyrus.moka.debug.MokaBreakpoint;
import org.eclipse.uml2.uml.NamedElement;

import hu.eltesoft.modelexecution.ide.Messages;
import hu.eltesoft.modelexecution.ide.debug.XUmlRtExecutionEngine;
import hu.eltesoft.modelexecution.ide.debug.jvm.VirtualMachineBrowser;
import hu.eltesoft.modelexecution.ide.debug.model.utils.CombiningElementDebugContentProvider;
import hu.eltesoft.modelexecution.ide.debug.registry.BreakpointRegistry;
import hu.eltesoft.modelexecution.ide.debug.ui.DebugViewController;
import hu.eltesoft.modelexecution.ide.debug.util.ModelUtils;
import hu.eltesoft.modelexecution.ide.launch.ModelExecutionLaunchConfig;

public class XUMLRTDebugTarget extends DelegatingDebugTarget {

	private Component defaultComponent = null;

	private List<Component> components = new LinkedList<>();

	private BreakpointRegistry breakpoints = new BreakpointRegistry();

	private ResourceSet resourceSet;

	private VirtualMachineBrowser vmBrowser;

	private ILaunch launch;

	private DebugViewController debugControl = new DebugViewController();

	private EObject entryPoint;

	public XUMLRTDebugTarget(VirtualMachineBrowser vmBrowser, XUmlRtExecutionEngine xUmlRtExecutionEngine,
			ResourceSet resourceSet, ILaunch launch) {
		super(null, xUmlRtExecutionEngine.getDebugTarget());
		this.entryPoint = ModelUtils
				.findEObject(ModelExecutionLaunchConfig.getEntryPoint(launch.getLaunchConfiguration()), resourceSet);
		this.vmBrowser = vmBrowser;
		this.resourceSet = resourceSet;
		this.launch = launch;

		setDebugTarget(this);
	}

	public DebugViewController getDebugControl() {
		return debugControl;
	}

	@Override
	public IThread[] getThreads() throws DebugException {
		List<StateMachineInstance> smInstances = getSmInstances();
		return smInstances.toArray(new IThread[smInstances.size()]);
	}

	@Override
	public boolean hasThreads() throws DebugException {
		return hasSMInstance();
	}

	@Override
	public String getName() {
		return ((NamedElement) entryPoint).getQualifiedName();
	}

	public void terminated() {
		if (defaultComponent != null) {
			debugControl.removeDebugElement(defaultComponent);
		}
		defaultComponent = null;
		for (Component component : components) {
			debugControl.removeDebugElement(component);
		}
		components.clear();
	}

	public void resumed() {
		List<StateMachineInstance> smInstances = getSmInstances();
		smInstances.forEach(inst -> {
			inst.clearStackFrames();
			debugControl.updateElement(inst);
		});

	}

	@Override
	public void breakpointAdded(IBreakpoint breakpoint) {
		mokaDebugTarget.breakpointAdded(breakpoint);
	}

	@Override
	public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
		mokaDebugTarget.breakpointRemoved(breakpoint, delta);
	}

	@Override
	public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
		mokaDebugTarget.breakpointChanged(breakpoint, delta);
	}

	public void addSMInstance(String classId, int instanceId, String originalName) {
		boolean selectElement = !hasSMInstance();
		if (defaultComponent == null) {
			defaultComponent = new Component(this, Messages.DebugTarget_default_component_label);
			debugControl.addDebugElement(this, defaultComponent);
		}
		StateMachineInstance added = defaultComponent.addStateMachineInstance(classId, instanceId, originalName);
		if (selectElement) {
			debugControl.expandAndSelect(added);
		}
	}

	public void removeSMInstance(String classId, int instanceId) {
		List<StateMachineInstance> smInstances = defaultComponent.getSmInstances();
		StateMachineInstance removedInstance = null;
		for (StateMachineInstance smInstance : smInstances) {
			if (smInstance.getClassId().equals(classId) && smInstance.getInstanceId() == instanceId) {
				removedInstance = smInstance;
			}
		}
		defaultComponent.removeStateMachineInstance(removedInstance);
		// remove default component if became empty
		if (!defaultComponent.hasSMInstance()) {
			debugControl.removeDebugElement(defaultComponent);
			defaultComponent = null;
		}
	}

	/**
	 * Marks each state machine as suspended. Can only be called when the
	 * underlying virtual machine is suspended.
	 *
	 * @param modelElement
	 *            the current element under the breakpoint (currently only a
	 *            state or transition)
	 */
	public void markThreadAsSuspended(EObject modelElement) {

		String actualSMInstance = vmBrowser.getActualSMInstance();
		List<StateMachineInstance> smInstances = getSmInstances();
		for (StateMachineInstance smInstance : smInstances) {
			StateMachineStackFrame stackFrame;
			if (smInstance.getName().equals(actualSMInstance)) {
				// this is necessary, because the model element could be a transition
				stackFrame = new StateMachineStackFrame(smInstance, (NamedElement) modelElement);
			} else {
				// the current state of the state machine will be queried later
				stackFrame = new StateMachineStackFrame(smInstance, resourceSet);
			}
			debugControl.updateElement(smInstance);
			smInstance.setStackFrames(new StackFrame[] { stackFrame });
			// force refresh of debug controls on toolbar
			debugControl.reselect();
<commit_msg>fixed debug target entry search<commit_after>package hu.eltesoft.modelexecution.ide.debug.model;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.papyrus.moka.debug.MokaBreakpoint;
import org.eclipse.uml2.uml.NamedElement;

import hu.eltesoft.modelexecution.ide.Messages;
import hu.eltesoft.modelexecution.ide.debug.XUmlRtExecutionEngine;
import hu.eltesoft.modelexecution.ide.debug.jvm.VirtualMachineBrowser;
import hu.eltesoft.modelexecution.ide.debug.model.utils.CombiningElementDebugContentProvider;
import hu.eltesoft.modelexecution.ide.debug.registry.BreakpointRegistry;
import hu.eltesoft.modelexecution.ide.debug.ui.DebugViewController;
import hu.eltesoft.modelexecution.ide.debug.util.ModelUtils;
import hu.eltesoft.modelexecution.ide.launch.ModelExecutionLaunchConfig;

public class XUMLRTDebugTarget extends DelegatingDebugTarget {

	private Component defaultComponent = null;

	private List<Component> components = new LinkedList<>();

	private BreakpointRegistry breakpoints = new BreakpointRegistry();

	private ResourceSet resourceSet;

	private VirtualMachineBrowser vmBrowser;

	private ILaunch launch;

	private DebugViewController debugControl = new DebugViewController();

	private EObject entryPoint;

	public XUMLRTDebugTarget(VirtualMachineBrowser vmBrowser, XUmlRtExecutionEngine xUmlRtExecutionEngine,
			ResourceSet resourceSet, ILaunch launch) {
		super(null, xUmlRtExecutionEngine.getDebugTarget());
		this.entryPoint = ModelUtils.javaNameToEObject(
				ModelExecutionLaunchConfig.getEntryPoint(launch.getLaunchConfiguration()), resourceSet);
		this.vmBrowser = vmBrowser;
		this.resourceSet = resourceSet;
		this.launch = launch;

		setDebugTarget(this);
	}

	public DebugViewController getDebugControl() {
		return debugControl;
	}

	@Override
	public IThread[] getThreads() throws DebugException {
		List<StateMachineInstance> smInstances = getSmInstances();
		return smInstances.toArray(new IThread[smInstances.size()]);
	}

	@Override
	public boolean hasThreads() throws DebugException {
		return hasSMInstance();
	}

	@Override
	public String getName() {
		return ((NamedElement) entryPoint).getQualifiedName();
	}

	public void terminated() {
		if (defaultComponent != null) {
			debugControl.removeDebugElement(defaultComponent);
		}
		defaultComponent = null;
		for (Component component : components) {
			debugControl.removeDebugElement(component);
		}
		components.clear();
	}

	public void resumed() {
		List<StateMachineInstance> smInstances = getSmInstances();
		smInstances.forEach(inst -> {
			inst.clearStackFrames();
			debugControl.updateElement(inst);
		});

	}

	@Override
	public void breakpointAdded(IBreakpoint breakpoint) {
		mokaDebugTarget.breakpointAdded(breakpoint);
	}

	@Override
	public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
		mokaDebugTarget.breakpointRemoved(breakpoint, delta);
	}

	@Override
	public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
		mokaDebugTarget.breakpointChanged(breakpoint, delta);
	}

	public void addSMInstance(String classId, int instanceId, String originalName) {
		boolean selectElement = !hasSMInstance();
		if (defaultComponent == null) {
			defaultComponent = new Component(this, Messages.DebugTarget_default_component_label);
			debugControl.addDebugElement(this, defaultComponent);
		}
		StateMachineInstance added = defaultComponent.addStateMachineInstance(classId, instanceId, originalName);
		if (selectElement) {
			debugControl.expandAndSelect(added);
		}
	}

	public void removeSMInstance(String classId, int instanceId) {
		List<StateMachineInstance> smInstances = defaultComponent.getSmInstances();
		StateMachineInstance removedInstance = null;
		for (StateMachineInstance smInstance : smInstances) {
			if (smInstance.getClassId().equals(classId) && smInstance.getInstanceId() == instanceId) {
				removedInstance = smInstance;
			}
		}
		defaultComponent.removeStateMachineInstance(removedInstance);
		// remove default component if became empty
		if (!defaultComponent.hasSMInstance()) {
			debugControl.removeDebugElement(defaultComponent);
			defaultComponent = null;
		}
	}

	/**
	 * Marks each state machine as suspended. Can only be called when the
	 * underlying virtual machine is suspended.
	 *
	 * @param modelElement
	 *            the current element under the breakpoint (currently only a
	 *            state or transition)
	 */
	public void markThreadAsSuspended(EObject modelElement) {

		String actualSMInstance = vmBrowser.getActualSMInstance();
		List<StateMachineInstance> smInstances = getSmInstances();
		for (StateMachineInstance smInstance : smInstances) {
			StateMachineStackFrame stackFrame;
			if (smInstance.getName().equals(actualSMInstance)) {
				// this is necessary, because the model element could be a
				// transition
				stackFrame = new StateMachineStackFrame(smInstance, (NamedElement) modelElement);
			} else {
				// the current state of the state machine will be queried later
				stackFrame = new StateMachineStackFrame(smInstance, resourceSet);
			}
			debugControl.updateElement(smInstance);
			smInstance.setStackFrames(new StackFrame[] { stackFrame });
			// force refresh of debug controls on toolbar
			debugControl.reselect();

{'fraction_non_alphanumeric': 0.089332254875528, 'fraction_numerical': 0.00017974296755639437, 'mean_word_length': 5.608076009501188, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7327373', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3127, 'n_words': 670}