Generating Python code from a diagram / UML / visual representation [closed]

老子叫甜甜 提交于 2021-02-08 10:20:26

问题


I have some Python classes that I pretend to manage with a visual tool of some sort. Each class has some methods that can perform operations (while also accepting arguments or returning values or other class instances).

Let's say that the classes are "Person", "Hat" and "Handwatch". Now, let's say that "Person" has the methods getName() and putOn(object).

From a code point of view, attaching a "handwatch" or a "hat" to a "person" (given the classes I just mentioned) would be as easy as:

person = Person()
if person.getName() == "Fred":
    handwatch = Handwatch()
    person.putOn(handwatch)
else:
    hat = Hat()
    person.putOn(hat)

But what if I wanted to be able to "draw" that sort of interaction between instances of my classes?

Imagine a visual UML-like tool that would let me "represent" code by dragging from a palette of "classes" and dropping "instances" of those classes, and that then would generate code that would behave exactly like that.

Are there any libraries that I could use in order to even begging implementing something like that? Or maybe even some literature on the subject?


回答1:


You cannot require to generate code from a diagram (a diagram is a normalized picture showing a part of the model), you are requiring a tool generating code from an activity (which can be shown in diagram(s)).

As a state machine an activity is a behavior and can represent the definition of an operation, contrarily to an interaction for instance (may be shown through a sequence diagram)

In your code it seems artificial to get the name of a newly created Person whose name is not given in parameter at the creation. Let say that instance is received through the input parameter person and the activity representing your code can be :

where the actions in green are call operation actions (there are named by the called operation to be clear), and the blue are create object actions (whose name indicates the class to be clear).

Also for the clarity of the diagram the pin 'name' has in fact the direction return etc

There is nothing against generating Python code from that activity, note also to help the definition of an action can be an opaque form where you can directly put Python code.

Do I know a modeler doing that ? no, sorry, even I imagined to do in BoUML through a plug-out in the same way I made a state machine generator (producing C++ code), but nobody asked for. So, just do it ?



来源:https://stackoverflow.com/questions/61899843/generating-python-code-from-a-diagram-uml-visual-representation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!