def _load_subtype_fact(self, xml_node): """ Load a subtype fact, which indicates a subtype constraint. Note, we chose not to move this node under <Constraints>, because it must be loaded prior to any associated XOR/IOR constraints. """ attribs, name = get_basic_attribs(xml_node) # Get super and sub type XML nodes factroles = find(xml_node, "FactRoles") super_node = find(factroles, "SupertypeMetaRole") sub_node = find(factroles, "SubtypeMetaRole") supertype_node = find(super_node, "RolePlayer") subtype_node = find(sub_node, "RolePlayer") # Look-up the corresponding object types try: supertype = self._elements[supertype_node.get("ref")] subtype = self._elements[subtype_node.get("ref")] except KeyError: raise Exception("Cannot load subtype constraint.") # Does this subtype constraint provide a path to the preferred ID? path = (xml_node.get("PreferredIdentificationPath") == "true") # Create constraint cons = Constraint.SubtypeConstraint(subtype, supertype, path, **attribs) # If there are additional constraints on the subtype (e.g. XOR or IOR), # their role sequence will consist of the subtype fact's roles. We will # redirect the id for those roles to this constraint, so that the covers # attribute is a list of SubtypeConstraints for constraints on subtypes. self._elements[super_node.get("id")] = cons self._elements[sub_node.get("id")] = cons self._add(cons)
def test_commit_and_rollback_subtype(self): """ Test commit and rollback of a subtype constraint. """ obj1 = ObjectType(name="O1") obj2 = ObjectType(name="O2") cons = Constraint.SubtypeConstraint(subtype=obj1, supertype=obj2) self.assertEquals(obj1.covered_by, []) self.assertEquals(obj1.direct_subtypes, []) self.assertEquals(obj1.direct_supertypes, []) self.assertEquals(obj2.covered_by, []) self.assertEquals(obj2.direct_subtypes, []) self.assertEquals(obj2.direct_supertypes, []) cons.commit() self.assertEquals(obj1.covered_by, [cons]) self.assertEquals(obj1.direct_subtypes, []) self.assertEquals(obj1.direct_supertypes, [obj2]) self.assertEquals(obj2.covered_by, [cons]) self.assertEquals(obj2.direct_subtypes, [obj1]) self.assertEquals(obj2.direct_supertypes, []) with self.assertRaises(NotImplementedError): cons.rollback() # The commented out code below was used before I decided to raise # NotImplementedError for rollback. See the comments in rollback(). """