Exemplo n.º 1
0
    def parse_rule(rule_str):
        # Example: daughter(Person, Parent) :- female(Person), parent(Parent, Person).
        rule_str = rule_str.strip().rstrip('.').replace(' ', '')
        sep_idx = rule_str.find(':-')

        # Get conclusion (lhs) and premises (rhs) seperated by ':-'
        conclusion = Fact.parse_fact(rule_str[:sep_idx])
        premises = []
        list_fact_str = rule_str[sep_idx + 2:].split('),')

        for idx, fact_str in enumerate(list_fact_str):
            if idx != len(list_fact_str) - 1:
                fact_str += ')'
            fact = Fact.parse_fact(fact_str)
            premises.append(fact)

        return Rule(conclusion, premises)
Exemplo n.º 2
0
    def parse_rule(rule_str):
        # Example: daughter(Person, Parent) :- female(Person), parent(Parent, Person).
        rule_str = rule_str.strip().rstrip('.').replace(
            ' ', '')  # format rule to standard
        sep_idx = rule_str.find(':-')

        # Get conclusion (lhs) and premises (rhs) seperated by ':-'
        conclusion = Fact.parse_fact(rule_str[:sep_idx])
        premises = []
        list_fact_str = rule_str[sep_idx + 2:].split(
            '),')  # get list of conditions, each condition separated by ')'

        for idx, fact_str in enumerate(list_fact_str):
            if idx != len(list_fact_str) - 1:
                fact_str += ')'  # because split by ')', some condition lost this character, we need to format it
            fact = Fact.parse_fact(fact_str)
            premises.append(fact)

        return Rule(conclusion, premises)
 def declare(kb, list_data):  # initialize a knowledge database
     while list_data:
         current_line, list_data = Sentence.read_list(
             list_data)  # return a comment/fact/rule and update list
         type = Sentence.classify(current_line)
         if type == 'fact':
             fact = Fact.parse_fact(current_line)
             kb.add_fact(fact)
             kb.add_fact_predicate(fact.predicate)
         elif type == 'rule':
             rule = Rule.parse_rule(current_line)
             kb.add_rule(rule)
             kb.add_rule_conclusion_predicate(rule.conclusion.predicate)
Exemplo n.º 4
0
    test_number = "04"
    inp_file = 'test/' + test_number + '/knowledge.pl'
    query_file = 'test/' + test_number + '/query.pl'
    output_file = 'test/' + test_number + '/answers.txt'

    kb = KnowledgeBase()
    with open(inp_file, 'r') as f_in:
        data_base = f_in.readlines()
        KnowledgeBase.declare(kb, data_base)

    print('Done initialize knowledge base from {}.'.format(inp_file))

    with open(query_file, 'r') as f_query:
        with open(output_file, 'w') as f_out:
            for query_str in f_query.readlines():
                alpha = Fact.parse_fact(query_str)
                alpha_str = str(alpha) + '.'
                print(alpha_str)
                query_conclusion = kb.query(alpha, "backward")
                if isinstance(query_conclusion, bool):
                    substs = query_conclusion.__str__()
                else:
                    substs = list(query_conclusion)
                substs_str = ' ;\n'.join([str(subst)
                                          for subst in substs]) + '.\n'
                print(substs_str)
                f_out.write(alpha_str + '\n')
                f_out.write(substs_str + '\n')

    print('Results of queries from {} are written to {}.'.format(
        query_file, output_file))