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"
Exemple #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()
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()
Exemple #4
0
def assert_fact(new_fact):
    statement = facts_and_rules.statement(new_fact)

    facts.append(statement)

    # We want to see if this new fact triggers any of our rules
    for rule in rules:
        support(infer(statement, rule), statement)
        # if this works, add new facts based on our rules
    return statement
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()
Exemple #6
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)
Exemple #7
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
Exemple #8
0
def remove_supports(fact):
    if fact in KB:
        print "Retracting: " + fact.pretty()
        for f in fact.facts:
            remove_supports(f)
        KB.remove(fact)


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()


for new_fact in facts:
    assert_fact(facts_and_rules.statement(new_fact))

for new_rule in rules:
    assert_rule(facts_and_rules.rule(new_rule[0], new_rule[1]))

ask(facts_and_rules.statement(["flat", "?x"]))

for f in KB:
    print f.pretty()
                        sign=False
                    print facts_and_rules.statement(facts_and_rules.instantiate(pattern.full, bindings)).pretty()
    if not bindings_lists:
        print "None is true:\t"
   # for b in bindings_lists:
    #    print "\nThis is true:\t"
      #  for pattern in patterns:
    #     print facts_and_rules.statement(facts_and_rules.instantiate(pattern.full,b)).pretty()

print "Asserting Rules\t"
for new_rule in rules:
    Assert_Rule(facts_and_rules.rule(new_rule[0], new_rule[1]))

print "Asserting facts\t"
for fact in facts:
    Assert(facts_and_rules.statement(fact))

print "Start Testing\t"
print "Testing Assert and retract\t"
print RB[0].lhs[0].full
print KB[0].full
# print KB[22].facts[0].full
print KB[23].full
print "Testing Ask"
#simply for testing ASK
TB=[]
TB.append(facts_and_rules.statement(['color', 'pyrami13', '`blue']))
TB.append(facts_and_rules.statement(['color', '?x', 'green']))
Ask(TB[0])
Ask(TB[1])
#test end