def big_Ask(patterns):
    bindings_lists = []
    sign = True
    for pattern in patterns:
        if bindings_lists != []:
            for pair in map(lambda b: (facts_and_rules.instantiate(pattern.full, b), b), bindings_lists):
                for fact in KB:
                    bindings = facts_and_rules.match(pair[0], fact)
                    if bindings != False:
                        for key in pair[1]:
                            bindings[key] = pair[1][key]
                        bindings_lists.append(bindings)
                        if sign:
                            print "This is true\t"
                            sign = False
                        print facts_and_rules.statement(facts_and_rules.instantiate(pattern.full, bindings)).pretty()


        else:
            for fact in KB:
                bindings = facts_and_rules.match(pattern, fact)
                if bindings != False:
                    bindings_lists.append(bindings)
                    if sign:
                        print "This is true\t"
                        sign=False
                    print facts_and_rules.statement(facts_and_rules.instantiate(pattern.full, bindings)).pretty()
    if not bindings_lists:
        print "None is true:\t"
Exemplo n.º 2
0
def ask(patterns):
    sets_of_bindings = []
    for pattern in patterns:
        bindings = []
        for fact in facts:
            if pattern.predicate == fact.predicate:
                # this means we've found a fact that matches our query
                binding = facts_and_rules.match(pattern, fact)
                if binding != False:
                    bindings.append(binding)
        if len(bindings) >= 1:
            sets_of_bindings.append(bindings)

    to_return = sets_of_bindings[0]
    if len(sets_of_bindings) > 1:
        for s in sets_of_bindings[1:]:
            to_remove = []
            for binding in to_return:
                if binding not in s:
                    to_remove.append(binding)
            #ew
            for b in to_remove:
                to_return.remove(b)

    return to_return
Exemplo n.º 3
0
def ask(pattern):
    for fact in KB:
        bindings_lists = []
        bindings = facts_and_rules.match(pattern, fact)
        if bindings != False:
            bindings_lists.append(bindings)
        for b in bindings_lists:
            print "This is true: \t",
            print facts_and_rules.statement(
                facts_and_rules.instantiate(pattern.full, b)).pretty()
def Ask(pattern):
    list_of_bindings_lists = []
    for fact in KB:
        bindings = facts_and_rules.match(pattern, fact)
        if bindings != False:
            list_of_bindings_lists.append(bindings)
    if not list_of_bindings_lists:
        print "This is False:\t"
    for b in list_of_bindings_lists:
        print "This is true: \t"
        print facts_and_rules.statement(facts_and_rules.instantiate(pattern.full, b)).pretty()
Exemplo n.º 5
0
def infer(fact, rule):
    # check the first element of the lhs of the rule against our fact
    bindings = facts_and_rules.match(rule.lhs[0], fact)

    if bindings != False:
        # if our rule only contains one argument, add a new fact
        if len(rule.lhs) == 1:
            if rule.type == "Assert":
                new_statement = facts_and_rules.instantiate(
                    rule.rhs.full, bindings)
                # this is a new fact that we can assert
                asserted_fact = assert_fact(new_statement)
                return asserted_fact
            else:
                # find the fact that needs to be retracted
                fact_to_find = facts_and_rules.statement(
                    facts_and_rules.instantiate(rule.rhs.full, bindings))
                for f in facts:
                    if facts_and_rules.match(fact_to_find, f) != False:
                        print "found"
                        retract(f)
                return False

        # otherwise, make a new rule with the remaining arguments with bound variables replaced
        else:
            new_lhs = []
            for arg in rule.lhs[1:]:
                new_lhs.append(facts_and_rules.instantiate(arg.full, bindings))
            new_rhs = facts_and_rules.instantiate(rule.rhs.full, bindings)
            if rule.type == "Retract":
                new_rhs[0] = '~' + new_rhs[0]
            asserted_rule = assert_rule((new_lhs, new_rhs))
            return asserted_rule
    else:
        # if we don't infer anything, return false
        return False
def infer_from_fact(fact):
    for r in RB:
        bindings = facts_and_rules.match(r.lhs[0], fact)
        if bindings != False:
            if len(r.lhs) == 1:
                fact_infered = facts_and_rules.statement(facts_and_rules.instantiate(r.rhs.full, bindings))
                fact.add_fact(fact_infered)
                dup = False
                for f in KB:
                    dup = dup | facts_and_rules.pattern_match(fact_infered, f)
                if not dup:
                    Assert(fact_infered)
                print "fact" + fact.pretty() + "fact inferred" + fact_infered.pretty()
            else:
                tests = map(lambda x : facts_and_rules.instantiate(x.full, bindings), r.lhs[1:])
                rhs = facts_and_rules.instantiate(r.rhs.full, bindings)
                new_rule = facts_and_rules.rule(tests, rhs)
                fact.add_rule(new_rule)
                Assert_Rule(new_rule)
        print "Hey there " + r.name + " matches " + fact.pretty()
Exemplo n.º 7
0
def infer_from_rule(rule):
    for f in KB:
        bindings = facts_and_rules.match(rule.lhs[0], f)
        if bindings != False:
            if len(rule.lhs) == 1:
                new_statement = facts_and_rules.statement(
                    facts_and_rules.instantiate(rule.rhs.full, bindings))
                if rule.type == "Assert":
                    f.add_fact(new_statement)
                    assert_fact(new_statement)
                    print "adding inference: " + str(new_statement.full)
                elif rule.type == "Retract":
                    retract(new_statement)
            else:
                tests = map(
                    lambda x: facts_and_rules.instantiate(x.full, bindings),
                    rule.lhs[1:])
                rhs = facts_and_rules.instantiate(rule.rhs.full, bindings)
                new_rule = facts_and_rules.rule(tests, rhs)
                rule.add_rule(new_rule)
                assert_rule(new_rule)
Exemplo n.º 8
0
def retract(item):
    for fact in KB:
        if facts_and_rules.match(item, fact) != False:
            remove_supports(fact)