Example #1
0
def equality_test():
    term1 = Term(True, 'X_0')
    term2 = Term(True, 'X_1')
    atom1 = Atom([term1, term1], 'p')
    atom2 = Atom([term1, term2], 'p')
    head = Atom([term1, term2], 'q')
    clause1 = Clause(head, [atom1, atom2])
    clause2 = Clause(head, [atom2, atom1])
    pred_dict = {}
    pred_dict[clause1] = 1
    pred_dict[clause2] = 1
    assert clause1 == clause2
    assert len(pred_dict) == 1
    def generate_clauses(self):
        '''Generate the clauses based on brute force approach with no optmization
        '''
        rule_matrix = []
        for rule in self.rules:
            clauses = []
            if (rule.allow_intensional):
                p = self.p_e + self.p_a + [self.target]
                p_i = self.p_a + [self.target]
                intensional_predicates = [atom.predicate for atom in p_i]
            else:
                p = self.p_e
            variables = [
                'X_%d' % i for i in range(0, self.target.arity + rule.v)
            ]
            target_variables = [
                'X_%d' % i for i in range(0, self.target.arity)
            ]

            # NOTE: Only allows 2 predicates and 2 arity maximum
            # TODO: Need for optimizing
            for i1 in range(0, len(p)):
                for v1 in variables:
                    for v2 in variables:
                        for i2 in range(i1, len(p)):
                            for v3 in variables:
                                for v4 in variables:
                                    # unsafe
                                    if not set(target_variables).issubset(
                                        [v1, v2, v3, v4]):
                                        continue
                                    head = Atom([
                                        Term(True, var)
                                        for var in target_variables
                                    ], self.target.predicate)

                                    body1 = Atom(
                                        [Term(True, v1),
                                         Term(True, v2)], p[i1].predicate)
                                    body2 = Atom(
                                        [Term(True, v3),
                                         Term(True, v4)], p[i2].predicate)
                                    clause = Clause(head, [body1, body2])
                                    if head == body1 or head == body2:
                                        continue
                                    # NOTE: Based on appendix requires to have a intensional predicate
                                    if rule.allow_intensional and not (
                                            body1.predicate
                                            in intensional_predicates
                                            or body2.predicate
                                            in intensional_predicates):
                                        continue
                                    # duplicate
                                    if clause not in clauses:
                                        clauses.append(clause)
            print(clauses)
            rule_matrix.append(clauses)
        return rule_matrix
Example #3
0
def f_c_test2():
    '''Testing f_c function
    '''
    term1 = Term(True, 'X_0')
    term2 = Term(True, 'X_1')
    target = Atom([term1], 'r')
    p_e = [Atom([term1, term2], 'p')]
    p_a = [Atom([term1, term2], 'q')]

    rule_template_1 = Rule_Template(0, False)
    rule_template_2 = Rule_Template(1, True)

    language_frame = Language_Frame(target, p_e, set(['a', 'b']))
    program_template = Program_Template(p_a,
                                        (rule_template_1, rule_template_2),
                                        300)

    term_a = Term(False, 'a')
    term_b = Term(False, 'b')
    # Background facts: p(a,a) p(a,b)
    background = [Atom([term_a, term_a], 'p'), Atom([term_a, term_b], 'p')]
    # Positive: p(b,a)
    positive = [Atom([term_b, term_a], 'p')]
    # Negative: p(b,b)
    negative = [Atom([term_b, term_b], 'p')]
    ilp = ILP(language_frame, background, positive, negative, program_template)
    initial_valuation, valuation_mapping = ilp.convert()

    term_x = Term(True, 'X_0')
    term_y = Term(True, 'X_1')
    term_z = Term(True, 'X_2')
    r = Atom([term_x], 'r')
    p = Atom([term_x, term_z], 'p')
    q = Atom([term_z, term_y], 'q')
    clause = Clause(r, [p, q])

    # Example from the book
    example_val = {
        Atom([term_a, term_a], 'p'): 1.0,
        Atom([term_a, term_b], 'p'): 0.9,
        Atom([term_a, term_a], 'q'): 0.1,
        Atom([term_b, term_a], 'q'): 0.2,
        Atom([term_b, term_b], 'q'): 0.8,
    }
    for key in example_val:
        initial_valuation[valuation_mapping[key]] = example_val[key]
    x_c = Inference.x_c(clause, valuation_mapping, ['a', 'b'])
    print(valuation_mapping)
    print(x_c)
    assert False
Example #4
0
def x_c_test():
    '''Testing f_c function
    '''
    term_x = Term(True, 'X_0')
    term_y = Term(True, 'X_1')
    term_z = Term(True, 'X_2')
    r = Atom([term_x, term_y], 'r')
    p = Atom([term_x, term_z], 'p')
    q = Atom([term_z, term_y], 'q')
    clause = Clause(r, [p, q])

    # Example from the book
    example_val = {
        Atom([term_a, term_a], 'p'): 1.0,
        Atom([term_a, term_b], 'p'): 0.9,
        Atom([term_a, term_a], 'q'): 0.1,
        Atom([term_b, term_a], 'q'): 0.2,
        Atom([term_b, term_b], 'q'): 0.8,
    }
    for key in example_val:
        initial_valuation[valuation_mapping[key]] = example_val[key]
    x_c = Inference.x_c(clause, valuation_mapping, ['a', 'b'])
    print(valuation_mapping)
    print(x_c)
    def generate_clauses(self):
        '''Generate all clauses with some level of optimization
        '''
        rule_matrix = []
        for rule in self.rules:
            # logger.info('Generating clauses')
            if rule == None:
                rule_matrix.append([None])
                continue
            clauses = []
            if (rule.allow_intensional):
                p = list(set(self.p_e + self.p_i + [self.target]))
                p_i = list(set(self.p_i))
                intensional_predicates = [atom.predicate for atom in p_i]
            else:
                p = list(set(self.p_e))
            variables = [
                'X_%d' % i for i in range(0, self.target.arity + rule.v)
            ]
            target_variables = [
                'X_%d' % i for i in range(0, self.target.arity)
            ]

            # Generate the body list
            body_list = []
            head = Atom([Term(True, var) for var in target_variables],
                        self.target.predicate)
            for var1 in variables:
                for var2 in variables:
                    term1 = Term(True, var1)
                    term2 = Term(True, var2)
                    body_list.append([term1, term2])
            # Generate the list
            added_pred = {}
            for ind1 in range(0, len(p)):
                pred1 = p[ind1]
                for b1 in body_list:
                    for ind2 in range(ind1, len(p)):
                        pred2 = p[ind2]
                        for b2 in body_list:
                            body1 = Atom(
                                [b1[index] for index in range(0, pred1.arity)],
                                pred1.predicate)
                            body2 = Atom(
                                [b2[index] for index in range(0, pred2.arity)],
                                pred2.predicate)

                            clause = Clause(head, [body1, body2])
                            # logger.info(clause)
                            # All variables in head should be in the body
                            if not set(target_variables).issubset(
                                [v.name for v in b1] + [v.name for v in b2]):
                                continue
                            elif head == body1 or head == body2:  # No Circular
                                continue
                            # NOTE: Based on appendix requires to have a intensional predicate
                            elif rule.allow_intensional and not (
                                    body1.predicate in intensional_predicates
                                    or body2.predicate
                                    in intensional_predicates):
                                continue
                            elif clause in added_pred:
                                continue
                            else:
                                added_pred[clause] = 1
                                clauses.append(clause)
            rule_matrix.append(clauses)
            # logger.info('Clauses Generated')
        return rule_matrix
Example #6
0
    def generate_clauses(self):
        '''Generate all clauses with some level of optimization
        '''
        rule_matrix = []
        for rule in self.rules:
            # logger.info('Generating clauses')
            if rule is None:
                rule_matrix.append([None])
                continue
            clauses = []
            if rule.allow_intensional:
                p = list(set(self.p_e + self.p_i + [self.target]))
                p_i = list(set(self.p_i))
                intensional_predicates = [atom.predicate for atom in p_i]
            else:
                p = list(set(self.p_e))
            variables = ['X_%d' %
                         i for i in range(0, self.target.arity + rule.v)]
            target_variables = ['X_%d' %
                                i for i in range(0, self.target.arity)]

            # Generate the body list
            body_list = []
            head = Atom(
                [Term(True, var) for var in target_variables], self.target.predicate)
            for var1 in variables:
                for var2 in variables:
                    term1 = Term(True, var1)
                    term2 = Term(True, var2)
                    body_list.append([term1, term2])
            # Generate the list
            added_pred = {}
            for ind1 in range(0, len(p)):
                pred1 = p[ind1]
                for b1 in body_list:
                    for ind2 in range(ind1, len(p)):
                        pred2 = p[ind2]
                        for b2 in body_list:
                            for negations in range(4):
                                if not rule.neg and negations > 0:
                                    continue
                                body1_atom = Atom([b1[index]
                                              for index in range(0, pred1.arity)], pred1.predicate)
                                body2_atom = Atom([b2[index]
                                              for index in range(0, pred2.arity)], pred2.predicate)

                                if negations == 0:  # No negation
                                    body1 = Literal(body1_atom, False)
                                    body2 = Literal(body2_atom, False)
                                if negations == 1: # First body is negated
                                    body1 = Literal(body1_atom, True)
                                    body2 = Literal(body2_atom, False)
                                if negations == 2: # Second body is negated
                                    body1 = Literal(body1_atom, False)
                                    body2 = Literal(body2_atom, True)
                                if negations == 3: # Both bodies are negated
                                    body1 = Literal(body1_atom, True)
                                    body2 = Literal(body2_atom, True)

                                clause = Clause(head, [body1, body2])
                                # logger.info(clause)
                                # All variables in head should be in the body
                                if not set(target_variables).issubset([v.name for v in b1] + [v.name for v in b2]):
                                    continue
                                elif head == body1 or head == body2:  # No Circular
                                    continue
                                # NOTE: Based on appendix requires to have a intensional predicate
                                elif rule.allow_intensional and not (body1.predicate in intensional_predicates or body2.predicate in intensional_predicates):
                                    continue
                                elif clause in added_pred:
                                    continue
                                # Disallow a clause with the head negated in the body
                                elif head.predicate == body1.predicate and body1.negated:
                                    continue
                                elif head.predicate == body2.predicate and body2.negated:
                                    continue
                                # Disallow positive and negative versions of the same literal in a clause
                                elif body1_atom == body2_atom and body1.negated != body2.negated:
                                    # If the variables of the clause are different then allow this clause
                                    variables1 = body1_atom.terms
                                    variables2 = body2_atom.terms
                                    add_clause = True
                                    for i in range(len(variables1)):
                                        if variables1[i] != variables2[i]:
                                            add_clause = False
                                            break
                                    if not add_clause:
                                        continue
                                else:
                                    added_pred[clause] = 1
                                    clauses.append(clause)
            rule_matrix.append(clauses)
            # logger.info('Clauses Generated')
        return rule_matrix
Example #7
0
from src.ilp import Language_Frame, Rule_Template, Program_Template
from src.utils import is_intensional

logging.basicConfig(filename="newfile.log",
                    format='%(asctime)s %(message)s',
                    filemode='w')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

term1 = Term(True, 'X')
term2 = Term(False, 'USA')
term3 = Term(True, 'Y')
atom1 = Atom([term1, term2], 'PresidentOf')
atom2 = Atom([term1, term2], 'BornIn')
atom3 = Atom([term1, term3], 'BornIn')
clause = Clause(atom1, [atom2])


def core_test():
    assert term1.isVariable == True
    assert term1.name == 'X'
    assert term2.isVariable != True
    assert term2.name == 'USA'
    assert atom1.predicate == 'PresidentOf'
    assert atom1.terms == [term1, term2]
    assert atom1.arity == 2
    assert clause.head == atom1
    assert clause.body == [atom2]


def equality_test():