File: 012analysis.t

package info (click to toggle)
libtm-perl 1.56-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,852 kB
  • sloc: perl: 35,234; sh: 565; makefile: 47
file content (465 lines) | stat: -rw-r--r-- 10,740 bytes parent folder | download | duplicates (3)
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
#-- test suite

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;
}

use Class::Trait;

use constant DONE => 1;

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

require_ok ('TM::Tree');
require_ok ('TM::Graph');
require_ok ('TM::Analysis');

if (DONE) { # tree
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
					  inline => '
(begets)
parent: adam eve
child: cain

(begets)
parent: adam eve
child: abel

(begets)
parent: adam eve
child: seth

(begets)
parent: adam eve
child: azura

#--

(begets)
parent: cain
child: enoch

#--

(begets)
parent: enoch
child: irad

#--

(begets)
parent: irad
child: mehajael

#--

(begets)
parent: seth
child: enosh

(begets)
parent: seth
child: noam

   ');

    can_ok 'TM::Tree',     'apply';
    can_ok 'TM::Graph',    'apply';
    can_ok 'TM::Analysis', 'apply';

    ok 'TM::Tree'->apply ($tm), '....applying to an instance';

#    Class::Trait->apply ($tm => 'TM::Tree'); # this makes perl 5.10 choke
    $tm->sync_in;

#warn Dumper $tm;

  my $pedigree =  $tm->tree ($tm->mids ('adam',
                                       'begets',
                                       'parent',
                                       'child')
                                        );

#warn Dumper $pedigree;

  ok ($pedigree->{lid} eq 'tm:adam', 'tree');
  ok (eq_set([ map { $_->{lid} } @{$pedigree->{children}} ],
             [ 'tm:cain', 'tm:abel', 'tm:azura', 'tm:seth' ]), 'tree');
  my ($seth) = grep ($_->{lid} eq 'tm:seth', @{$pedigree->{children}});
  ok (eq_set([ map { $_->{lid} } @{$seth->{children}} ],
             [ 'tm:noam', 'tm:enosh' ]), 'tree');

  my $pedigree2 =  $tm->tree_x ($tm->mids ('adam',
                                           'begets',
                                           'parent',
                                           'child')
                                           );

#warn Dumper $pedigree2;

    use Test::Deep::NoTest;
    eq_deeply( $pedigree2, $pedigree, "tree = tree_x" );
}

if (DONE) { # taxonomy
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
					  inline => '
aaa subclasses thing

bbb subclasses thing

ccc subclasses aaa

ddd subclasses aaa

');
    'TM::Tree'->apply ($tm);
    $tm->sync_in;

#warn Dumper $tm;

    my $taxo =  $tm->taxonomy ($tm->mids ('aaa'));

#warn Dumper $taxo;

    ok ($taxo->{lid} eq 'tm:aaa',                'taxo 1');
    ok (eq_set([ map { $_->{lid} } @{$taxo->{children}} ],
	       [ 'tm:ccc', 'tm:ddd' ]),          'taxo 2');

    $taxo =  $tm->taxonomy;
#warn Dumper $taxo;
    ok ($taxo->{lid} eq 'thing',                 'taxo 3');
    ok (eq_set([ map { $_->{lid} } @{$taxo->{children}} ],
	       [ 'tm:aaa', 'tm:bbb' ]),          'taxo 4');
    my ($aaa) = grep ($_->{lid} eq 'tm:aaa', @{$taxo->{children}});
    ok (eq_set([ map { $_->{lid} } @{$aaa->{children}} ],
	       [ 'tm:ccc', 'tm:ddd' ]),          'taxo 5');

}

if (DONE) { # clustering
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
					  inline => '
# cluster 1
aaa subclasses bbb

bbb is-a ccc

(ddd)
eee: fff
ggg: hhh
iii: ccc

#------------------------------------
# cluster 2
zzz subclasses yyy

zzz subclasses xxx

www is-a zzz

(vvv)
uuu: rrr
sss: ttt
qqq: www
    ');
    $tm->sync_in;

#warn Dumper $tm; exit;

    'TM::Graph'->apply ($tm);

    my $clusters = $tm->clusters (use_lid => 0);
#    foreach (@$clusters) {
#	print "we are connnected: ", join (",", @$_), "\n\n";
#    }

    my ($c1) = grep (grep ($_ eq 'tm:aaa', @$_), @$clusters);
    ok (eq_set ($c1,  [
    'tm:fff',
    'tm:aaa',
    'tm:bbb',
    'tm:hhh',
    'tm:ccc'
    ]), 'cluster 1');
    my ($c2) = grep (grep ($_ eq 'tm:www', @$_), @$clusters);
    ok (eq_set ($c2,  [
    'tm:www',
    'tm:yyy',
    'tm:xxx',
    'tm:ttt',
    'tm:rrr',
    'tm:zzz'
    ]), 'cluster 2');
    my ($c3) = grep (grep ($_ eq 'tm:sss', @$_), @$clusters);
    ok (eq_set ($c3, [    'tm:sss',  ]), 'cluster 3');

    $clusters = $tm->clusters (use_roles => 1, use_type => 1, use_lid => 0);
#warn Dumper $clusters;
    my ($c4) = grep (grep ($_ eq 'tm:aaa', @$_), @$clusters);

    is (scalar @$c4, 33, 'cluster 4');
}

if (DONE) { # graph
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
					  inline => '

# this test data has NOTHING todo with Bond University
# any similarity with morons anywhere is completely coinci-whatever

adam (human)

(is-subclass-of)
subclass: halb-lustiger
superclass: bio-unit

(is-subclass-of)
subclass: human
superclass: halb-lustiger

(begets)
parent: adam eve
child: cain

(bigots)
parent: adam
child: candrews

candrews (halb-lustiger)

(forgets)
forgetter: candrews
fact: everything

(forgets)
forgetter: everything
fact: nothing

(begets)
parent: adam eve
child: abel

(bigots)
parent: abel
child: imorriso

imorriso (halb-lustiger)

(begets)
parent: adam eve
child: seth

(begets)
parent: adam eve
child: azura

#--

(begets)
parent: cain
child: enoch

#--

(begets)
parent: enoch
child: irad

#--

(begets)
parent: irad
child: mehajael

#--

(begets)
parent: seth
child: enosh

(begets)
parent: seth
child: noam

   ');

    'TM::Graph'->apply ($tm);
    $tm->sync_in;

    ok ($tm->is_path ([ 'adam' ],  [ [ 'isa' ] ], 'human'), 'adam is human');
    ok ($tm->is_path ([ 'adam' ],  [ [ 'isa' ] ], 'halb-lustiger'), 'adam is halb-lustiger');
    ok ($tm->is_path ([ 'human' ], [ [ 'iko' ] ], 'bio-unit'), 'humans are bio units');

    my %paths = (
                  'cain'  => [ [ 'begets' ] ],
                  'enoch' => [ [ 'begets' ], [ 'begets' ] ],
                  'irad'  => [ [ 'begets' ], [ 'begets' ], [ 'begets' ] ],
# test begets*
                  'irad'  => (bless [ [ 'begets' ] ], '*'),
                  'noam'  => (bless [ [ 'begets' ] ], '*'),
# test begets | bigots
                  'candrews' => [ [ 'begets', 'bigots' ] ],
                  'imorriso' => (bless [ [ 'begets', 'bigots' ] ], '*'),

                  'everything' => [ [ 'bigots' ], [ 'forgets' ] ],
                  'nothing'    => [ [ 'bigots' ], [ 'forgets' ], [ 'forgets' ] ],
                  'nothing'    => [ [ 'bigots' ], [ bless [ [ 'forgets' ] ],'*' ] ],
                );


   foreach my $p (keys %paths) {
      next if $p =~ /\*/;
      ok ($tm->is_path ([ 'adam' ], $paths{$p}, $p),  'stories which start bad, end bad '. Dumper $paths{$p});
   }

#warn Dumper $tm;

   my @ns = $tm->neighborhood (0, [ 'adam' ]);
   ok (eq_set ([ 'tm:adam' ], [ map { $_->{end} } @ns ]), '0-length neighborhood');
   is (scalar @{ $ns[0]->{path} }, 0, '0-length neighborhood path');

      @ns = $tm->neighborhood (1, [ 'adam' ]);
      my %expect1 = (
              'tm:human'   => 'isa',
	      'tm:eve'     => 'tm:begets',
	      'tm:cain'    => 'tm:begets',
	      'tm:candrews'=> 'tm:bigots',
	      'tm:abel'    => 'tm:begets',
	      'tm:seth'    => 'tm:begets',
	      'tm:azura'   => 'tm:begets',
      );

      is (scalar @ns, 8, "1-length neighborhood");
      foreach my $t (keys %expect1) {
         my ($n) = grep { $_->{end} eq $t } @ns;
         ok (defined $n, "1-length neighborhood $t detected ");
         ok (eq_array ($n->{path}, [ $expect1{$t} ]), "1-length neighborhood path");
      }


#      ok (eq_set (
##warn Dumper
#             [ { path => [],
#                 end  => 'tm:adam' },
#               map { { path => [ $expect1{$_} ], 
##                       end  => $_ } } keys %expect1 ],
#             [@ns]),'1-length neighborhood')
#;

#warn Dumper \@ns;

}

if (DONE) { # statistics
    use TM;
    my $tm1 = new TM;
    'TM::Analysis'->apply ($tm1);
    'TM::Graph'->apply ($tm1);

    my $stats1 = $tm1->statistics;
#warn Dumper $stats1;

    is ($stats1->{'nr_asserts'},  8, 'nr_asserts');
    is ($stats1->{'nr_toplets'},  scalar $tm1->toplets, 'nr_toplets');
    is ($stats1->{'nr_clusters'}, 15, 'nr_clusters');

    use TM::Materialized::AsTMa;
    my $tm2 = new TM::Materialized::AsTMa (baseuri => 'tm:',
					   inline => '
aaa subclasses bbb

bbb is-a ccc
    ');
    $tm2->sync_in;
#warn Dumper $tm2;

    'TM::Analysis'->apply ($tm2);
    my $stats2 = $tm2->statistics;
#warn Dumper $stats;

    is ($stats2->{'nr_asserts'},  $stats1->{'nr_asserts'} + 2,     'nr_asserts');
    is ($stats2->{'nr_toplets'},  $stats1->{'nr_toplets'} + 3,     'nr_toplets');
    is ($stats2->{'nr_clusters'}, 15,                              'nr_clusters');


}

if (DONE) { # orphanage
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
					  inline => '
aaa subclasses bbb

bbb is-a ccc
    ');
    $tm->sync_in;
    'TM::Analysis'->apply ($tm);

    my $o = $tm->orphanage;
#warn Dumper $o;

    ok (!grep ($_ eq 'tm:bbb', @{$o->{untyped}}),        'bbb is not untyped');
    ok ( grep ($_ eq 'tm:ccc', @{$o->{untyped}}),        'ccc is     untyped');

    ok (!grep ($_ eq 'tm:ccc', @{$o->{empty}}),          'ccc is not untyped');
    ok ( grep ($_ eq 'tm:aaa', @{$o->{empty}}),          'aaa is     untyped');

    ok (!grep ($_ eq 'tm:aaa', @{$o->{unclassified}}),   'aaa is not unclassified');
    ok ( grep ($_ eq 'tm:ccc', @{$o->{unclassified}}),   'ccc is     unclassified');

    ok (!grep ($_ eq 'tm:bbb', @{$o->{unspecified}}),    'bbb is not unspecified');
    ok ( grep ($_ eq 'tm:aaa', @{$o->{unspecified}}),    'aaa is     unspecified');

    $o = TM::Analysis::orphanage ($tm, 'untyped');
    ok ($o->{untyped}, 'only untyped');
}

if (DONE) {
    use TM::Materialized::AsTMa;
    my $tm = new TM::Materialized::AsTMa (baseuri => 'tm:',
                                          inline => '
aaa subclasses bbb

bbb is-a ccc

    ')->sync_in;

    $tm->assert (Assertion->new (type => 'seldom',   roles => [ 'xxx' ], players => [ 'ramsti'.$_ ]))
       for (0..0);
    $tm->assert (Assertion->new (type => 'frequent', roles => [ 'xxx' ], players => [ 'ramsti'.$_ ]))
       for (0..10);
    $tm->assert (Assertion->new (type => 'inflated', roles => [ 'xxx' ], players => [ 'ramsti'.$_ ]))
       for (0..100);

    'TM::Analysis'->apply ($tm);
    my $E = $tm->entropy;

    ok (! (grep { ! $E->{$_} } map { $_->[TM->TYPE] } $tm->asserts), 'any assoc type missing');

    ok (! (grep { $_ < 0 }    values %$E), 'all > 0');
    ok (! (grep { $_ > 0.37 } values %$E), 'all < 0.37');

    ok ($E->{'tm:seldom'}   < $E->{'tm:frequent'}, 'seldom   < frequent');
    ok ($E->{'tm:inflated'} < $E->{'tm:frequent'}, 'inflated < frequent');
#warn Dumper $E;
}


__END__