def assertFile(file, remove):
    # Initialise
    counts = [0,0]
    
    # Find the last edge ID (so anything past is removed)
    lastID = int(telnet.command("lastedge").split("|")[0])
    
    # Read each line, asserting and (optionally) removing
    lines = file.read().splitlines()
    i = 0
    for line in lines:
        edgid = assertEdge(line.split("\t")[0])
    
        # Count the result
        if edgid is not -1:
            counts[0] += 1
            logFile.write("ACCEPTED: " + line + "\n")
        else:
            counts[1] += 1
            logFile.write("REJECTED: " + line + "\n")
    
        # Remove the edge
        if remove and edgid > lastID:
            telnet.command("removeedge " + str(edgid) + " T")
        i += 1
        if (i % 500 == 0):
            print(str(i) + " complete from " + str(file.name))
    print(str(counts[0]) + "/" + str(counts[0] + counts[1]))
    return counts
def group(file):
    lines = file.read().splitlines()
    # Test assert each edge
    parsed = []
    for line in lines:
        query = telnet.command("addedge " + line.replace(subject(line), testnode) + " (\"DisjointTest\")")
        parseID = int(query.split('|')[0])
        if parseID != -1:
            parsed.append(line)
            logging.debug('ACCEPTED:' + line)
        else:
            logging.debug('REJECTED:' + line + ' - ' + query)
        # Revert TestNode
        unassertToID(lastID)
    
    paired = [(subject(x), x) for x in parsed]
    keys = set([x[0] for x in paired])
    return dict([(x, [y[1].replace(x, testnode) for y in paired if y[0] == x]) for x in keys])
def unassertToID(lastID):
    while True:
        nextID = int(telnet.command("nextedge " + str(lastID)).split("|")[0])
        if nextID is -1:
            break
        telnet.command("removeedge " + str(nextID) + " T")
def assertEdge(edge):
    return int(telnet.command("addedge " + edge + " (\"DisjointTest\")").split("|")[0])
    return re.search('\(\S+ (\S+) .+\)', edgeStr).group(1)
    
def pairwiseAssert():
    # For each keypair
    for key in keys:
        # We don't actually need to assert for 'key' - we can use a test node to clear all edges
        print(key + ":")
        trueVals = (trueMap[key] if key in trueMap else [])
        falseVals = (falseMap[key] if key in falseMap else [])
        pairwiseEval(trueVals, falseVals, key)
    
    
    
    
telnet.init(2426, True)
telnet.command('removenode TestNode T')
testnode = telnet.command('addnode TestNode ("DisjointTest")').split('|')[0]

# Logging initialisation
logging.basicConfig(format='%(levelname)s:%(message)s',filename='disjoint.log',level=logging.DEBUG)
with open('disjoint.log', 'w'):
    pass

# Get the files open, and assert each true edge (should stay) and false edge (should not stay)
disjoints = open(sys.argv[1], 'r')
trueFile = open(sys.argv[2], 'r')
falseFile = open(sys.argv[3], 'r')
print("Files read in")

# Organise each assertion into groups by subject
lastID = int(telnet.command("lastedge").split("|")[0])