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