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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
// 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
import (
"cmp"
"crypto/md5"
"flag"
"fmt"
"hash"
"io"
"os"
"path/filepath"
"reflect"
"slices"
"sort"
"testing"
"text/tabwriter"
"time"
"golang.org/x/exp/rand"
)
var (
origSeed = flag.Int64("seed", 1, "specify random seed to use for each test (negative for Unix time)")
tests = flag.String("test", "*-in.n[qt]", "specify test case in testdata")
)
func TestIsoCanonicalHashes(t *testing.T) {
seed := uint64(*origSeed)
if *origSeed < 0 {
seed = uint64(time.Now().UnixNano())
}
defer func() {
if t.Failed() && *origSeed < 0 {
t.Logf("time based seed: %d", seed)
}
}()
// Number of times to run IsoCanonicalHashes to check consistency.
const retries = 5
// Share a global hash function to ensure that we
// are resetting the function internally on each use.
hash := md5.New()
glob, err := filepath.Glob(filepath.Join("testdata", *tests))
if err != nil {
t.Fatalf("Failed to open test suite: %v", err)
}
for _, path := range glob {
name := filepath.Base(path)
t.Run(name, func(t *testing.T) {
src := rand.NewSource(seed)
f, err := os.Open(path)
if err != nil {
t.Fatalf("Failed to open test suite in %q: %v", path, err)
}
var statements []*Statement
dec := NewDecoder(f)
for {
s, err := dec.Unmarshal()
if err != nil {
if err == io.EOF {
break
}
t.Fatalf("Unexpected error reading from %q: %v", path, err)
}
statements = append(statements, s)
}
f.Close()
for _, decomp := range []bool{false, true} {
t.Run(fmt.Sprintf("decomp=%t", decomp), func(t *testing.T) {
var last map[string][]byte
for i := 0; i < retries; i++ {
curr, terms := IsoCanonicalHashes(statements, decomp, true, hash, make([]byte, 16))
if !hashesDisjoint(terms) {
t.Errorf("IsoCanonicalHashes did not uniquely identify nodes %q with decomp=%t",
name, decomp)
}
if last != nil {
last := relabelStatements(statements, termsFor(last, hash))
sortSimpleLexicalStatements(last)
curr := relabelStatements(statements, termsFor(curr, hash))
sortSimpleLexicalStatements(curr)
if !reflect.DeepEqual(last, curr) {
t.Errorf("IsoCanonicalHashes was not stable between runs on %q with decomp=%t",
name, decomp)
t.Log("Current run:")
for _, s := range curr {
t.Logf("\t%s", s)
}
t.Log("Previous run:")
for _, s := range last {
t.Logf("\t%s", s)
}
break
}
}
last = curr
}
hashes := last
ok := allUnique(hashes)
if !ok {
t.Errorf("Failed to get unique hashes for %q disjoint with decomp=%t", name, decomp)
t.Logf("skipping %q decomp=%t", path, decomp)
return
}
// Test that a graph is not isomorphic with one generated
// by deleting the last statement.
t.Run("isomorphic G != G-s", func(t *testing.T) {
if len(statements) == 0 {
return
}
if Isomorphic(statements, statements[:len(statements)-1], decomp, hash) {
t.Error("Isomorphic(G, G-s)=true")
}
})
// Test that a graph is not isomorphic with one generated
// by hashing the first grounded statement.
t.Run("isomorphic G != Gμ(g)", func(t *testing.T) {
mangled, mangTerms := mangleFirstIL(statements, hash)
if mangTerms == nil {
// All terms were blanks.
return
}
if Isomorphic(statements, mangled, decomp, hash) {
t.Error("Isomorphic(G, Gμ(g))=true")
}
})
// Test that a graph is not isomorphic with one generated
// by merging the first two lexically sorted blank nodes
// into one.
t.Run("isomorphic G != G(b1∪b2)", func(t *testing.T) {
mangled, mangTerms := mergeFirst2B(statements)
if mangTerms == nil {
// All terms were blanks.
return
}
if Isomorphic(statements, mangled, decomp, hash) {
t.Error("Isomorphic(G, G(b1∪b2))=true")
}
})
// Relabel a copy of the statements and then sort.
orig := relabelStatements(statements, termsFor(hashes, hash))
sortSimpleLexicalStatements(orig)
for _, perm := range []struct {
name string
data func() ([]*Statement, map[string]string)
}{
{
name: "reverse statements",
data: func() ([]*Statement, map[string]string) { return reverseStatements(statements) },
},
{
name: "permute statements",
data: func() ([]*Statement, map[string]string) { return permuteStatements(statements, src) },
},
{
name: "permute blank labels",
data: func() ([]*Statement, map[string]string) { return permuteBlanks(statements, src) },
},
{
name: "hash blank labels",
data: func() ([]*Statement, map[string]string) { return hashBlanks(statements, md5.New()) },
},
{
name: "reverse statements and hash blank labels",
data: func() ([]*Statement, map[string]string) {
// Reordering must come first since it does not return
// a non-nil terms map, but hashBlanks does.
s, _ := reverseStatements(statements)
return hashBlanks(s, md5.New())
},
},
{
name: "permute statements and hash blank labels",
data: func() ([]*Statement, map[string]string) {
// Reordering must come first since it does not return
// a non-nil terms map, but hashBlanks does.
s, _ := permuteStatements(statements, src)
return hashBlanks(s, md5.New())
},
},
} {
t.Run(perm.name, func(t *testing.T) {
if debug {
fmt.Fprintf(os.Stderr, "\n%q %q decomp=%t:\n", path, perm.name, decomp)
}
altStatements, terms := perm.data()
altHashes, altTerms := IsoCanonicalHashes(altStatements, decomp, true, hash, make([]byte, 16))
ok := allUnique(altHashes) && hashesDisjoint(altTerms)
if !ok {
t.Errorf("Failed to get unique hashes for %q alternative disjoint %q with decomp=%t",
path, perm.name, decomp)
}
if debug {
fmt.Fprintln(os.Stderr, "Name mappings from original dataset:")
keys := make([]string, len(hashes))
var i int
for k := range hashes {
keys[i] = k
i++
}
slices.Sort(keys)
w := tabwriter.NewWriter(os.Stderr, 0, 4, 8, ' ', 0)
for _, k := range keys {
fmt.Fprintf(w, "\t%s\t%s\n", k, translate(k, terms))
}
w.Flush()
fmt.Fprintln(os.Stderr)
}
// Relabel a copy of the alternative statements and then sort.
alt := relabelStatements(altStatements, termsFor(altHashes, hash))
sortSimpleLexicalStatements(alt)
for i := range statements {
if *orig[i] != *alt[i] { // Otherwise we have pointer inequality.
t.Errorf("Unexpected statement in %q %q decomp=%t:\ngot: %#v\nwant:%#v",
path, perm.name, decomp, orig[i], alt[i])
break
}
}
if !Isomorphic(statements, altStatements, decomp, hash) {
t.Errorf("Isomorphic(G, perm(G))=false in %q %q decomp=%t",
path, perm.name, decomp)
}
})
}
})
}
})
}
}
func permuteStatements(s []*Statement, src rand.Source) ([]*Statement, map[string]string) {
rnd := rand.New(src)
m := make([]*Statement, len(s))
for x, y := range rnd.Perm(len(s)) {
m[x] = s[y]
}
return m, nil
}
func reverseStatements(s []*Statement) ([]*Statement, map[string]string) {
m := make([]*Statement, len(s))
for i, j := 0, len(s)-1; i < len(s); i, j = i+1, j-1 {
m[j] = s[i]
}
return m, nil
}
func permuteBlanks(s []*Statement, src rand.Source) ([]*Statement, map[string]string) {
rnd := rand.New(src)
terms := make(map[string]string)
for _, e := range s {
for _, t := range []string{
e.Subject.Value,
e.Predicate.Value,
e.Object.Value,
e.Label.Value,
} {
if t == "" {
continue
}
terms[t] = t
}
}
var blanks []string
for t := range terms {
if isBlank(t) {
blanks = append(blanks, t)
}
}
slices.Sort(blanks)
for x, y := range rnd.Perm(len(blanks)) {
terms[blanks[x]] = blanks[y]
}
m := relabelStatements(s, terms)
return m, terms
}
func hashBlanks(s []*Statement, h hash.Hash) ([]*Statement, map[string]string) {
terms := make(map[string]string)
for _, e := range s {
for _, t := range []string{
e.Subject.Value,
e.Predicate.Value,
e.Object.Value,
e.Label.Value,
} {
if !isBlank(t) {
continue
}
h.Reset()
h.Write([]byte(t))
terms[t] = fmt.Sprintf("_:%0*x", 2*h.Size(), h.Sum(nil))
}
}
m := relabelStatements(s, terms)
return m, terms
}
func mangleFirstIL(s []*Statement, h hash.Hash) ([]*Statement, map[string]string) {
terms := make(map[string]string)
for _, e := range s {
for _, t := range []string{
e.Subject.Value,
e.Predicate.Value,
e.Object.Value,
e.Label.Value,
} {
if isBlank(t) {
continue
}
h.Reset()
h.Write([]byte(t))
terms[t] = fmt.Sprintf(`"%0*x"`, 2*h.Size(), h.Sum(nil))
return relabelStatements(s, terms), terms
}
}
m := relabelStatements(s, nil)
return m, nil
}
func mergeFirst2B(s []*Statement) ([]*Statement, map[string]string) {
terms := make(map[string]string)
for _, e := range s {
for _, t := range []string{
e.Subject.Value,
e.Predicate.Value,
e.Object.Value,
e.Label.Value,
} {
if !isBlank(t) {
continue
}
terms[t] = t
}
}
if len(terms) < 2 {
return relabelStatements(s, nil), nil
}
blanks := make([]string, len(terms))
i := 0
for _, b := range terms {
blanks[i] = b
i++
}
slices.Sort(blanks)
terms[blanks[1]] = terms[blanks[0]]
m := relabelStatements(s, terms)
return m, nil
}
func hashesDisjoint(terms map[string]map[string]bool) bool {
for _, t := range terms {
if len(t) != 1 {
return false
}
}
return true
}
func TestLexicalStatements(t *testing.T) {
if *tests == "" {
*tests = "*"
}
hash := md5.New()
glob, err := filepath.Glob(filepath.Join("testdata", *tests))
if err != nil {
t.Fatalf("Failed to open test suite: %v", err)
}
for _, path := range glob {
f, err := os.Open(path)
if err != nil {
t.Fatalf("Failed to open test suite in %q: %v", path, err)
}
var statements []*Statement
dec := NewDecoder(f)
for {
s, err := dec.Unmarshal()
if err != nil {
if err == io.EOF {
break
}
t.Fatalf("Unexpected error reading from %q: %v", path, err)
}
statements = append(statements, s)
}
f.Close()
for _, decomp := range []bool{false, true} {
hashes, _ := IsoCanonicalHashes(statements, decomp, true, hash, make([]byte, 16))
terms := termsFor(hashes, hash)
// Sort a copy of the statements based on hashes and then relabel.
indirect := make([]*Statement, len(statements))
copy(indirect, statements)
sort.Sort(lexicalStatements{indirect, hashes})
indirect = relabelStatements(indirect, terms)
// Relabel a copy of the statements and then sort.
direct := relabelStatements(statements, terms)
sortSimpleLexicalStatements(direct)
for i := range statements {
if *indirect[i] != *direct[i] { // Otherwise we have pointer inequality.
t.Errorf("Unexpected ordering of indirect sort in %q:\ngot: %#v\nwant:%#v",
path, indirect[i], direct[i])
}
}
}
}
}
func termsFor(hashes map[string][]byte, hash hash.Hash) map[string]string {
terms := make(map[string]string)
for t, h := range hashes {
if isBlank(t) {
terms[t] = fmt.Sprintf("_:%0*x", 2*hash.Size(), h)
}
}
return terms
}
// sortSimpleLexicalStatements implements lexical statement sorting on the
// literal values without interpolation.
func sortSimpleLexicalStatements(statements []*Statement) {
slices.SortFunc(statements, func(a, b *Statement) int {
if n := cmp.Compare(unquoteIRI(a.Subject.Value), unquoteIRI(b.Subject.Value)); n != 0 {
return n
}
// Always IRI.
if n := cmp.Compare(unquoteIRI(a.Predicate.Value), unquoteIRI(b.Predicate.Value)); n != 0 {
return n
}
return cmp.Compare(unquoteIRI(a.Object.Value), unquoteIRI(b.Object.Value))
})
}
func relabelStatements(s []*Statement, terms map[string]string) []*Statement {
m := make([]*Statement, len(s))
for i, e := range s {
n := *e
n.Subject = Term{Value: translate(n.Subject.Value, terms)}
n.Predicate = Term{Value: translate(n.Predicate.Value, terms)}
n.Object = Term{Value: translate(n.Object.Value, terms)}
n.Label = Term{Value: translate(n.Label.Value, terms)}
m[i] = &n
}
return m
}
func BenchmarkIsoCanonicalHashes(b *testing.B) {
hash := md5.New()
benchmarks := []string{
"test019-in.nq",
"test044-in.nq",
}
for _, name := range benchmarks {
path := filepath.Join("testdata", name)
b.Run(name, func(b *testing.B) {
f, err := os.Open(path)
if err != nil {
b.Fatalf("Failed to open test suite in %q: %v", path, err)
}
var statements []*Statement
dec := NewDecoder(f)
for {
s, err := dec.Unmarshal()
if err != nil {
if err == io.EOF {
break
}
b.Fatalf("Unexpected error reading from %q: %v", path, err)
}
statements = append(statements, s)
}
f.Close()
nodes := make(map[string]bool)
for _, s := range statements {
for _, t := range []string{
s.Subject.Value,
s.Predicate.Value,
s.Object.Value,
s.Label.Value,
} {
if t != "" {
nodes[t] = true
}
}
}
n := len(nodes)
for _, decomp := range []bool{false, true} {
b.Run(fmt.Sprintf("decomp=%t", decomp), func(b *testing.B) {
for i := 0; i < b.N; i++ {
hashes, _ := IsoCanonicalHashes(statements, decomp, true, hash, make([]byte, 16))
if len(hashes) != n {
b.Fatalf("unexpected number of hashes: %d != %d", len(hashes), len(statements))
}
}
})
}
})
}
}
|