summaryrefslogtreecommitdiff
path: root/test/comparison_graph
blob: 655d23d7be159a4739e154d2149d47ec0241b5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
"""
This is not an automated test program-- it generates a viewable graph
that shows the clusters of equal objects.

"""

import os, datetime
from rdflib import Literal, URIRef, BNode, Namespace
import yapgvb
import rdflib
XSD = Namespace("http://www.w3.org/2001/XMLSchema#")
EX = Namespace("http://example.com/")

pyValues = '''\
u"foo"
"foo"
Literal("foo")
Literal("foo", datatype=XSD['string'])
Literal("foo", datatype=EX['bar'])
Literal("foo", "fr")
URIRef("foo")
BNode("foo")
Literal("5")
Literal("5", datatype=XSD['decimal'])
Literal("5", datatype=XSD['string'])
5
5.0
"5"
4.99999999999999999
Literal("4.99999999999999999")
Literal("4.99999999999999999", datatype=XSD['decimal'])
False
"False"
"false"
Literal("False")
Literal("false", datatype=XSD['boolean'])
Literal(False)
Literal(False, datatype=XSD['boolean'])
Literal(0, datatype=XSD['boolean'])
Literal("http://example.com/foo/")
URIRef("http://example.com/foo/")
URIRef("http://example.com/foo%2F")
"2006-04-15"
Literal("2006-04-15")
Literal("2006-04-15", datatype=XSD['date'])
Literal("2006-04-15Z", datatype=XSD['date'])
Literal("2006-04-15-00:00", datatype=XSD['date'])
datetime.date(2006, 4, 15)
'''.strip().splitlines()

# http://www.w3.org/TR/xmlschema-2/#date says these are the same
pyValues.extend([
    'Literal("2002-10-10+13:00", datatype=XSD["date"])', 
    'Literal("2002-10-09-11:00", datatype=XSD["date"])'
    ])

# see also http://www.w3.org/2001/sw/DataAccess/tests/#date-1

import cgitb
cgitb.enable(format='txt')

gv = yapgvb.Graph('rdflib comparisons', strict=True)
#gv.defaultdist = 100
#gv.mindist = 500
#gv.nodesep = 2
#gv.size = [8, 8]
#gv.pack = "false"

gv.sep = .5
#gv.overlap = "false"

gv.add_node("using rdflib version %s" % rdflib.__version__, color="green")

nodes = [(py, eval(py)) for py in pyValues]

gvNode = {}
for py, val in nodes:
    label = py
    label = label.replace('99999999999999999', '99...')
    label = label.replace('datatype=', 'dt=')
    n = gvNode[py] = gv.add_node(label)
    n.shape = 'box'
    n.fontsize = 10
    n.margin = .0005, .00002

for i, (yPy, yVal) in enumerate(nodes):
    for j, (xPy, xVal) in enumerate(nodes):
        true = xVal == yVal

        if not true and not (xVal != yVal):
            # neither equal nor nonequal!
            edge = gv.add_edge(gvNode[xPy], gvNode[yPy])
            edge.label = "__ne__ failed"
            edge.color = "red"

        if true:
            edge = gv.add_edge(gvNode[xPy], gvNode[yPy])
            edge.label = "="
            edge.style = "bold"
            edge.len = 2.5

            if xPy == yPy:
                # try to shorten the self-loops
                edge.headport = 'w'
                edge.tailport = 'w'
                edge.weight = .2
                edge.len = .2

gv.layout(yapgvb.engines.circo)
out = "/tmp/comparison_graph.png"
gv.render(out)
print "wrote: %s" % out

raise SystemExit

# this part makes an html table, but the graph is much prettier
from nevow import flat, tags as T

rows = [T.tr[T.th, [T.th[i] for i in range(len(nodes))]]]

for i, (yPy, yVal) in enumerate(nodes):
    cols = [T.th["%s) " % i, yPy]]
    for j, (xPy, xVal) in enumerate(nodes):
        true = xVal == yVal

        symbol = T.xml("≠")
        if true:
            symbol = "="
        cols.append(T.td(class_=str(true))[symbol])
    rows.append(T.tr[cols])
    
print flat.flatten(
    T.html[
    T.head[
    T.style['''
    table {
      border: 1px solid black;
      border-collapse: collapse;
    }
    td, th {
      border: 1px solid black;
    }
    td {
      width: 1em;
      text-align: center;
    }
    th {
      text-align: left;
    }
    .True {
      background: #92E892;
    }
    .False {
      color: gray;
    }
    ''']
    ],
    T.body[

    T.h1["Comparisons using rdflib %s" % rdflib.__version__],

    T.table[rows]]])