| 1 | #!perl | ||||||||||
| 2 | |||||||||||
| 3 | # ########################################################################## # | ||||||||||
| 4 | # Title: Data stream sink | ||||||||||
| 5 | # Creation date: 2007-03-05 | ||||||||||
| 6 | # Author: Michael Zedeler | ||||||||||
| 7 | # Description: Receives data from data stream | ||||||||||
| 8 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Target.pm,v $ | ||||||||||
| 9 | # Repository: kronhjorten | ||||||||||
| 10 | # State: $State: Exp $ | ||||||||||
| 11 | # Documentation: inline | ||||||||||
| 12 | # Recepient: - | ||||||||||
| 13 | # #TODO Sources should not by default be constructed with an explicit typespec, since this may be derived from the data source | ||||||||||
| 14 | # ########################################################################## # | ||||||||||
| 15 | |||||||||||
| 16 | package DS::Target; | ||||||||||
| 17 | |||||||||||
| 18 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 11 | ▶ | use Carp qw{ croak cluck confess carp }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 11 | ▶ | use Carp::Assert; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 11 | our ($VERSION) = $DS::VERSION; | |||||||||
| 23 | 11 | our ($REVISION) = '$Revision: 1.2 $' =~ /(\d+\.\d+)/; | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | 15 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 27 | 15 | my( $class, $source_type, $source ) = @_; | |||||||||
| 28 | |||||||||||
| 29 | 15 | my $self = {}; | |||||||||
| 30 | 15 | bless $self, $class; | |||||||||
| 31 | |||||||||||
| 32 | 15 | ▶ | if( defined( $source_type ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 8 | $self->in_type( $source_type ); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 15 | ▶ | if( defined( $source ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | $self->attach_source( $source ); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 15 | return $self; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # Receives row - invoked by source. | ||||||||||
| 44 | 1 | ▶ | sub receive_row { | ||||||||
|
Subroutine
receive_row: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 1 | confess("This method is abstract. Don't call it. Override it."); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 12 | ▶ | sub attach_source { | ||||||||
|
Subroutine
attach_source: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 49 | 12 | my( $self, $source ) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 12 | my $result; | |||||||||
| 52 | |||||||||||
| 53 | 12 | assert( $source, '$source should be defined' ); | |||||||||
| 54 | 12 | assert( $source->isa('DS::Source'), "ref(\$source) = " . ref( $source ) . " != DS::Source" ); | |||||||||
| 55 | |||||||||||
| 56 | # First break link with old source, if any | ||||||||||
| 57 | 11 | ▶ | if( $self->{source} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | $self->{source}->{target} = undef; | |||||||||
| 59 | } | ||||||||||
| 60 | 11 | ▶ | if( $self->source( $source ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 10 | $source->target( $self ); | |||||||||
| 62 | 10 | $result = 1; | |||||||||
| 63 | } | ||||||||||
| 64 | 10 | return $result; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # This is a primarily private method | ||||||||||
| 68 | # Important caveat: this method is just a field accessor method. | ||||||||||
| 69 | # Maintaining consistent links with source is handled by attach_source | ||||||||||
| 70 | 62 | ▶ | sub source { | ||||||||
|
Subroutine
source: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 71 | 62 | my( $self, $source ) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 62 | my $result; | |||||||||
| 74 | 62 | ▶ | if( $source ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 32 | assert($source->isa('DS::Source')); | |||||||||
| 76 | 32 | ▶ | if( my $source_type = $source->out_type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 31 | ▶ | if( not $self->validate_source_type( $source_type ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 3 | my $target_fields = join(", ", keys %{$self->in_type->{fields}}); | |||||||||
| 79 | 3 | my $source_fields = join(", ", keys %{$source_type->{fields}}); | |||||||||
| 80 | 3 | croak("Validation of source ($source with fields $source_fields) for me ($self with fields $target_fields) failed."); | |||||||||
| 81 | } #TODO Consider throwing a type incompatibility exception here | ||||||||||
| 82 | } else { | ||||||||||
| 83 | 1 | cluck("Type checking of stream from $source to $self skipped because source has no outgoing type specification."); | |||||||||
| 84 | } | ||||||||||
| 85 | 29 | $self->{source} = $source; | |||||||||
| 86 | 29 | $result = 1; | |||||||||
| 87 | } else { | ||||||||||
| 88 | 30 | $result = $self->{source}; | |||||||||
| 89 | } | ||||||||||
| 90 | 59 | return $result; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 20 | ▶ | sub validate_source_type { | ||||||||
|
Subroutine
validate_source_type: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 94 | 20 | my( $self, $source_type ) = @_; | |||||||||
| 95 | 20 | my $result = 1; | |||||||||
| 96 | # We accept if the sender of data are passing more fields to us, than we require | ||||||||||
| 97 | 20 | ▶ | if( $self->in_type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 20 | $result = $source_type->contains( $self->in_type ); | |||||||||
| 99 | } else { | ||||||||||
| 100 | 0 | cluck("Type checking of stream to $self skipped because $self has no ingoing type specification."); | |||||||||
| 101 | } | ||||||||||
| 102 | 20 | return $result; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 75 | ▶ | sub in_type { | ||||||||
|
Subroutine
in_type: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 106 | 75 | my( $self, $type ) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 75 | my $result; | |||||||||
| 109 | 75 | ▶ | if( defined( $type ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 22 | assert($type->isa('DS::TypeSpec')); | |||||||||
| 111 | 21 | $self->{in_type} = $type; | |||||||||
| 112 | } else { | ||||||||||
| 113 | 53 | $result = $self->{in_type}; | |||||||||
| 114 | } | ||||||||||
| 115 | 74 | return $result; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 11 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||