Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/tools/rdfs2dot.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 """ | |
2 A commandline tool for drawing RDFS Class diagrams in Graphviz DOT | |
3 format | |
4 | |
5 You can draw the graph of an RDFS file directly: | |
6 | |
7 .. code-block: bash | |
8 | |
9 rdf2dot my_rdfs_file.rdf | dot -Tpng | display | |
10 """ | |
11 | |
12 import rdflib.extras.cmdlineutils | |
13 | |
14 import sys | |
15 import itertools | |
16 import collections | |
17 | |
18 | |
19 from rdflib import XSD, RDF, RDFS | |
20 | |
21 | |
22 XSDTERMS = [XSD[x] for x in ( | |
23 "anyURI", "base64Binary", "boolean", "byte", "date", "dateTime", "decimal", | |
24 "double", "duration", "float", "gDay", "gMonth", "gMonthDay", "gYear", | |
25 "gYearMonth", "hexBinary", "ID", "IDREF", "IDREFS", "int", "integer", | |
26 "language", "long", "Name", "NCName", "negativeInteger", "NMTOKEN", | |
27 "NMTOKENS", "nonNegativeInteger", "nonPositiveInteger", "normalizedString", | |
28 "positiveInteger", "QName", "short", "string", "time", "token", | |
29 "unsignedByte", "unsignedInt", "unsignedLong", "unsignedShort")] | |
30 | |
31 EDGECOLOR = "blue" | |
32 NODECOLOR = "black" | |
33 ISACOLOR = "black" | |
34 | |
35 | |
36 def rdfs2dot(g, stream, opts={}): | |
37 """ | |
38 Convert the RDFS schema in a graph | |
39 writes the dot output to the stream | |
40 """ | |
41 | |
42 fields = collections.defaultdict(set) | |
43 nodes = {} | |
44 | |
45 def node(x): | |
46 | |
47 if x not in nodes: | |
48 nodes[x] = "node%d" % len(nodes) | |
49 return nodes[x] | |
50 | |
51 def label(x, g): | |
52 | |
53 l = g.value(x, RDFS.label) | |
54 if l is None: | |
55 try: | |
56 l = g.namespace_manager.compute_qname(x)[2] | |
57 except: | |
58 pass # bnodes and some weird URIs cannot be split | |
59 return l | |
60 | |
61 stream.write("digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n") | |
62 | |
63 for x in g.subjects(RDF.type, RDFS.Class): | |
64 n = node(x) | |
65 | |
66 for x, y in g.subject_objects(RDFS.subClassOf): | |
67 x = node(x) | |
68 y = node(y) | |
69 stream.write("\t%s -> %s [ color=%s ] ;\n" % (y, x, ISACOLOR)) | |
70 | |
71 for x in g.subjects(RDF.type, RDF.Property): | |
72 for a, b in itertools.product( | |
73 g.objects(x, RDFS.domain), g.objects(x, RDFS.range)): | |
74 if b in XSDTERMS or b == RDFS.Literal: | |
75 l = label(b, g) | |
76 if b == RDFS.Literal: | |
77 l = "literal" | |
78 fields[node(a)].add((label(x, g), l)) | |
79 else: | |
80 # if a in nodes and b in nodes: | |
81 stream.write( | |
82 "\t%s -> %s [ color=%s, label=\"%s\" ];\n" % ( | |
83 node(a), node(b), EDGECOLOR, label(x, g))) | |
84 | |
85 for u, n in list(nodes.items()): | |
86 stream.write("# %s %s\n" % (u, n)) | |
87 f = ["<tr><td align='left'>%s</td><td>%s</td></tr>" % | |
88 x for x in sorted(fields[n])] | |
89 opstr = "%s [ shape=none, color=%s label=< <table color='#666666'" + \ | |
90 " cellborder=\"0\" cellspacing='0' border=\"1\"><tr>" + \ | |
91 "<td colspan=\"2\" bgcolor='grey'><B>%s</B></td>" + \ | |
92 "</tr>%s</table> > ] \n" | |
93 stream.write(opstr % (n, NODECOLOR, label(u, g), "".join(f))) | |
94 | |
95 stream.write("}\n") | |
96 | |
97 | |
98 def _help(): | |
99 sys.stderr.write(""" | |
100 rdfs2dot.py [-f <format>] files... | |
101 Read RDF files given on STDOUT, writes a graph of the RDFS schema in | |
102 DOT language to stdout | |
103 -f specifies parser to use, if not given, | |
104 | |
105 """) | |
106 | |
107 | |
108 def main(): | |
109 rdflib.extras.cmdlineutils.main(rdfs2dot, _help) | |
110 | |
111 if __name__ == '__main__': | |
112 main() |