コード例 #1
0
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"
コード例 #2
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()
コード例 #3
0
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()
コード例 #4
0
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()
コード例 #5
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)
コード例 #6
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