Ptidej-v5.2-master/Ptidej Solver Tests/src/ptidej/solver/helper/AntipatternClassesExtractor.java
/*******************************************************************************
 * Copyright (c) 2001-2014 Yann-Gal Guhneuc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Yann-Gal Guhneuc and others, see in file; API and its implementation
 ******************************************************************************/
package ptidej.solver.helper;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Make for a version a unique list of classes playing a role in patterns form lists generated by EntitiesPlayingRolesExtractorPerVersion
 * @author Imane
 *
 */
public class AntipatternClassesExtractor {

	final static String[] smells = new String[] { "AntiSingleton",
			"BaseClassKnowsDerivedClass", "BaseClassShouldBeAbstract", "Blob",
			"ClassDataShouldBePrivate", "ComplexClass",
			"FunctionalDecomposition", "LargeClass", "LazyClass", "LongMethod",
			"LongParameterList", "ManyFieldAttributesButNotComplex",
			"MessageChains", "RefusedParentBequest", "SpaghettiCode",
			"SpeculativeGenerality", "SwissArmyKnife", "TraditionBreaker" };

	/**
	 * 
	 * @param aClassesList
	 * @return
	 */
	private static Map initialiseAntipatternsMap(final List aClassesList) {

		final Map patternsMap = new LinkedHashMap();
		final Iterator iterator = aClassesList.iterator();
		while (iterator.hasNext()) {

			final String tab[] =
				new String[AntipatternClassesExtractor.smells.length + 1];
			for (int k = 0; k < tab.length; k++) {
				tab[k] = "NO";
			}
			final String clazz = (String) iterator.next();
			patternsMap.put(clazz, tab);
		}

		return patternsMap;
	}

	/**
	* Has to be updated to the analysed project - setup the following variables
	* designPatternsFilesPath
	* antipatternsFilesPath
	* outputBasePath
	* releases
	* @param args
	*/
	public static void main(final String[] args) {

		final StringBuffer buffer = new StringBuffer().append("Class;");
		for (int i = 0; i < AntipatternClassesExtractor.smells.length; i++) {
			buffer.append(AntipatternClassesExtractor.smells[i]).append(";");
		}

		buffer.append("InAP");
		final String patternsMapTitle = buffer.toString();

		//		String antipatternsFilesPath =
		//			"../Ptidej Solver Data/Pattern Classes/Checkstyle/Antipatterns/";
		//
		//		String outputBasePath =
		//			"/Polymtl/Data/PatternsAndTests/checkstyle/outputs/APCrossData/";
		//		String classesListFolderPath =
		//			"/Polymtl/Data/PatternsAndTests/checkstyle/outputs/classesPerVersion/";

		final String antipatternsFilesPath =
			"../Ptidej Solver Data/Pattern Classes/ArgoUml/Antipatterns/";

		final String outputBasePath =
			"/Polymtl/Data/PatternsAndTests/argouml/outputs/APCrossData/";
		final String classesListFolderPath =
			"/Polymtl/Data/PatternsAndTests/argouml/outputs/classesPerVersion/";

		final String[] antipatternsFolders =
			new File(antipatternsFilesPath).list();
		for (int i = 0; i < antipatternsFolders.length; i++) {
			final String currentFolderPath =
				new StringBuffer()
					.append(antipatternsFilesPath)
					.append(File.separatorChar)
					.append(antipatternsFolders[i])
					.toString();
			final String releaseName = antipatternsFolders[i];

			final String[] antipatternsFiles =
				new File(currentFolderPath).list();
			final String classesListPath =
				new StringBuffer()
					.append(classesListFolderPath)
					.append(releaseName)
					.append(".csv")
					.toString();
			final List classesList = Utils.readIntoList(classesListPath, true);

			Map antipatternsMap =
				AntipatternClassesExtractor
					.initialiseAntipatternsMap(classesList);

			antipatternsMap =
				AntipatternClassesExtractor.updateMapWithAP_Classes(
					antipatternsMap,
					antipatternsFiles,
					currentFolderPath);

			final String mapPath =
				new StringBuffer()
					.append(outputBasePath)
					.append(releaseName)
					.append("_APCrossData.csv")
					.toString();
			Utils.writeMapInCSV(antipatternsMap, mapPath, patternsMapTitle);

		}
		System.out
			.println("AntipatternClassesExtractor: End of the extraction");

	}

	/**
	 * 
	 * @param patternsMap
	 * @param patternsFiles
	 * @return
	 */
	private static Map updateMapWithAP_Classes(
		final Map patternsMap,
		final String[] patternsFiles,
		final String sourceBasePath) {

		for (int i = 0; i < patternsFiles.length; i++) {
			final String currentFilePath =
				new StringBuffer()
					.append(sourceBasePath)
					.append(File.separator)
					.append(patternsFiles[i])
					.toString();
			final List patternClassesList =
				Utils.readIntoList(currentFilePath, false);
			final Iterator iterator = patternClassesList.iterator();
			int missedClasses = 0;
			while (iterator.hasNext()) {
				final String patternClass = (String) iterator.next();
				if (patternsMap.containsKey(patternClass)) {
					((String[]) patternsMap.get(patternClass))[i] = "YES";
					((String[]) patternsMap.get(patternClass))[AntipatternClassesExtractor.smells.length] =
						"YES";
				}
				else {
					missedClasses++;

				}
			}
			//interfaces are missed
			System.out.println("MissedClasses " + patternsFiles[i] + " "
					+ missedClasses);
		}

		return patternsMap;
	}

}
