Exemplo n.º 1
0
    def consume_clause(self, head):
        self.consume()  # consuming ':'

        if not head:
            raise CommandException(
                'clause', 'Your clause should have a term before \':-\'')
            return error

        args = []

        self.consume()  # consuming '-'
        while not self.is_end():
            args.append(self.parse_line())
            self.consume()

        return logic.Clause(head=head, body=args)
Exemplo n.º 2
0
def resolveTest(path):
    global correct
    f = open(path)
    premises = set()

    solution = f.readline().strip()
    for line in f:
        literals = set()
        lineSplit = line.split()
        for lit in lineSplit:
            if lit.startswith('-'):
                literals.add(logic.Literal(lit[1:], (0, 0), True))
            else:
                literals.add(logic.Literal(lit, (0, 0), False))
        last = logic.Clause(literals)
        premises.add(last)
    goal = last
    premises.remove(last)
    resolution = logic.resolution(premises, goal)
    if str(resolution) == solution:
        correct += 1
    return 'Calculated: ' + str(resolution) + ' Correct solution: ' + solution
Exemplo n.º 3
0
def generate(string, heuristic, showInfo, showComments):

	# STEP 0 :
	# -----
	# Sanitize
	# Either using 0's or line breaks

	# replace multiple spaces by a single one
	clean = re.sub('[ ]+', ' ', string)

	# remove leading spaces
	clean = re.sub('^[ ]+', '', clean, flags=re.MULTILINE)

	# split the string into lines and filter empty lines
	lines = filter(lambda l : l not in [' ',''] , clean.strip().split('\n'))


	# STEP 1 :
	# -----
	# Get comments and info and display them to user
	comments = filter(lambda l : l[0] in ['p', 'c'], lines)
	if comments and showComments :
		print " Comments : \n-----------"
		print '\n'.join(map(lambda x : ' ' + x[1:].strip(), comments))
		print 


	# STEP 2 :
	# -----
	# Get the actual CNF
	cnf = lines[len(comments):]


	# STEP 3 :
	# -----
	# Split into clauses
	# Either using the 0 character or line breaks (search in clean)
	if re.search('[ ]+0[ ]*', clean):
		
		# 0's are used for the end of clause rather than \n
		cnf = ' '.join(cnf)
		cnf = re.sub('[ ]+0[ ]*','\n', cnf)
		cnf = filter(lambda l : l not in [' ',''] , cnf.strip().split('\n'))


	# STEP 4 :
	# -----
	# create a variables for each absolute number
	
	# get absolute numbers
	snumbers = ' '.join(cnf).split(' ')
	numbers = map(lambda x : int(x), snumbers)
	absolutes = map(lambda x : abs(x), numbers)

	# create variables from absolute numbers
	variables = { i : logic.Var(i) for i in set(absolutes) }


	# STEP 5 :
	# -----
	# create a literal for each number from the variables
	literals = { i : logic.Literal(variables[abs(i)], (i >= 0)) for i in set(numbers) }


	# STEP 6 :
	# -----
	# create a clause for each line
	sclauses = [ x.split(' ') for x in cnf ]
	clauses = [ logic.Clause(map(lambda x : literals[int(x)], line)) for line in sclauses ]


	# STEP 7 :
	# -----
	# display info to user
	if showInfo :
		print " CNF Infos : \n-----------"
		print " %r variables and %r clauses " % (len(variables), len(clauses))
		print


	# STEP 8 :
	# -----
	# create CNF with empty solutions
	return logic.CNF(clauses,[], heuristic, [], [])