Created
October 24, 2018 06:02
-
-
Save ptsefton/38882a78faa265ccbb9a3f47837c2ef9 to your computer and use it in GitHub Desktop.
Quick hack to generate plantuml from a DataCrate file showing how instruments and agents relate to create agents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import os | |
def as_array(val): | |
if not isinstance(val, list): | |
return [val] | |
else: | |
return val | |
parser = argparse.ArgumentParser() | |
parser.add_argument('infile', help='Catalog file to load') | |
args = vars(parser.parse_args()) | |
root_dir, _ = os.path.split(args['infile']) | |
with open(args["infile"], 'r') as cat: | |
dc = json.load(cat) | |
id_lookup = {} | |
for item in dc["@graph"]: | |
id_lookup[item["@id"]] = item | |
uml = "@startuml\n" | |
agents = {} | |
for item in [col for col in dc["@graph"] if "CreateAction" in as_array(col["@type"])]: | |
if "agent" in item: | |
for ag in as_array(item["agent"]): | |
if "@id" in ag: | |
if not(ag["@id"] in agents): | |
agents[ag["@id"]] = True | |
uml += '"%s" as %s\n' % (id_lookup[ag["@id"]]["name"], id_lookup[ag["@id"]]["name"].replace(" ", "_")) | |
uml += "[CreateAction:\\n %s] -up-> %s : agent" % (item["@id"], id_lookup[ag["@id"]]["name"].replace(" ", "_")) | |
uml += "\n" | |
if "result" in item: | |
for ag in as_array(item["result"]): | |
if "@id" in ag: | |
uml += "[CreateAction:\\n %s] -down-> [%s:\\n%s] : result" % (item["@id"], as_array(id_lookup[ag["@id"]]["@type"])[0], id_lookup[ag["@id"]]["name"]) | |
uml += "\n" | |
if "object" in item: | |
for ag in as_array(item["object"]): | |
if "@id" in ag: | |
uml += "[CreateAction:\\n %s] -> [%s:\\n%s] : object" % (item["@id"], as_array(id_lookup[ag["@id"]]["@type"])[0], id_lookup[ag["@id"]]["name"]) | |
uml += "\n" | |
if "instrument" in item: | |
for ag in as_array(item["instrument"]): | |
if "@id" in ag: | |
uml += "[CreateAction:\\n %s] --down--> [%s:\\n%s] : instrument" % (item["@id"], as_array(id_lookup[ag["@id"]]["@type"])[0], id_lookup[ag["@id"]]["name"]) | |
uml += "\n" | |
uml += "@enduml" | |
print(uml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment