#2004 Matthias Granberry. This file is provided with the hope that
#someone will find it useful. Feel free to use any part of it for any
#purpose.
import sys
try:
filename = sys.argv[1]
formulalines = open(filename).readlines()
except:
print 'Usage:', sys.argv[0], ''
sys.exit(0)
fset = {'A':[],'B':[]}
i = 0
while i < len(formulalines):
s = formulalines[i].split()
if len(s) > 5: #This might be a new formula
if s[0] == 'Formula': #it is, read the number of clauses,
#A/B membership, etc
flist = fset[s[2]] #the third token is either A/B
fclauses = int(s[3]) # the number of clauses is fourth
fclause = 0
i = i + 2 #skip next line
formula = []
while fclause < fclauses:
clause = []
s = formulalines[i].split() #first line of clauses
fclause = int(s[0]) #the number of the clause
cliterals = int(s[1]) #number of literals
literal = 0 #the first line of literals
for j in range (2, len(s)):
clause.append( int(s[j])) #append the literal to the clause
literal += 1
while literal < cliterals: #read any following lines of
#literals
i = i + 1
s = formulalines[i].split()
for j in range (0, len(s)):
clause.append( int(s[j]))
literal += 1
formula.append (clause) #the formula is all clauses ORed
#together
i = i + 1
flist.append (formula) #append the finished formula
else:
i = i + 1 #skip the line
else:
i = i + 1 #go to the next line
#generate code for the 20 'true means vote for A' formulas
for formula in fset['A']:
print 'if (',
first = True
for clause in formula:
if not first:
print '||',
else:
first = False
print '((formula & (',
num = 0
numpos = 0
for literal in clause:
num = num | (1 << abs(literal) - 1)
#print '|', (1 < 0:
numpos = numpos | (1 << literal - 1)
print num, ')) == (',numpos,'))'
print ') {vote++;} else vote--;'
#and 20 for 'true means vote for B' formulas
for formula in fset['B']:
print 'if (',
first = True
for clause in formula:
if not first:
print '||',
else:
first = False
print '((formula & (',
num = 0
numpos = 0
for literal in clause:
num = num | (1 << abs(literal) - 1)
#print '|', (1 < 0:
numpos = numpos | (1 << literal - 1)
print num, ')) == (',numpos,'))'
print ') {vote--;} else vote++;'