'''
Created on 13 Jun 2015

@author: marinos
'''
from time import time
import argparse

import Analyses


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Trivial Equivalent mutant Detector: TCE for Java')
    
    parser.add_argument('--rt_jar', required=True, help = 'path to the =rt.jar= file')
    parser.add_argument('--soot', required=True, help = 'path to the =soot-trunk.jar= file')
    parser.add_argument('--mujava_res', required=True, help = 'path to the directory that contains the mutants that were generated by mujava')
    parser.add_argument('--exp_dir', required=True, help = 'path to the experiments directory. The directory must include a =bin/= directory with the classes of the project as used by mujava and an =optimisations/= directory')
    
    parser.add_argument('--methods', nargs='*', default=[], help = 'method(s) to be considered; if not provided, consider all methods. Note that the names of the methods are the ones returned by mujava')
    parser.add_argument('--mutants', type = int, default=0, help = 'the maximum number of mutants to optimise; if not provided, optimise them all')
    parser.add_argument('--action', choices=['Opt', 'Ned', 'Ted', 'Ned-dupes', 'Ted-dupes'], required=True,
                        help = 'The analysis to be performed: Opt: Optimisation via Soot; Ned: TCE via javac; Ted: TCE via Soot; {Ned,Ted}-dupes: Duplicated mutant detection')

    args = parser.parse_args()
    print args

    Analyses.rt_jar_location = args.rt_jar
    Analyses.soot_executable = args.soot
    mujava_result_dir = args.mujava_res
    experiment_dir = args.exp_dir
    
    included_methods = args.methods
    max_handled_mutants = args.mutants
    action = args.action

    print included_methods
    print max_handled_mutants
    print action
    print mujava_result_dir
    print experiment_dir

    print Analyses.rt_jar_location
    print Analyses.soot_executable

     
##################### Test Subjects ##########################################
     
    # Triangle
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/Triangle-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/Triangle'
#     equiv_muts_per_class = {}
#     equiv_muts_per_class['Triangle'] = {}
#     equiv_muts_per_class['Triangle']['int_classify(int,int,int)'] = ['AOIS_129', 'AOIS_130', 'AOIS_133', 'AOIS_134', 'AOIS_137', 'AOIS_138', 'AOIS_139', 'AOIS_140', 'AOIS_25', 'AOIS_26', 'AOIS_37', 'AOIS_38', 'AOIS_49', 'AOIS_50', 'AOIS_77', 'AOIS_78', 'AOIS_81', 'AOIS_82', 'AOIS_83', 'AOIS_84', 'AOIU_2']

#     # Bisect
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/Bisect-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/Bisect'
#     included_methods = ['double_sqrt(double)']
#     equiv_muts_per_class = {}
#     equiv_muts_per_class['Bisect'] = {}
#     equiv_muts_per_class['Bisect'][included_methods[0]] = ['AOIS_43', 'AOIS_44', 'AOIS_47', 'AOIS_48', 'AOIS_59', 'AOIS_60', 'AOIS_73', 'AOIS_74', 'AOIS_79', 'AOIS_80', 'AOIU_4']
    
    
#     # Commons
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/Commons-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/Commons'
#     included_methods = ['java.lang.String_capitalize(java.lang.String,char)', 'java.lang.String_wrap(java.lang.String,int,java.lang.String,boolean)']
#     included_methods = ['java.lang.String_capitalize(java.lang.String,char)']
#     included_methods = ['java.lang.String_wrap(java.lang.String,int,java.lang.String,boolean)']
#     equiv_muts_per_class = {}
#     equiv_muts_per_class['WordUtils'] = {}
#     equiv_muts_per_class['WordUtils']['java.lang.String_capitalize(java.lang.String,char)'] = ['AOIS_95', 'AOIS_96']
#     equiv_muts_per_class['WordUtils']['java.lang.String_wrap(java.lang.String,int,java.lang.String,boolean)'] = ['AOIS_41', 'AOIS_42', 'AOIS_47', 'AOIS_48', 'AOIS_75', 'AOIS_76', 'AOIS_81', 'AOIS_82', 'AOIS_83', 'AOIS_84', 'AOIS_89', 'AOIS_90']


#     # Pamvotis
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/Pamvotis-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/Pamvotis'
#     included_methods = ['void_addNode(int,int,int,int,int,int)', 'boolean_removeNode(int)']
#     included_methods = ['void_addNode(int,int,int,int,int,int)']
#     included_methods = ['boolean_removeNode(int)']
#     equiv_muts_per_class = {}
#     equiv_muts_per_class['Simulator'] = {}
#     equiv_muts_per_class['Simulator']['void_addNode(int,int,int,int,int,int)'] = ['AORB_950', 'AORB_954', 'AOIS_2267', 'AORB_949', 'AOIU_256', 'AOIS_2258', 'AOIS_2268', 'AOIS_2260', 'AOIS_2271', 'AOIS_2263', 'AOIS_2272', 'AOIS_2261', 'AOIS_2265', 'LOI_657', 'AOIS_2256', 'AORB_956', 'AOIS_2266', 'AOIS_2257', 'AORB_953', 'AOIS_2269', 'AOIS_2259', 'AOIS_2270', 'AOIS_2128', 'AOIS_2127', 'AORB_955', 'AOIS_2264', 'AOIU_258', 'AORB_951', 'AOIS_2255', 'AORB_952', 'AOIS_2262']
#     equiv_muts_per_class['Simulator']['boolean_removeNode(int)'] = ['AOIS_2292', 'AOIS_2291', 'AOIS_2297', 'AOIS_2302', 'AOIS_2298', 'AOIS_2301']
#     included_methods = ['boolean_removeSource(int)', 'void_addSource(pamvotis.sources.Source)','boolean_removeNode(int)', 'void_addNode(int,int,int,int,int,int)']


#     # XStream
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/XStream-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/XStream'
#     included_methods = ['java.lang.String_decodeName(java.lang.String)']


#     # JodaTime
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/JodaTime-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/JodaTime'
#     included_methods = ['long_add(long,int)']

    # H2
    # mujava_result_dir = '/home/marinos/Desktop/soot-test/h2-res/result'
    # experiment_dir = '/home/marinos/Desktop/soot-test/h2'

#     # ant
#     mujava_result_dir = '/home/marinos/Desktop/soot-test/ant-res/result'
#     experiment_dir = '/home/marinos/Desktop/soot-test/ant'

    
##############################################################################
    
    if action == 'Opt':
        print 'Optimisation phase started'
        start_time = time()
                
        Analyses.optimise(mujava_result_dir, experiment_dir, included_methods, max_handled_mutants)
        
        print 'Finished: ', (time() - start_time), ' secs'
        
        
    if 'Ted' in action:
        print 'Detection with TCE via Soot started'
        start_time = time()
        
        muts_per_class, equiv_muts_per_class = Analyses.ted(experiment_dir, included_methods)
        
        print 'Finished: ', (time() - start_time), ' secs'
        print
        print '*** Statistics ***'
        print 'MutOps for all mutants'
        muts_per_operator = Analyses.mutsPerOperatorInDict(muts_per_class)
        print
        print 'MutOps for equivalent mutants'
        equivs_per_operator = Analyses.mutsPerOperatorInDict(equiv_muts_per_class)
        
        Analyses.printEquivsORDupsPercentagePerOperator(muts_per_operator, equivs_per_operator)
        
    
    if 'Ned' in action:
        print 'Detection with TCE via javac started'
        start_time = time()
                
        muts_per_class, equiv_muts_per_class = Analyses.ned(mujava_result_dir, included_methods)
        
        print 'Finished: ', (time() - start_time), ' secs'
        print
        print '*** Statistics ***'
        print 'MutOps for all mutants'
        muts_per_operator = Analyses.mutsPerOperatorInDict(muts_per_class)
        print
        print 'MutOps for equivalent mutants'
        equivs_per_operator = Analyses.mutsPerOperatorInDict(equiv_muts_per_class)
        
        Analyses.printEquivsORDupsPercentagePerOperator(muts_per_operator, equivs_per_operator)
        
        
    if 'Ted-dupes' in action:
        print 'Detection of *duplicated* started (Soot) '
        start_time = time()
                
        muts_per_class, dupes_per_class = Analyses.findDups(experiment_dir, included_methods, equiv_muts_per_class)
        
        print 'Finished: ', (time() - start_time), ' secs'
        print
        print '*** Total Statistics ***'
        Analyses.dupesPerOperatorInDict(muts_per_class, dupes_per_class)
        
    
    if 'Ned-dupes' in action:
        print 'Detection of *NED-duplicated* started (javac) '
        start_time = time()
                
        muts_per_class, dupes_per_class = Analyses.nedFindDups(mujava_result_dir, included_methods, equiv_muts_per_class) 
        
        print 'Finished: ', (time() - start_time), ' secs'
        print
        print '*** Total Statistics ***'
        Analyses.dupesPerOperatorInDict(muts_per_class, dupes_per_class)
