package de.ugoe.cs.eventbench.jfc.commands;

import java.security.InvalidParameterException;
import java.util.Collection;
import java.util.List;

import de.ugoe.cs.eventbench.CommandHelpers;
import de.ugoe.cs.eventbench.data.GlobalDataContainer;
import de.ugoe.cs.eventbench.jfc.JFCLogParser;
import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
import de.ugoe.cs.eventbench.jfc.data.JFCTargetComparator;
import de.ugoe.cs.util.console.Command;
import de.ugoe.cs.util.console.Console;

/**
 * <p>
 * Command to parse an XML file with sessions monitored by EventBench's
 * JFCMonitor.
 * </p>
 * 
 * @author Steffen Herbold
 * @version 1.0
 */
public class CMDparseJFC implements Command {

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.ugoe.cs.util.console.Command#run(java.util.List)
	 */
	@Override
	public void run(List<Object> parameters) {
		String filename;
		String sequencesName = "sequences";

		try {
			filename = (String) parameters.get(0);
			if (parameters.size() >= 2) {
				sequencesName = (String) parameters.get(1);
			}
		} catch (Exception e) {
			throw new InvalidParameterException();
		}
		JFCTargetComparator.setMutable(true);
		JFCLogParser parser = new JFCLogParser();

		parser.parseFile(filename);
		Collection<List<JFCEvent>> sequences = parser.getSequences();

		Console.traceln("Pre-computing event target equalities.");
		// compare all Events to a dummy event to make sure they are known by
		// the JFCTargetComparator
		JFCEvent dummyEvent = new JFCEvent("dummy");
		for (List<JFCEvent> sequence : sequences) {
			for (JFCEvent event : sequence) {
				event.equals(dummyEvent);
			}
		}
		JFCTargetComparator.setMutable(false);

		if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
			CommandHelpers.dataOverwritten(sequencesName);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.ugoe.cs.util.console.Command#help()
	 */
	@Override
	public void help() {
		Console.println("Usage: parseJFC <filename> {<sequencesName>}");
	}

}
