import os, time, argparse
from datetime import datetime

from pm4py.objects.log.importer.csv import factory as csv_importer
from pm4py.objects.log.exporter.xes import factory as xes_exporter
from pm4py.objects.log.importer.xes import factory as xes_importer
from pm4py.objects.petri.importer import pnml as pnml_importer

from pm4py.evaluation.replay_fitness import factory as replay_factory
from pm4py.evaluation.precision import factory as precision_factory

from conf.settings import DATA_PATH

WORK_PATH = os.path.abspath(os.getcwd())

def readFile(f_name1, f_name2, unique=False):
    traces = []

    skipped = 0

    with open(f_name1) as file:
        file_contents = file.read()
        file_contents = file_contents.split("\n")
        print("Number of train traces are:", str(len(file_contents)))
        for row in file_contents:
            if unique:
                if row not in traces:
                    traces.append(row)
                else:
                    skipped += 1
            else:
                traces.append(row)

    with open(f_name2) as file:
        file_contents = file.read()
        file_contents = file_contents.split("\n")
        print("Number of generated traces are:", str(len(file_contents)))
        for row in file_contents:
            if unique:
                if row not in traces:
                    traces.append(row)
                else:
                    skipped += 1
            else:
                traces.append(row)

    f_traces = []
    for trace in traces:
        f_trace = []
        t = trace.split(" ")
        for i in t:
            if i != "" and "<" not in i:
                f_trace.append(i)
        if len(f_trace) > 0:
            f_traces.append(f_trace)

    print("Number of traces are:", str(len(f_traces)))
    print("Number of skipped traces are:", str(skipped))
    return f_traces

def writeToFile(file, lst):
    with open(file, 'w') as outfile:
        for entry in lst:
            outfile.write(str(entry) + "\n")

def convertToCsv(traces, to_path):
    lines = []

    case = 0
    timestamp = 0
    line = "concept:name,case:concept:name,time:timestamp"
    lines.append(line)
    for trace in traces:
        for event in trace:
            timestamp = timestamp + 1
            dt_object = datetime.fromtimestamp(timestamp)

            line = str(event) + "_" + "," + str(case) + "," + str(dt_object)
            lines.append(line)

        case = case + 1

    writeToFile(str(to_path), lines)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--system', help='Which system (e.g. pb_system_5_3)', required=True)
    parser.add_argument('-sfx', '--suffix', help='Suffix (chosen epoch, e.g. 1981)', required=True)
    parser.add_argument('-j', '--job', help='Job (0/1)', required=True)
    parser.add_argument('-pn', '--pn', help='Petri net file to evaluate', required=True)
    parser.add_argument('-strategy', '--strategy', help='naive/mh', required=True)
    args = parser.parse_args()

    system = args.system
    suffix = int(args.suffix)
    job = args.job
    pn = args.pn
    strategy = args.strategy

    if DATA_PATH is None:
        train_file = os.path.join(WORK_PATH, "data", "variants", system + "_train.txt")
        gen_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + ".txt")
        csv_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.csv")
        xes_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.xes")
        pn_file = os.path.join(WORK_PATH, "data", "pns", system, pn)
    else:
        train_file = os.path.join(DATA_PATH, "variants", system + "_train.txt")
        gen_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + ".txt")
        csv_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.csv")
        xes_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.xes")
        pn_file = os.path.join(DATA_PATH, "pns", system, pn)


    """ READ FILES AND CONVERT TO XES """
    traces = readFile(train_file,gen_file, unique=True)
    convertToCsv(traces=traces, to_path=csv_file)
    time.sleep(1)

    log = csv_importer.import_event_log(csv_file)
    xes_exporter.export_log(log, xes_file)
    time.sleep(1)

    """ PERFORM MEASUREMENT ON PN AND XES"""
    log = xes_importer.import_log(xes_file)
    net, initial_marking, final_marking = pnml_importer.import_net(pn_file)

    fitness = replay_factory.apply(log, net, initial_marking, final_marking)
    print("Fitness=", fitness)

    precision = precision_factory.apply(log, net, initial_marking, final_marking)
    print("Precision=", precision)

    fitness = fitness["log_fitness"]
    generalization = 2 * ((fitness * precision) / (fitness + precision))

    if strategy == "mh":
        print("**** ", str(system), " Job ", str(job), " on PN ", str(pn_file), " using MH SAMPLING on suffix ", str(suffix)," ***")
    elif strategy == "naive":
        print("**** ", str(system), " Job ", str(job), " on PN ", str(pn_file), " using NAIVE SAMPLING on suffix ", str(suffix), " ***")
    else:
        raise ValueError("Unknown strategy.")
    print("AVATAR Generalization=", generalization)
[((526, 537), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (535, 537), False, 'import os, time, argparse\n'), ((2537, 2562), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (2560, 2562), False, 'import os, time, argparse\n'), ((4595, 4608), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (4605, 4608), False, 'import os, time, argparse\n'), ((4620, 4659), 'pm4py.objects.log.importer.csv.factory.import_event_log', 'csv_importer.import_event_log', (['csv_file'], {}), '(csv_file)\n', (4649, 4659), True, 'from pm4py.objects.log.importer.csv import factory as csv_importer\n'), ((4664, 4702), 'pm4py.objects.log.exporter.xes.factory.export_log', 'xes_exporter.export_log', (['log', 'xes_file'], {}), '(log, xes_file)\n', (4687, 4702), True, 'from pm4py.objects.log.exporter.xes import factory as xes_exporter\n'), ((4707, 4720), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (4717, 4720), False, 'import os, time, argparse\n'), ((4777, 4810), 'pm4py.objects.log.importer.xes.factory.import_log', 'xes_importer.import_log', (['xes_file'], {}), '(xes_file)\n', (4800, 4810), True, 'from pm4py.objects.log.importer.xes import factory as xes_importer\n'), ((4853, 4886), 'pm4py.objects.petri.importer.pnml.import_net', 'pnml_importer.import_net', (['pn_file'], {}), '(pn_file)\n', (4877, 4886), True, 'from pm4py.objects.petri.importer import pnml as pnml_importer\n'), ((4902, 4964), 'pm4py.evaluation.replay_fitness.factory.apply', 'replay_factory.apply', (['log', 'net', 'initial_marking', 'final_marking'], {}), '(log, net, initial_marking, final_marking)\n', (4922, 4964), True, 'from pm4py.evaluation.replay_fitness import factory as replay_factory\n'), ((5013, 5078), 'pm4py.evaluation.precision.factory.apply', 'precision_factory.apply', (['log', 'net', 'initial_marking', 'final_marking'], {}), '(log, net, initial_marking, final_marking)\n', (5036, 5078), True, 'from pm4py.evaluation.precision import factory as precision_factory\n'), ((3206, 3272), 'os.path.join', 'os.path.join', (['WORK_PATH', '"""data"""', '"""variants"""', "(system + '_train.txt')"], {}), "(WORK_PATH, 'data', 'variants', system + '_train.txt')\n", (3218, 3272), False, 'import os, time, argparse\n'), ((3777, 3827), 'os.path.join', 'os.path.join', (['WORK_PATH', '"""data"""', '"""pns"""', 'system', 'pn'], {}), "(WORK_PATH, 'data', 'pns', system, pn)\n", (3789, 3827), False, 'import os, time, argparse\n'), ((3859, 3917), 'os.path.join', 'os.path.join', (['DATA_PATH', '"""variants"""', "(system + '_train.txt')"], {}), "(DATA_PATH, 'variants', system + '_train.txt')\n", (3871, 3917), False, 'import os, time, argparse\n'), ((4398, 4440), 'os.path.join', 'os.path.join', (['DATA_PATH', '"""pns"""', 'system', 'pn'], {}), "(DATA_PATH, 'pns', system, pn)\n", (4410, 4440), False, 'import os, time, argparse\n'), ((2290, 2323), 'datetime.datetime.fromtimestamp', 'datetime.fromtimestamp', (['timestamp'], {}), '(timestamp)\n', (2312, 2323), False, 'from datetime import datetime\n')]