Exemplo n.º 1
0
    def commit(self):
        """ Commit side effects. """
        for orig_role in self.corr:
            ref_roles = orig_role.player.ref_roles
            new_role = self.corr[orig_role]

            if orig_role in ref_roles and new_role not in ref_roles:
                ref_roles.append(new_role)

        FactType.commit(self)
Exemplo n.º 2
0
    def test_commit_rollback_iuc(self):
        """ Test commit and rollback of internal uniqueness constraints."""
        obj1 = EntityType(name="O1")
        obj2 = EntityType(name="V1")

        fact1 = FactType(name="F1")
        role1 = fact1.add_role(player=obj1)
        role2 = fact1.add_role(player=obj2)
        fact1.commit()

        fact2 = FactType(name="F2")
        role3 = fact2.add_role(player=obj1)
        fact2.commit()

        cons1 = Constraint.UniquenessConstraint(covers=[role1],
                                                identifier_for=None)
        cons2 = Constraint.UniquenessConstraint(covers=[role2],
                                                identifier_for=obj1)

        self.assertEquals(role1.covered_by, [])
        self.assertEquals(role2.covered_by, [])

        self.assertEquals(obj1.identifying_constraint, None)
        self.assertEquals(obj1.ref_roles, [])
        self.assertItemsEqual(obj1.roles, [role1, role3])

        cons1.commit()
        cons2.commit()

        self.assertEquals(role1.covered_by, [cons1])
        self.assertEquals(role2.covered_by, [cons2])
        self.assertEquals(obj1.identifying_constraint, cons2)
        self.assertEquals(obj1.ref_roles, [role1])

        cons1.rollback()
        cons2.rollback()

        self.assertEquals(role1.covered_by, [])
        self.assertEquals(role2.covered_by, [])
        self.assertEquals(obj1.identifying_constraint, None)
        self.assertEquals(obj1.ref_roles, [])
Exemplo n.º 3
0
    def test_commit_and_rollback_affect_on_role_unique(self):
        """ Test affect of commit and rollback on role.unique """
        fact = FactType("AHasB")
        fact.add_role(ObjectType("A"))
        fact.add_role(ObjectType("B"))
        fact.commit()

        role = fact.roles[0]
        self.assertFalse(role.unique)

        # Unique after covered by a simple IUC
        uniq1 = Constraint.UniquenessConstraint(covers=[role])
        uniq1.commit()
        self.assertTrue(role.unique)

        # No longer unique after rollback
        uniq1.rollback()
        self.assertFalse(role.unique)

        # Not unique after covered by spanning IUC
        uniq2 = Constraint.UniquenessConstraint(covers=[role, fact.roles[1]])
        uniq2.commit()
        self.assertFalse(role.unique)

        # Unique again, covered by simple IUC
        uniq1.commit()
        self.assertTrue(role.unique)

        # Cover by a second simple IUC
        uniq3 = Constraint.UniquenessConstraint(covers=[role])
        uniq3.commit()

        # Still unique after rollback, because it's still covered by a simple IUC
        uniq1.rollback()
        self.assertTrue(role.unique)

        # No longer unique: not covered by any simple IUCs.
        uniq3.rollback()
        self.assertFalse(role.unique)
Exemplo n.º 4
0
    def test_add_role(self):
        """ Confirm we can add a role to the fact type. """
        obj1 = ObjectType(name="Person")
        obj2 = ObjectType(name="School")

        fact_type = FactType(name="PersonAttendsSchoolWithPersonAndPerson")

        role1 = fact_type.add_role(obj1)
        role2 = fact_type.add_role(obj2)
        role3 = fact_type.add_role(obj1)
        role4 = fact_type.add_role(obj1)

        self.assertEquals(fact_type.arity(), 4)

        self.assertIs(role1.fact_type, fact_type)
        self.assertIs(role1.player, obj1)
        self.assertEquals(role1.name, "Person")

        self.assertIs(role2.fact_type, fact_type)
        self.assertIs(role2.player, obj2)
        self.assertEquals(role2.name, "School")

        self.assertIs(role3.fact_type, fact_type)
        self.assertIs(role3.player, obj1)
        self.assertEquals(role3.name, "Person2")

        self.assertIs(role4.fact_type, fact_type)
        self.assertIs(role4.player, obj1)
        self.assertEquals(role4.name, "Person3")

        self.assertItemsEqual(obj1.roles, [])
        self.assertItemsEqual(obj2.roles, [])

        fact_type.commit()

        self.assertItemsEqual(obj1.roles, [role1, role3, role4])
        self.assertItemsEqual(obj2.roles, [role2])
Exemplo n.º 5
0
    def test_commit_rollback_euc(self):
        """ Test commit and rollback of external uniqueness constraint."""
        obj1 = EntityType(name="O1")
        obj2 = EntityType(name="V1")
        obj3 = EntityType(name="V2")

        fact1 = FactType(name="F1")
        role11 = fact1.add_role(player=obj1)
        role12 = fact1.add_role(player=obj2)
        fact1.commit()

        fact2 = FactType(name="F2")
        role21 = fact2.add_role(player=obj1)
        role23 = fact2.add_role(player=obj3)
        fact2.commit()

        fact3 = FactType(name="F3")
        role31 = fact3.add_role(player=obj1)
        fact3.commit()

        cons = Constraint.UniquenessConstraint(covers=[role12, role23],
                                               identifier_for=obj1)

        self.assertEquals(obj1.identifying_constraint, None)
        self.assertEquals(obj1.ref_roles, [])
        self.assertItemsEqual(obj1.roles, [role11, role21, role31])

        cons.commit()

        self.assertEquals(obj1.identifying_constraint, cons)
        self.assertItemsEqual(obj1.ref_roles, [role11, role21])

        cons.rollback()

        self.assertEquals(obj1.identifying_constraint, None)
        self.assertEquals(obj1.ref_roles, [])