def backchain_to_goal_tree(rules, hypothesis): matches = OR() bindings = {} tree = OR() for rule in rules: consequent = rule.consequent()[0] if match(consequent, hypothesis): matches.append(rule) bindings = variables(consequent) if len(matches) < 1: return hypothesis else: for rule in matches: for exp in rule.antecedent(): n_hypothesis = populate(exp, bindings) tree.append(backchain_to_goal_tree(rules, n_hypothesis)) return simplify(tree)
def iterate_node(rules, node, mapping): if isinstance(node, str): leaf = populate(node, mapping) return backchain_to_goal_tree(rules,leaf) elif isinstance(node, OR): return (OR([iterate_node(rules, exp, mapping) for exp in node])) elif isinstance(node, AND): return (AND([iterate_node(rules, exp, mapping) for exp in node]))
def myfunc(rule, rules, deneme, hypothesis): yedek = OR() for ante in rule.antecedent(): deger = myfunc2(rule, rules, populate(ante, deneme), deneme) #print(deger) if deger != OR() and deger != rule.antecedent(): yedek.append(deger) #print(yedek) #print(populate(ante,deneme)) #print(simplify(yedek)) if yedek == OR(): return OR() #return AND(yedek,populate(ante,deneme)) if type(rule.antecedent()) == OR: return OR(yedek, myfunc3(rule, deneme)) return AND(yedek, myfunc3(rule, deneme))
def find_ante(rules, hypothesis): antecedents = [hypothesis] for rule in rules: result = match(rule.consequent(), hypothesis) if result != None: ante = populate(rule.antecedent(), result) if isinstance(ante, AND): antecedents.append( simplify(AND([find_ante(rules, cond) for cond in ante]))) elif isinstance(ante, OR): antecedents.append( simplify(OR([find_ante(rules, cond) for cond in ante]))) else: antecedents.append(find_ante(rules, ante)) return simplify(OR(antecedents))
def backchain_to_goal_tree(rules, hypothesis): if len(rules) == 0: return hypothesis # initilialise a new node which will contain all of the matching consequences goal_tree = OR() # try to match the hypothesis to a THEN statment in the rules for i in range(len(rules)): # get the other potential matching leaf rule = rules[i] # check if there is a match matching = match(rule.consequent()[0], hypothesis) # in not none you have found a match if matching != None: # get the consequent and the antecedent cns = rule.consequent()[0] ant = rule.antecedent() if isinstance(ant, list): sub = AND() if isinstance(ant, OR): sub = OR() for statment in ant: # print(statment) # now recursivly call backchain on the ants statments new_node = backchain_to_goal_tree( rules, populate(statment, matching)) sub.append(new_node) # sub.append(ant) goal_tree.append(sub) else: goal_tree.append( backchain_to_goal_tree(rules, populate(ant, matching))) else: goal_tree.append(hypothesis) return simplify(goal_tree)
def myfunc2(rule, rules, hypothesis, deneme): yedek = OR() for rule in rules: for cons in rule.consequent(): #print(cons) if match(cons, hypothesis): deneme = match(cons, hypothesis) #print(rule.antecedent()) antelist.append(hypothesis) yedek.append(hypothesis) gelendeger = myfunc(rule, rules, deneme, hypothesis) if gelendeger != OR(): yedek.append(gelendeger) else: yedek.append(populate(rule.antecedent(), deneme)) #print(simplify(yedek)) return yedek
def backchain_test(rules, hypothesis): consequent_match_list = [] goal_tree = OR() antecedent_hypotheses = AND() for rule in rules: for consequent in rule.consequent(): if not match(consequent, hypothesis) == None: print populate(consequent, match(consequent, hypothesis)) return 'hello kitty'
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ if isinstance(hypothesis, OR): return OR([backchain_to_goal_tree(rules, x) for x in hypothesis]) elif isinstance(hypothesis, AND): return AND([backchain_to_goal_tree(rules, x) for x in hypothesis]) else: return simplify(OR(hypothesis, OR([backchain_to_goal_tree(rules, populate(rule.antecedent(), match(rule.consequent(), hypothesis))) for rule in rules if match(rule.consequent(), hypothesis) is not None])))
def backchain_to_goal_tree(rules, hypothesis): result2 = [hypothesis] # while set[newh]!= set([hypothesis]): for condition in rules: resuls1 = [] if isinstance(condition, IF): result = condition.antecedent() condition = condition.consequent() data = match(condition[0], hypothesis) if data is not None: # print(type(result)) if not isinstance(result, list): result = [result] for r in result: if isinstance(r, OR): for i in range(len(r)): result3 = [] newh = populate(r[i], data) # result3.append(newh) result3.append(backchain_to_goal_tree(rules, newh)) resuls1.append(OR(result3)) elif isinstance(r, AND): for i in range(len(r)): result3 = [] newh = populate(r[i], data) # result3.append(newh) result3.append(backchain_to_goal_tree(rules, newh)) resuls1.append(AND(result3)) else: newh = populate(r, data) # resuls1.append(newh) # print(newh) newresult = backchain_to_goal_tree(rules, newh) print(newresult) resuls1.append(newresult) if isinstance(result, AND): result2.append(AND(resuls1)) else: result2.append(OR(resuls1)) print(simplify(OR(result2))) return simplify(OR(result2))
def backchain_to_goal_tree(rules, hypothesis): i = 0 tree = OR() for rule in rules: #for ante in rule.antecedent(): #if i>=1 and i<len(rule.antecedent()): #birsey.append(ante) #i+=1 for cons in rule.consequent(): if match(cons, hypothesis) or hypothesis == cons: deneme = match(cons, hypothesis) #bakalim=(backchain_to_goal_tree,populate(rule.antecedent(),deneme),rules) #print(rule) tree.append( OR(hypothesis, myfunc(rule, rules, deneme, hypothesis))) if tree == OR(): return ['stuff'] antelist.clear() return simplify(tree)
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ #find a rule which has the hypothesis as the conclusion and testing its premises # Rule Z14: IF( AND( '(?x) is a bird', #in rule Z3 '(?x) does not fly', #opposite of rule Z4 '(?x) swims', '(?x) has black and white color'), THEN('(?x) is a penguin')) # penguin = opus? #OR('opus is a penguin', AND( #OR('opus is a bird', 'opus has feathers'), #AND('opus flies', 'opus lays eggs')) #'opus does not fly', #'opus swims', #'opus has black and white color' )) #Rule Z14: if results = [hypothesis] for rule in rules: consequent = rule.consequent() for expr in consequent: bindings = match(expr, hypothesis) if bindings or expr == hypothesis: antecedent = rule.antecedent() if isinstance(antecedent, str): new_hypothesis = populate(antecedent, bindings) results.append( backchain_to_goal_tree(rules, new_hypothesis)) results.append(new_hypothesis) else: statements = [ populate(ante_expr, bindings) for ante_expr in antecedent ] new_results = [] for statement in statements: new_results.append( backchain_to_goal_tree(rules, statement)) results.append(create_statement(new_results, antecedent)) return simplify(OR(results))
def backchain_to_goal_tree(rules, hypothesis): #rules that result in hyp options = [] op_i = 0 #varibale stuff ????? for rule in rules: #print(rule) for con in rule.consequent(): var = match(con, hypothesis) if (var or var == {}): options.append([]) nomatch = False #print(rule.antecedent()) ##if and/or loop else no loop!!!!! if (isinstance(rule.antecedent(), (list, tuple))): for ant in rule.antecedent(): #get num of rules that make it up res = (backchain_to_goal_tree(rules, populate(ant, var))) #print(res, hypoth(?x) has rhythm and musicesis) options[op_i].append(res) #print(options) if (isinstance(rule.antecedent(), AND)): options[op_i] = (AND(options[op_i])) else: options[op_i] = (OR(options[op_i])) else: res = (backchain_to_goal_tree( rules, populate(rule.antecedent(), var))) #print(res, hypoth(?x) has rhythm and musicesis) options[op_i] = (AND(res)) op_i += 1 options = OR([hypothesis] + [simplify(term) for term in options]) #print(hypothesis) #print("G: ",simplify(options)) return simplify(options)
def ret_hypothesis(rules, hypothesis): hypothesis = hypothesis rules = rules store = OR() store.append(hypothesis) for rule in rules: antecedence = rule.antecedent() consequence = rule.consequent() if match(consequence, hypothesis) == {}: fin_antecedent = antecedence if isinstance(fin_antecedent, AND): temp = [] for clause in fin_antecedent: temp.append(ret_hypothesis(rules, clause)) store.append(AND(temp)) elif isinstance(fin_antecedent, OR): temp = [] for clause in fin_antecedent: temp.append(ret_hypothesis(rules, clause)) store.append(OR(temp)) else: store.append(ret_hypothesis(rules, fin_antecedent)) elif match(consequence, hypothesis) == None: pass else: store_binding = match(consequence, hypothesis) fin_antecedent = populate(antecedence, store_binding) if isinstance(fin_antecedent, AND): temp = [] for clause in fin_antecedent: temp.append(ret_hypothesis(rules, clause)) store.append(AND(temp)) elif isinstance(fin_antecedent, OR): temp = [] for clause in fin_antecedent: temp.append(ret_hypothesis(rules, clause)) store.append(OR(temp)) else: store.append(ret_hypothesis(rules, fin_antecedent)) return store
def backchain_to_goal_tree(rules, hypothesis): unbound = [(rule.antecedent(), match(rule.consequent()[0], hypothesis)) for rule in rules if not match(rule.consequent()[0], hypothesis) is None] bound = OR(hypothesis) for (ante, binding) in unbound: if type(ante) is AND: node_list = [populate(exp, binding) for exp in ante] bound.append( AND([ backchain_to_goal_tree(rules, node) for node in node_list ])) elif type(ante) is OR: node_list = [populate(exp, binding) for exp in ante] bound.append( OR([backchain_to_goal_tree(rules, node) for node in node_list])) else: node = populate(ante, binding) bound.append(backchain_to_goal_tree(rules, node)) return simplify(bound)
def backchain_to_goal_tree(rules, hypothesis): output = OR(hypothesis) matches = [ populate(r.antecedent(), match(r.consequent()[0], hypothesis)) for r in rules if matcher(r, hypothesis) ] if len(matches) > 0: for m in matches: if isinstance(m, AND): to_append = AND() else: to_append = OR() if not isinstance(m, (AND, OR)): m = AND(m) for a in m: child_append = OR(a) child_append.append(backchain_to_goal_tree(rules, a)) to_append.append(child_append) output.append(to_append) else: output.append(OR()) return simplify(output)
def backchain_to_goal_tree(rules, hypothesis): ## raise NotImplementedError goal_tree = OR(hypothesis) for rule in rules: consequce = rule.consequent()[0] bindings = match(consequce, hypothesis) sub_tree = [] if bindings is not None and len(bindings) > 0: antecedent = rule.antecedent() for elem in antecedent: new_hypothesis = populate(elem, bindings) pop_elem = backchain_to_goal_tree(rules, new_hypothesis) sub_tree.append(simplify(pop_elem)) if isinstance(antecedent, AND): sub_tree = AND(sub_tree) elif isinstance(antecedent, OR): sub_tree = OR(sub_tree) if len(sub_tree) > 0: goal_tree.append(sub_tree) return goal_tree
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ # Need to account for case when neither AND nor OR tree = OR(hypothesis) for rule in rules: con = rule.consequent() ant = rule.antecedent() isOR = isinstance(ant, OR) isAND = isinstance(ant, AND) mat = match(con, hypothesis) if mat == None: continue elif mat == {}: if isOR: tree.append(OR([backchain_to_goal_tree(rules, a) for a in ant])) elif isAND: tree.append(AND([backchain_to_goal_tree(rules, a) for a in ant])) else: tree.append(backchain_to_goal_tree(rules, ant)) else: if isOR: tree.append(OR([backchain_to_goal_tree(rules, populate(a, mat)) for a in ant])) elif isAND: tree.append(AND([backchain_to_goal_tree(rules, populate(a, mat)) for a in ant])) else: tree.append(backchain_to_goal_tree(rules, populate(ant, mat))) return simplify(tree)
def backchain_to_goal_tree(rules, hypothesis): tree = [] tree.append(hypothesis) for rule in rules: for c in rule.consequent(): m = match(c, hypothesis) if m != None: if isinstance(rule.antecedent(), list): new_rules = [] for ant in rule.antecedent(): print ant ant = populate(ant, m) new_rules.append(backchain_to_goal_tree(rules, ant)) if isinstance(rule.antecedent(), AND): tree.append(AND(new_rules)) elif isinstance(rule.antecedent(), OR): tree.append(OR(new_rules)) else: tree += new_rules else: ant = populate(rule.antecedent(), m) tree.append(backchain_to_goal_tree(rules, ant)) return simplify(OR(tree))
def backchain_to_goal_tree(rules, hypothesis): tree = hypothesis for rule in rules: if not type(rule.consequent()) is str: for consequent in rule.consequent(): matches = match(consequent, hypothesis) if (matches != None): if not type(rule.antecedent()) is str: curtree = None ruletype = False if isinstance(rule.antecedent(), AND): ruletype = True for antecedent in rule.antecedent(): if curtree == None: curtree = backchain_to_goal_tree( rules, populate(antecedent, matches)) else: if ruletype: curtree = AND( curtree, backchain_to_goal_tree( rules, populate(antecedent, matches))) else: curtree = OR( curtree, backchain_to_goal_tree( rules, populate(antecedent, matches))) tree = OR(tree, curtree) else: tree = OR( tree, backchain_to_goal_tree( rules, populate(rule.antecedent(), matches))) tree = simplify(tree) return tree
def backchain_to_goal_tree(rules, hypothesis): if not rules: return hypothesis tree = OR(hypothesis) for rule in rules: for consequent in rule.consequent(): matches = match(consequent, hypothesis) if matches == None: continue variables = {} for name, value in matches.items(): variables[name] = value antecedents = rule.antecedent() # If antecedents is a string, behave as it is a list with single item - unify antecedents handling if isinstance(antecedents, str): antecedents = [antecedents] # Determine type of antecedents if isinstance(rule.antecedent(), AND): statements = AND() else: statements = OR() # For each antecedent generate new possible subtree for antecedent in antecedents: new_hypothesis = populate(antecedent, variables) subtree = backchain_to_goal_tree(rules, new_hypothesis) statements.append(subtree) tree.append(simplify(statements)) return simplify(tree)
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ list_candidates = [hypothesis] for rule in rules: bindings = match(rule.consequent(), hypothesis) if bindings != None: candidates = [] if isinstance(rule.antecedent(), AND) or isinstance( rule.antecedent(), OR): for statement in rule.antecedent(): candidates.append( backchain_to_goal_tree(rules, populate(statement, bindings))) else: candidates.append( backchain_to_goal_tree( rules, populate(rule.antecedent(), bindings))) if isinstance(rule.antecedent(), AND): list_candidates.append(AND(candidates)) else: list_candidates.append(OR(candidates)) return simplify(OR(list_candidates))
def backchain_to_goal_tree(rules, hypothesis): """ Takes a hypothesis (string) and a list of rules (list of IF objects), returning an AND/OR tree representing the backchain of possible statements we may need to test to determine if this hypothesis is reachable or not. This method should return an AND/OR tree, that is, an AND or OR object, whose constituents are the subgoals that need to be tested. The leaves of this tree should be strings (possibly with unbound variables), *not* AND or OR objects. Make sure to use simplify(...) to flatten trees where appropriate. """ cons_matches = [hypothesis] #always OR for r in rules: var = match(r.consequent(), hypothesis) if var!=None: ant = r.antecedent() ant_matches = [] t = type(ant) next_hypo = populate(ant, var) if t == str: ant_matches.append(backchain_to_goal_tree(rules, next_hypo)) else: for a in next_hypo: ant_matches.append(backchain_to_goal_tree(rules, a)) if t == OR: cons_matches.append(OR(ant_matches)) else: cons_matches.append(AND(ant_matches)) return simplify(OR(cons_matches))
def backchain_to_goal_tree(rules, hypothesis): OR_list = [hypothesis] for rule in rules: for con in rule.consequent(): bindings = match(con, hypothesis) if bindings is not None: ant = rule.antecedent() if isinstance(ant, str): new_hypo = populate(ant, bindings) OR_list.append(backchain_to_goal_tree(rules, new_hypo)) elif isinstance(ant, AND): lst = [] for term in ant: new_hypo = populate(term, bindings) lst.append(backchain_to_goal_tree(rules, new_hypo)) OR_list.append(AND(lst)) else: lst = [] for term in ant: new_hypo = populate(term, bindings) lst.append(backchain_to_goal_tree(rules, new_hypo)) OR_list.append(OR(lst)) break return simplify(OR(OR_list))
def backchain_to_goal_tree(rules, hypothesis): print "backchain_to_goal_tree", hypothesis possible = [hypothesis] for rule in rules: print "rule", rule for consequence in rule.consequent(): bindings = match(consequence, hypothesis) if bindings != None: print "bindings", bindings sub_hypothesis = populate(rule.antecedent(), bindings) print "sub_hypothesis", sub_hypothesis sub_hypothesis_extended = extendGoalTree(rules, sub_hypothesis) possible.append(sub_hypothesis_extended) return simplify(OR(possible))
def backchain_to_goal_tree(rules, hypothesis): requirements = [hypothesis] for r in rules: binding = match_hypothesis_against_consequents(r.consequent(), hypothesis) if not (isinstance(binding, dict) or binding): continue else: new_hypotheses = populate(r.antecedent(), binding) ## Recursively back chaining on the new hypotheses. if isinstance(new_hypotheses, basestring): requirements.append( backchain_to_goal_tree(rules, new_hypotheses) ) else: requirements.append( new_hypotheses.__class__(*[backchain_to_goal_tree(rules, h) for h in new_hypotheses]) ) return simplify(OR(requirements))
def backchain_to_goal_tree(rules, hypothesis): # result = [hypothesis] # for rule in rules: # for expr in rule.consequent(): # if (match(expr, hypothesis) or expr) == hypothesis: # if isinstance(rule.antecedent(), str): # result.append(backchain_to_goal_tree(rules, populate(rule.antecedent(), match(expr, hypothesis)))) # result.append(populate(rule.antecedent(), match(expr, hypothesis))) # else: # entries = [populate(itera, match(expr, hypothesis)) for itera in rule.antecedent()] # new_res = [] # for entry in entries: # new_res.append(backchain_to_goal_tree(rules, entry)) # result.append(create_statement(new_res, rule.antecedent())) # return simplify(OR(result)) # # def create_statement(entries, rule): # if isinstance(rule, AND): # return AND(entries) # if isinstance(rule, OR): # return OR(entries) results = [hypothesis] for rule in rules: for expr in rule.consequent(): if match(expr, hypothesis) or expr == hypothesis: if isinstance(rule.antecedent(), str): results.append( backchain_to_goal_tree( rules, populate(rule.antecedent(), match(expr, hypothesis)))) results.append( populate(rule.antecedent(), match(expr, hypothesis))) else: statements = [ populate(ante_expr, match(expr, hypothesis)) for ante_expr in rule.antecedent() ] new_results = [] for statement in statements: new_results.append( backchain_to_goal_tree(rules, statement)) results.append( create_statement(new_results, rule.antecedent())) return simplify(OR(results))
def backchain_to_goal_tree(rules, hypothesis): matching_rules = [] runningOR = OR() for rule in rules: match_result = match(rule._action[0], hypothesis) if match_result != None: runningOR.append(populate(rule._action[0], match_result)) conditional = rule._conditional runningCondition = AND() for condition in range(0, len(conditional)): condition2 = populate(conditional[condition], match_result) runningCondition.append( backchain_to_goal_tree(rules, condition2)) runningOR.append(runningCondition) if len(runningOR) != 0: return simplify(runningOR) else: return hypothesis
def backchain_to_goal_tree(rules, hypothesis): results = [hypothesis] for rule in rules: consequent = rule.consequent() for expr in consequent: bindings = match(expr, hypothesis) if bindings or expr == hypothesis: antecedent = rule.antecedent() if isinstance(antecedent, str): new_hypothesis = populate(antecedent, bindings) results.append(backchain_to_goal_tree(rules, new_hypothesis)) results.append(new_hypothesis) else: statements = [populate(ante_expr, bindings) for ante_expr in antecedent] new_results = [] for statement in statements: new_results.append(backchain_to_goal_tree(rules, statement)) results.append(create_statement(new_results, antecedent)) return simplify(OR(results))
def backchain_to_goal_tree(rules, hypothesis): matches = [hypothesis] for rule in rules: var_names = match(rule.consequent()[0], hypothesis) if var_names is not None: temp = copy.deepcopy(rule.antecedent()) if isinstance(temp, list): for i in range(len(temp)): temp[i] = simplify( backchain_to_goal_tree(rules, populate(temp[i], var_names))) matches.append(simplify(temp)) else: matches.append( simplify( populate(backchain_to_goal_tree(rules, temp), var_names))) return simplify(OR(matches))
def backchain_to_goal_tree(rules, hypothesis): # check rules for matching consequents # i) no matches -> hypothesis is a leaf # ii) matches -> OR(antecedent) or_cond = OR() # always append the hypothesis or_cond.append(hypothesis) for rule in rules: bindings = match(rule.consequent()[0], hypothesis) if bindings: # append the goal tree from the antecedent ante = rule.antecedent() if isinstance(ante, RuleExpression): or_cond.append( expand_subtree(ante, bindings, rules) ) return simplify( or_cond )