File: 042merge.t

package info (click to toggle)
libtm-perl 1.53-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,780 kB
  • ctags: 594
  • sloc: perl: 34,611; sh: 377; makefile: 50
file content (543 lines) | stat: -rw-r--r-- 14,773 bytes parent folder | download | duplicates (4)
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
542
543
use strict;
use warnings;

# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More qw(no_plan);

use Data::Dumper;
$Data::Dumper::Indent = 1;

sub _chomp {
    my $s = shift;
    chomp $s;
    return $s;
}

#ok(1);
#exit;

#== TESTS =====================================================================


#-- check various flavours of merging for the number of resulting topics -----------------

my %Ts = (
   t0815 => '
%s (rumsti ramsti) reifies http://www.0815.com/
bn: something completely different
bn: first version of 0815
oc @ scope1 (type1): http://www.rumsti.com/
oc @ scope2 (type2): http://www.romsti.com/
sin: http://IamIdonquichote.com/
sin: http://IamIdonquichote.com2/
',

   t0815a => '
%s (ramsti remsti)
bn: second version of 0815
oc @ scope2 (type2): http://www.ramsti.com/
',

   t0816 => '
%s reifies http://www.0815.com/
bn: something different
',

   t0816b => '
%s reifies http://www.0816.com/
bn: 0816
oc @ scope2 (type2): http://www.ramsti.com/
sin: http://IamIdonquichote.com/
',

   t0816c => '
%s
bn: something different
',

   t0817 => '
%s
bn : something completely different
bn @ xxxyyy: and yet another one
oc @ scope2 (type2): http://www.romsti.com/
sin: http://IamIdonquichote.com/
',

   t0818 => '
%s (rumsti ramsti)
bn : something completely different
oc @ scope1 (type1): http://www.rumsti.com/
sin: http://IamIdonquichote.com2/
',

   t0819 => '
%s
bn: another name for Don Quichote
sin: http://IamIdonquichote.com/
',

   t0820 => '
%s (ramsti romsti)
bn @ zzz : something completely different
oc @ scope2 (type2): http://www.romsti.com/
sin: http://IamIdonquichote.com2/
',

   t0821 => '
%s
bn @ xxxyyy: and yet another one
bn @ zzz : something completely different
',

   t0822 => '
%s
bn @ xxxzzz : something completely different
',

   t0823 => '
%s reifies http://www.0816.com/
bn: something different
');

sub mk_file {
  my @topics = @_;

  use File::Temp qw/ tempfile /;
  my ($fh, $filename) = tempfile( );
  print $fh join ("", (map { eval "\$$_" } @topics));
  close ($fh);

  return $filename;
}

use TM::AsTMa::Fact;

sub _parse {
    my $c = shift;
    my $text = shift;
    my $ms = new TM (baseuri => 'tm:', consistency => $c);
    my $p  = new TM::AsTMa::Fact (store => $ms);
    my $i  = $p->parse ($text);
    return $ms;
}


# sub _oldparse {
#     my $c = shift;
#     my $s = shift;
#     use TM::Materialized::AsTMa;
#     my $tm = new TM::Materialized::AsTMa (consistency => $c,
# 					  inline      => $s);
#     $tm->sync_in;
#     return $tm;
# }

use TM;
use TM::PSI;

sub _closure { # computes transitive closure
    my %r = @_;
    my %graph;
    foreach (keys %r) {
	$graph{$_}->{$r{$_}} = 1;
	$graph{$r{$_}}->{$_} = 1;
    }
	
    { # Floyd-Warshall
	my @vertices = keys %graph;
	
	foreach my $k (@vertices) {
	    foreach my $i (@vertices) {
		foreach my $j (@vertices) {
		    $graph{$j}->{$i} = $graph{$i}->{$j} = 1 
			if (($graph{$k}->{$j} && $graph{$i}->{$k})
			||
			    ($graph{$j}->{$k} && $graph{$k}->{$i}));
		}
	    }
	}
    }
    return \%graph;
}

{
    my %tests = (
# Address/Indicator Based
		 'address identical' => {
		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
		     topics  => [ qw (t0815 t0816) ],
		     results => { t0815 => 't0816' },
		 },
		 'address identical' => {
		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
		     topics  => [ qw (t0815 t0816 t0819) ],
		     results => { t0815 => 't0816',
				  t0819 => 't0815' },
		 },
 		 'sharing a subjectIndicator' => {
 		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
 		     topics  => [ qw (t0815 t0819) ],
 		     results => { t0815 => 't0819' },
 		 },
 		 'sharing a subjectIndicator (indirect)' => {
 		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
 		     topics  => [ qw (t0815 t0817 t0818 t0820) ],
 		     results => { t0817 => 't0815',
				  t0818 => 't0815',
				  t0820 => 't0815' }
		 },
		 'sharing a subjectIndicator (indirect)' => {
		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
		     topics  => [ qw (t0815 t0817 t0818 t0820) ],
		     results => { t0817 => 't0815',
				  t0818 => 't0815',
				  t0820 => 't0815' }
		 },
 		 'sharing a subjectIndicator (indirect, reorder)' => {
 		     merging => [ TM->Subject_based_Merging, TM->Indicator_based_Merging ],
 		     topics  => [ qw (t0817 t0820 t0818 t0815) ],
 		     results => { t0817 => 't0815',
				  t0818 => 't0815',
				  t0820 => 't0815' }
 		 },
 		 'sharing a subjectAddress (indirect, reorder)' => {
 		     merging => [ TM->Subject_based_Merging ],
 		     topics  => [ qw (t0817 t0816 t0818 t0815) ],
 		     results => { t0816 => 't0815', }
 		 },
# TNC Based merging
 		 'sharing "something completely different" (indirect, reorder)' => {
 		     merging => [ TM->TNC_based_Merging ],
 		     topics  => [ qw (t0817 t0816 t0820 t0815) ],
 		     results => { t0817 => 't0815', }
 		 },
 		 'sharing "zzz @ something completely different" (indirect, reorder)' => {
 		     merging => [ TM->TNC_based_Merging ],
 		     topics  => [ qw (t0816c t0819 t0820 t0821 t0823) ],
 		     results => { t0820  => 't0821',
			          t0816c => 't0823', }
 		 },
# 		 'TNC: all 4 merged' => {
# 		     merging => [ 'Subject_based_Merging', 'Topic_Naming_Constraint' ],
# 		     topics  => [ qw (t0815 t0817 t0820 t0821) ],
# 		     results => { t0815 => 't0816' },
#		 },
# 		 'TNC: only 2 merged' => {
# 		     merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
# 		     topics => [ qw (t0815 t0817 t0820) ],
# 		     result => 2,
# 		 },
# 		 'TNC: none merged 1 (scope mismatch)' => {
# 		     merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
# 		     topics => [ qw (t0815 t0820) ],
# 		     result => 2,
# 		 },
# 		 'TNC: none merged 2 (scope mismatch)' => {
# 		     merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
# 		     topics => [ qw (t0821 t0822) ],
# 		     result => 2,
# 		 },
	    );

    foreach my $t (sort keys %tests) {
#warn "working on test '$t'";
	foreach my $p (permutations (@{$tests{$t}->{topics}})) {
#warn "working on permutation ".join (" ", @$p);

            my $i;
	    my %X = map { $_ => sprintf ("X-%02d", $i++) } @$p;
#warn Dumper \%X;

            my $astma = join ("\n\n", map { sprintf $Ts{$_}, $X{$_} } keys %X);
	    my $tm    = _parse ($tests{$t}->{merging}, $astma);

	    my $ms = $tm;
	    foreach my $a (@{$tests{$t}->{topics}}) {
		foreach my $b (@{$tests{$t}->{topics}}) {
		    next if $a eq $b;
		    my ($ta, $tb) = $ms->midlet ($ms->mids ($X{$a}, $X{$b}));
#warn Dumper $ta, $tb;
		    ok ($ta != $tb, "$t: $a not same as $b");
		}
	    }

	    $tm->consolidate;

	    my $res_closure = _closure (%{$tests{$t}->{results}});
	    foreach my $a (@{$tests{$t}->{topics}}) {
		foreach my $b (@{$tests{$t}->{topics}}) {
		    next if $a eq $b;                                  # well
		    my ($ta, $tb) = $ms->midlet ($ms->mids ($X{$a}, $X{$b}));
		    if ($res_closure->{$a} and $res_closure->{$a}->{$b} or
			$res_closure->{$b} and $res_closure->{$b}->{$a}) {
#warn Dumper $ta, $tb;
			ok ($ta == $tb, "$t: $a same as $b") or 
			    warn "---------
$astma
".Dumper (\%X).
Dumper ($tests{$t}->{merging}).
"
  for permutation ".join (" ", @$p) and exit;

		    } else {
			ok ($ta != $tb, "$t: $a not same as $b") or 
			    warn "---------
$astma
".Dumper (\%X).
Dumper ($tests{$t}->{merging}).
"
  for permutation ".join (" ", @$p) and exit;
		    }
		}
	    }
	}
    }

#	    foreach my $a (keys %{$tests{$t}->{results}}) {
#		my $b = $tests{$t}->{results}->{$a};
#		my ($ta, $tb) = $ms->toplet ($X{$a}, $X{$b});
##warn Dumper $ta, $tb;
#		ok ($ta == $tb, "$t: $a same as $b");
#		if ($ta != $tb) { # this is an error
#		}
#	    }
}

sub permutations {
    my @ps;
    permute ([ @_ ], [], \@ps);
    return @ps;
}
    

sub permute {
    my @items = @{ $_[0] };
    my @perms = @{ $_[1] };
    my $ps    =    $_[2];

    unless (@items) {
	push @$ps, \@perms;
    } else {
        my @newitems;
        my @newperms;
        foreach my $i (0.. $#items) {
            @newitems = @items;
            @newperms = @perms;
            unshift (@newperms, splice (@newitems, $i, 1));
            permute ([@newitems], [@newperms], $ps);
        }
    }
}


__END__

{
    my $tm = _parse ([ 'Id_based_Merging', TM->Subject_based_Merging ], qw(t0815 t0816b));
    eval {
        $tm->consolidate;
	ok (0, 'should fail here');
    }; like ($@, qr/different subject/, _chomp($@));
}


#MIX ADD/IND
# Id Based
# 		 'id: all merged' => {
# 		     merging => [ 'Id_based_Merging' ],
# 		     topics => [ qw (t0815 t0815a) ],
# 		     result => 1,
# 		 },
# 		 'id: none merged' => {
# 		     merging => [ 'Id_based_Merging' ],
# 		     topics => [ qw (t0815 t0817) ],
# 		     result => 2,
# 		 },



#------------------------------------------------------------

{
  my $tm = new TM (consistency => { merge => [ TM->Subject_based_Merging ] },
                   tau         => "file:".mk_file (qw(t0818 t0819 t0820)));
  is (scalar $tm->toplets,                    $npt+10, 'merge all topics');
# chars von 818, no dup
  is ($tm->toplet ('t0820'), $tm->toplet ('t0818'), 'merged topics same reference');
  my $t818 = $tm->toplet ('t0818');
  ok (eq_set ( [ map { $_->[VALUE] } grep ($_->[KIND] == TM::Maplet::KIND_SIN, @{$t818->[CHARS]}) ],
	       [ qw (http://IamIdonquichote.com2/ t0820) ]), 't0818 indicators merged');
  
}

__END__

{
  my $tm = new TM (consistency => { merge => [ TM->Subject_based_Merging ] },
                   tau         => "file:".mk_file (qw(t0815 t0816)));
##  warn Dumper $tm;
  is (scalar $tm->toplets,                    $npt+7, 'merge all topics');
}


__END__

#-- check setting of consistency ---------------------------------------------------------

#-- detect erroneous situations -----------------------------------------------------

eval {
  my $tm = new TM (consistency => { merge => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ] },
		   tau         => "file:".mk_file (qw(t0823 t0816)));
  warn Dumper $tm;
}; like ($@, qr/incompatible/i, 'detected duplicate resourceRef');


__END__

#-- check result details of merging ------------------------------------------------
{
  my @topics = qw (t0820 t0817 t0816 t0818);
  my $astma = join ("", (map { eval "\$$_" } @topics));
  my $tm = new XTM (consistency => { merge => [ 'all' ]},
		    tie => new XTM::AsTMa (auto_complete => 0,
					   text => $astma));
  
  is (@{$tm->topics()},                    1, 'merge all topics');
  foreach (@topics) {
    is (@{$tm->topics("id regexps /$_/")}, 1, "find $_");
  }
  cmp_ok ($tm->topic ($topics[0]), '==', $tm->topic ($topics[-1]), 'same topic references');

  foreach my $t (@topics) {
    my $a = eval "\$$t";
    while ($a =~ s/bn.*?:\s*(.+?)\n//s) {
      my $bn = $1;
      is (scalar @{$tm->topics ("baseName regexps /$bn/")}, 1, "merged baseName '$bn'");
    }
  }


  my $t = $tm->topic ($topics[0]);
#print Dumper $t;
  ok (defined $t, 'topic id is still t0820');

  is (scalar @{$t->occurrences}, 3, 'eliminated occurrences');
  is (scalar @{$t->baseNames},   4, 'eliminated baseNames');
  is (scalar @{$t->instanceOfs}, 4, 'eliminated instanceOfs');
  is (scalar @{$t->subjectIdentity->references}, 2, 'eliminated subjectIndicators');

  is ($t->subjectIdentity->resourceRef->href, 'http://www.0815.com/', 'merged resourceRef');
  is (scalar @{$t->subjectIdentity->references}, 2, 'merged subjectIndicators');

  foreach my $y (qw(rumsti romsti)) {
    ok ($t->has_instanceOf ($y), "merged instanceOfs $y");
  }
  foreach (qw(rumsti romsti)) {
    is (scalar @{$tm->topics ("occurrence regexps /$_/")}, 1, "merged occurrence $_");
  }
}



my %tests = (
# TNC Based merging
	     'TNC: all 4 merged' => {
				merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
		       topics => [ qw (t0815 t0817 t0820 t0821) ],
		       result => 1,
		      },
	     'TNC: only 2 merged' => {
				merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
		       topics => [ qw (t0815 t0817 t0820) ],
		       result => 2,
		      },
	     'TNC: none merged 1 (scope mismatch)' => {
				merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
		       topics => [ qw (t0815 t0820) ],
		       result => 2,
		      },
	     'TNC: none merged 2 (scope mismatch)' => {
				merging => [ 'Id_based_Merging', 'Topic_Naming_Constraint' ],
		       topics => [ qw (t0821 t0822) ],
		       result => 2,
		      },
# Subject Based
	     'Subj: resourceRef identical' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0815 t0816) ],
		       result => 1,
		      },
	     'Subj: sharing a subjectIndicator' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0815 t0819) ],
		       result => 1,
		      },
	     'Subj: backward topicRef' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0816 t0817) ],
		       result => 1,
		      },
	     'Subj: forward topicRef' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0817 t0816) ],
		       result => 1,
		      },
	     'Subj: backward forward topicRef' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0817 t0816 t0818) ],
		       result => 1,
		      },
	     'Subj: forward backward forward topicRef' => {
				merging => [ 'Id_based_Merging', TM->Subject_based_Merging ],
		       topics => [ qw (t0820 t0817 t0816 t0818) ],
		       result => 1,
		      },
# Id Based
	     'id: all merged' => {
				merging => [ 'Id_based_Merging' ],
		       topics => [ qw (t0815 t0815a) ],
		       result => 1,
		      },
	     'id: none merged' => {
				merging => [ 'Id_based_Merging' ],
		       topics => [ qw (t0815 t0817) ],
		       result => 2,
		      },
	    );

foreach my $t (sort keys %tests) {
  my $astma = join ("", (map { eval "\$$_" } @{$tests{$t}->{topics}}));
  my $tm = new XTM (consistency => { merge => $tests{$t}->{merging} },
		    tie => new XTM::AsTMa (auto_complete => 0,
					   text => $astma));
  is (@{$tm->topics()}, $tests{$t}->{result}, $t);
#print Dumper $tm;
}

#__END__



{ # test for Jan
  my $tm = new XTM (consistency => { merge => [ TM->Subject_based_Merging ] });

  use XTM::Path;
  my $xtmp = new XTM::Path;

  my $t1 = $xtmp->create ('topic[baseNameString = "rumsti"]');
  $t1->add_defaults;
  $tm->add ($t1);
  my $t2 = $xtmp->create ('topic[baseNameString = "rumsti"]');
  $t2->add_defaults;
  $tm->add ($t2);
  is (@{$tm->topics}, 2, 'same name, no TNC, no merge');
#  print Dumper $tm;
}

#__END__


__END__