-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_io_rdf.py
executable file
·282 lines (248 loc) · 12.4 KB
/
test_io_rdf.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
"""Testing the io.rdf module."""
# Copyright Permission.io, Inc. (formerly known as Algebraix Data Corporation), Copyright (c) 2022.
#
# This file is part of algebraixlib <http://github.com/AlgebraixData/algebraixlib>.
#
# algebraixlib is free software: you can redistribute it and/or modify it under the terms of version
# 3 of the GNU Lesser General Public License as published by the Free Software Foundation.
#
# algebraixlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with algebraixlib.
# If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------------------------------
import inspect
import io
import os
from rdflib import BNode, Literal, URIRef, XSD
import sys
import unittest
import algebraixlib.algebras.clans as clans
from algebraixlib.mathobjects import Atom, Couplet, Set
# noinspection PyUnresolvedReferences
from data_rdf import basic_graphs as bg, result_tables as rt
# noinspection PyProtectedMember
from algebraixlib.import_export.rdf import _convert_identifier_to_mathobject, import_graph, \
export_table
from algebraixlib.util.rdf import is_file_url, get_file_url, is_triple, is_absolute_triple, \
is_graph, is_absolute_graph, triple_match, join, make_triple
# This graph is extracted from examples.sampleRdfGraph.py
sample_graph = """
<id:jeff> <rdf:name> 'jeff' .
<id:jeff> <rdf:type> <cat:engineer> .
<id:james> <rdf:name> 'james' .
<id:james> <rdf:type> <cat:engineer> .
<id:james> <fav:mathobject> <type:couplet> .
<id:donald> <rdf:type> <cat:duck> .
<id:donald> <rdf:name> 'donald duck' .
<id:donald> <fav:mathobject> <type:atom> .
"""
class RdfTest(unittest.TestCase):
"""Test the io.rdf module."""
print_examples = False
def test_is_file_url(self):
"""Test determining if a string is an URL"""
self.assertFalse(is_file_url("file:"))
self.assertTrue(is_file_url("file://"))
def test_get_file_url(self):
cwd_url = get_file_url(os.getcwd()) + '/'
if sys.platform == 'win32':
cwd_drive = os.getcwd()[:2]
self.assertEqual(get_file_url('/abc/de'), 'file:///{dr}/abc/de'.format(dr=cwd_drive))
self.assertEqual(get_file_url(r'\bbc\de'), 'file:///{dr}/bbc/de'.format(dr=cwd_drive))
self.assertEqual(get_file_url('//cbc/de'), 'file://cbc/de')
self.assertEqual(get_file_url(r'C:\dbc\de'), 'file:///C:/dbc/de')
self.assertEqual(get_file_url('C:/ebc/de'), 'file:///C:/ebc/de')
self.assertEqual(get_file_url(r'fbc\de'), cwd_url + 'fbc/de')
else:
self.assertEqual(get_file_url('/abc/de'), 'file:///abc/de')
self.assertEqual(get_file_url('//cbc/de'), 'file://cbc/de')
self.assertEqual(get_file_url('fbc/de'), cwd_url + 'fbc/de')
def test_is_graph(self):
"""Test identifying a graph."""
graph = Set({make_triple(1, 2, 3)})
self.assertTrue(is_graph(graph))
# not a clan
not_graph = Atom('a')
self.assertFalse(is_graph(not_graph))
# not regular
not_graph = Set({Couplet('a', 1), Couplet('a', 2), Couplet('a', 3)})
self.assertFalse(is_graph(not_graph))
# left set is not spo
not_graph = Set({Couplet('a', 1), Couplet('b', 2), Couplet('c', 3)})
self.assertFalse(is_graph(not_graph))
def test_is_absolute_graph(self):
"""Test identifying an absolute graph."""
graph = Set({make_triple(1, 2, 3)})
self.assertTrue(is_absolute_graph(graph))
# not a clan
not_graph = Atom('a')
self.assertFalse(is_absolute_graph(not_graph))
# not regular
not_graph = Set({Couplet('a', 1), Couplet('a', 2), Couplet('a', 3)})
self.assertFalse(is_absolute_graph(not_graph))
# left set is not spo
not_graph = Set({Couplet('a', 1), Couplet('b', 2), Couplet('c', 3)})
self.assertFalse(is_absolute_graph(not_graph))
def test_is_triple(self):
"""Test identifying a triple."""
# a relation with left set 'spo' and cardinality 3
triple = Set({Couplet('s', 1), Couplet('p', 2), Couplet('o', 3)})
self.assertTrue(is_triple(triple))
# a relation with left set 'abc'
not_triple = Set({Couplet('a', 1), Couplet('b', 2), Couplet('c', 3)})
self.assertFalse(is_triple(not_triple))
# a relation with cardinality 4
not_triple = Set({Couplet('s', 1), Couplet('p', 2), Couplet('o', 3), Couplet('a', 1)})
self.assertFalse(is_triple(not_triple))
# not a relation
not_triple = Set({Atom('s'), Atom('p'), Atom('o')})
self.assertFalse(is_triple(not_triple))
def test_is_absolute_triple(self):
"""Test identifying an absolute triple."""
# a relation with left set 'spo' and cardinality 3
absolute_triple = Set({Couplet('s', 1), Couplet('p', 2), Couplet('o', 3)})
self.assertTrue(is_absolute_triple(absolute_triple))
# a relation with left set 'abc'
not_absolute_triple = Set({Couplet('a', 1), Couplet('b', 2), Couplet('c', 3)})
self.assertFalse(is_absolute_triple(not_absolute_triple))
# a relation with cardinality 4
not_absolute_triple = Set(Couplet('s', 1), Couplet('p', 2), Couplet('o', 3),
Couplet('a', 1))
self.assertFalse(is_absolute_triple(not_absolute_triple))
# not a relation
not_absolute_triple = Set({Atom('s'), Atom('p'), Atom('o')})
self.assertFalse(is_absolute_triple(not_absolute_triple))
def test_make_triple(self):
self.assertEqual(
make_triple(1, 2, 3),
Set([Couplet('s', 1), Couplet('p', 2), Couplet('o', 3)])
)
@unittest.skipIf(sys.version_info >= (3, 6), "test_import_graph fails for Python 3.6")
def test_import_graph(self):
"""Test importing clans from files and strings (function import_graph())."""
def check_graph(graph_mo, graph_ref):
self.assertEqual(graph_mo.get_ground_set(), clans.get_absolute_ground_set())
self.assertEqual(graph_mo.get_left_set(), Set('s', 'p', 'o'))
self.assertEqual(graph_mo, graph_ref)
for graph_id in bg.keys():
graph_data = bg[graph_id]
if 'file' in graph_data:
graph = import_graph(graph_data['file']())
check_graph(graph, graph_data['mo']())
else:
assert 'graph' in graph_data
graph1 = import_graph(io.StringIO(graph_data['graph']()))
check_graph(graph1, graph_data['mo']())
def test_convert_identifier_to_mathobject(self):
"""Test the function _convert_identifier_to_mathobject()."""
self.assertRaises(TypeError, lambda: _convert_identifier_to_mathobject(5))
self.assertEqual(
Atom(URIRef('http://server/path')),
_convert_identifier_to_mathobject(URIRef('http://server/path')))
self.assertEqual(
Atom(BNode('http://server/path')),
_convert_identifier_to_mathobject(BNode('http://server/path')))
self.assertNotEqual(
Atom(BNode('http://server/path')),
_convert_identifier_to_mathobject(URIRef('http://server/path')))
self.assertEqual(
Atom(1),
_convert_identifier_to_mathobject(Literal('1', datatype=XSD.integer)))
self.assertEqual(
Atom(1.0),
_convert_identifier_to_mathobject(Literal('1', datatype=XSD.double)))
self.assertNotEqual(
Atom(1),
_convert_identifier_to_mathobject(Literal('1', datatype=XSD.double)))
self.assertEqual(
Atom('1'),
_convert_identifier_to_mathobject(Literal('1', datatype=XSD.string)))
self.assertNotEqual(
Atom(1),
_convert_identifier_to_mathobject(Literal('1', datatype=XSD.string)))
def test_export_table(self):
table = rt['1']
lefts = table.get_left_set()
self.assertRaises(AttributeError, lambda: export_table(lefts, table, '1'))
csv = io.StringIO(newline='')
self.assertRaises(AssertionError, lambda: export_table(csv, lefts, table, '1'))
csv = io.StringIO(newline='')
export_table(file_or_path=csv, lefts=lefts, table=table, out_format='csv')
csv_str = csv.getvalue()
if self.print_examples:
print('csv:', csv_str)
self.assertEqual(csv_str, """\
"?cnt", "?eng", "?fav", "?name", "_:b0"
"6", "id:james", "type:couplet", "james", "blank node"
""")
json = io.StringIO(newline='')
export_table(file_or_path=json, lefts=lefts, table=table, out_format='json')
json_str = json.getvalue()
if self.print_examples:
print('json:', json_str)
self.assertEqual(json_str, """\
{"head":{"vars":["?cnt", "?eng", "?fav", "?name", "_:b0"]},
"results":{"bindings":[
{"?cnt": {"type":"literal", "datatype":"http://www.w3.org/2001/XMLSchema#integer", "value":"6"}
, "?eng": {"type":"uri", "value":"id:james"}
, "?fav": {"type":"uri", "value":"type:couplet"}
, "?name": {"type":"literal", "value":"james"}
, "_:b0": {"type":"bnode", "value":"blank node"}
}
]}}""")
table = rt['2']
lefts = table.get_left_set()
csv = io.StringIO(newline='')
export_table(file_or_path=csv, lefts=lefts, table=table, out_format='csv')
csv_str = csv.getvalue()
if self.print_examples:
print('csv:', csv_str)
json = io.StringIO(newline='')
export_table(file_or_path=json, lefts=lefts, table=table, out_format='json')
json_str = json.getvalue()
if self.print_examples:
print('json:', json_str)
def test_triple_match_assert(self):
self.assertRaises(AssertionError, lambda: triple_match(Atom(1)))
def test_triple_match(self):
# Determine the number of types in the graph data
graph_type_cnt = sample_graph.count('rdf:type')
# Import the example graph as data algebra MathObject.
graph = import_graph(io.StringIO(sample_graph), rdf_format='turtle')
# Extract the triples that have a type and include their id
triples_matched = triple_match(graph, '?id', URIRef('rdf:type'), '?type')
# Verify that the number of sets matched equals the number of types in the graph
self.assertEqual(len(triples_matched), graph_type_cnt)
def test_join_binary(self):
clan1 = Set(Set([Couplet(1, 'one')]))
clan2 = Set(Set([Couplet(2, 'two')]))
answer = Set(Set([Couplet(1, 'one'), Couplet(2, 'two')]))
joined = join(clan1, clan2)
# data is unordered so don't use equality, use symmetric difference
self.assertEqual(0, len(answer.data ^ joined.data))
def test_join_ternary(self):
clan1 = Set(Set([Couplet(1, 'one')]))
clan2 = Set(Set([Couplet(2, 'two')]))
clan3 = Set(Set([Couplet(3, 'three')]))
answer = Set(Set([Couplet(1, 'one'), Couplet(2, 'two'), Couplet(3, 'three')]))
joined = join(clan1, clan2, clan3)
# data is unordered so don't use equality, use symmetric difference
self.assertEqual(0, len(answer.data ^ joined.data))
def test_join_quaternary(self):
clan1 = Set(Set([Couplet(1, 'one')]))
clan2 = Set(Set([Couplet(2, 'two')]))
clan3 = Set(Set([Couplet(3, 'three')]))
clan4 = Set(Set([Couplet(4, 'four')]))
answer = Set(Set(Couplet(1, 'one'), Couplet(2, 'two'), Couplet(3, 'three'),
Couplet(4, 'four')))
joined = join(clan1, clan2, clan3, clan4)
# data is unordered so don't use equality, use symmetric difference
self.assertEqual(0, len(answer.data ^ joined.data))
# --------------------------------------------------------------------------------------------------
if __name__ == '__main__':
# The print is not really necessary. It helps making sure we always know what we ran in the IDE.
print('main: {file}'.format(file=os.path.basename(inspect.getfile(inspect.currentframe()))))
unittest.main()