🆕 NEW IN 4.0
In this section a simple JSON document importer is implemented. The format of the JSON supported by this importer is just an example and not based on any existing format.
When the script is run through the File -> Import -> Import via Script -> Open Script… command, it will ask for a file to import and then a dialog allowing for two options: Create new document and whether Connections are predecessors or successors:
The format of this JSON file allows for a list of “nodes” that are interpreted as entities and “groups” which are interpreted as, well, groups. Only fields for titles, entity classes, edges (as “connections”) and children elements of groups are parsed. It is left to an excessive to the reader to add annotations, junctors, project management data, etc.
The example file generates the following diagram:
JSON Importer Code
# import_json_example.py# import the contents of a simple JSON file into Flying Logic# option for having connections be to predecessors or successors# creates entities, groups and edges, but not junctors# Copyright 2023 Flying Logicimport jsonfrom javax.swing import Box, BoxLayout, JLabel, JCheckBox, JComboBoximportItemLabel ="Import from JSON Example"# importDocument: required function for an importer# parameters# file: filename of the file to importdefimportDocument(file):# make a UI using Java masterBox =Box(BoxLayout.Y_AXIS)# new document? controlBox =Box(BoxLayout.X_AXIS) newDocCheckbox =JCheckBox("Create new document") controlBox.add(newDocCheckbox) controlBox.add(Box.createHorizontalGlue()) masterBox.add(controlBox)# connections meaning controlBox =Box(BoxLayout.X_AXIS) controlBox.add(JLabel("Connections are: ")) connectionsComboBox =JComboBox(["Predecessors", "Successors"]) controlBox.add(connectionsComboBox) masterBox.add(controlBox)# display dialog and collect optionsif0== Application.request("JSON Import Settings", masterBox, ("Cancel", "OK")):return# get options from user choices createNewDocument = newDocCheckbox.isSelected() connectionsAsSuccessors = (connectionsComboBox.selectedIndex ==1)# create a new doc if desiredif createNewDocument: theDoc = Application.newDocument()else: theDoc = document# open and parse JSON filewithopen(file, "r")as infile: json_object = infile.read() data = json.loads(json_object) entityDict ={} groupDict ={}# process "nodes" as entities# we will create all entties and then deal with connectionsif'nodes'in data:for node in data['nodes']:# with None parameter, no need to clear selection entity = theDoc.addEntityToTarget(None)[0] entity.title = node['title'] entity.entityClass = theDoc.getEntityClassByName(node['type']) entityDict[node['index']]= (entity, node['connections'])# process "groups" as groups# we will create all groups and then deal with childrenif'groups'in data:for node in data['groups']:# with None parameter, no need to clear selection group = theDoc.newGroup(None)[0] group.title = node['title'] groupDict[node['index']]= (group, node['children'])# process connectionsfor data in entityDict.values(): entity = data[0]for connection in data[1]: other = entityDict[connection][0]if connectionsAsSuccessors: theDoc.connect(entity, other)else: theDoc.connect(other, entity)# process children, which can be an entity or groupfor data in groupDict.values(): group = data[0]for index in data[1]:if index in entityDict: child = entityDict[index][0]else: child = groupDict[index][0] child.parent = group
Example DOT Exporter
The code below exports a document to a DOT (GraphViz) file, but with less features than the native export option in Flying Logic.
# export_dot.py# a simple DOT format exporter, less complete then the native version in Flying Logic# Copyright 2013 Arciem LLC# required variable that provided the label for the item in Flying Logic export menuexportMenuLabel ="Export Diagram to simple DOT format"# exportDocument: required function for an exporter# parameters# file: filename of the file to exportdefexportDocument(file):# open output file using Python file I/O fh =open(file, 'w') fh.write("digraph graphname {\n")for elem in document.all:if elem.isGroup or elem.isEdge:continue# use the element unique id’s (eid) to create unique id’s in DOTif elem.isEntity: fh.write("\tn"+str(elem.eid) +" [label=\""+ elem.title +"\"];\n")if elem.isJunctor: fh.write("\tn"+str(elem.eid) +" [label=\""+ elem.operator.abbreviation +"];\n")for outEdge in elem.outEdges: fh.write("\tn"+str(elem.eid) +" -> n"+str(outEdge.target.eid) +";\n") fh.write("\t}\n") fh.close()