Suppose I have a kind of UML activity diagram with 1-n forks and joins. The assignment of the individual nodes relative to each other is defined by an XML relationship table via UUIDs where each sourceand target attribute's UUID represents such an individual node.

The following, highlighted example should clarify this:

Example The UML diagram flow starts at

source = "b1edbc32-a6f0-4a41-a836-0482d80db226" (connection node 1)

and ends with

target = "ef347ef9-3b53-45ab-918e-6f8b29a68dc0" (connection node 5)

In between you can see in the "flow" from one (or more) sources to one (or more) targets. The exemplary fork/join is colored in green/light blue.

I would now like to map this structure into a nested JSON array, e.g.

"nodes": [
    {
        "name": "initNode",
        "uid": "b1edbc32-a6f0-4a41-a836-0482d80db226",
        "nodes": [
            {
                "name": "followUpNode",
                "uid": "9ea88f3e-4b80-473d-a86f-0ff16b790727",
                "nodes": [

                 // ...   

                ]    
            }
        ]
    }
]

What's the best suited programming approach to achieve this with Qt5 C++ utilizing QDom... and QJson... stuff? I just need some thought-provoking impulses.

Some kind of looping....or recursion? While JSON as the data format for the output is mandatory, the shown JSON example layout is not. If there's a better approach than those nested nodes arrays (how to handle the multiple fork/join parallelism?!) then please let me know, too.

Any example is appreciated!

  • Are you sure that you have a class diagram and not an activity diagram ("forks", "joins", "flow") ? ANd what's your data structure that you want to serialize as a json ? – Christophe Nov 7 at 22:58
  • Yes, you're perfectly right. I've changed the original question. Each referenced UML element represents an automation activity with parameters each or a fork or join. – FlKo Nov 7 at 23:01
up vote 1 down vote accepted

Your XML file describes a directed graph. This graph is define by a set of unique nodes identified by an uid (and perhaps some more attributes), and a set of directed edges relating a source node to a target node.

It's not fully clear what you want to represent in your JSON:

  • Is it the full graph ? In this case, the nested structure could only represent a graph without cycles (i.e. a tree, according to the tree definition in the graph theory). As soon as you have a cycle:

    • the same node might be redefined several times as it appear in the nesting of several nodes;
    • this could only be avoided using the spanning tree algorithm to remove the cycles, but then you would have some edges missing.
  • Or is it some path across the graph ? In this case the nesting just expresses a succession of nodes traversed by the path, and the uid in your JSON would not document a definition of the nodes (to be defined elsewhere). The consequence is that:

  • in the root collection, you would have all the possible start node

  • the nested nodes only contain a single nested node (i.e. the next node to be traversed).

For the latter, all you need to do is to construct the graph in memory, and find the starting nodes (i.e. no incoming edge). Then for each starting node, you could consider a depth first graph traversal until you read a final node (i.e. no outgoing node):

  • It's an algorithm that is very easily implemented in a recursive fashion. Unfortunately, the recursive version doesn't make it easy to write from beginning to end every path found.
  • A more elaborate implementation is to use a queue on which you dequeue the first path, extend its last node using all outgoing edges, and then enqueue all the extended paths. This makes the production of the JSON easier since every path that reaches an end node can then be written to the JSON file
  • Your link to 'depth first graph traversal' was a great tip!! – FlKo Nov 8 at 10:14

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.