File: iso_canonical_example_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (249 lines) | stat: -rw-r--r-- 5,356 bytes parent folder | download
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
// Copyright ©2020 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rdf_test

import (
	"crypto/md5"
	"fmt"
	"log"
	"os"
	"slices"
	"strings"
	"text/tabwriter"

	"gonum.org/v1/gonum/graph/formats/rdf"
)

func ExampleIsoCanonicalHashes_isomorphisms() {
	for _, statements := range []string{
		`
<https://example.com/1> <https://example.com/2> <https://example.com/3> .
<https://example.com/3> <https://example.com/4> <https://example.com/5> .
`,
		`
_:a <ex:q> <ex:p> .
_:b <ex:q> <ex:p> .
_:c <ex:p> _:a .
_:d <ex:p> _:b .
_:c <ex:r> _:d .
`,
		`
_:a1 <ex:q> <ex:p> .
_:b1 <ex:q> <ex:p> .
_:c1 <ex:p> _:a1 .
_:d1 <ex:p> _:b1 .
_:c1 <ex:r> _:d1 .
`,
		`
# G
<ex:p> <ex:q> _:a .
<ex:p> <ex:q> _:b .
<ex:s> <ex:p> _:a .
<ex:s> <ex:r> _:c .
_:c <ex:p> _:b .

# H
<ex:p> <ex:q> _:d .
<ex:p> <ex:q> _:e .
_:f <ex:p> _:d .
_:f <ex:r> _:g .
_:g <ex:p> _:e .
`,
		`
_:greet <l:is> "hola"@es .
`,
	} {
		// Decode the statement stream.
		dec := rdf.NewDecoder(strings.NewReader(statements))
		var s []*rdf.Statement
		for {
			l, err := dec.Unmarshal()
			if err != nil {
				break
			}
			s = append(s, l)
		}

		// Get the node label to hash look-up table.
		hashes, _ := rdf.IsoCanonicalHashes(s, false, true, md5.New(), make([]byte, 16))

		// Get all the blank nodes.
		var blanks []string
		for k := range hashes {
			if strings.HasPrefix(k, "_:") {
				blanks = append(blanks, k)
			}
		}
		slices.Sort(blanks)

		if len(blanks) == 0 {
			fmt.Println("No blank nodes.")
		} else {
			w := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0)
			fmt.Fprintln(w, "Node\tHash")
			for _, k := range blanks {
				fmt.Fprintf(w, "%s\t%032x\n", k, hashes[k])
			}
			w.Flush()
		}
		fmt.Println()
	}

	// Output:
	//
	// No blank nodes.
	//
	// Node Hash
	// _:a  d4db6df055d5611e9d8aa6ea621561d1
	// _:b  ad70e47f2b026064c7f0922060512b9a
	// _:c  dafd81e6fa603d3e11c898d631e8673f
	// _:d  7e318557b09444e88791721becc2a8e7
	//
	// Node Hash
	// _:a1 d4db6df055d5611e9d8aa6ea621561d1
	// _:b1 ad70e47f2b026064c7f0922060512b9a
	// _:c1 dafd81e6fa603d3e11c898d631e8673f
	// _:d1 7e318557b09444e88791721becc2a8e7
	//
	// Node Hash
	// _:a  44ad49b6df3aea91ddbcef932c93e3b4
	// _:b  ba3ffd8b271a8545b1a3a9042e75ce4b
	// _:c  34e1bd90b6758b4a766e000128caa6a6
	// _:d  eb2a47c1032f623647d0497a2ff74052
	// _:e  1d9ed02f28d87e555feb904688bc2449
	// _:f  d3b00d36ea503dcc8d234e4405feab81
	// _:g  55127e4624c0a4fe5990933a48840af8
	//
	// Node    Hash
	// _:greet 0d9ba18a037a3fa67e46fce821fe51b4
}

func ExampleIsoCanonicalHashes_isomorphicParts() {
	for _, statements := range []string{
		`
# Part 1
_:a <ex:q> <ex:p> .
_:b <ex:q> <ex:p> .
_:c <ex:p> _:a .
_:d <ex:p> _:b .
_:c <ex:r> _:d .

# Part 2
_:a1 <ex:q> <ex:p> .
_:b1 <ex:q> <ex:p> .
_:c1 <ex:p> _:a1 .
_:d1 <ex:p> _:b1 .
_:c1 <ex:r> _:d1 .
`,
	} {
		// Decode the statement stream.
		dec := rdf.NewDecoder(strings.NewReader(statements))
		var s []*rdf.Statement
		for {
			l, err := dec.Unmarshal()
			if err != nil {
				break
			}
			s = append(s, l)
		}

		// Get the node label to hash look-up table. This time
		// we will decompose the dataset into splits and not
		// distinguish nodes. This will then group nodes from
		// the two isomorphic parts. Otherwise each node in
		// the complete dataset would get a unique hash.
		hashes, _ := rdf.IsoCanonicalHashes(s, true, false, md5.New(), make([]byte, 16))

		// Get all the blank nodes.
		var blanks []string
		for k := range hashes {
			if strings.HasPrefix(k, "_:") {
				blanks = append(blanks, k)
			}
		}
		slices.Sort(blanks)

		if len(blanks) == 0 {
			fmt.Println("No blank nodes.")
		} else {
			w := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0)
			fmt.Fprintln(w, "Node\tHash")
			for _, k := range blanks {
				fmt.Fprintf(w, "%s\t%032x\n", k, hashes[k])
			}
			w.Flush()
		}
		fmt.Println()
	}

	// Output:
	//
	// Node Hash
	// _:a  d4db6df055d5611e9d8aa6ea621561d1
	// _:a1 d4db6df055d5611e9d8aa6ea621561d1
	// _:b  ad70e47f2b026064c7f0922060512b9a
	// _:b1 ad70e47f2b026064c7f0922060512b9a
	// _:c  dafd81e6fa603d3e11c898d631e8673f
	// _:c1 dafd81e6fa603d3e11c898d631e8673f
	// _:d  7e318557b09444e88791721becc2a8e7
	// _:d1 7e318557b09444e88791721becc2a8e7
}

func ExampleC14n() {
	for _, statements := range []string{
		`
_:a <ex:q> <ex:p> .
_:b <ex:q> <ex:p> .
_:c <ex:p> _:a .
_:d <ex:p> _:b .
_:c <ex:r> _:d .
`,
		`
_:c1 <ex:p> _:a1 .
_:b1 <ex:q> <ex:p> .
_:d1 <ex:p> _:b1 .
_:a1 <ex:q> <ex:p> .
_:c1 <ex:r> _:d1 .
`,
	} {
		// Decode the statement stream.
		dec := rdf.NewDecoder(strings.NewReader(statements))
		var s []*rdf.Statement
		for {
			l, err := dec.Unmarshal()
			if err != nil {
				break
			}
			s = append(s, l)
		}

		// Get the hash to term label look-up table.
		_, terms := rdf.IsoCanonicalHashes(s, false, true, md5.New(), make([]byte, 16))

		relabeled, err := rdf.C14n(nil, s, terms)
		if err != nil {
			log.Fatal(err)
		}

		for _, s := range relabeled {
			fmt.Println(s)
		}
		fmt.Println()
	}

	// Output:
	//
	// _:c14n0 <ex:p> _:c14n1 .
	// _:c14n1 <ex:q> <ex:p> .
	// _:c14n2 <ex:q> <ex:p> .
	// _:c14n3 <ex:p> _:c14n2 .
	// _:c14n3 <ex:r> _:c14n0 .
	//
	// _:c14n0 <ex:p> _:c14n1 .
	// _:c14n1 <ex:q> <ex:p> .
	// _:c14n2 <ex:q> <ex:p> .
	// _:c14n3 <ex:p> _:c14n2 .
	// _:c14n3 <ex:r> _:c14n0 .
}