__author__ = 'fatma'
from os import listdir
import subprocess
from os.path import isfile, join
from subprocess import PIPE, Popen
import re

linkDir = 'C:\\Users\\fatma\\Documents\\workspace\\Links\\'
nodeDir = 'C:\\Users\\fatma\\Documents\\workspace\\Nodes\\'
pairDir = 'C:\\Users\\fatma\\Documents\\workspace\\Pairs\\'


def wrapper(*args):
    process = Popen(list(args), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return stdout

linkFileList = [ join(linkDir,f) for f in listdir(linkDir) if isfile(join(linkDir,f)) ]
nodeFileList = [ join(nodeDir,f) for f in listdir(nodeDir) if isfile(join(nodeDir,f)) ]
pairFileList = [ join(pairDir,f) for f in listdir(pairDir) if isfile(join(pairDir,f)) ]
size = len(linkFileList)
text_file = open("Output.txt", "w")
#text_file2 = open("OutputFeasible.txt", "w")
numOfFeasibleCplex = 0
numOfFeasibleHeuristic = 0
avgEnergyCostHeuristic = 0
avgEnergyCostCplex = 0
avgRelayCostHeuristic = 0
avgRelayCostCplex = 0
for count in range(0, size):
    x = wrapper('java', '-jar', 'C:\\Users\\fatma\\Documents\\workspace\\HeuristicRunnable.jar', nodeFileList[count], linkFileList[count], pairFileList[count])
    y = wrapper('java', '-jar', 'C:\\Users\\fatma\\Documents\\workspace\\CplexRunnable.jar', linkFileList[count], nodeFileList[count], pairFileList[count])
   
    m = re.findall(r"Total Cost:\s?([-+]?\d+)", x)
    n = re.findall(r"Objective fonksiyon\s?([-+]?\d+)", y)
    p = re.findall(r"Relay Cost:\s?([-+]?\d+)", x)
    q = re.findall(r"Relay Cost:\s?([-+]?\d+)", y)
	
    if m[0] != "-1":
        numOfFeasibleHeuristic += 1
        avgEnergyCostHeuristic += float(m[0])
        avgRelayCostHeuristic += float(p[0])
    #concat = m[0] + ' '
    #if m[0] != "-1" and n:
        #concat2 = m[0] + ' ' + n[0] + '\n'
        #text_file2.write(concat2)
    if n:
        #concat = concat + n[0] + '\n'
        numOfFeasibleCplex += 1
        avgEnergyCostCplex += float(n[0])
        avgRelayCostCplex += float(q[0])
        if m[0] == "-1":
            print(count)
    #else:
        #concat = concat + "-1" + '\n'
    #text_file.write(concat)
avgEnergyCostHeuristic = avgEnergyCostHeuristic/numOfFeasibleHeuristic
avgEnergyCostCplex = avgEnergyCostCplex/numOfFeasibleCplex
avgRelayCostHeuristic = avgRelayCostHeuristic/numOfFeasibleHeuristic
avgRelayCostCplex = avgRelayCostCplex/numOfFeasibleCplex
text_file.write("Average energy cost CPLEX ")
text_file.write(str(avgEnergyCostCplex))
text_file.write("\n")
text_file.write("Average energy cost Heuristic ")
text_file.write(str(avgEnergyCostHeuristic))
text_file.write("\n")
text_file.write("Average relay cost CPLEX ")
text_file.write(str(avgRelayCostCplex))
text_file.write("\n")
text_file.write("Average relay cost Heuristic ")
text_file.write(str(avgRelayCostHeuristic))
text_file.write("\n")
text_file.write("Num Of Feasible CPLEX ")
text_file.write(str(numOfFeasibleCplex))
text_file.write("\n")
text_file.write("Num Of Feasible Heuristic ")
text_file.write(str(numOfFeasibleHeuristic))

text_file.close()
#text_file2.close()
print("Num Of Feasible CPLEX ", numOfFeasibleCplex)
print("Num Of Feasible Heuristic ", numOfFeasibleHeuristic)