-
Notifications
You must be signed in to change notification settings - Fork 4
/
databaseToJSON.py
296 lines (238 loc) · 10.2 KB
/
databaseToJSON.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#! /usr/bin/env python
from __future__ import print_function
import os, sys, uuid
import itertools
from io import open
from collections import defaultdict
from version_guard import isString
import zlib
try:
import cPickle as pickle
except:
import pickle
try:
import ujson as json
except:
print('UltraJSON not available; falling back to Python library.')
import json
from rmBitmasks import *
from renderJustification import printOp
Version = u'5.1'
DatabaseVersion = u'5.1'
class VersionError(Exception):
def __init__(self, targetVersion, actualVersion):
super(VersionError, self).__init__(u'Version mismatch: found v{0}, targeting v{1}'.format(actualVersion, targetVersion))
class Zoo:
_nextUID = 0
nodes = {}
meta = {'edgeKinds': [],
'colorings': [],
'graphviz': {}}
def addNode(label, definition='', key=None, uid=None, edges={}, properties={}, tags=[]):
if uid is None:
uid = _nextUID
_nextUID += 1
n = Node(uid, label, definition, key, edges, properties, tags)
if key is None:
nodes[label] = n
else:
nodes[key] = n
def addEdgeKind(label, functionBody):
self.meta['edgeKinds'].append({'label': label, 'functionBody': functionBody})
def addColoring(self, name, colors, labels, coloringFunction):
self.meta['colorings'].append(Coloring(name, colors, labels, coloringFunction))
def __getitem__(self, key):
return self.nodes[key]
def __setitem__(self, key, item):
self.nodes[key] = item
def __contains__(self, key):
return (key in self.nodes)
def __init__(self, edgeKinds=[], rankdir='TB'):
self.meta['edgeKinds'] = edgeKinds
self.meta['graphviz'] = {'rankdir': rankdir}
class Coloring:
def __init__(self, name, colors, labels, coloringFunction):
self.label = name
self.colors = [{'color': color, 'label': label} for color,label in zip(colors,labels)]
self.coloring = coloringFunction
class Node:
def __init__(self, uid, label, definition='', key=None, edges={}, properties={}, tags=[]):
if key is None:
key = label
self.uid = uid
self.label = label
self.definition = definition
self.key = key
self.edges = edges
self.properties = properties
self.tags = tags
def addEdge(self, dstKey, properties={}):
self.edges[dstKey] = Edge(self.key, dstKey, properties)
def addProperty(self, name, justification, value=None, description='', uid=None):
self.properties[name] = Property(justification, value, description, uid)
class Edge:
def __init__(self, srcKey, dstKey, properties={}):
self.srcKey = srcKey
self.dstKey = dstKey
self.properties = properties
def addProperty(self, name, justification, value=None, description='', uid=None):
self.properties[name] = Property(justification, value, description, uid)
class Property:
def __init__(self, justification, value=None, description='', uid=None):
if uid is None:
uid = uuid.uuid4()
self.value = value
self.justification = justification
self.description = description
self.uid = uid
class Justification:
weight = 0
direct = None
composite = None
def __init__(self, direct=None, composite=None, weight=None):
if direct is None and composite is None:
raise ValueError('Justifications must contain some justification.')
if direct is not None and composite is not None:
raise ValueError('Justifications are either direct or composite, not both.')
if direct is not None:
self.weight = 1
self.direct = direct
if composite is not None:
if weight is None:
raise ValueError('Composite justifications must specify their weights.')
self.weight = weight
self.composite = composite
def loadDatabase(databaseName, quiet=False):
with open(databaseName, mode='rb') as databaseFile:
compressedDatabase = databaseFile.read()
pickledDatabase = zlib.decompress(compressedDatabase)
setDatabase(pickle.loads(pickledDatabase))
def getDatabase():
return {'version': DatabaseVersion,
'principles': principles,
'implication': (implies, notImplies),
'conservation': (conservative, nonConservative),
'form': form,
'primary': (primary, primaryIndex),
'justify': justify}
def setDatabase(database):
if database['version'] != DatabaseVersion:
raise VersionError(DatabaseVersion, database['version'])
global principles
principles = database['principles']
global implies, notImplies
implies, notImplies = database['implication']
global conservative, nonConservative
conservative, nonConservative = database['conservation']
global form
form = database['form']
global primary, primaryIndex
primary, primaryIndex = database['primary']
global justify
justify = database['justify']
if __name__ == '__main__':
databaseTitle = 'database'
if os.path.splitext(databaseTitle)[1] == '':
databaseName = databaseTitle + os.extsep + 'dat'
else:
databaseName = databaseTitle
loadDatabase(databaseName)
primaryIndex += sorted(principles - primary)
meta = {
'tags': [],
'edgeKinds': [],
'colorings': [],
'about': {'description': 'The <a href="https://rmzoo.math.uconn.edu/">RM Zoo</a> is a program to '
'help organize reverse-mathematical relations between mathematical '
'principles, particularly those that fail to be equivalent to any of the '
'big five subsystems of second-order arithmetic. Its primary goal is to '
'make it easier to see known results and open questions, and thus '
'hopefully to serve as a useful tool to researchers in the field. As a '
'secondary goal, the Zoo provides an interactive annotated bibliography '
'of the field, collecting results in a standard machine-readable format.'},
'graphviz': {}
}
translationFunction = "if('{0}' in edge.properties) return 1; " \
"if('{1}' in edge.properties) return 0; " \
"return 2;"
for red in Reduction:
if red == Reduction.none: continue
posName = red.name + u'i'
negName = red.name + u'ni'
kindNode = {'label': '$\rightarrow_{\rm ' + red.name + '}$',
'key': posName,
'edges': [r.name + u'i' for r in Reduction.list(Reduction.weaker(red) & ~red)]}
kind = {'label': '$\rightarrow_{\rm ' + red.name + '}$',
'functionBody': translationFunction.format(posName, negName),
'node': kindNode}
meta['edgeKinds'].append(kind)
for f in Form:
if f == Form.none: continue
posName = f.name + u'c'
negName = f.name + u'nc'
#TODO: Implement better labels for conservation results
kindNode = {'label': posName,
'key': posName,
'edges': [r.name + u'i' for r in Reduction.list(Reduction.weaker(red) & ~red)]}
kind = {'label': posName,
'functionBody': translationFunction.format(posName, negName),
'node': kindNode}
nodes = {}
for i,p in enumerate(primaryIndex):
nodes[p] = dict()
nodes[p]['uid'] = i
#TODO: Implement labels
nodes[p]['label'] = p
#TODO: Implement definitions
nodes[p]['definition'] = ''
nodes[p]['edges'] = {dst:{'srcKey':p,
'dstKey':dst,
'properties':dict()} for dst in principles}
nodes[p]['properties'] = {}
nodes[p]['tags'] = []
uid = i+1
properties = {}
for p in sorted(principles):
for f in Form.list(form[p]):
#TODO: Implement justifications for forms
properties[(p, u'form', f)] = {'uid': uid,
'value': f.name,
'justification': {'weight': 1,
'direct': 'Observed'}}
nodes[p]['properties'][f.name] = properties[(p, u'form', f)]
uid += 1
for f,j in justify.items():
if f in properties:
continue
toJustify = [(f, j, {'uid': uid})]
uid += 1
while toJustify:
fact,jst,prop = toJustify.pop()
done = True
if isString(jst):
prop['justification'] = {'weight': 1, 'direct': jst}
elif all(ref in properties for ref in jst):
prop['justification'] = {'weight': 1 + sum(properties[ref]['justification']['weight'] for ref in jst),
'composite': [properties[ref]['uid'] for ref in jst]}
else:
done = False
toJustify.append((fact, jst, prop))
for ref in jst:
if ref in properties:
continue
toJustify.append((ref, justify[ref], {'uid': uid}))
uid += 1
if done:
properties[fact] = prop
a,op,b = fact
opCtx,opCore = op
if opCore in u'->':
coreName = u'i'
elif opCore == u'-|>':
coreName = u'ni'
else:
coreName = opCore
opName = opCtx.name + coreName
nodes[a]['edges'][b]['properties'][opName] = prop
with open('rmzoo.json', 'w') as f:
json.dump({'nodes': nodes, 'meta': meta}, f, sort_keys=True, indent=4)