def test_undo_attribute(self): from gaphor.core.modeling import Element from gaphor.core.modeling.properties import attribute event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) class A(Element): attr = attribute("attr", bytes, default="default") a = element_factory.create(A) assert a.attr == "default", a.attr undo_manager.begin_transaction() a.attr = "five" undo_manager.commit_transaction() assert a.attr == "five" undo_manager.undo_transaction() assert a.attr == "default", a.attr undo_manager.redo_transaction() assert a.attr == "five" undo_manager.shutdown()
def element_factory(): event_manager = EventManager() element_dispatcher = ElementDispatcher(event_manager, UMLModelingLanguage()) element_factory = ElementFactory(event_manager, element_dispatcher) yield element_factory element_factory.shutdown() element_dispatcher.shutdown() event_manager.shutdown()
def test_uml_associations(self): from gaphor.core.modeling.event import AssociationUpdated from gaphor.core.modeling.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("derived_a", A, 0, 1, A.a1, A.a2) A.derived_b = derivedunion("derived_b", A, 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) == 2 ), events # both AssociationSet and DerivedSet 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) == 4, events assert events[2].property is A.a1 finally: event_manager.unsubscribe(handler) undo_manager.shutdown()
def generate( filename: PathLike, outfile: PathLike, overridesfile: Optional[PathLike] = None, ) -> None: """Generates the Python data model. Opens the Gaphor model, generates the list of classes using the element factory, and then creates a new Python data model using a relationship search tree. """ element_factory = ElementFactory() modeling_language = UMLModelingLanguage() with open(filename): storage.load( filename, element_factory, modeling_language, ) classes: List = element_factory.lselect(UML.Class) classes, enumerations = find_enumerations(classes) classes = filter_out_gaphor_profile(classes) # Lambda key sort issue in mypy: https://github.com/python/mypy/issues/9656 classes = sorted( (cls for cls in classes if cls.name[0] != "~"), key=lambda c: c.name # type: ignore ) hierarchy = create_class_hierarchy(classes) uml_classes = filter_uml_classes(classes, modeling_language) with open(outfile, "w") as f: f.write(header) for cls in uml_classes: f.write(f"from gaphor.UML import {cls.name}\n") cls_written: Set[Element] = set(uml_classes) for cls in hierarchy.keys(): cls.attribute.sort(key=lambda a: a.name or "") # type: ignore[attr-defined] write_class_def(cls, hierarchy, f, cls_written) f.write("\n\n") for cls in hierarchy.keys(): write_properties(cls, f, enumerations) for cls in hierarchy.keys(): write_subsets(cls, f) element_factory.shutdown()
def test_undo_association_1_x(self): from gaphor.core.modeling import Element from gaphor.core.modeling.properties import association event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) class A(Element): pass class B(Element): pass A.one = association("one", B, 0, 1, opposite="two") B.two = association("two", A, 0, 1) a = element_factory.create(A) b = element_factory.create(B) assert a.one is None assert b.two is None undo_manager.begin_transaction() a.one = b undo_manager.commit_transaction() assert a.one is b assert b.two is a assert len(undo_manager._undo_stack) == 1 assert len(undo_manager._undo_stack[0]._actions) == 2, undo_manager._undo_stack[ 0 ]._actions undo_manager.undo_transaction() assert a.one is None assert b.two is None assert undo_manager.can_redo() assert len(undo_manager._redo_stack) == 1 assert len(undo_manager._redo_stack[0]._actions) == 2, undo_manager._redo_stack[ 0 ]._actions undo_manager.redo_transaction() assert len(undo_manager._undo_stack) == 1 assert len(undo_manager._undo_stack[0]._actions) == 2, undo_manager._undo_stack[ 0 ]._actions assert b.two is a assert a.one is b undo_manager.shutdown()
def test_verifier(): factory = ElementFactory(EventManager()) c = factory.create(UML.Class) p = factory.create(UML.Property) c.ownedAttribute = p assert not orphan_references(factory) # Now create a separate item, not part of the factory: m = UML.Comment(id="acd123") m.annotatedElement = c assert m in c.comment assert orphan_references(factory)
def generate( filename: PathLike, outfile: PathLike, overridesfile: Optional[PathLike] = None, ) -> None: """Generates the Python data model. Opens the Gaphor model, generates the list of classes using the element factory, and then creates a new Python data model using a relationship search tree. """ element_factory = ElementFactory() modeling_language = UMLModelingLanguage() with open(filename): storage.load( filename, element_factory, modeling_language, ) with open(outfile, "w") as f: f.write(header) classes: List = element_factory.lselect( lambda e: e.isKindOf(UML.Class)) classes, enumerations = find_enumerations(classes) classes = sorted((cls for cls in classes if cls.name[0] != "~"), key=lambda c: c.name) trees = create_class_trees(classes) create_referenced(classes) uml_classes = filter_uml_classes(classes, modeling_language) for cls in uml_classes: f.write(f"from gaphor.UML import {cls.name}\n") cls_written: Set[Element] = set(uml_classes) for cls in trees.keys(): cls.attribute.sort( key=lambda a: a.name or "") # type: ignore[attr-defined] write_class_def(cls, trees, f, cls_written) f.write("\n\n") for cls in trees.keys(): write_properties(cls, f, enumerations) element_factory.shutdown()
def element_factory(): event_manager = EventManager() event_manager.subscribe(handler) clear_events() factory = ElementFactory(event_manager) yield factory clear_events()
def test_element_factory_undo(self): from gaphor.core.modeling import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) undo_manager.begin_transaction() p = element_factory.create(Element) assert undo_manager._current_transaction assert undo_manager._current_transaction._actions assert undo_manager.can_undo() undo_manager.commit_transaction() assert undo_manager.can_undo() assert element_factory.size() == 1 undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert element_factory.size() == 0 undo_manager.redo_transaction() assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 1 assert element_factory.lselect()[0] is p undo_manager.shutdown()
def test_element_factory_rollback(self): from gaphor.core.modeling import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) undo_manager.begin_transaction() element_factory.create(Element) assert undo_manager._current_transaction assert undo_manager._current_transaction._actions assert undo_manager.can_undo() undo_manager.rollback_transaction() assert not undo_manager.can_undo() assert element_factory.size() == 0 undo_manager.shutdown()
def element_factory(): ef = ElementFactory() ef.create(UML.Package).name = "package" c1 = ef.create(UML.Class) c1.name = "class" c2 = ef.create(UML.Class) c2.name = "class" i1 = ef.create(UML.Interface) i1.name = "interface" c1.ownedAttribute = ef.create(UML.Property) c1.ownedAttribute[0].name = "attr" c1.ownedOperation = ef.create(UML.Operation) c1.ownedOperation[0].name = "oper" c1.ownedOperation[0].formalParameter = ef.create(UML.Parameter) UML.model.create_dependency(c1, c2) UML.model.create_generalization(c1, c2) UML.model.create_association(c1, c2) return ef
def test_undo_association_1_n(self): from gaphor.core.modeling import Element from gaphor.core.modeling.properties import association event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) class A(Element): pass class B(Element): pass A.one = association("one", B, lower=0, upper=1, opposite="two") B.two = association("two", A, lower=0, upper="*", opposite="one") a1 = element_factory.create(A) a2 = element_factory.create(A) b1 = element_factory.create(B) element_factory.create(B) undo_manager.begin_transaction() b1.two = a1 undo_manager.commit_transaction() assert a1 in b1.two assert b1 is a1.one assert len(undo_manager._undo_stack) == 1 assert len(undo_manager._undo_stack[0]._actions) == 2, undo_manager._undo_stack[ 0 ]._actions undo_manager.undo_transaction() assert len(b1.two) == 0 assert a1.one is None assert undo_manager.can_redo() assert len(undo_manager._redo_stack) == 1 assert len(undo_manager._redo_stack[0]._actions) == 2, undo_manager._redo_stack[ 0 ]._actions undo_manager.redo_transaction() assert a1 in b1.two assert b1 is a1.one undo_manager.begin_transaction() b1.two = a2 undo_manager.commit_transaction() assert a1 in b1.two assert a2 in b1.two assert b1 is a1.one assert b1 is a2.one undo_manager.shutdown()
def element_factory(event_manager): element_factory = ElementFactory( event_manager, ElementDispatcher(event_manager, UMLModelingLanguage())) yield element_factory element_factory.shutdown()
def element_factory(): event_manager = EventManager() element_factory = ElementFactory() yield element_factory element_factory.shutdown() event_manager.shutdown()
def element_factory(event_manager): return ElementFactory(event_manager)
def factory(): event_manager = EventManager() return ElementFactory(event_manager)
def element_factory(event_manager): # noqa: F811 return ElementFactory( event_manager, ElementDispatcher(event_manager, MockModelingLanguage()))
def element_factory(event_manager): return ElementFactory( event_manager, ElementDispatcher(event_manager, UMLModelingLanguage()))
def test_redo_stack(self): from gaphor.core.modeling import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) undo_manager.begin_transaction() p = element_factory.create(Element) assert undo_manager._current_transaction assert undo_manager._current_transaction._actions assert undo_manager.can_undo() undo_manager.commit_transaction() assert undo_manager.can_undo() assert element_factory.size() == 1, element_factory.size() with Transaction(event_manager): element_factory.create(Element) assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 2 undo_manager.undo_transaction() assert undo_manager.can_undo() assert 1 == len(undo_manager._undo_stack) assert 1 == len(undo_manager._redo_stack) assert undo_manager.can_redo() assert element_factory.size() == 1 undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert 0 == len(undo_manager._undo_stack) assert 2 == len(undo_manager._redo_stack) # assert element_factory.size() == 0 undo_manager.redo_transaction() assert 1 == len(undo_manager._undo_stack) assert 1 == len(undo_manager._redo_stack) assert undo_manager.can_undo() assert undo_manager.can_redo() assert element_factory.size() == 1 undo_manager.redo_transaction() assert undo_manager.can_undo() assert not undo_manager.can_redo() assert element_factory.size() == 2 assert p in element_factory.lselect() undo_manager.shutdown()
def component_registry(): component_registry = gaphor.services.componentregistry.ComponentRegistry() component_registry.register("element_factory", ElementFactory()) return component_registry