<commit_before>/****************************************************************************
 * Copyright (c) 2008, 2012 <NAME> 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:
 *    <NAME> - initial API and implementation 
 ****************************************************************************/

package org.yakindu.sct.generator.c.gtest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.yakindu.sct.generator.core.GeneratorExecutor;
import org.yakindu.sct.model.sgen.GeneratorModel;
import org.yakindu.sct.model.sgraph.Statechart;
import org.yakindu.sct.test.models.SCTUnitTestModels;


/**
 * @author <NAME> - Initial contribution and API
 * @author <NAME>
 * 
 */
public class GTestHelper {

	private final Object owner;

	/**
	 * 
	 */
	public GTestHelper(Object owner) {
		this.owner = owner;
	}

	public void compile() {
		try {
			copyFilesFromBundleToFolder();
			IResource resource = ResourcesPlugin.getWorkspace().getRoot()
					.findMember(getTargetPath());
			File directory = resource.getLocation().toFile();
			List<String> command = createCommand();

			ProcessBuilder processBuilder = new ProcessBuilder(command)
					.directory(directory);
			Process process = processBuilder.redirectErrorStream(true).start();
			String message = readProcessInputStream(process);
			
			boolean wait = true;
			int exitCode = 0;
			
			do {
				wait = false;

				// waiting fro the processes termination
				try {
					process.waitFor();
				} catch ( InterruptedException e ) { /* we ignore if waiting was interrupted ... */ }

				// if we get an exit code then we know that the process is finished
				try {
					exitCode = process.exitValue();
				} catch ( IllegalThreadStateException e ) {
					wait = true; // if we get an exception then the process has not finisched ...
				}
			} while (wait);

			if (exitCode != 0) {
				throw new RuntimeException("Compilation failed (exit status "
						+ process.exitValue() + "):\n" + message);
			}
		} catch (Error e) {
			throw e;
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				ResourcesPlugin
						.getWorkspace()
						.getRoot()
						.refreshLocal(IResource.DEPTH_INFINITE,
								new NullProgressMonitor());
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void generate() {

		IPath targetPath = getTargetPath();

		// copy model to junit workspace
		copyFileFromBundleToFolder(getModelBundle(), getModelPath(), targetPath);

		String sgenFileName = getTestProgram() + ".sgen";
		copyFileFromBundleToFolder(getTestBundle(), sgenFileName, targetPath);

		IPath path = new Path(sgenFileName);
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		Resource sgenResource = loadResource(file);
		GeneratorModel model = (GeneratorModel) sgenResource.getContents().get(
				0);
		model.getEntries().get(0).setElementRef(getStatechart());
		new GeneratorExecutor().executeGenerator(model);
	}

	protected Statechart getStatechart() {
		IPath path = new Path(getTargetPath().toString() + "/"
				+ getModelPath().lastSegment());
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		Resource resource = loadResource(file);
		return (Statechart) resource.getContents().get(0);
	}

	protected Resource loadResource(IFile file) {
		Resource resource = null;
		URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(),
				true);
		resource = new ResourceSetImpl().getResource(uri, true);
		return resource;
	}
<commit_msg>Minor typo correction<commit_after>/****************************************************************************
 * Copyright (c) 2008, 2012 <NAME> 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:
 *    <NAME> - initial API and implementation 
 ****************************************************************************/

package org.yakindu.sct.generator.c.gtest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.yakindu.sct.generator.core.GeneratorExecutor;
import org.yakindu.sct.model.sgen.GeneratorModel;
import org.yakindu.sct.model.sgraph.Statechart;
import org.yakindu.sct.test.models.SCTUnitTestModels;


/**
 * @author <NAME> - Initial contribution and API
 * @author <NAME>
 * 
 */
public class GTestHelper {

	private final Object owner;

	/**
	 * 
	 */
	public GTestHelper(Object owner) {
		this.owner = owner;
	}

	public void compile() {
		try {
			copyFilesFromBundleToFolder();
			IResource resource = ResourcesPlugin.getWorkspace().getRoot()
					.findMember(getTargetPath());
			File directory = resource.getLocation().toFile();
			List<String> command = createCommand();

			ProcessBuilder processBuilder = new ProcessBuilder(command)
					.directory(directory);
			Process process = processBuilder.redirectErrorStream(true).start();
			String message = readProcessInputStream(process);
			
			boolean wait = true;
			int exitCode = 0;
			
			do {
				wait = false;

				// waiting for the processes termination
				try {
					process.waitFor();
				} catch ( InterruptedException e ) { /* we ignore if waiting was interrupted ... */ }

				// if we get an exit code then we know that the process is finished
				try {
					exitCode = process.exitValue();
				} catch ( IllegalThreadStateException e ) {
					wait = true; // if we get an exception then the process has not finished ...
				}
			} while (wait);

			if (exitCode != 0) {
				throw new RuntimeException("Compilation failed (exit status "
						+ process.exitValue() + "):\n" + message);
			}
		} catch (Error e) {
			throw e;
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				ResourcesPlugin
						.getWorkspace()
						.getRoot()
						.refreshLocal(IResource.DEPTH_INFINITE,
								new NullProgressMonitor());
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void generate() {

		IPath targetPath = getTargetPath();

		// copy model to JUnit workspace
		copyFileFromBundleToFolder(getModelBundle(), getModelPath(), targetPath);

		String sgenFileName = getTestProgram() + ".sgen";
		copyFileFromBundleToFolder(getTestBundle(), sgenFileName, targetPath);

		IPath path = new Path(sgenFileName);
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		Resource sgenResource = loadResource(file);
		GeneratorModel model = (GeneratorModel) sgenResource.getContents().get(
				0);
		model.getEntries().get(0).setElementRef(getStatechart());
		new GeneratorExecutor().executeGenerator(model);
	}

	protected Statechart getStatechart() {
		IPath path = new Path(getTargetPath().toString() + "/"
				+ getModelPath().lastSegment());
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		Resource resource = loadResource(file);
		return (Statechart) resource.getContents().get(0);
	}

	protected Resource loadResource(IFile file) {
		Resource resource = null;
		URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(),
				true);
		resource = new ResourceSetImpl().getResource(uri, true);
		return resource;
	}

{'fraction_non_alphanumeric': 0.14441272861824717, 'fraction_numerical': 0.003594460302357543, 'mean_word_length': 4.276073619631902, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7255311', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2688, 'n_words': 652}