<commit_before>package cc.topicexplorer.chain;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.log4j.Logger;

/**
 * Collects dependencies of commands mentioned in the catalog and gets them
 * ordered.
 * <p>
 * Each map follows the semantic that a key of a map is dependent of the
 * correspondent value (respectively dependent of each element of the values
 * ArrayList).
 */
public class DependencyCollector {

	private Catalog catalog;
	private Logger logger = Logger.getRootLogger();

	/**
	 * Class constructor taking the catalog argument and sets it in this class.
	 * 
	 * @param catalog
	 */
	public DependencyCollector(Catalog catalog) {
		this.catalog = catalog;
	}

	public DependencyCollector() {

	}

	/**
	 * Creates a file in dot format. A -> B means that A depends of B. A dashed
	 * line represents an optional dependency. It accesses the global dependency
	 * maps, so it must be executed before the maps are changed, e.g. before
	 * executing the orderCommands method because it changes the maps.
	 */
	private void makeDotFile(Map<String, Set<String>> composedDependencies,
			Map<String, Set<String>> optionalDependencies, String name) {
		String dotContent = "digraph G { \n";
		dotContent += "rankdir = BT; \n";
		dotContent += "node [shape=record]; \n";
		dotContent += "edge [arrowhead=vee]; \n";

		for (String key : composedDependencies.keySet()) {
			if (composedDependencies.get(key).isEmpty()) {
				dotContent += key + "; \n";
			} else {
				for (String value : composedDependencies.get(key)) {
					dotContent += (key + " -> " + value + "; \n");
				}
			}
		}

		for (String key : optionalDependencies.keySet()) {
			if (!optionalDependencies.get(key).isEmpty()) {
				for (String value : optionalDependencies.get(key)) {
					dotContent += (key + " -> " + value + " [style = dotted] " + "; \n");
				}
			}
		}

		dotContent += "}";

		try {
			File dir = new File("etc");
			if (!dir.exists()) {
				dir.mkdir();			
			}
			
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
					"etc/graph" + name + ".dot"));
			bufferedWriter.write(dotContent);
			bufferedWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Checks if the name is contained as key in the dependecies map. If it is,
	 * it takes the value of the key (an arrayList) and merges it with the given
	 * beforeDependencies. After merging, the compoundBeforeList is set as new
	 * value of the key. Otherwise the name and the beforeDependencies are added
	 * as new key-value-pair to the dependencies.
	 * <p>
	 * If there is an element in the afterDependencies, each element of the list
	 * must be added to the dependencies as key with the name as correspondent
	 * value. If that pair is contained the old and the new list have to be
	 * merged, otherwise only the new key-value-pair is added.
	 * 
	 * @param name
	 * @param dependencies
	 * @param afterDependencies
	 * @param beforeDependencies
	 */
	private void updateDependencies(String name,
			Map<String, Set<String>> dependencies,
			Set<String> afterDependencies, Set<String> beforeDependencies) {
//		Set<String> compoundBeforeList = new HashSet<String>();
//
//		if (dependencies.containsKey(name)) {
//			compoundBeforeList.addAll(dependencies.get(name));
//		}
//		compoundBeforeList.addAll(beforeDependencies);
//		
//		dependencies.put(name, compoundBeforeList);
		
		if (dependencies.containsKey(name)) {
			dependencies.get(name).addAll(beforeDependencies);
		}
		
		

//		if (!afterDependencies.isEmpty()) {
//			Set<String> compoundAfterList = new HashSet<String>();
//			for (String key : afterDependencies) {
//				compoundAfterList.add(name);
//				if (dependencies.containsKey(key)) {
//					compoundAfterList.addAll(dependencies.get(key));
//				}
//				dependencies.put(key, compoundAfterList);
//			}
//		}
		
	if (!afterDependencies.isEmpty()) {
		for (String key : afterDependencies) {
			if (dependencies.containsKey(key)) {
				dependencies.get(key).add(name);
			} else {				
				dependencies.put(key, new HashSet<String>(Arrays.asList(name)));
			}
		}
	}
		
		
	}

	/**
	 * Every command of the catalog will be executed with the dependencyContext.
	 * Then a command should set its dependencies in the dependencyContext.<commit_msg>refactored updateDependencies(..)<commit_after>package cc.topicexplorer.chain;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.Command;
import org.apache.log4j.Logger;

/**
 * Collects dependencies of commands mentioned in the catalog and gets them
 * ordered.
 * <p>
 * Each map follows the semantic that a key of a map is dependent of the
 * correspondent value (respectively dependent of each element of the values
 * ArrayList).
 */
public class DependencyCollector {

	private Catalog catalog;
	private Logger logger = Logger.getRootLogger();

	/**
	 * Class constructor taking the catalog argument and sets it in this class.
	 * 
	 * @param catalog
	 */
	public DependencyCollector(Catalog catalog) {
		this.catalog = catalog;
	}

	public DependencyCollector() {

	}

	/**
	 * Creates a file in dot format. A -> B means that A depends of B. A dashed
	 * line represents an optional dependency. It accesses the global dependency
	 * maps, so it must be executed before the maps are changed, e.g. before
	 * executing the orderCommands method because it changes the maps.
	 */
	private void makeDotFile(Map<String, Set<String>> composedDependencies,
			Map<String, Set<String>> optionalDependencies, String name) {
		String dotContent = "digraph G { \n";
		dotContent += "rankdir = BT; \n";
		dotContent += "node [shape=record]; \n";
		dotContent += "edge [arrowhead=vee]; \n";

		for (String key : composedDependencies.keySet()) {
			if (composedDependencies.get(key).isEmpty()) {
				dotContent += key + "; \n";
			} else {
				for (String value : composedDependencies.get(key)) {
					dotContent += (key + " -> " + value + "; \n");
				}
			}
		}

		for (String key : optionalDependencies.keySet()) {
			if (!optionalDependencies.get(key).isEmpty()) {
				for (String value : optionalDependencies.get(key)) {
					dotContent += (key + " -> " + value + " [style = dotted] " + "; \n");
				}
			}
		}

		dotContent += "}";

		try {
			File dir = new File("etc");
			if (!dir.exists()) {
				dir.mkdir();			
			}
			
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
					"etc/graph" + name + ".dot"));
			bufferedWriter.write(dotContent);
			bufferedWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Checks if the name is contained as key in the dependecies map. If it is,
	 * it takes the value of the key (an arrayList) and merges it with the given
	 * beforeDependencies. After merging, the compoundBeforeList is set as new
	 * value of the key. Otherwise the name and the beforeDependencies are added
	 * as new key-value-pair to the dependencies.
	 * <p>
	 * If there is an element in the afterDependencies, each element of the list
	 * must be added to the dependencies as key with the name as correspondent
	 * value. If that pair is contained the old and the new list have to be
	 * merged, otherwise only the new key-value-pair is added.
	 * 
	 * @param name
	 * @param dependencies
	 * @param afterDependencies
	 * @param beforeDependencies
	 */
	private void updateDependencies(String name,
			Map<String, Set<String>> dependencies,
			Set<String> afterDependencies, Set<String> beforeDependencies) {
//		Set<String> compoundBeforeList = new HashSet<String>();
//
//		if (dependencies.containsKey(name)) {
//			compoundBeforeList.addAll(dependencies.get(name));
//		}
//		compoundBeforeList.addAll(beforeDependencies);
//		
//		dependencies.put(name, compoundBeforeList);
		
		if (dependencies.containsKey(name)) {
			dependencies.get(name).addAll(beforeDependencies);
		} else {
			dependencies.put(name, beforeDependencies);
		}
		
		

//		if (!afterDependencies.isEmpty()) {
//			Set<String> compoundAfterList = new HashSet<String>();
//			for (String key : afterDependencies) {
//				compoundAfterList.add(name);
//				if (dependencies.containsKey(key)) {
//					compoundAfterList.addAll(dependencies.get(key));
//				}
//				dependencies.put(key, compoundAfterList);
//			}
//		}
		
	if (!afterDependencies.isEmpty()) {
		for (String key : afterDependencies) {
			if (dependencies.containsKey(key)) {
				dependencies.get(key).add(name);
			} else {				
				dependencies.put(key, new HashSet<String>(Arrays.asList(name)));
			}
		}
	}
		
		
	}

	/**
	 * Every command of the catalog will be executed with the dependencyContext.
	 * Then a command should set its dependencies in the dependencyContext.
{'fraction_non_alphanumeric': 0.12030951876192496, 'fraction_numerical': 0.00021199915200339198, 'mean_word_length': 3.5867768595041323, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, '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': '4266926', 'n_tokens_mistral': 3153, 'n_tokens_neox': 2727, 'n_words': 853}