def test_uml_associations(self): from zope import component from gaphor.UML.interfaces import IAssociationChangeEvent from gaphor.UML.properties import association, derivedunion from gaphor.UML import Element class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association('a1', A, upper=1) A.a2 = association('a2', A, upper=1) A.b1 = association('b1', A, upper='*') A.b2 = association('b2', A, upper='*') A.b3 = association('b3', A, upper=1) A.derived_a = derivedunion('derived_a', 0, 1, A.a1, A.a2) A.derived_b = derivedunion('derived_b', 0, '*', A.b1, A.b2, A.b3) events = [] @component.adapter(IAssociationChangeEvent) def handler(event, events=events): events.append(event) compreg = Application.get_service('component_registry') compreg.register_handler(handler) try: a = A() undo_manager = UndoManager() undo_manager.init(Application) undo_manager.begin_transaction() a.a1 = A() undo_manager.commit_transaction() assert len(events) == 1, events assert events[0].property is A.a1 assert undo_manager.can_undo() undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert len(events) == 2, events assert events[1].property is A.a1 finally: compreg.unregister_handler(handler) undo_manager.shutdown()
def test_uml_associations(self): from gaphor.UML.event import AssociationUpdated from gaphor.UML.properties import association, derivedunion from gaphor.UML import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association("a1", A, upper=1) A.a2 = association("a2", A, upper=1) A.b1 = association("b1", A, upper="*") A.b2 = association("b2", A, upper="*") A.b3 = association("b3", A, upper=1) A.derived_a = derivedunion(A, "derived_a", 0, 1, A.a1, A.a2) A.derived_b = derivedunion(A, "derived_b", 0, "*", A.b1, A.b2, A.b3) events = [] @event_handler(AssociationUpdated) def handler(event, events=events): events.append(event) event_manager.subscribe(handler) try: a = element_factory.create(A) undo_manager.begin_transaction() a.a1 = element_factory.create(A) undo_manager.commit_transaction() assert len(events) == 1, events assert events[0].property is A.a1 assert undo_manager.can_undo() undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert len(events) == 2, events assert events[1].property is A.a1 finally: event_manager.unsubscribe(handler) undo_manager.shutdown()
def test_uml_associations(self): from gaphor.UML.event import AssociationChangeEvent from gaphor.UML.properties import association, derivedunion from gaphor.UML import Element class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association("a1", A, upper=1) A.a2 = association("a2", A, upper=1) A.b1 = association("b1", A, upper="*") A.b2 = association("b2", A, upper="*") A.b3 = association("b3", A, upper=1) A.derived_a = derivedunion("derived_a", 0, 1, A.a1, A.a2) A.derived_b = derivedunion("derived_b", 0, "*", A.b1, A.b2, A.b3) events = [] @event_handler(AssociationChangeEvent) def handler(event, events=events): events.append(event) em = Application.get_service("event_manager") em.subscribe(handler) try: a = A(factory=self.element_factory) undo_manager = UndoManager() undo_manager.init(Application) undo_manager.begin_transaction() a.a1 = A(factory=self.element_factory) undo_manager.commit_transaction() assert len(events) == 1, events assert events[0].property is A.a1 assert undo_manager.can_undo() undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert len(events) == 2, events assert events[1].property is A.a1 finally: em.unsubscribe(handler) undo_manager.shutdown()
def test_uml_associations(self): from gaphor.UML.event import AssociationChangeEvent from gaphor.UML.properties import association, derivedunion from gaphor.UML import Element class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association("a1", A, upper=1) A.a2 = association("a2", A, upper=1) A.b1 = association("b1", A, upper="*") A.b2 = association("b2", A, upper="*") A.b3 = association("b3", A, upper=1) A.derived_a = derivedunion("derived_a", 0, 1, A.a1, A.a2) A.derived_b = derivedunion("derived_b", 0, "*", A.b1, A.b2, A.b3) events = [] @event_handler(AssociationChangeEvent) def handler(event, events=events): events.append(event) compreg = Application.get_service("component_registry") compreg.register_handler(handler) try: a = A(factory=self.element_factory) undo_manager = UndoManager() undo_manager.init(Application) undo_manager.begin_transaction() a.a1 = A(factory=self.element_factory) undo_manager.commit_transaction() assert len(events) == 1, events assert events[0].property is A.a1 assert undo_manager.can_undo() undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert len(events) == 2, events assert events[1].property is A.a1 finally: compreg.unregister_handler(handler) undo_manager.shutdown()
def test_derivedunion(): class A(Element): pass A.a = association("a", A) A.b = association("b", A, 0, 1) A.u = derivedunion(A, "u", object, 0, "*", A.a, A.b) a = A() assert len(a.a) == 0, f"a.a = {a.a}" assert len(a.u) == 0, f"a.u = {a.u}" a.a = b = A() a.a = c = A() assert len(a.a) == 2, f"a.a = {a.a}" assert b in a.a assert c in a.a assert len(a.u) == 2, f"a.u = {a.u}" assert b in a.u assert c in a.u a.b = d = A() assert len(a.a) == 2, f"a.a = {a.a}" assert b in a.a assert c in a.a assert d == a.b assert len(a.u) == 3, f"a.u = {a.u}" assert b in a.u assert c in a.u assert d in a.u
def test_derivedunion_notify(): class A(Element): pass class E(Element): notified = False def notify(self, name, pspec): if name == "u": self.notified = True E.a = association("a", A) E.u = derivedunion(E, "u", A, 0, "*", E.a) e = E() assert e.notified is False e.a = A() assert e.notified is True
def test_derivedunion_notify_for_single_derived_property(): class A(Element): pass class E(Element): notified = False def handle(self, event): if event.property is E.u: self.notified = True E.a = association("a", A) E.u = derivedunion(E, "u", A, 0, "*", E.a) e = E() e.a = A() assert e.notified is True
def test_derivedunion_listmixins(): class A(Element): pass A.a = association("a", A) A.b = association("b", A) A.u = derivedunion(A, "u", A, 0, "*", A.a, A.b) A.name = attribute("name", str, "default") a = A() a.a = A() a.a = A() a.b = A() a.a[0].name = "foo" a.a[1].name = "bar" a.b[0].name = "baz" assert list(a.a[:].name) == ["foo", "bar"] assert sorted(list(a.u[:].name)) == ["bar", "baz", "foo"]
def test_derivedunion_events(): from gaphor.UML.event import DerivedAdded, DerivedDeleted class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association("a1", A, upper=1) A.a2 = association("a2", A, upper=1) A.b1 = association("b1", A, upper="*") A.b2 = association("b2", A, upper="*") A.b3 = association("b3", A, upper=1) A.derived_a = derivedunion(A, "derived_a", object, 0, 1, A.a1, A.a2) A.derived_b = derivedunion(A, "derived_b", object, 0, "*", A.b1, A.b2, A.b3) events = [] @event_handler(AssociationUpdated) def handler(event, events=events): events.append(event) Application.register_handler(handler) try: a = A() a.a1 = A() assert len(events) == 2 assert events[0].property is A.derived_a assert events[1].property is A.a1 assert a.derived_a is a.a1 a.a1 = A() assert len(events) == 4, len(events) assert a.derived_a is a.a1 a.a2 = A() # Should not emit DerivedSet assert len(events) == 5, len(events) assert events[4].property is A.a2 del events[:] old_a1 = a.a1 del a.a1 assert len(events) == 2, len(events) assert events[0].property is A.derived_a assert events[0].new_value is a.a2, "{} {} {}".format( a.a1, a.a2, events[3].new_value) assert events[0].old_value is old_a1, "{} {} {}".format( a.a1, a.a2, events[3].old_value) assert events[1].property is A.a1 del events[:] old_a2 = a.a2 del a.a2 assert len(events) == 2, len(events) assert events[0].property is A.derived_a assert events[0].new_value is None, "{} {} {}".format( a.a1, a.a2, events[5].new_value) assert events[0].old_value is old_a2, "{} {} {}".format( a.a1, a.a2, events[5].old_value) assert events[1].property is A.a2 del events[:] assert len(events) == 0, len(events) a.b1 = A() assert len(events) == 2 assert events[0].property is A.derived_b assert events[1].property is A.b1 a.b2 = A() assert len(events) == 4 assert events[2].property is A.derived_b assert events[3].property is A.b2 a.b2 = A() assert len(events) == 6 assert events[4].property is A.derived_b assert events[5].property is A.b2 a.b3 = A() assert len(events) == 8, len(events) assert events[6].property is A.derived_b assert events[7].property is A.b3 # Add b3's value to b2, should not emit derived union event a.b2 = a.b3 assert len(events) == 9, len(events) assert events[8].property is A.b2 # Remove b3's value to b2 del a.b2[a.b3] assert len(events) == 10, len(events) assert events[9].property is A.b2 a.b3 = A() assert len(events) == 13, len(events) assert events[10].property is A.derived_b assert isinstance(events[10], DerivedDeleted), type(events[10]) assert events[11].property is A.derived_b assert isinstance(events[11], DerivedAdded), type(events[11]) assert events[12].property is A.b3 del a.b3 assert len(events) == 15, len(events) assert events[13].property is A.derived_b assert isinstance(events[13], DerivedDeleted), type(events[10]) assert events[14].property is A.b3 finally: Application.unregister_handler(handler)
ReceiveSignalEvent.signal = association("signal", Signal, lower=1, upper=1) # 23: override MultiplicityElement.lower derives MultiplicityElement.lowerValue MultiplicityElement.lower = derived( "lower", object, 0, 1, MultiplicityElement.lowerValue ) MultiplicityElement.lower.filter = lambda obj: [obj.lowerValue] # MultiplicityElement.lower = MultiplicityElement.lowerValue # 28: override MultiplicityElement.upper derives MultiplicityElement.upperValue MultiplicityElement.upper = derived( "upper", object, 0, 1, MultiplicityElement.upperValue ) MultiplicityElement.upper.filter = lambda obj: [obj.upperValue] # MultiplicityElement.upper = MultiplicityElement.upperValue # 127: override Property.isComposite derives Property.aggregation # Property.isComposite = property(lambda self: self.aggregation == 'composite') Property.isComposite = derivedunion("isComposite", bool, 0, 1, Property.aggregation) Property.isComposite.filter = lambda obj: [obj.aggregation == "composite"] RedefinableElement.redefinedElement = derivedunion( "redefinedElement", RedefinableElement, 0, "*", Property.redefinedProperty, Classifier.redefinedClassifier, Operation.redefinedOperation, Interface.redefinedInterface, Behavior.redefinedBehavior, Connector.redefinedConnector, ) Classifier.attribute = derivedunion( "attribute",