<commit_before>/*******************************************************************************
 * Copyright (c) 2012-2014 INRIA.
 * 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 fr.inria.soctrace.tools.importer.otf2.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import fr.inria.soctrace.lib.model.EventProducer;
import fr.inria.soctrace.lib.model.EventType;
import fr.inria.soctrace.lib.model.utils.ModelConstants.EventCategory;
import fr.inria.soctrace.lib.model.utils.SoCTraceException;
import fr.inria.soctrace.lib.utils.IdManager;
import fr.inria.soctrace.tools.importer.otf2.reader.Otf2PrintWrapper;

/**
 * Otf2 pre-parser.
 * 
 * Parse the output of "otf2-print -G" to retrieve: the event producer hierarchy, the event types
 * and other meta-data.
 * 
 * @author "<NAME> <<EMAIL>>"
 * @author "<NAME> <<EMAIL>>"
 */
class Otf2PreParser {

	private Otf2Parser theParser;
	private int totNumberOfLines = 0;
	private IdManager etIdManager = new IdManager();
	private IdManager epIdManager = new IdManager();

	public Otf2PreParser(Otf2Parser aParser) {
		theParser = aParser;
	}

	/**
	 * Check that the otf2-print is working and parse the otf2 definitions (created with otf2-print
	 * with the -G option) and get all sort of info (event producers, event types, etc.)
	 * 
	 * @param monitor
	 *            progress monitor
	 * @throws SoCTraceException
	 */
	public void parseDefinitons(IProgressMonitor monitor) throws SoCTraceException {

		if (!checkExternalProgram()) {
			monitor.setCanceled(true);
			return;
		}

		try {
			monitor.subTask("Getting event producers and event types");
			List<String> args = new ArrayList<String>();
			args.add("-G");
			args.add(theParser.getTraceFile());
			Otf2PrintWrapper wrapper = new Otf2PrintWrapper(args);

			BufferedReader br = wrapper.execute(monitor);

			String line;

			while ((line = br.readLine()) != null && !monitor.isCanceled()) {
				if (line.isEmpty() || !line.contains(" "))
					continue;

				String keyword = line.substring(0, line.indexOf(" "));
				if (keyword.equals(Otf2Constants.CLOCK_PROPERTIES)) {
					parseClockProperties(line);
				} else if (keyword.equals(Otf2Constants.REGION)) {
					parseRegion(line);
				} else if (keyword.equals(Otf2Constants.LOCATION_GROUP)) {
					parseLocationGroup(line);
				} else if (keyword.equals(Otf2Constants.SYSTEM_TREE_NODE)) {
					parseTreeNode(line);
				} else if (keyword.equals(Otf2Constants.METRIC_MEMBER)) {
					parseMetricMember(line);
				} else if (keyword.equals(Otf2Constants.LOCATION)) {
					parseLocation(line);
				}
			}
			br.close();

			createStaticTypes();
			theParser.setNumberOfLines(totNumberOfLines);
<commit_msg>OTF2 importer: added checking of error in preparsing by otfPrint<commit_after>/*******************************************************************************
 * Copyright (c) 2012-2014 INRIA.
 * 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 fr.inria.soctrace.tools.importer.otf2.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import fr.inria.soctrace.lib.model.EventProducer;
import fr.inria.soctrace.lib.model.EventType;
import fr.inria.soctrace.lib.model.utils.ModelConstants.EventCategory;
import fr.inria.soctrace.lib.model.utils.SoCTraceException;
import fr.inria.soctrace.lib.utils.IdManager;
import fr.inria.soctrace.tools.importer.otf2.reader.Otf2PrintWrapper;

/**
 * Otf2 pre-parser.
 * 
 * Parse the output of "otf2-print -G" to retrieve: the event producer hierarchy, the event types
 * and other meta-data.
 * 
 * @author "<NAME> <<EMAIL>>"
 * @author "<NAME> <<EMAIL>>"
 */
class Otf2PreParser {

	private Otf2Parser theParser;
	private int totNumberOfLines = 0;
	private IdManager etIdManager = new IdManager();
	private IdManager epIdManager = new IdManager();

	public Otf2PreParser(Otf2Parser aParser) {
		theParser = aParser;
	}

	/**
	 * Check that the otf2-print is working and parse the otf2 definitions (created with otf2-print
	 * with the -G option) and get all sort of info (event producers, event types, etc.)
	 * 
	 * @param monitor
	 *            progress monitor
	 * @throws SoCTraceException
	 */
	public void parseDefinitons(IProgressMonitor monitor) throws SoCTraceException {

		if (!checkExternalProgram()) {
			monitor.setCanceled(true);
			return;
		}

		try {
			monitor.subTask("Getting event producers and event types");
			List<String> args = new ArrayList<String>();
			args.add("-G");
			args.add(theParser.getTraceFile());
			Otf2PrintWrapper wrapper = new Otf2PrintWrapper(args);

			BufferedReader br = wrapper.execute(monitor);

			String line;

			while ((line = br.readLine()) != null && !monitor.isCanceled()) {
				if (line.isEmpty() || !line.contains(" "))
					continue;

				String keyword = line.substring(0, line.indexOf(" "));
				if (keyword.equals(Otf2Constants.CLOCK_PROPERTIES)) {
					parseClockProperties(line);
				} else if (keyword.equals(Otf2Constants.REGION)) {
					parseRegion(line);
				} else if (keyword.equals(Otf2Constants.LOCATION_GROUP)) {
					parseLocationGroup(line);
				} else if (keyword.equals(Otf2Constants.SYSTEM_TREE_NODE)) {
					parseTreeNode(line);
				} else if (keyword.equals(Otf2Constants.METRIC_MEMBER)) {
					parseMetricMember(line);
				} else if (keyword.equals(Otf2Constants.LOCATION)) {
					parseLocation(line);
				} // No known keyword was found. Check for error 
				else if (line.contains("error:")) {
					// Close the file
					br.close();
					// Stop importation
					monitor.setCanceled(true);
					throw new SoCTraceException(
							"File could not be parsed by otfPrint which returned the following error: "
									+ line);
				}
			}
			br.close();

			createStaticTypes();
			theParser.setNumberOfLines(totNumberOfLines);

{'fraction_non_alphanumeric': 0.15658104774343345, 'fraction_numerical': 0.01001306051371354, 'mean_word_length': 4.249047981721249, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1571629', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2056, 'n_words': 497}