| 1 | #!perl | ||||||||||||
| 2 | |||||||||||||
| 3 | # ########################################################################## # | ||||||||||||
| 4 | # Title: Type specification | ||||||||||||
| 5 | # Creation date: 2007-03-05 | ||||||||||||
| 6 | # Author: Michael Zedeler | ||||||||||||
| 7 | # Description: Class holding type specifications for data streams | ||||||||||||
| 8 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/TypeSpec.pm,v $ | ||||||||||||
| 9 | # Repository: kronhjorten | ||||||||||||
| 10 | # State: $State: Exp $ | ||||||||||||
| 11 | # Documentation: inline | ||||||||||||
| 12 | # Recepient: - | ||||||||||||
| 13 | # ########################################################################## # | ||||||||||||
| 14 | |||||||||||||
| 15 | package DS::TypeSpec; | ||||||||||||
| 16 | |||||||||||||
| 17 | 12 | ▶ | use base qw{ Clone }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 12 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 12 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 12 | ▶ | use Carp::Assert; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 12 | ▶ | use List::MoreUtils qw{ any all }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 12 | ▶ | use DS::TypeSpec::Field; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 12 | our ($VERSION) = $DS::VERSION; | |||||||||||
| 26 | 12 | our ($REVISION) = '$Revision: 1.2 $' =~ /(\d+\.\d+)/; | |||||||||||
| 27 | |||||||||||||
| 28 | |||||||||||||
| 29 | 29 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 30 | 29 | my( $class, $arg1, $arg2 ) = @_; | |||||||||||
| 31 | |||||||||||||
| 32 | 29 | my $name; | |||||||||||
| 33 | my $fields; | ||||||||||||
| 34 | 29 | ▶ | if( $arg1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 23 | ▶ | if( ref( $arg1 ) eq '' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 23 | $name = $arg1; | |||||||||||
| 37 | 23 | ▶ | if( $arg2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 19 | $fields = $arg2; | |||||||||||
| 39 | } | ||||||||||||
| 40 | } else { | ||||||||||||
| 41 | 0 | $fields = $arg1; | |||||||||||
| 42 | } | ||||||||||||
| 43 | 23 | ▶ | if( $fields ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 19 | should(ref($fields) , 'ARRAY'); | |||||||||||
| 45 | } | ||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 29 | ▶ | my $self = bless { | ||||||||||
|
Condition100%2/2
$name || ''
MC/DC100%1/1
$name || ''
$name
Truth table100%2/2
A
A= $name
| |||||||||||||
| 49 | name => $name || '', | ||||||||||||
| 50 | fields => {} | ||||||||||||
| 51 | }, $class; | ||||||||||||
| 52 | |||||||||||||
| 53 | 29 | ▶ | if( $fields ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 19 | $self->add_fields( $fields ); | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 29 | return $self; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 19 | ▶ | sub add_fields { | ||||||||||
|
Subroutine
add_fields: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 61 | 19 | my( $self, $fields ) = @_; | |||||||||||
| 62 | |||||||||||||
| 63 | 19 | foreach my $field (@$fields) { | |||||||||||
| 64 | 50 | $self->add_field( $field ); | |||||||||||
| 65 | } | ||||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 56 | ▶ | sub add_field { | ||||||||||
|
Subroutine
add_field: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||||
| 69 | 56 | my( $self, $field ) = @_; | |||||||||||
| 70 | |||||||||||||
| 71 | 56 | ▶ | if( ref( $field ) eq '' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | $field = new DS::TypeSpec::Field( $field ); | |||||||||||
| 73 | } | ||||||||||||
| 74 | 56 | assert($field->isa('DS::TypeSpec::Field')); | |||||||||||
| 75 | 56 | ▶ | if( $self->{fields}->{ $field->{name} } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | croak("Can't add field to data stream type spec, since another field with the same name already exists"); | |||||||||||
| 77 | } else { | ||||||||||||
| 78 | 56 | $self->{fields}->{ $field->{name} } = $field; | |||||||||||
| 79 | } | ||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | ▶ | sub remove_fields { | ||||||||||
|
Subroutine
remove_fields: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 83 | 0 | my( $self, $fields ) = @_; | |||||||||||
| 84 | |||||||||||||
| 85 | 0 | foreach my $field (@$fields) { | |||||||||||
| 86 | 0 | $self->remove_field( $field ); | |||||||||||
| 87 | } | ||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub remove_field { | ||||||||||
|
Subroutine
remove_field: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 91 | 0 | my( $self, $field ) = @_; | |||||||||||
| 92 | |||||||||||||
| 93 | 0 | my $field_name; | |||||||||||
| 94 | 0 | ▶ | if( not ref($field) eq '' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | should($field->isa, 'DS::TypeSpec::Field'); | |||||||||||
| 96 | 0 | $field_name = $field->{name}; | |||||||||||
| 97 | } else { | ||||||||||||
| 98 | 0 | $field_name = $field; | |||||||||||
| 99 | } | ||||||||||||
| 100 | 0 | ▶ | if( not $self->{fields}->{ $field->{name} } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | croak("Can't remove field from data stream type spec - name not recognized. The name is $field_name, but I only have " . join(", ", keys %{$self->{fields}})); | |||||||||||
| 102 | } else { | ||||||||||||
| 103 | 0 | delete $self->{fields}->{ $field->{name} }; | |||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub fields { | ||||||||||
|
Subroutine
fields: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 108 | 0 | my( $self, $fields ) = @_; | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | my $result = 1; | |||||||||||
| 111 | 0 | ▶ | if( $fields ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | should(ref($fields), 'ARRAY'); | |||||||||||
| 113 | 0 | my %remove_fields = ( %{$self->{fields}} ); | |||||||||||
| 114 | 0 | foreach my $field ( @$fields ) { | |||||||||||
| 115 | 0 | ▶ | if( $self->{fields}->{$field} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | 0 | $self->add_field( $field ); | |||||||||||
| 117 | 0 | delete $remove_fields{ $field }; | |||||||||||
| 118 | } | ||||||||||||
| 119 | } | ||||||||||||
| 120 | 0 | $self->{fields} = $fields; | |||||||||||
| 121 | } else { | ||||||||||||
| 122 | 0 | $result = $self->{fields}; | |||||||||||
| 123 | } | ||||||||||||
| 124 | 0 | return $result; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | sub field_names { | ||||||||||
|
Subroutine
field_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 128 | 0 | my( $self, $fields ) = @_; | |||||||||||
| 129 | 0 | return keys %{$self->{fields}}; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 0 | ▶ | sub keys_locked { | ||||||||||
|
Subroutine
keys_locked: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 133 | 0 | my( $self, $keys_locked ) = @_; | |||||||||||
| 134 | |||||||||||||
| 135 | 0 | my $result = 1; | |||||||||||
| 136 | 0 | ▶ | if( $keys_locked ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 0 | ▶ | $self->{keys_locked} = $keys_locked ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | } else { | ||||||||||||
| 139 | 0 | $keys_locked = $self->{keys_locked}; | |||||||||||
| 140 | } | ||||||||||||
| 141 | 0 | return $result; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 0 | ▶ | sub values_readonly { | ||||||||||
|
Subroutine
values_readonly: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 145 | 0 | my( $self, $values_readonly ) = @_; | |||||||||||
| 146 | |||||||||||||
| 147 | 0 | my $result = 1; | |||||||||||
| 148 | 0 | ▶ | if( $values_readonly ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 0 | ▶ | $self->{values_readonly} = $values_readonly ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | } else { | ||||||||||||
| 151 | 0 | $values_readonly = $self->{values_readonly}; | |||||||||||
| 152 | } | ||||||||||||
| 153 | 0 | return $result; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 23 | ▶ | sub contains { | ||||||||||
|
Subroutine
contains: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 157 | 23 | my( $self, $other ) = @_; | |||||||||||
| 158 | |||||||||||||
| 159 | 23 | my $result; | |||||||||||
| 160 | |||||||||||||
| 161 | 23 | ▶ | if( $other->isa('DS::TypeSpec::Any') ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | 2 | $result = 1; | |||||||||||
| 163 | } else { | ||||||||||||
| 164 | # This is equivalent to the subset operator in mathematics | ||||||||||||
| 165 | # For all of the $other fields | ||||||||||||
| 166 | 44 | ▶ | $result = all { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 167 | 44 | my $other = $_; | |||||||||||
| 168 | # There must be one key with the same name | ||||||||||||
| 169 | 44 | ▶ | any { $_ eq $other } keys %{$self->{fields}}; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 170 | 21 | } keys %{$other->{fields}}; | |||||||||||
| 171 | } | ||||||||||||
| 172 | |||||||||||||
| 173 | 23 | return $result; | |||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | 4 | ▶ | sub project { | ||||||||||
|
Subroutine
project: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||
| 177 | 4 | my( $self, $arg1, $arg2 ) = @_; | |||||||||||
| 178 | |||||||||||||
| 179 | 4 | my $name = ''; | |||||||||||
| 180 | 4 | my $new_fields; | |||||||||||
| 181 | 4 | ▶ | if( $arg1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 4 | ▶ | if( ref( $arg1 ) eq '' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 4 | $name = $arg1; | |||||||||||
| 184 | 4 | ▶ | if( $arg2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 4 | $new_fields = $arg2; | |||||||||||
| 186 | } | ||||||||||||
| 187 | } else { | ||||||||||||
| 188 | 0 | $new_fields = $arg1; | |||||||||||
| 189 | } | ||||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | # if( ref( $fields ) eq 'ARRAY' ) { | ||||||||||||
| 193 | # my $new_fields = {}; | ||||||||||||
| 194 | # foreach my $field ( @$fields ) { | ||||||||||||
| 195 | # $new_fields->{$field} = 1; | ||||||||||||
| 196 | # } | ||||||||||||
| 197 | # $fields = $new_fields; | ||||||||||||
| 198 | # } | ||||||||||||
| 199 | 4 | should(ref($new_fields), 'HASH'); | |||||||||||
| 200 | |||||||||||||
| 201 | 4 | my $new_spec = new DS::TypeSpec( $name ); | |||||||||||
| 202 | |||||||||||||
| 203 | 4 | foreach my $new_field (keys %$new_fields) { | |||||||||||
| 204 | 8 | ▶ | if( my $field = $self->{fields}->{ $new_fields->{$new_field} } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 205 | 6 | my $new_field_obj = $field->clone(); | |||||||||||
| 206 | 6 | $new_field_obj->{name} = $new_field; | |||||||||||
| 207 | 6 | $new_spec->add_field( $new_field_obj ); | |||||||||||
| 208 | } else { | ||||||||||||
| 209 | 2 | croak("Can't limit to field $new_field since it is not in the original type"); | |||||||||||
| 210 | } | ||||||||||||
| 211 | } | ||||||||||||
| 212 | 2 | return $new_spec; | |||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | 12 | 1; | |||||||||||
| 216 | |||||||||||||
| 217 | #TODO Add sorting and unique constraints. Possibly also field order (or maybe not?!?) | ||||||||||||