Пример #1
0
def add_rules(model):
    model.add_rule(Rule("10: Block(P1, A) & Block(P2, A) & Similar(P1, P2) & P1 != P2 -> Friends(P1, P2) ^2"))
    model.add_rule(Rule("10: Block(P1, A) & Block(P2, A) & Block(P3, A) & Friends(P1, P2) & Friends(P2, P3) & P1 != P2 & P2 != P3 & P1 != P3 -> Friends(P1, P3) ^2"))
    model.add_rule(Rule("10: Block(P1, A) & Block(P2, A) & Friends(P1, P2) & P1 != P2 -> Friends(P2, P1) ^2"))

    # Negative prior.
    model.add_rule(Rule("1: !Friends(P1, P2) ^2"))
Пример #2
0
def similarity(model, combination=False, squared=True):
    if not combination:
        model.add_rule(
            Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B)",
                 squared=squared))
        model.add_rule(
            Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0')",
                 squared=squared))

    model.add_rule(
        Rule("1.0: Knows(A, B) & SameTastes(A, B) & (A != B) -> Trusts(A, B)",
             squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & !SameTastes(A, B) & (A != B) -> !Trusts(A, B)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(A, C) & Trusts(A, B) & SameTastes(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(A, C) & !Trusts(A, B) & SameTastes(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, C) & Knows(B, C) & Trusts(A, C) & SameTastes(A, B) & (A != B) & (B != C) & (A != C) -> Trusts(B, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, C) & Knows(B, C) & !Trusts(A, C) & SameTastes(A, B) & (A != B) & (B != C) & (A != C) -> !Trusts(B, C)",
            squared=squared))
Пример #3
0
def personality_rules(model, combination=False, squared=True):
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusts(A, B) -> TrustWorthy(B)",
             squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusts(A, B) -> Trusting(A)",
             squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & !Trusts(A, B) -> !TrustWorthy(B)",
             squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & !Trusts(A, B) -> !Trusting(A)",
             squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusting(A) & TrustWorthy(B) -> Trusts(A, B)",
             squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & !Trusting(A) & !TrustWorthy(B) -> !Trusts(A, B)",
            squared=squared))

    if not combination:
        model.add_rule(
            Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B)",
                 squared=squared))
        model.add_rule(
            Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0')",
                 squared=squared))
Пример #4
0
    def _add_rules(self, model, relations):
        """
        Add rules connecting entities together.
        """
        model.add_rule(Rule('1.0: ~spam_msg(M) ^2'))
        model.add_rule(Rule('1.0: prior_msg(M) -> spam_msg(M) ^2'))

        for relation in relations:
            var = relation[0].upper()
            r1 = '1.0: has_{}(M, {}) & spam_{}({}) -> spam_msg(M) ^2'
            r2 = '1.0: has_{}(M, {}) & spam_msg(M) -> spam_{}({}) ^2'

            model.add_rule(Rule('1.0: ~spam_{}({}) ^2'.format(relation, var)))
            model.add_rule(Rule(r1.format(relation, var, relation, var)))
            model.add_rule(Rule(r2.format(relation, var, relation, var)))
Пример #5
0
def add_rules(model):
    # Neighborhood rules.
    model.add_rule(
        Rule("1.0: HasCat(A, C) & Link(A, B) & (A != B) >> HasCat(B, C) ^2"))
    model.add_rule(
        Rule("1.0: HasCat(A, C) & Link(B, A) & (A != B) >> HasCat(B, C) ^2"))

    # Per category rules
    for i in range(NUM_CATEGORIES):
        # Categories are 1-indexed.
        rule_string = "HasCat(A, '%d') & Link(A, B) >> HasCat(B, '%d')" % (
            i + 1, i + 1)
        model.add_rule(Rule(rule_string, weight=1.0, squared=True))

    # Ensure that HasCat sums to 1
    model.add_rule(Rule("HasCat(A, +C) = 1 ."))

    # Prior
    model.add_rule(Rule("0.001: !HasCat(A, N) ^2"))
Пример #6
0
def add_rules(model):
    model.add_rule(Rule("0.50: Bias(A, P) >> Votes(A, P) ^2"))
    model.add_rule(
        Rule("0.30: Votes(A, P) & KnowsWell(B, A) >> Votes(B, P) ^2"))
    model.add_rule(Rule("0.10: Votes(A, P) & Knows(B, A) >> Votes(B, P) ^2"))
    model.add_rule(Rule("0.05: Votes(A, P) & Boss(B, A) >> Votes(B, P) ^2"))
    model.add_rule(Rule("0.10: Votes(A, P) & Mentor(B, A) >> Votes(B, P) ^2"))
    model.add_rule(
        Rule("0.70: Votes(A, P) & OlderRelative(B, A) >> Votes(B, P) ^2"))
    model.add_rule(Rule("0.80: Votes(A, P) & Idol(B, A) >> Votes(B, P) ^2"))

    # Negative prior
    model.add_rule(Rule("0.01: !Votes(A, P) ^2"))

    # Partial functional on votes.
    model.add_rule(Rule("Votes(A, +B) <= 1 ."))
Пример #7
0
def add_rules(model):
    # Look for text similarity.
    model.add_rule(
        Rule(
            "40.0: AuthorName(A1, N1) & AuthorName(A2, N2) & SimName(N1, N2) & (A1 != A2) -> SameAuthor(A1, A2) ^2"
        ))
    model.add_rule(
        Rule(
            "40.0: PaperTitle(P1, T1) & PaperTitle(P2, T2) & SimTitle(T1, T2) & (P1 != P2) -> SamePaper(P1, P2) ^2"
        ))

    # Pure transitivity
    model.add_rule(
        Rule("""
        20.0: AuthorBlock(A1, B) & AuthorBlock(A2, B) & AuthorBlock(A3, B)
            & SameAuthor(A1, A2) & SameAuthor(A2, A3)
            & (A1 != A3) & (A1 != A2) & (A2 != A3)
            -> SameAuthor(A1, A3) ^2
    """))

    # Coauthor rectangle closure.
    model.add_rule(
        Rule("""
        20.0: AuthorBlock(A1, B1) & AuthorBlock(A2, B1) & AuthorBlock(CA1, B2) & AuthorBlock(CA2, B2) &
            & AuthorOf(A1, P1) & AuthorOf(A2, P2)
            & AuthorOf(CA1, P1) & AuthorOf(CA2, P2) & SameAuthor(CA1, CA2)
            & (A1 != CA1) & (A2 != CA2) & (P1 != P2)
            -> SameAuthor(A1, A2) ^2
    """))

    # Paper rectangle closure.
    model.add_rule(
        Rule("""
        10.0: AuthorBlock(A1, B1) & AuthorBlock(A2, B1)
            & AuthorOf(A1, P1) & AuthorOf(A2, P2)
            & SamePaper(P1, P2)
            -> SameAuthor(A1, A2) ^2
    """))

    # Self-refernece.
    model.add_rule(Rule("SameAuthor(A, A) = 1.0 ."))
    model.add_rule(Rule("SamePaper(P, P) = 1.0 ."))

    # Negative priors.
    model.add_rule(Rule("1.0: !SameAuthor(A1, A2) ^2"))
    model.add_rule(Rule("1.0: !SamePaper(A1, A2) ^2"))
Пример #8
0
def add_rules (model):
    model.add_rule(Rule("20: Lived(P1, L) & Lived(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2"))
    model.add_rule(Rule("5: Lived(P1, L1) & Lived(P2, L2) & (P1 != P2) & (L1 != L2) -> !Knows(P1, P2) ^2"))
    model.add_rule(Rule("10: Likes(P1, L) & Likes(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2"))
    model.add_rule(Rule("5: Knows(P1, P2) & Knows(P2, P3) & (P1 != P3) -> Knows(P1, P3) ^2"))
    model.add_rule(Rule("Knows(P1, P2) = Knows(P2, P1) ."))
    model.add_rule(Rule("5: !Knows(P1, P2) ^2"))
Пример #9
0
def add_rules(model):
    model.add_rule(Rule('20: Lived(P1, L) & Lived(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'))
    model.add_rule(Rule('5: Lived(P1, L1) & Lived(P2, L2) & (P1 != P2) & (L1 != L2) -> !Knows(P1, P2) ^2'))
    model.add_rule(Rule('10: Likes(P1, L) & Likes(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'))
    model.add_rule(Rule('5: Knows(P1, P2) & Knows(P2, P3) & (P1 != P3) -> Knows(P1, P3) ^2'))
    model.add_rule(Rule('Knows(P1, P2) = Knows(P2, P1) .'))
    model.add_rule(Rule('5: !Knows(P1, P2) ^2'))
Пример #10
0
    def test_load_from_file(self):
        path = os.path.join(PSLTest.SIMPLE_ACQUAINTANCES_PSL_DIR, 'simple-acquaintances.psl')
        rules = Rule.load_from_file(path)

        expected = [
            '20.00: Lived(P1, L) & Lived(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2',
            '5.00: Lived(P1, L1) & Lived(P2, L2) & (P1 != P2) & (L1 != L2) -> !Knows(P1, P2) ^2',
            '10.00: Likes(P1, L) & Likes(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2',
            '5.00: Knows(P1, P2) & Knows(P2, P3) & (P1 != P3) -> Knows(P1, P3) ^2',
            'Knows(P1, P2) = Knows(P2, P1) .',
            '5.00: !Knows(P1, P2) ^2',
        ]

        self.assertEquals(len(rules), len(expected))
        for i in range(len(rules)):
            self.assertEquals(rules[i].to_string(2), expected[i])
Пример #11
0
def run():
    model = Model(MODEL_NAME)

    # Add Predicates

    knows_predicate = Predicate('Knows', closed=False, size=2)
    model.add_predicate(knows_predicate)

    likes_predicate = Predicate('Likes', closed=True, size=2)
    model.add_predicate(likes_predicate)

    lived_predicate = Predicate('Lived', closed=True, size=2)
    model.add_predicate(lived_predicate)

    # Add Data

    path = os.path.join(DATA_DIR, 'knows_obs.txt')
    knows_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'lived_obs.txt')
    lived_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'likes_obs.txt')
    likes_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'knows_targets.txt')
    knows_predicate.add_data_file(Partition.TARGETS, path)

    path = os.path.join(DATA_DIR, 'knows_truth.txt')
    knows_predicate.add_data_file(Partition.TRUTH, path)

    # Add Rules
    model.add_rule(
        Rule('20: Lived(P1, L) & Lived(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'
             ))
    model.add_rule(
        Rule(
            '5: Lived(P1, L1) & Lived(P2, L2) & (P1 != P2) & (L1 != L2) -> !Knows(P1, P2) ^2'
        ))
    model.add_rule(
        Rule('10: Likes(P1, L) & Likes(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'
             ))
    model.add_rule(
        Rule(
            '5: Knows(P1, P2) & Knows(P2, P3) & (P1 != P3) -> Knows(P1, P3) ^2'
        ))
    model.add_rule(Rule('Knows(P1, P2) = Knows(P2, P1) .'))
    model.add_rule(Rule('5: !Knows(P1, P2) ^2'))

    # Run Inference
    results = model.infer(psl_config=ADDITIONAL_PSL_OPTIONS)

    return results
Пример #12
0
def balance5rules(model, recip=False, squared=True):

    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & !Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & Trusts(A, B) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & Trusts(B, A) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    # two-sided prior
    model.add_rule(
        Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B)", squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0')", squared=squared))
    if recip:
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, A) & Trusts(A, B) -> Trusts(B, A)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, A) & !Trusts(A, B) -> !Trusts(B, A)",
                squared=squared))
Пример #13
0
    def test_set_weight(self):
        rule = Rule('1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) ^2')
        self.assertEquals(rule.weight(), 1.0)

        rule.set_weight(5.0)
        self.assertEquals(rule.weight(), 5.0)

        rule.set_weight(0.0)
        self.assertEquals(rule.weight(), 0.0)

        try:
            rule.set_weight(-1.0)
            self.fail('No exception when setting negative weight.')
        except RuleError as ex:
            # Expected
            pass

        rule = Rule('Nice(A) & Nice(B) & (A != B) -> Friends(A, B) .')
        try:
            rule.set_weight(1.0)
            self.fail('No exception when setting weight of an unweighted rule.')
        except RuleError as ex:
            # Expected
            pass
Пример #14
0
def status_rules(model, inv=False, squared=True):
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & !Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & Trusts(A, B) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))

    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & !Trusts(A, B) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & Trusts(B, A) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & !Trusts(B, A) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))

    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & Trusts(B, A) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & !Trusts(B, A) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))

    model.add_rule(
        Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B)", squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0')", squared=squared))

    if inv:
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, A) & Trusts(A, B) -> !Trusts(B, A)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, A) & !Trusts(A, B) -> Trusts(B, A)",
                squared=squared))
Пример #15
0
def cyclic_bal_rules(model, unbalanced=False, squared=True):

    model.add_rule(
        Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B)", squared=squared))
    model.add_rule(
        Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0')", squared=squared))

    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(C, A) & Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(C, A)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(C, A) & !Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(C, A)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & Trusts(A,B) & Trusts(A,C) & (A != B) & (B != C) & (A != C) -> Trusts(C,B)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & Trusts(A,B) & !Trusts(A,C) & (A != B) & (B != C) & (A != C) -> !Trusts(C,B)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & !Trusts(A,B) & Trusts(A,C) & (A != B) & (B != C) & (A != C) -> !Trusts(C,B)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & knows(A, C) & knows(C, B) & !Trusts(A,B) & !Trusts(A,C) & (A != B) & (B != C) & (A != C) -> Trusts(C,B)",
            squared=squared))

    if unbalanced:
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, C) & Knows(C, A) & Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(C, A)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(B, C) & Knows(C, A) & Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(C, A)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & Trusts(A,B) & Trusts(A,C) & (A != B) & (B != C) & (A != C) -> !Trusts(C,B)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & Trusts(A,B) & !Trusts(A,C) & (A != B) & (B != C) & (A != C) -> Trusts(C,B)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & Knows(A, C) & Knows(C, B) & !Trusts(A,B) & Trusts(A,C) & (A != B) & (B != C) & (A != C) ->  Trusts(C,B)",
                squared=squared))
        model.add_rule(
            Rule(
                "1.0: Knows(A, B) & knows(A, C) & knows(C, B) & !Trusts(A,B) & !Trusts(A,C) & (A != B) & (B != C) & (A != C) -> !Trusts(C,B)",
                squared=squared))
Пример #16
0
    def test_set_squared(self):
        rule = Rule('1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) ^2')
        self.assertEquals(rule.squared(), True)

        rule.set_squared(False)
        self.assertEquals(rule.squared(), False)

        rule.set_squared(True)
        self.assertEquals(rule.squared(), True)

        rule = Rule('Nice(A) & Nice(B) & (A != B) -> Friends(A, B) .')
        try:
            rule.set_squared(True)
            self.fail('No exception when squared an unweighted rule.')
        except RuleError as ex:
            # Expected
            pass
Пример #17
0
    def test_init_args(self):
        failing_configs = [
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B) .',
                'weighted': True
            }, 'Unweighted mismatch.'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
                'weighted': False,
            }, 'Weighted mismatch.'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) .',
            }, 'Both weighted and unweighted.'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B) ^2 .',
            }, 'Squared unweighted.'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B) . ^2',
            }, 'Unweighted squared.'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) . ^2',
            }, 'Weight unweighted squared.'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
            }, 'Missing weight 1.'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
                'weighted': True,
            }, 'Missing weight 2.'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
                'weight': 0.0,
            }, 'Weight mismatch.'),
            ({
                'rule_string': '-1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
            }, 'Negative weight (string).'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
                'weight': -1.0,
            }, 'Negative weight (arg).'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) ^2',
                'squared': False,
            }, 'Squred mismatch 1.'),
            ({
                'rule_string': '1.0: Nice(A) & Nice(B) & (A != B) -> Friends(A, B) ^1',
                'squared': True,
            }, 'Squred mismatch 2.'),
            ({
                'rule_string': 'Nice(A) & Nice(B) & (A != B) -> Friends(A, B)',
                'weighted': False,
                'squared': True,
            }, 'Squred mismatch 2.'),
        ]

        for (args, reason) in failing_configs:
            try:
                rule = Rule(**args)
                self.fail('Failed to raise exception on: ' + reason)
            except RuleError as ex:
                # Expected
                pass
Пример #18
0
def add_rules(model):
    # FFpp
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))
    # FFpm
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # FFmp
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & !Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # FFmm
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & !Trusts(A, B) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))

    # FBpp
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & Trusts(A, B) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))
    # FBpm
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & Trusts(A, B) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # FBmp
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & !Trusts(A, B) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # FBmm
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & !Trusts(A, B) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))

    # BFpp
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & Trusts(B, A) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))
    # BFpm
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & Trusts(B, A) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # BFmp
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & !Trusts(B, A) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # BFmm
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & !Trusts(B, A) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))

    # BBpp
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & Trusts(B, A) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))
    # BBpm
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & Trusts(B, A) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # BBmp
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & !Trusts(B, A) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C) ^2"))
    # BBmm
    model.add_rule(Rule("1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & !Trusts(B, A) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C) ^2"))

    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, A) & Trusts(A, B) -> Trusts(B, A) ^2"))
    model.add_rule(Rule("1.0: Knows(A, B) & Knows(B, A) & !Trusts(A, B) -> !Trusts(B, A) ^2"))

    # two-sided prior
    model.add_rule(Rule("1.0: Knows(A, B) & Prior('0') -> Trusts(A, B) ^2"))
    model.add_rule(Rule("1.0: Knows(A, B) & Trusts(A, B) -> Prior('0') ^2"))
Пример #19
0
 def read_rules_from_file(predicate_path):
     for line in predicate_path.open("r").readlines():
         yield Rule(line)
Пример #20
0
def balanceExtended(model, squared=True):
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(B, C) & Knows(A, C) & !Trusts(A, B) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & Trusts(A, B) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & !Trusts(A, B) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(A, B) & Knows(C, B) & Knows(A, C) & !Trusts(A, B) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & Trusts(B, A) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & !Trusts(B, A) & Trusts(B, C) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(B, C) & Knows(A, C) & !Trusts(B, A) & !Trusts(B, C) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & Trusts(B, A) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & Trusts(B, A) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & !Trusts(B, A) & Trusts(C, B) & (A != B) & (B != C) & (A != C) -> !Trusts(A, C)",
            squared=squared))
    model.add_rule(
        Rule(
            "1.0: Knows(B, A) & Knows(C, B) & Knows(A, C) & !Trusts(B, A) & !Trusts(C, B) & (A != B) & (B != C) & (A != C) -> Trusts(A, C)",
            squared=squared))
def add_rules(model):
    model.add_rule(Rule("025: VALCAT(B, C) & SAMEENTITY(A, B) & CAT(A, C) -> CAT(B, C) ^2"))
    model.add_rule(Rule("025: VALREL(B, Z, R) & SAMEENTITY(A, B) & REL(A, Z, R) -> REL(B, Z, R) ^2"))
    model.add_rule(Rule("025: VALREL(Z, B, R) & SAMEENTITY(A, B) & REL(Z, A, R) -> REL(Z, B, R) ^2"))
    model.add_rule(Rule("100: VALCAT(A, D) & SUB(C, D) & CAT(A, C) -> CAT(A, D) ^2"))
    model.add_rule(Rule("100: VALREL(A, B, S) & RSUB(R, S) & REL(A, B, R) -> REL(A, B, S) ^2"))
    model.add_rule(Rule("100: VALCAT(A, D) & MUT(C, D) & CAT(A, C) -> !CAT(A, D) ^2"))
    model.add_rule(Rule("100: VALREL(A, B, S) & RMUT(R, S) & REL(A, B, R) -> !REL(A, B, S) ^2"))
    model.add_rule(Rule("100: VALREL(B, A, S) & INV(R, S) & REL(A, B, R) -> REL(B, A, S) ^2"))
    model.add_rule(Rule("100: VALCAT(A, C) & DOMAIN(R, C) & REL(A, B, R) -> CAT(A, C) ^2"))
    model.add_rule(Rule("100: VALCAT(B, C) & RANGE2(R, C) & REL(A, B, R) -> CAT(B, C) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_GENERAL(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) & CANDREL_GENERAL(A, B, R) -> REL(A, B, R) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_CBL(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) & CANDREL_CBL(A, B, R) -> REL(A, B, R) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_CMC(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_CPL(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) & CANDREL_CPL(A, B, R) -> REL(A, B, R) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_MORPH(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & CANDCAT_SEAL(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) & CANDREL_SEAL(A, B, R) -> REL(A, B, R) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) & PROMCAT_GENERAL(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) & PROMREL_GENERAL(A, B, R) -> REL(A, B, R) ^2"))

    # Priors
    model.add_rule(Rule("002: VALCAT(A, C) -> !CAT(A, C) ^2"))
    model.add_rule(Rule("002: VALREL(A, B, R) -> !REL(A, B, R) ^2"))
    model.add_rule(Rule("001: VALCAT(A, C) -> CAT(A, C) ^2"))
    model.add_rule(Rule("001: VALREL(A, B, R) -> REL(A, B, R) ^2"))
Пример #22
0
def add_rules(model):
    # Priors from local classifiers
    model.add_rule(
        Rule("50: Has(U, S) & Predicts(S, U, A, L) -> Is(U, A, L) ^2"))
    model.add_rule(
        Rule("50: Has(U, S) & ~Predicts(S, U, A, L) -> ~Is(U, A, L) ^2"))

    # Collective Rules for relational signals
    model.add_rule(
        Rule("100: Joins(U, G) & Joins(V, G) & Is(V, A, L) -> Is(U, A, L) ^2"))
    model.add_rule(
        Rule("100: Joins(U, G) & Joins(V, G) & ~Is(V, A, L) -> ~Is(U, A, L) ^2"
             ))
    model.add_rule(
        Rule("10: Likes(U, T) & Likes(V, T) & Is(V, A, L) -> Is(U, A, L) ^2"))
    model.add_rule(
        Rule(
            "10: Likes(U, T) & Likes(V, T) & ~Is(V, A, L) -> ~Is(U, A, L) ^2"))

    model.add_rule(Rule("1: Friend(U, V) & Is(V, A, L)-> Is(U, A, L) ^2"))
    model.add_rule(Rule("1: Friend(U, V) & ~Is(V, A, L)-> ~Is(U, A, L) ^2"))
    model.add_rule(Rule("1: Friend(V, U) & Is(V, A, L)-> Is(U, A, L) ^2"))
    model.add_rule(Rule("1: Friend(V, U) & ~Is(V, A, L)-> ~Is(U, A, L) ^2"))

    # Ensure that user has one attribute
    model.add_rule(Rule("1: Is(U, A, +L) = 1"))