ConnectorQuery/Constraint.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 286 CC 33 SCAR 42.6
CC
33
Cov
0%
parse
182.0
normalize
132.0
parse_string
12.0
CRAP 71.0
1 package Bio::ConnectDots::ConnectorQuery::Constraint;
2 use strict;
3 use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS);
4 use Class::AutoClass;
5 use Bio::ConnectDots::Util;
6 use Bio::ConnectDots::Parser;
7 use Bio::ConnectDots::ConnectorQuery::Term;
8 @ISA = qw(Class::AutoClass);
9  
10 @AUTO_ATTRIBUTES=qw(_term _op constants);
11 %SYNONYMS=();
12 @OTHER_ATTRIBUTES=qw(term op ct_alias cs_alias labels labelids termlist);
13 %DEFAULTS=(_op=>'=');
14 Class::AutoClass::declare(__PACKAGE__);
15  
16 sub _init_self {
17 my($self,$class,$args)=@_;
18 return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this
19 $self->term or $self->term(new Bio::ConnectDots::ConnectorQuery::Term);
20 }
21  
22 # legal formats:
23 # 1) old form -- ARRAY or HASH of cs_alias => [label constant] or [label op constant]
24 # 2) single query string which may include multiple constraints AND'ed together
25 # 3) single Constraint object
26 # 4) ARRAY of (1) query strings and (2) Constraint objects
27  
28 sub parse {
29 my($class,$constraints)=@_;
30 my $parsed=[];
31 my $parser=new Bio::ConnectDots::Parser;
32 # ARRAY is old form if even number of elements, element 0 is scalar, element 1 is ARRAY
33 if (('ARRAY' eq ref $constraints) && @$constraints &&
34 @$constraints%2==0 && !ref $constraints->[0] && 'ARRAY' eq ref $constraints->[1]) {
35 my $hash;
36 while(@$constraints) {
37 my($alias,$constraint)=(shift @$constraints,shift @$constraints);
38 my $constraint_list=$hash->{$alias} || ($hash->{$alias}=[]);
39 push(@$constraint_list,$constraint);
40 }
41 $constraints=$hash;
42 }
43 # HASH is always old form. Old form ARRAY turned into HASH in 'if' above
44 # Note 'if' -- not 'elsif'
45 if ('HASH' eq ref $constraints) {
46 while (my($cs_alias,$constraint_list)=each %$constraints) {
47 $constraint_list=[$constraint_list] unless 'ARRAY' eq ref $constraint_list->[0];
48 for my $constraint (@$constraint_list) {
49 my($labels,$op,$constant);
50 $class->throw("Illegal constraint format ".value_as_string($constraint).
51 ": must have 2 or 3 elements")
52 unless @$constraint==2 || @$constraint==3;
53 ($labels,$constant)=@$constraint if @$constraint==2;
54 ($labels,$op,$constant)=@$constraint if @$constraint==3;
55 $constant=$parser->parse_constant_value($constant); # handle constant lists
56 push(@$parsed,
57 $class->new(-termlist=>[$cs_alias,$labels],-op=>$op,-constant=>$constant));
58 }
59 }
60 } elsif (!ref $constraints) { # string
61 push(@$parsed,$class->parse_string($constraints,$parser));
62 } elsif (UNIVERSAL::isa($constraints,__PACKAGE__)) {
63 push(@$parsed,$constraints);
64 } elsif ('ARRAY' eq ref $constraints) { # new form ARRAY
65 for my $constraint (@$constraints) {
66 if (!ref $ $constraint) {
67 push(@$parsed,$class->parse_string($constraint,$parser));
68 } elsif (UNIVERSAL::isa($constraint,__PACKAGE__)) {
69 push(@$parsed,$constraint);
70 } else {
71 $class->throw("llegal constraint format ".value_as_string($constraint).
72 ": must be string or Constraint object to appear in new ARRAY format");
73 }
74 }
75 } else {
76 $class->throw("Unrecognized constraint form ".value_as_string($constraints).
77 ": strange type! Not scalar, Constraint object, ARRAY, or HASH");
78 }
79 wantarray? @$parsed: $parsed
80 }
81 sub parse_string {
82 my($class,$constraints,$parser)=@_;
83 my $parsed=[];
84 my $parsed_constraints=$parser->parse_constraints($constraints);
85 if ($parsed_constraints) {
86 for my $constraint (@$parsed_constraints) {
87 my($term,$op,$constant)=@$constraint{qw(term op constant)};
88 push(@$parsed,
89 $class->new(-termlist=>$term,-op=>$op,-constants=>$constant));
90 }
91 }
92 wantarray? @$parsed: $parsed;
93 }
94  
95 sub normalize {
96 my($self)=@_;
97 $self->term->normalize;
98 my $op=$self->op;
99 my $constants=$self->constants;
100 $op or $op=$constants? '=': 'EXISTS';
101  
102 if ('ARRAY' eq ref $constants) {
103 $self->throw("Invalid constraint".$self->as_string.": nested list constants are not supported")
104 if grep {'ARRAY' eq ref $_} @$constants;
105 $self->throw("Invalid constraint".$self->as_string.": empty list constants are not supported")
106 unless @$constants;
107 # normalize ops with list constants
108 if ($op eq '=') {
109 $self->op('IN');
110 } elsif ($op eq "!=") {
111 $self->op('NOT IN');
112 } elsif ($op=~/</) { # range op: just compare to end of range
113 my $max=maxb(@$constants); # does numeric or alpha max as appropriate
114 $self->constants([$max]);
115 } elsif ($op=~/>/) { # range op: just compare to end of range
116 my $min=minb(@$constants); # does numeric or alpha min as appropriate
117 $self->constants([$min]);
118 }
119 } elsif (!ref $constants) { # change single value to list
120 $self->throw("Invalid constraint".$self->as_string.": no constant provided")
121 unless $op eq 'EXISTS' || defined $constants;
122 $constants=$self->constants([$constants]);
123 } else {
124 $self->throw("Invalid constraint".$self->as_string.": strange type!");
125 }
126 $self;
127 }
128  
129 sub validate {
130 my($self,$name2ct_alias,$name2cs_alias)=@_;
131 $self->term->validate($name2ct_alias,$name2cs_alias);
132 $self;
133 }
134  
135 sub term {
136 my $self=shift @_;
137 my $term=@_? $self->_term($_[0]): $self->_term;
138 $term or $term=$self->_term(new Bio::ConnectDots::ConnectorQuery::Term);
139 $term;
140 }
141 sub op {
142 my $self=shift @_;
143 my $op=@_? $self->_op($_[0]): $self->_op;
144 $op or $op='=';
145 $op;
146 }
147 sub ct_alias {
148 my $self=shift @_;
149 my $ct_alias=@_? $self->term->ct_alias($_[0]): $self->term->ct_alias;
150 $ct_alias;
151 }
152 sub cs_alias {
153 my $self=shift @_;
154 my $cs_alias=@_? $self->term->cs_alias($_[0]): $self->term->cs_alias;
155 $cs_alias;
156 }
157 sub ct {$_[0]->term->ct;}
158 sub cs {$_[0]->term->cs;}
159 sub cs_id {$_[0]->term->cs_id;}
160 sub alias {$_[0]->term->alias;} # returns either ct or cs alias, whichever is set
161 sub alias_name {$_[0]->term->alias_name;}
162 sub labels {
163 my $self=shift @_;
164 my $labels=@_? $self->term->labels($_[0]): $self->term->labels;
165 $labels;
166 }
167 sub label_ids {
168 my $self=shift @_;
169 my $label_ids=@_? $self->term->label_ids($_[0]): $self->term->label_ids;
170 $label_ids;
171 }
172 sub termlist {
173 my $self=shift @_;
174 my $termlist=@_? $self->term->termlist($_[0]): $self->term->termlist;
175 $termlist;
176 }
177  
178  
179 sub as_string {
180 my($self)=@_;
181 my $term=$self->term->as_string;
182 my $op=$self->op;
183 my $constants=value_as_string($self->constants);
184 return "$term $op $constants";
185 }
186  
187 1;
188