ConnectorQuery/Term.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 306 CC 31 SCAR 50.7
CC
31
Cov
0%
normalize
380.0
validate
30.0
alias
12.0
CRAP 159.7
1 package Bio::ConnectDots::ConnectorQuery::Term;
2 use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS);
3 use strict;
4 use Class::AutoClass;
5 use Bio::ConnectDots::Util;
6 @ISA = qw(Class::AutoClass);
7  
8 @AUTO_ATTRIBUTES=qw(ct_alias column cs_alias labels label_ids termlist);
9 %SYNONYMS=();
10 @OTHER_ATTRIBUTES=qw();
11 %DEFAULTS=(termlist=>[]);
12 Class::AutoClass::declare(__PACKAGE__);
13  
14 sub _init_self {
15 my($self,$class,$args)=@_;
16 return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this
17 }
18  
19 sub alias { # returns either ct or cs alias, whichever is set
20 my($self)=@_;
21 my $ct_alias=$self->ct_alias;
22 return $ct_alias if $ct_alias;
23 my $cs_alias=$self->cs_alias;
24 return $cs_alias if $cs_alias;
25 undef;
26 }
27 # Be careful with the following delegated accessors: not valid until validation time
28 sub ct {
29 my($self)=@_;
30 my $ct_alias=$self->ct_alias;
31 $ct_alias? $ct_alias->target_object: undef;
32 }
33 sub cs {
34 my($self)=@_;
35 my $ct=$self->ct;
36 return $ct->column2cs->{$self->column} if $ct;
37 my $cs_alias=$self->cs_alias;
38 # my $version=
39 return $cs_alias->target_object if $cs_alias;
40 $self->throw("Malformed Term object".value_as_string($self).": Should have been caught ealier!");
41 }
42 sub ct_name {my $ct=$_[0]->ct; $ct? $ct->name: undef;}
43 sub ct_id {my $ct=$_[0]->ct; $ct? $ct->db_id: undef;}
44 sub cs_id {$_[0]->cs->db_id;}
45 sub cs_name {$_[0]->cs->name;}
46 sub alias_name {$_[0]->alias->alias_name;}
47 sub label2labelid {$_[0]->cs->label2labelid;}
48  
49 sub normalize {
50 my($self)=@_;
51 my $termlist=$self->termlist;
52 my($ct_alias,$column,$cs_alias,$labels)=$self->get(qw(ct_alias column cs_alias labels));
53 # set components if unset, or check for equality
54 if (@$termlist==3) {
55 my $i=0;
56 $self->throw("termlist and ct_alias are inconsistent: \$termlist=".$termlist->[$i].
57 ", \$ct_alias=$ct_alias")
58 if defined $ct_alias && $termlist->[$i++] ne $ct_alias;
59 $self->throw("termlist and column are inconsistent: \$termlist=".$termlist->[$i].
60 ", \$column=$column")
61 if defined $column && $termlist->[$i++] ne $column;
62 $self->throw("termlist and labels are inconsistent: \$termlist=".$termlist->[$i].
63 ", \$labels=$labels")
64 if defined $labels && $termlist->[$i++] ne $labels;
65 my $i=0;
66 defined $ct_alias or $self->ct_alias($termlist->[$i++]);
67 defined $column or $self->column($termlist->[$i++]);
68 defined $labels or $self->labels($termlist->[$i++]);
69 } elsif (@$termlist==2) { # cs_alias.labels
70 my $i=0;
71 $self->throw("termlist and cs_alias are inconsistent: \$termlist=".$termlist->[$i].
72 ", \$cs_alias=$cs_alias")
73 if defined $cs_alias && $termlist->[$i++] ne $cs_alias;
74 $self->throw("termlist and labels are inconsistent: \$termlist=".$termlist->[$i].
75 ", \$labels=$labels")
76 if defined $labels && $termlist->[$i++] ne $labels;
77 my $i=0;
78 defined $cs_alias or $self->cs_alias($termlist->[$i++]);
79 defined $labels or $self->labels($termlist->[$i++]);
80 } elsif (@$termlist==1) { # cs_alias.<labels=cs_alias>
81 $self->throw("termlist and cs_alias are inconsistent: \$termlist=".$termlist->[0].
82 ", \$cs_alias=$cs_alias")
83 if defined $cs_alias && $termlist->[0] ne $cs_alias;
84 $self->throw("termlist and labels are inconsistent: \$termlist=".$termlist->[0].
85 ", \$labels=$labels")
86 if defined $labels && $termlist->[0] ne $labels;
87 defined $cs_alias or $self->cs_alias($termlist->[0]);
88 defined $labels or $self->labels($termlist->[0]);
89 } elsif (@$termlist>3) {
90 $self->throw("Invalid termlist ".value_as_string($termlist).
91 ": must have 0-3 elements, not ".(@$termlist+0));
92 }
93 $self->throw("Invalid term ".$self->as_string.
94 ": must have ct_alias, column, and labels or cs_alias and labels")
95 unless ((($self->ct_alias && $self->column) || $self->cs_alias) && $self->labels &&
96 !($self->column && $self->cs_alias));
97 $self->throw("Invalid term ".$self->as_string.": * can only appear as labels")
98 if grep /\*/, ($self->ct_alias,$self->cs_alias);
99 $self->throw("Invalid term ".$self->as_string.": lists can only appear as labels")
100 if grep {'ARRAY' eq ref $_} ($self->ct_alias,$self->cs_alias);
101  
102 $labels=$self->labels;
103 $self->throw("Invalid term ".$self->as_string.": no label!")
104 if !$labels || ('ARRAY' eq ref $labels && !@$labels);
105 $self->throw("Invalid term ".$self->as_string.": nested label lists are not supported")
106 if 'ARRAY' eq ref $labels && grep {'ARRAY' eq ref $_} @$labels;
107  
108 $labels=[] if $labels eq '*'; # empty labels list means '*' hereafter
109 $self->labels('ARRAY' eq ref $labels? $labels: [$labels]);
110 $self->termlist([grep {defined $_} $self->get(qw(ct_alias column cs_alias labels))]);
111 $self;
112 }
113  
114 # replace alias names with Alias objects
115 # check column and lookup ConnectorSet
116 # check labels and lookup labelids
117 sub validate {
118 my($self,$name2ct_alias,$name2cs_alias)=@_;
119 my($ct_alias,$column,$cs_alias,$labels)=$self->get(qw(ct_alias column cs_alias labels));
120 $self->ct_alias($name2ct_alias->{$ct_alias}) if $ct_alias;
121 $self->cs_alias($name2cs_alias->{$cs_alias}) if $cs_alias;
122 my $cs=$self->cs; # DO NOT MOVE UP: cs method not valid until now!
123 $self->throw("Invalid column ".$column." for ConntectorTable ".$self->ct_alias->target_name)
124 if $ct_alias && !$cs;
125
126 my $label_ids=[];
127 my $label2labelid=$self->label2labelid;
128 for my $label (@$labels) {
129 my $label_id=$label2labelid->{$label};
130 $self->throw("Invalid label $label for ConnectorSet ".$self->cs_name) unless $label_id;
131 push(@$label_ids,$label_id);
132 }
133 $self->label_ids($label_ids);
134 $self;
135 }
136  
137 sub as_string {
138 my($self)=@_;
139 join('.',map {value_as_string($_)} @{$self->termlist});
140 }
141  
142 1;
143