Exemple #1
0
 def handler():
     try:
         dist = pkg_resources.get_distribution("gaphor")
         path = os.path.join(dist.location, "test-diagrams/diagram-#4.gaphor")
         load(path, self.element_factory)
     finally:
         Gtk.main_quit()
Exemple #2
0
    def test_load_save(self):
        """Test loading and saving models"""

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "test-diagrams/simple-items.gaphor")

        with io.open(path, "r") as ifile:
            storage.load(ifile, factory=self.element_factory)

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with io.open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        with io.open("tmp.gaphor", "w") as ofile:

            ofile.write(copy)

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        self.assertEqual(copy, orig, "Saved model does not match copy")
Exemple #3
0
 def handler():
     try:
         path = distribution().locate_file(
             "test-diagrams/diagram-#4.gaphor")
         load(path, self.element_factory)
     finally:
         Gtk.main_quit()
Exemple #4
0
def element_factory(session):
    element_factory = session.get_service("element_factory")
    modeling_language = session.get_service("modeling_language")
    path = distribution().locate_file("test-models/issue_53.gaphor")
    load(path, element_factory, modeling_language)
    yield element_factory
    element_factory.shutdown()
Exemple #5
0
    def save_and_load(self, filename):
        factory = self.element_factory

        f = open(filename, 'r')
        storage.load(f, factory=self.element_factory)
        f.close()

        self.backup_service.backup()

        elements = list(map(factory.lookup, list(factory.keys())))

        orig = StringIO()
        storage.save(XMLWriter(orig), factory=self.element_factory)

        self.backup_service.restore()

        restored = list(map(factory.lookup, list(factory.keys())))

        assert len(elements) == len(restored)
        assert elements != restored

        copy = StringIO()
        storage.save(XMLWriter(copy), factory=self.element_factory)

        orig = orig.getvalue()
        copy = copy.getvalue()
        assert len(orig) == len(copy)
Exemple #6
0
 def load_old_model():
     with open(path, "r") as ifile:
         storage.load(
             ifile,
             factory=self.element_factory,
             modeling_language=self.modeling_language,
         )
Exemple #7
0
def element_factory(session):
    element_factory = session.get_service("element_factory")
    dist = importlib_metadata.distribution("gaphor")
    path = dist.locate_file("test-diagrams/issue_53.gaphor")
    load(path, element_factory)
    yield element_factory
    element_factory.shutdown()
Exemple #8
0
    def test_load_and_save_of_a_model(self):
        path = distribution().locate_file("test-models/simple-items.gaphor")

        with open(path, "r") as ifile:
            storage.load(
                ifile,
                factory=self.element_factory,
                modeling_language=self.modeling_language,
            )

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        assert copy == orig, "Saved model does not match copy"
Exemple #9
0
    def test_load_save(self):

        """Test loading and saving models"""

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "test-diagrams/simple-items.gaphor")

        with io.open(path, "r") as ifile:
            storage.load(ifile, factory=self.element_factory)

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with io.open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        with io.open("tmp.gaphor", "w") as ofile:

            ofile.write(copy)

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        self.assertEqual(copy, orig, "Saved model does not match copy")
Exemple #10
0
    def test_load_save(self):

        """Test loading and saving models"""

        dist = importlib_metadata.distribution("gaphor")
        path = dist.locate_file("test-diagrams/simple-items.gaphor")

        with open(path, "r") as ifile:
            storage.load(ifile, factory=self.element_factory)

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        with open("tmp.gaphor", "w") as ofile:

            ofile.write(copy)

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        assert copy == orig, "Saved model does not match copy"
Exemple #11
0
    def save_and_load(self, filename):
        factory = self.element_factory

        f = open(filename, 'r')
        storage.load(f, factory=self.element_factory)
        f.close()
        
        self.backup_service.backup()
        
        elements = map(factory.lookup, factory.keys())

        orig = StringIO()
        storage.save(XMLWriter(orig), factory=self.element_factory)

        self.backup_service.restore()

        restored = map(factory.lookup, factory.keys())

        assert len(elements) == len(restored)
        assert elements != restored

        copy = StringIO()
        storage.save(XMLWriter(copy), factory=self.element_factory)

        orig = orig.getvalue()
        copy = copy.getvalue()
        assert len(orig) == len(copy)
Exemple #12
0
 def handler():
     try:
         path = distribution().locate_file(
             "test-models/diagram-#4.gaphor")
         load(path, self.element_factory, self.modeling_language)
     finally:
         Gtk.main_quit()
Exemple #13
0
 def handler():
     try:
         dist = pkg_resources.get_distribution('gaphor')
         path = os.path.join(dist.location, 'test-diagrams/diagram-#4.gaphor')
         load(path, self.element_factory)
     finally:
         gtk.main_quit()
Exemple #14
0
 def handler():
     try:
         dist = importlib_metadata.distribution("gaphor")
         path = dist.locate_file("test-diagrams/diagram-#4.gaphor")
         load(path, self.element_factory)
     finally:
         Gtk.main_quit()
Exemple #15
0
    def test_load_save(self):
        
        """Test loading and saving models"""
        
        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location, 'test-diagrams/simple-items.gaphor')
        
        with open(path, 'r') as ifile:
            
            storage.load(ifile, factory=self.element_factory)
            
        pf = PseudoFile()
        
        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, 'r') as ifile:
            
            orig = ifile.read()
            
        copy = pf.data

        with open('tmp.gaphor', 'w') as ofile:
            
            ofile.write(copy)
            
        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub('%VER%', orig)
        copy = expr.sub('%VER%', copy)

        self.assertEquals(copy, orig, 'Saved model does not match copy')
Exemple #16
0
    def test_load_save(self):
        """Test loading and saving models"""

        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location, 'test-diagrams/simple-items.gaphor')

        with open(path, 'r') as ifile:
            storage.load(ifile, factory=self.element_factory)

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, 'r') as ifile:
            orig = ifile.read()

        copy = pf.data

        with open('tmp.gaphor', 'w') as ofile:
            ofile.write(copy)

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub('%VER%', orig)
        copy = expr.sub('%VER%', copy)

        self.assertEquals(copy, orig, 'Saved model does not match copy')
Exemple #17
0
    def test_tagged_values_upgrade(self):
        """Test tagged values upgrade in Gaphor 0.15.0
        """
        f = open('test-diagrams/taggedvalues-pre015.gaphor')
        storage.load(f, factory=self.element_factory)
        f.close()

        diagrams = list(self.kindof(UML.Diagram))
        self.assertEquals(1, len(diagrams))
        diagram = diagrams[0]
        classes = diagram.canvas.select(lambda e: isinstance(e, items.ClassItem))
        profiles = self.element_factory.lselect(lambda e: isinstance(e, UML.Profile))
        stereotypes = self.element_factory.lselect(lambda e: isinstance(e, UML.Stereotype))

        self.assertEquals(2, len(classes))
        c1, c2 = classes

        self.assertEquals(1, len(profiles))
        profile = profiles[0]
        self.assertEquals('version 0.15 conversion', profile.name)

        self.assertEquals(1, len(stereotypes))
        stereotype = stereotypes[0]
        self.assertEquals('Tagged', stereotype.name)
        self.assertEquals(profile, stereotype.namespace)
        self.assertEquals('c1', c1.subject.name)
        self.assertEquals('c2', c2.subject.name)
        self.assertEquals(stereotype, c1.subject.appliedStereotype[0].classifier[0])
        self.assertEquals(stereotype, c2.subject.appliedStereotype[0].classifier[0])
        self.assertEquals('t1', c1.subject.appliedStereotype[0].slot[0].definingFeature.name)
        self.assertEquals('t2', c1.subject.appliedStereotype[0].slot[1].definingFeature.name)
        self.assertEquals('t5', c2.subject.appliedStereotype[0].slot[0].definingFeature.name)
        self.assertEquals('t6', c2.subject.appliedStereotype[0].slot[1].definingFeature.name)
        self.assertEquals('t7', c2.subject.appliedStereotype[0].slot[2].definingFeature.name)
Exemple #18
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        load("test-diagrams/diagram-#4.gaphor", self.element_factory)
Exemple #19
0
    def setUp(self):
        Application.init()
        element_factory = Application.get_service('element_factory')
        from gaphor.storage.storage import load

        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location, 'tests/issue_53.gaphor')
        load(path, element_factory)
Exemple #20
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        load('test-diagrams/diagram-#4.gaphor', self.element_factory)
Exemple #21
0
    def setUp(self):
        Application.init()
        element_factory = Application.get_service("element_factory")
        from gaphor.storage.storage import load

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "test-diagrams/issue_53.gaphor")
        load(path, element_factory)
Exemple #22
0
    def setUp(self):
        Application.init()
        element_factory = Application.get_service("element_factory")
        from gaphor.storage.storage import load

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "test-diagrams/issue_53.gaphor")
        load(path, element_factory)
    def test_export(self):
        load("tests/cr2-superstate.gaphor", self.element_factory)
        zaakspec = self.get_service("crea-zaakspecificatie-export")

        pf = PseudoFile()
        zaakspec.export(pf)
        expected = ""
        assert pf.data == EXPECTED, '"""%s"""' % pf.data
Exemple #24
0
    def test_load_uml_metamodel(self):
        path = distribution().locate_file("models/UML.gaphor")

        with open(path) as ifile:
            storage.load(
                ifile,
                factory=self.element_factory,
                modeling_language=self.modeling_language,
            )
Exemple #25
0
    def test_load_uml_metamodel(self):
        """
        Test if the meta model can be loaded.
        """

        path = distribution().locate_file("gaphor/UML/uml2.gaphor")

        with open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)
Exemple #26
0
 def test_load_uml_metamodel(self):
     """
     Test if the meta model can be loaded.
     """
     try:
         f = open('gaphor/UML/uml2.gaphor')
         storage.load(f, factory=self.element_factory)
     finally:
         f.close()
Exemple #27
0
def element_factory():
    Application.init()
    element_factory = Application.get_service("element_factory")
    dist = importlib_metadata.distribution("gaphor")
    path = dist.locate_file("test-diagrams/issue_53.gaphor")
    load(path, element_factory)
    yield element_factory
    Application.get_service("element_factory").shutdown()
    Application.shutdown()
Exemple #28
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        path = distribution().locate_file("test-diagrams/diagram-#4.gaphor")
        load(path, self.element_factory)
Exemple #29
0
    def test_association_upgrade(self):
        """Test association navigability upgrade in Gaphor 0.15.0
        """

        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location,
                            'test-diagrams/associations-pre015.gaphor')

        with open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)

        diagrams = list(self.kindof(uml2.Diagram))
        self.assertEquals(1, len(diagrams))
        diagram = diagrams[0]
        assocs = diagram.canvas.select(
            lambda e: isinstance(e, items.AssociationItem))
        assert len(assocs) == 8
        a1, a2 = [a for a in assocs if a.subject.name == 'nav']

        self.assertTrue(a1.head_end.subject.navigability)
        self.assertTrue(a1.tail_end.subject.navigability)
        self.assertTrue(a2.head_end.subject.navigability)
        self.assertTrue(a2.tail_end.subject.navigability)

        self.assertTrue(len(a1.head_end.subject.type.ownedAttribute) == 1)
        self.assertTrue(len(a1.tail_end.subject.type.ownedAttribute) ==
                        2)  # association end and an attribute

        # use cases and actors - no owned attributes as navigability is realized
        # by association's navigable owned ends
        self.assertTrue(len(a2.head_end.subject.type.ownedAttribute) == 0)
        self.assertTrue(len(a2.tail_end.subject.type.ownedAttribute) == 0)

        a1, a2 = [a for a in assocs if a.subject.name == 'nonnav']
        self.assertTrue(a1.head_end.subject.navigability is False)
        self.assertTrue(a1.tail_end.subject.navigability is False)
        self.assertTrue(a2.head_end.subject.navigability is False)
        self.assertTrue(a2.tail_end.subject.navigability is False)

        a1, a2 = [a for a in assocs if a.subject.name == 'unk']
        self.assertTrue(a1.head_end.subject.navigability is None)
        self.assertTrue(a1.tail_end.subject.navigability is None)
        self.assertTrue(a2.head_end.subject.navigability is None)
        self.assertTrue(a2.tail_end.subject.navigability is None)

        a, = [a for a in assocs if a.subject.name == 'sided']
        assert a.head_end.subject.name == 'cs'
        assert a.tail_end.subject.name == 'int'
        self.assertTrue(a.head_end.subject.navigability is False)
        self.assertTrue(a.tail_end.subject.navigability is True)

        a, = [a for a in assocs if a.subject.name == 'sided2']
        assert a.head_end.subject.name == 'cs'
        assert a.tail_end.subject.name == 'int'
        self.assertTrue(a.head_end.subject.navigability is None)
        self.assertTrue(a.tail_end.subject.navigability is True)
Exemple #30
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "test-diagrams/diagram-#4.gaphor")
        load(path, self.element_factory)
Exemple #31
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location, 'test-diagrams/diagram-#4.gaphor')
        load(path, self.element_factory)
Exemple #32
0
    def load(data):
        """Load data from specified string."""
        element_factory.flush()
        assert not list(element_factory.select())

        f = StringIO(data)
        storage.load(f,
                     factory=element_factory,
                     modeling_language=modeling_language)
        f.close()
Exemple #33
0
    def test_load_uml_metamodel(self):
        """
        Test if the meta model can be loaded.
        """

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "gaphor/UML/uml2.gaphor")

        with io.open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)
Exemple #34
0
    def test_load_uml_metamodel(self):
        """
        Test if the meta model can be loaded.
        """

        dist = pkg_resources.get_distribution("gaphor")
        path = os.path.join(dist.location, "gaphor/UML/uml2.gaphor")

        with io.open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)
Exemple #35
0
    def test_association_upgrade(self):
        """Test association navigability upgrade in Gaphor 0.15.0
        """
        
        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location, 'test-diagrams/associations-pre015.gaphor')
        
        with open(path) as ifile:
            
            storage.load(ifile, factory=self.element_factory)

        diagrams = list(self.kindof(UML.Diagram))
        self.assertEquals(1, len(diagrams))
        diagram = diagrams[0]
        assocs = diagram.canvas.select(lambda e: isinstance(e, items.AssociationItem))
        assert len(assocs) == 8
        a1, a2 = [a for a in assocs if a.subject.name == 'nav']

        self.assertTrue(a1.head_end.subject.navigability)
        self.assertTrue(a1.tail_end.subject.navigability)
        self.assertTrue(a2.head_end.subject.navigability)
        self.assertTrue(a2.tail_end.subject.navigability)

        self.assertTrue(len(a1.head_end.subject.type.ownedAttribute) == 1)
        self.assertTrue(len(a1.tail_end.subject.type.ownedAttribute) == 2) # association end and an attribute

        # use cases and actors - no owned attributes as navigability is realized
        # by association's navigable owned ends
        self.assertTrue(len(a2.head_end.subject.type.ownedAttribute) == 0)
        self.assertTrue(len(a2.tail_end.subject.type.ownedAttribute) == 0)

        a1, a2 = [a for a in assocs if a.subject.name == 'nonnav']
        self.assertTrue(a1.head_end.subject.navigability is False)
        self.assertTrue(a1.tail_end.subject.navigability is False)
        self.assertTrue(a2.head_end.subject.navigability is False)
        self.assertTrue(a2.tail_end.subject.navigability is False)


        a1, a2 = [a for a in assocs if a.subject.name == 'unk']
        self.assertTrue(a1.head_end.subject.navigability is None)
        self.assertTrue(a1.tail_end.subject.navigability is None)
        self.assertTrue(a2.head_end.subject.navigability is None)
        self.assertTrue(a2.tail_end.subject.navigability is None)

        a, = [a for a in assocs if a.subject.name == 'sided']
        assert a.head_end.subject.name == 'cs'
        assert a.tail_end.subject.name == 'int'
        self.assertTrue(a.head_end.subject.navigability is False)
        self.assertTrue(a.tail_end.subject.navigability is True)

        a, = [a for a in assocs if a.subject.name == 'sided2']
        assert a.head_end.subject.name == 'cs'
        assert a.tail_end.subject.name == 'int'
        self.assertTrue(a.head_end.subject.navigability is None)
        self.assertTrue(a.tail_end.subject.navigability is True)
Exemple #36
0
 def load(self, data):
     """
     Load data from specified string. Update ``TestCase.diagram``
     attribute to hold new loaded diagram.
     """
     from gaphor.storage import storage
     f = StringIO(data)
     storage.load(f, factory=self.element_factory)
     f.close()
     
     self.diagram = self.element_factory.lselect(lambda e: e.isKindOf(UML.Diagram))[0]
Exemple #37
0
    def load(self, data):
        """
        Load data from specified string. Update ``TestCase.diagram``
        attribute to hold new loaded diagram.
        """
        from gaphor.storage import storage
        f = StringIO(data)
        storage.load(f, factory=self.element_factory)
        f.close()

        self.diagram = self.element_factory.lselect(lambda e: e.isKindOf(uml2.Diagram))[0]
Exemple #38
0
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()
Exemple #39
0
    def test_connection(self):
        """
        Test connection loading of an association and two classes.
        (Should count for all line-like objects alike if this works).
        """
        c1 = self.create(items.ClassItem, UML.Class)
        c2 = self.create(items.ClassItem, UML.Class)
        c2.matrix.translate(200, 200)
        self.diagram.canvas.update_matrix(c2)
        assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1,
                                                                 200, 200)

        a = self.create(items.AssociationItem)

        self.connect(a, a.head, c1)
        head_pos = a.head.pos

        self.connect(a, a.tail, c2)
        tail_pos = a.tail.pos

        self.diagram.canvas.update_now()

        assert a.head.pos.y == 0, a.head.pos
        assert a.tail.pos.x == 10, a.tail.pos
        # assert a.tail.y == 200, a.tail.pos
        assert a.subject

        fd = StringIO()
        storage.save(XMLWriter(fd), factory=self.element_factory)
        data = fd.getvalue()
        fd.close()

        old_a_subject_id = a.subject.id

        self.element_factory.flush()
        assert not list(self.element_factory.select())
        fd = StringIO(data)
        storage.load(fd, factory=self.element_factory)
        fd.close()

        diagrams = list(self.kindof(UML.Diagram))
        self.assertEqual(1, len(diagrams))
        d = diagrams[0]
        a = d.canvas.select(lambda e: isinstance(e, items.AssociationItem))[0]
        self.assertTrue(a.subject is not None)
        self.assertEqual(old_a_subject_id, a.subject.id)
        cinfo_head = a.canvas.get_connection(a.head)
        self.assertTrue(cinfo_head.connected is not None)
        cinfo_tail = a.canvas.get_connection(a.tail)
        self.assertTrue(cinfo_tail.connected is not None)
        self.assertTrue(not cinfo_head.connected is cinfo_tail.connected)
Exemple #40
0
    def load(self, data):
        """
        Load data from specified string. Update ``TestCase.diagram``
        attribute to hold new loaded diagram.
        """
        from gaphor.storage import storage

        f = StringIO(data)
        storage.load(f,
                     factory=self.element_factory,
                     modeling_language=self.modeling_language)
        f.close()

        self.diagram = self.element_factory.lselect(UML.Diagram)[0]
Exemple #41
0
    def test_connection(self):
        """
        Test connection loading of an association and two classes.
        (Should count for all line-like objects alike if this works).
        """
        c1 = self.create(items.ClassItem, UML.Class)
        c2 = self.create(items.ClassItem, UML.Class)
        c2.matrix.translate(200, 200)
        self.diagram.canvas.update_matrix(c2)
        assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1, 200, 200)

        a = self.create(items.AssociationItem)

        self.connect(a, a.head, c1)
        head_pos = a.head.pos

        self.connect(a, a.tail, c2)
        tail_pos = a.tail.pos

        self.diagram.canvas.update_now()

        assert a.head.pos.y == 0, a.head.pos
        assert a.tail.pos.x == 10, a.tail.pos
        #assert a.tail.y == 200, a.tail.pos
        assert a.subject

        fd = StringIO()
        storage.save(XMLWriter(fd), factory=self.element_factory)
        data = fd.getvalue()
        fd.close()

        old_a_subject_id = a.subject.id

        self.element_factory.flush()
        assert not list(self.element_factory.select())
        fd = StringIO(data)
        storage.load(fd, factory=self.element_factory)
        fd.close()

        diagrams = list(self.kindof(UML.Diagram))
        self.assertEquals(1, len(diagrams))
        d = diagrams[0]
        a = d.canvas.select(lambda e: isinstance(e, items.AssociationItem))[0]
        self.assertTrue(a.subject is not None)
        self.assertEquals(old_a_subject_id, a.subject.id)
        cinfo_head = a.canvas.get_connection(a.head)
        self.assertTrue(cinfo_head.connected is not None)
        cinfo_tail = a.canvas.get_connection(a.tail)
        self.assertTrue(cinfo_tail.connected is not None)
        self.assertTrue(not cinfo_head.connected is cinfo_tail.connected)
Exemple #42
0
    def test_tagged_values_upgrade(self):
        """Test tagged values upgrade in Gaphor 0.15.0
        """

        dist = pkg_resources.get_distribution('gaphor')
        path = os.path.join(dist.location,
                            'test-diagrams/taggedvalues-pre015.gaphor')

        with open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)

        diagrams = list(self.kindof(uml2.Diagram))
        self.assertEquals(1, len(diagrams))
        diagram = diagrams[0]
        classes = diagram.canvas.select(
            lambda e: isinstance(e, items.ClassItem))
        profiles = self.element_factory.lselect(
            lambda e: isinstance(e, uml2.Profile))
        stereotypes = self.element_factory.lselect(
            lambda e: isinstance(e, uml2.Stereotype))

        self.assertEquals(2, len(classes))
        c1, c2 = classes

        self.assertEquals(1, len(profiles))
        profile = profiles[0]
        self.assertEquals('version 0.15 conversion', profile.name)

        self.assertEquals(1, len(stereotypes))
        stereotype = stereotypes[0]
        self.assertEquals('Tagged', stereotype.name)
        self.assertEquals(profile, stereotype.namespace)
        self.assertEquals('c1', c1.subject.name)
        self.assertEquals('c2', c2.subject.name)
        self.assertEquals(stereotype,
                          c1.subject.appliedStereotype[0].classifier[0])
        self.assertEquals(stereotype,
                          c2.subject.appliedStereotype[0].classifier[0])
        self.assertEquals(
            't1', c1.subject.appliedStereotype[0].slot[0].definingFeature.name)
        self.assertEquals(
            't2', c1.subject.appliedStereotype[0].slot[1].definingFeature.name)
        self.assertEquals(
            't5', c2.subject.appliedStereotype[0].slot[0].definingFeature.name)
        self.assertEquals(
            't6', c2.subject.appliedStereotype[0].slot[1].definingFeature.name)
        self.assertEquals(
            't7', c2.subject.appliedStereotype[0].slot[2].definingFeature.name)
Exemple #43
0
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()
Exemple #44
0
    def test_save_and_load_of_association_with_two_connected_classes(
            self, case):
        c1 = case.create(ClassItem, UML.Class)
        c2 = case.create(ClassItem, UML.Class)
        c2.matrix.translate(200, 200)
        case.diagram.request_update(c2)
        case.diagram.update_now((c1, c2))
        assert tuple(c2.matrix_i2c) == (1, 0, 0, 1, 200, 200)

        a = case.create(AssociationItem)

        case.connect(a, a.head, c1)
        case.connect(a, a.tail, c2)

        case.diagram.update_now((c1, c2, a))

        assert a.head.pos.y == 0, a.head.pos
        assert a.tail.pos.x == 200, a.tail.pos
        assert a.tail.pos.y == 200, a.tail.pos
        assert a.subject

        fd = StringIO()
        storage.save(XMLWriter(fd), factory=case.element_factory)
        data = fd.getvalue()
        fd.close()

        old_a_subject_id = a.subject.id

        case.element_factory.flush()
        assert not list(case.element_factory.select())
        fd = StringIO(data)
        storage.load(fd,
                     factory=case.element_factory,
                     modeling_language=case.modeling_language)
        fd.close()

        diagrams = list(case.kindof(UML.Diagram))
        assert len(diagrams) == 1
        d = diagrams[0]
        a = next(d.select(lambda e: isinstance(e, AssociationItem)))
        assert a.subject is not None
        assert old_a_subject_id == a.subject.id
        cinfo_head = a.diagram.connections.get_connection(a.head)
        assert cinfo_head.connected is not None
        cinfo_tail = a.diagram.connections.get_connection(a.tail)
        assert cinfo_tail.connected is not None
        assert cinfo_head.connected is not cinfo_tail.connected
Exemple #45
0
    def test_load_save(self):
        f = open('test-diagrams/simple-items.gaphor', 'r')
        storage.load(f, factory=self.element_factory)
        f.close()

        pf = PseudoFile()
        storage.save(XMLWriter(pf), factory=self.element_factory)

        f = open('test-diagrams/simple-items.gaphor', 'r')
        orig = f.read()
        f.close()

        copy = pf.data

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub('%VER%', orig)
        copy = expr.sub('%VER%', copy)

        f = open('tmp.gaphor', 'w')
        f.write(copy)
        f.close()


        self.assertEquals(copy, orig)
Exemple #46
0
if not args:
    parser.print_help()
    # sys.exit(1)


factory = UML.ElementFactory()


name_re = None
if options.regex:
    name_re = re.compile(options.regex, re.I)

# we should have some gaphor files to be processed at this point
for model in args:
    message("loading model %s" % model)
    storage.load(model, factory)
    message("\nready for rendering\n")

    for diagram in factory.select(lambda e: e.isKindOf(UML.Diagram)):
        odir = pkg2dir(diagram.package)

        # just diagram name
        dname = diagram.name
        # full diagram name including package path
        pname = "%s/%s" % (odir, dname)

        if options.underscores:
            odir = odir.replace(" ", "_")
            dname = dname.replace(" ", "_")

        if name_re and not name_re.search(pname):
Exemple #47
0
 def load_old_model():
     with io.open(path, "r") as ifile:
         storage.load(ifile, factory=self.element_factory)
Exemple #48
0
    def test_lifeline_messages_upgrade(self):
        """Test message occurrence specification upgrade in Gaphor 0.15.0
        """
        f = open('test-diagrams/lifelines-pre015.gaphor')
        storage.load(f, factory=self.element_factory)
        f.close()

        diagrams = list(self.kindof(UML.Diagram))
        self.assertEquals(1, len(diagrams))
        diagram = diagrams[0]

        lifelines = diagram.canvas.select(lambda e: isinstance(e, items.LifelineItem))
        occurrences = self.kindof(UML.MessageOccurrenceSpecification)
        messages = self.kindof(UML.Message)

        self.assertEquals(2, len(lifelines))
        self.assertEquals(12, len(messages))
        # 2 * 12 but there are 4 lost/found messages
        self.assertEquals(20, len(set(occurrences)))

        l1, l2 = lifelines
        if l1.subject.name == 'a2':
            l1, l2 = l2, l1
        def find(name):
            return (m for m in messages if m.name == name).next()
        m1 = find('call()')
        m2 = find('callx()')
        m3 = find('cally()')
        # inverted messages
        m4 = find('calla()')
        m5 = find('callb()')

        self.assertTrue(m1.sendEvent.covered is l1.subject)
        self.assertTrue(m2.sendEvent.covered is l1.subject)
        self.assertTrue(m3.sendEvent.covered is l1.subject)

        self.assertTrue(m1.receiveEvent.covered is l2.subject)
        self.assertTrue(m2.receiveEvent.covered is l2.subject)
        self.assertTrue(m3.receiveEvent.covered is l2.subject)

        # test inverted messages
        self.assertTrue(m4.sendEvent.covered is l2.subject)
        self.assertTrue(m5.sendEvent.covered is l2.subject)

        self.assertTrue(m4.receiveEvent.covered is l1.subject)
        self.assertTrue(m5.receiveEvent.covered is l1.subject)

        m = find('simple()')
        self.assertTrue(m.sendEvent.covered is l1.subject)
        self.assertTrue(m.receiveEvent.covered is l2.subject)

        m = find('found1()')
        self.assertTrue(m.sendEvent is None)
        self.assertTrue(m.receiveEvent.covered is l1.subject)

        m = find('found2()')
        self.assertTrue(m.sendEvent is None)
        self.assertTrue(m.receiveEvent.covered is l1.subject)

        m = find('rfound1()')
        self.assertTrue(m.sendEvent.covered is l1.subject)
        self.assertTrue(m.receiveEvent is None)

        m = find('lost1()')
        self.assertTrue(m.sendEvent.covered is l1.subject)
        self.assertTrue(m.receiveEvent is None)

        m = find('lost2()')
        self.assertTrue(m.sendEvent.covered is l1.subject)
        self.assertTrue(m.receiveEvent is None)

        m = find('rlost1()')
        self.assertTrue(m.sendEvent is None)
        self.assertTrue(m.receiveEvent.covered is l1.subject)
Exemple #49
0
    def test_it(self):
        """
        Test an issue when loading a freshly created action diagram.
        """
        ef = self.element_factory
        storage.load('test-diagrams/action-issue.gaphor', ef)

        actions = ef.lselect(lambda e: e.isKindOf(UML.Action))
        flows = ef.lselect(lambda e: e.isKindOf(UML.ControlFlow))
        self.assertEqual(3, len(actions))
        self.assertEqual(3, len(flows))

        # Actions live in partitions:
        partitions = ef.lselect(lambda e: e.isKindOf(UML.ActivityPartition))
        self.assertEquals(2, len(partitions))

        # Okay, so far the data model is saved correctly. Now, how do the
        # handles behave?
        diagrams = ef.lselect(lambda e: e.isKindOf(UML.Diagram))
        self.assertEquals(1, len(diagrams))
        
        canvas = diagrams[0].canvas
        assert 9 == len(canvas.get_all_items())
        # Part, Part, Act, Act, Part, Act, Flow, Flow, Flow

        for e in actions + flows:
            self.assertEquals(1, len(e.presentation), e)
        for i in canvas.select(lambda e: isinstance(e, (FlowItem, ActionItem))):
            self.assertTrue(i.subject, i)

        # Loaded as:
        # 
        # actions[1] --> flows[0, 1]
        # flows[0, 2] --> actions[0]
        # flows[1] --> actions[2] --> flows[2]

        # start element:
        self.assertSame(actions[1].outgoing[0], flows[0])
        self.assertSame(actions[1].outgoing[1], flows[1])
        self.assertFalse(actions[1].incoming)

        cinfo, = canvas.get_connections(handle=flows[0].presentation[0].head)
        self.assertSame(cinfo.connected, actions[1].presentation[0])
        cinfo, = canvas.get_connections(handle=flows[1].presentation[0].head)
        self.assertSame(cinfo.connected, actions[1].presentation[0])

        # Intermediate element:
        self.assertSame(actions[2].incoming[0], flows[1])
        self.assertSame(actions[2].outgoing[0], flows[2])
        
        cinfo, = canvas.get_connections(handle=flows[1].presentation[0].tail)
        self.assertSame(cinfo.connected, actions[2].presentation[0])
        cinfo, = canvas.get_connections(handle=flows[2].presentation[0].head)
        self.assertSame(cinfo.connected, actions[2].presentation[0])

        # Final element:
        self.assertSame(actions[0].incoming[0], flows[0])
        self.assertSame(actions[0].incoming[1], flows[2])

        cinfo, = canvas.get_connections(handle=flows[0].presentation[0].tail)
        self.assertSame(cinfo.connected, actions[0].presentation[0])
        cinfo, = canvas.get_connections(handle=flows[2].presentation[0].tail)
        self.assertSame(cinfo.connected, actions[0].presentation[0])
        
        # Test the parent-child connectivity
        for a in actions:
            p, = a.inPartition
            self.assertTrue(p)
            self.assertTrue(canvas.get_parent(a.presentation[0]))
            self.assertSame(canvas.get_parent(a.presentation[0]), p.presentation[0])
Exemple #50
0
 def setUp(self):
     Application.init()
     element_factory = Application.get_service('element_factory')
     from gaphor.storage.storage import load
     load('tests/issue_53.gaphor', element_factory)
Exemple #51
0
 def handler():
     try:
         load("test-diagrams/diagram-#4.gaphor", self.element_factory)
     finally:
         gtk.main_quit()