Beispiel #1
0
def forward_chaining(kb, theorem, verbose=True):
    # Salvăm baza de date originală, lucrăm cu o copie
    local_kb = deepcopy(kb)
    # Două variabile care descriu starea căutării
    got_new_facts = True  # s-au găsit fapte noi la ultima căutare
    is_proved = False  # a fost demostrată teorema
    # Verificăm dacă teorema este deja demonstrată
    for fact in filter(is_fact, local_kb):
        if unify(fact, theorem):
            if verbose:
                print("This already in KB: " + print_formula(fact, True))
            is_proved = True
            break
    while (not is_proved) and got_new_facts:
        got_new_facts = False
        for rule in filter(is_rule, local_kb):
            # Pentru fiecare regulă
            new_facts = apply_rule(rule, list(filter(is_fact, local_kb)))
            new_facts = list(
                filter(
                    lambda fact: not any(
                        list(
                            filter(lambda orig: is_equal_to(fact, orig),
                                   local_kb))), new_facts))
            if new_facts:
                if verbose:
                    print("Applied rule: " + print_formula(rule, True) +
                          ", obtained " + str(len(new_facts)) + " new facts.")
                if any(filter(lambda t: is_variable(t), get_args(get_conclusion(rule)))) and \
                        any(filter(lambda fact: is_equal_to(fact, get_conclusion(rule)), new_facts)):
                    print(
                        "Demonstration is too general, the conclusion is not instantiated (facts obtained:",
                        ",".join([print_formula(f, True)
                                  for f in new_facts]), ").")
                    return False
                got_new_facts = True
                for fact in new_facts:
                    #if verbose: print("New fact: " + print_formula(fact, True))
                    if unify(fact, theorem) != False:
                        is_proved = True
                        add_statement(local_kb, fact)
                        if verbose:
                            print("Now in KB: " + print_formula(fact, True))
                        break
                    add_statement(local_kb, fact)
            if is_proved:
                break
    if verbose:
        if is_proved:
            print("The theorem is TRUE!")
        else:
            print("The theorem is FALSE!")
    return is_proved
Beispiel #2
0
def print_state(stare):
	for atom in stare:
		print_formula(atom)
Beispiel #3
0

def is_fact(formula):
    return is_positive_literal(formula)


def is_rule(formula):
    return get_head(formula) == 'or' or get_head(formula) == 'and'


# Test!
# formula: P(x) ^ Q(x) -> R(x)
f = make_or(make_neg(make_atom("P", make_var("x"))),
            make_neg(make_atom("Q", make_var("x"))),
            make_atom("R", make_var("x")))
print(" ; ".join([print_formula(p, True)
                  for p in get_premises(f)]))  # Should be P(?x) ; Q(?x)
print_formula(get_conclusion(f))  # Should be R(?x)
print(is_rule(f))  # must be True
print(is_fact(f))  # must be False
print(is_fact(get_conclusion(f)))  # must be True
print(is_rule(get_conclusion(f)))  # must be False


def equal_terms(t1, t2):
    if is_constant(t1) and is_constant(t2):
        return get_value(t1) == get_value(t2)
    if is_variable(t1) and is_variable(t2):
        return get_name(t1) == get_name(t2)
    if is_function_call(t1) and is_function(t2):
        if get_head(t1) != get_head(t2):