~ubuntu-branches/ubuntu/trusty/horae/trusty

« back to all changes in this revision

Viewing changes to 0CPAN/STAR-Parser-0.59/lib/STAR/Checker.pm

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package STAR::Checker;
2
 
 
3
 
use STAR::DataBlock;
4
 
use STAR::Dictionary;
5
 
 
6
 
use strict;
7
 
use Time::localtime;
8
 
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
9
 
 
10
 
$VERSION = '0.02';
11
 
 
12
 
#  $Id: Checker.pm,v 1.2 2000/12/19 22:54:56 helgew Exp $   RCS Identification
13
 
 
14
 
 
15
 
####################
16
 
# Constructor: new #
17
 
####################
18
 
 
19
 
sub new {
20
 
    my $proto = shift;
21
 
    my $class = ref($proto) || $proto;
22
 
    my $self = {};
23
 
    bless ($self,$class);
24
 
    return $self;
25
 
}
26
 
 
27
 
 
28
 
#######################
29
 
# Class method: check #
30
 
#######################
31
 
 
32
 
sub check {
33
 
 
34
 
    my ($self, @parameters) = @_;                 
35
 
    my ($data,$dict,$options);
36
 
    $options = "";
37
 
        
38
 
    while ($_ = shift @parameters) {
39
 
       $data = shift @parameters if /-datablock/;
40
 
       $dict = shift @parameters if /-dictionary/;
41
 
       $options = shift @parameters if /-options/;
42
 
    }
43
 
 
44
 
    my ($n, $d, $save, @saves, $cat, @cats, $item, @items);
45
 
    my (@depend_items, $depend_item);
46
 
    my ($value, @values);
47
 
    my (%dict_lookup, %item_lookup, %cat_lookup);
48
 
    my (@parent_items, @child_items);
49
 
    my (%cp_hash, $cp_hash_ref);   #child parent hash
50
 
    my ($mand);
51
 
    my ($construct, @constructs, $code, @code_data, @codes, %item_types);
52
 
    my ($debug, $log,$problem);
53
 
 
54
 
    $log = 1       if $options =~ /l/;
55
 
    $debug = 1     if $options =~ /d/;
56
 
    
57
 
    if ( $data->type eq 'dictionary' ) {
58
 
        print STDERR "Method check_against_dict is to be invoked only on\n",
59
 
          "DataBlock objects, not on dictionaries themselves.\n";
60
 
        return;
61
 
    }
62
 
 
63
 
    print STDERR "-"x50,"\n" if $log;
64
 
    print STDERR "$0 ", ctime(),"\n" if $log;
65
 
    print STDERR "Checking ",$data->title,
66
 
      " against ",$dict->title,"\n" if $log;
67
 
 
68
 
    @items = $data->get_items;
69
 
    @cats = $data->get_categories;
70
 
    @saves = $dict->get_save_blocks;
71
 
 
72
 
    #make a dictionary lookup hash -- keys: lowercase, values: original case
73
 
    foreach $save (@saves) {
74
 
        $dict_lookup{lc($save)} = $save;
75
 
    }
76
 
 
77
 
    #same for an file item lookup hash
78
 
    foreach $item (@items) {
79
 
        $item_lookup{lc($item)} = $item;
80
 
    }
81
 
 
82
 
    #same for a file category lookup hash
83
 
    foreach $cat (@cats) {
84
 
        $cat_lookup{lc($cat)} = $cat;
85
 
    }
86
 
 
87
 
    # 1) checking whether items are present in dictionary
88
 
    # ---------------------------------------------------
89
 
 
90
 
    print STDERR "Checking whether items are present in dictionary\n" if $log;
91
 
 
92
 
    foreach $item (@items) {
93
 
        if ( ! exists $dict_lookup{lc($item)} ) {
94
 
            $problem=1;
95
 
            print STDERR "\t$item not in dictionary\n" if $log;
96
 
        }
97
 
    }
98
 
  
99
 
    # 2) checking for presence of mandatory items in file
100
 
    # ---------------------------------------------------
101
 
 
102
 
    print STDERR "Checking whether mandatory items ",
103
 
     "are present in file\n" if $log;
104
 
 
105
 
    foreach $save ( @saves ) {
106
 
        if ( $save =~ /^(_\S+?)\.\S+/ ) { # $save is item, not cat 
107
 
            $cat = $1;
108
 
            $item = $save;
109
 
            $mand = ($dict->get_item_data(-save=>$save,
110
 
                                 -item=>"_item.mandatory_code"))[0];
111
 
            if ( $mand eq "yes" ) {  #item is mandatory
112
 
                if ( exists $cat_lookup{lc($cat)} ) { #the cat is in the file
113
 
                    if ( ! exists $item_lookup{lc($item)} ) { #oops, should've
114
 
                                                             #been present
115
 
                        $problem=1;
116
 
                        print STDERR "\t$item not present\n" if $log;
117
 
                    }
118
 
                }
119
 
            }
120
 
        }
121
 
    } 
122
 
    
123
 
    # 3) checking for presence of dependent items in file
124
 
    # ---------------------------------------------------
125
 
 
126
 
    print STDERR "Checking whether dependent items",
127
 
     " are present in file\n" if $log;
128
 
 
129
 
    foreach $item ( @items ) {
130
 
        if ( exists $dict_lookup{lc($item)} ) {
131
 
            @depend_items = $dict->get_item_data(
132
 
                              -save=>$dict_lookup{lc($item)},
133
 
                              -item=>"_item_dependent.dependent_name");
134
 
            foreach $depend_item ( @depend_items ) {
135
 
                if ( ! exists $item_lookup{lc($depend_item)} ) {
136
 
                    $problem=1;
137
 
                    print STDERR "\t$depend_item not present ",
138
 
                      "(required by $item)\n" if $log;
139
 
                }
140
 
            }
141
 
        }
142
 
    }
143
 
 
144
 
    # 4) checking for presence of parent items
145
 
    # ----------------------------------------
146
 
 
147
 
    print STDERR "Checking for presence of parent items\n" if $log;
148
 
 
149
 
    if ( -r "cp_hash" ) {
150
 
        print "Retrieving previously stored cp_hash\n" if $log;
151
 
        $cp_hash_ref = Storable::retrieve("cp_hash");
152
 
        %cp_hash = %$cp_hash_ref;
153
 
    }
154
 
    else {
155
 
        print "Assembling and storing new cp_hash\n" if $log;
156
 
        foreach $save ( @saves ) {
157
 
            @parent_items  = $dict->get_item_data(-save=>$save,
158
 
                                      -item=>"_item_linked.parent_name");
159
 
            @child_items   = $dict->get_item_data(-save=>$save,
160
 
                                      -item=>"_item_linked.child_name");
161
 
            if ( $#parent_items >=0 ) {
162
 
                foreach $n ( 0..$#parent_items ) {
163
 
                    $cp_hash{lc($child_items[$n])} = lc($parent_items[$n]);
164
 
                }
165
 
            }
166
 
        }
167
 
        Storable::store \%cp_hash, "cp_hash";
168
 
    }
169
 
 
170
 
    foreach $item ( @items ) {
171
 
        if ( exists $cp_hash{lc($item)} ) {
172
 
            if ( ! exists $item_lookup{$cp_hash{lc($item)}} ) {
173
 
                print STDERR "\t",$cp_hash{lc($item)}, " not present ",
174
 
                  "(parent to $item)\n" if $log;
175
 
            }
176
 
        }
177
 
    }
178
 
    
179
 
    # 5) checking for correct item types
180
 
    # ----------------------------------
181
 
 
182
 
    print STDERR "Checking values against type definitions\n" if $log;
183
 
 
184
 
    @constructs=$dict->get_item_data(-save=>'-',
185
 
                                     -item=>'_item_type_list.construct');
186
 
    @codes=$dict->get_item_data(-save=>'-',
187
 
                                -item=>'_item_type_list.code');
188
 
    foreach $n (0..$#codes) {
189
 
        $item_types{$codes[$n]} = $constructs[$n];
190
 
    }
191
 
 
192
 
    foreach $item ( @items ) {
193
 
        $code="";
194
 
        print STDERR "data item: $item\n" if $debug;
195
 
        print STDERR "dict item: ",$dict_lookup{lc($item)},"\n" if $debug;
196
 
        if ($dict_lookup{lc($item)}) {
197
 
            $code = ($dict->get_item_data
198
 
                          (-save=>$dict_lookup{lc($item)},
199
 
                           -item=>'_item_type.code'))[0];
200
 
                     # not all items have this defined
201
 
            $construct = $item_types{$code} if $code;
202
 
        }
203
 
                
204
 
        if ( !$code ) {
205
 
            print STDERR "type code undefined\n" if $debug;
206
 
        }
207
 
        else {
208
 
            @values = $data->get_item_data(-item=>$item);
209
 
            print STDERR "values 0..",$#values,"\n" if $debug;
210
 
            $n=0;
211
 
            foreach $value (@values) {
212
 
                if ( $value eq '.' || $value eq '?' ) {
213
 
                    print STDERR "$n item value undefined\n" if $debug;
214
 
                }
215
 
                elsif ( $value =~ /^$construct$/ ) {
216
 
                    print STDERR "$n type $code ok\n" if $debug;
217
 
                }              
218
 
                else {
219
 
                    $problem = 1;
220
 
                    if ($log) {
221
 
                        print STDERR "\t","-"x14,"\n","\ttype mismatch:\n"; 
222
 
                        print STDERR "\titem: $item\n";
223
 
                        print STDERR "\titeration: $n\n";
224
 
                        print STDERR "\tvalue: $value\n";
225
 
                        print STDERR "\tcode: $code\n";
226
 
                        print STDERR "\tconstruct: $construct\n";
227
 
                    }
228
 
                }
229
 
            $n++;
230
 
            }
231
 
        }
232
 
    }
233
 
    return ( $problem ? 0 : 1 );  #returns 1 if check ok (no problem)
234
 
                                  #returns 0 if problem found
235
 
}
236
 
 
237
 
1;
238
 
__END__
239
 
 
240
 
=head1 NAME
241
 
 
242
 
STAR::Checker - Perl extension for checking DataBlock objects
243
 
 
244
 
=head2 Version
245
 
 
246
 
This documentation refers to version 0.02 of this module.
247
 
 
248
 
=head1 SYNOPSIS
249
 
 
250
 
  use STAR::Checker;
251
 
 
252
 
  $check = STAR::Checker->check( -datablock=>$ARGV[0],
253
 
                                 -dictionary=>$ARGV[1] );
254
 
 
255
 
=head1 DESCRIPTION
256
 
 
257
 
Contains the checker object, with methods for checking DataBlock object against 
258
 
STAR rules and against a specified dictionary.
259
 
DataBlock objects are created by Parser and modified by DataBlock.
260
 
 
261
 
=head1 CLASS METHODS
262
 
 
263
 
=head2 check
264
 
 
265
 
  Usage:   $check = STAR::Checker->check(-datablock=>$data, 
266
 
                                         -dictionary=>$dict [,
267
 
                                         -options=>$options ] );
268
 
 
269
 
Checks the DataBlock object C<$data> against the dictionary object 
270
 
C<$dict> (see STAR::Parser and STAR::DataBlock). Checks 1) whether 
271
 
all items in the DataBlock are defined in the dictionary, 
272
 
2) whether mandatory items are present in the file, 3) whether dependent 
273
 
items are present in the file (e.g. cartn_x makes cartn_y and cartn_z 
274
 
dependent), 4) whether parent items are present,  
275
 
and 5) whether the item values in the DataBlock conform to the item type 
276
 
definitions in the dictionary.
277
 
 
278
 
Returns 1 if the check was successful (no problems were found), 
279
 
and 0 if the check was unsuccessful (problems were found). 
280
 
A list of the specific problems is written to STDERR when C<-options=E<gt>'l'> 
281
 
is specified.
282
 
 
283
 
=head1 AUTHOR
284
 
 
285
 
Wolfgang Bluhm, mail@wbluhm.com
286
 
 
287
 
=head2 Acknowledgments
288
 
 
289
 
Thanks to Phil Bourne, Helge Weissig, Anne Kuller, Doug Greer, 
290
 
Michele Bluhm, and others for support, help, and comments.
291
 
 
292
 
=head1 COPYRIGHT
293
 
 
294
 
A full copyright statement is provided with the distribution
295
 
Copyright (c) 2000 University of California, San Diego
296
 
 
297
 
=head1 SEE ALSO
298
 
 
299
 
STAR::Parser, STAR::DataBlock, STAR::Dictionary.
300
 
 
301
 
=cut