File Coverage

blib/lib/App/Asciio/Connections.pm
Criterion Covered Total %
statement 39 100 39.0
branch 3 20 15.0
condition 0 9 0.0
subroutine 10 16 62.5
pod 0 10 0.0
total 52 155 33.5


line stmt bran cond sub pod time code
1              
2             package App::Asciio;
3              
4             $|++ ;
5              
6 4     4   28 use strict;
  4         8  
  4         175  
7 4     4   22 use warnings;
  4         20  
  4         332  
8              
9 4     4   28 use Data::TreeDumper ;
  4         7  
  4         433  
10 4     4   27 use Clone;
  4         9  
  4         175  
11 4     4   34 use List::Util qw(min max) ;
  4         14  
  4         244  
12 4     4   23 use List::MoreUtils qw(any minmax first_value) ;
  4         7  
  4         28  
13              
14             #-----------------------------------------------------------------------------
15              
16             sub connect_elements
17             {
18 169     169 0 303 my ($self, @elements) = @_ ;
19              
20 169         479 my @possible_connections = $self->get_possible_connections(@elements) ;
21              
22 169         486 $self->add_connections(@possible_connections) ;
23             }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub add_connections
28             {
29 169     169 0 319 my ($self, @connections) = @_ ;
30              
31 169         567 $self->flash_new_connections(@connections) ;
32              
33 169         281 push @{$self->{CONNECTIONS}}, @connections ;
  169         375  
34 169         533 $self->{MODIFIED }++ ;
35             }
36              
37             #-----------------------------------------------------------------------------
38              
39             sub get_possible_connections
40             {
41 169     169 0 297 my ($self, @elements) = @_ ;
42              
43 169         306 my @possible_connections ;
44             my %connected_connectors ;
45              
46 169         354 for my $element (@elements)
47             {
48 169         624 my @connectors = $element->get_connector_points() ;
49            
50 169 50       412 last unless @connectors ;
51            
52             # tbd: optimize search by eliminating those elements that are too far
53 169 50       305 for my $connectee (reverse grep { $_->is_autoconnect_enabled() || $_->is_border_connection_allowed() } @{$self->{ELEMENTS}})
  169         562  
  169         451  
54             {
55 169 50       885 next if $connectee == $element ; # dont connect to self
56            
57 0         0 for my $connector (@connectors)
58             {
59             my @connections = $connectee->match_connector
60             (
61             # translate coordinates to connectee reference
62             ($element->{X} - $connectee->{X}) + $connector->{X},
63             ($element->{Y} - $connectee->{Y}) + $connector->{Y},
64 0         0 ) ;
65            
66             # make connection if possible. connect to a single point
67 0 0 0     0 if(defined $connections[0] && ! exists $connected_connectors{$element.$connector->{NAME}})
68             {
69 0         0 push @possible_connections,
70             {
71             CONNECTED => $element,
72             CONNECTOR =>$connector,
73             CONNECTEE => $connectee,
74             CONNECTION => $connections[0],
75             } ;
76            
77 0         0 $connected_connectors{$element.$connector->{NAME}}++ ;
78 0         0 next ;
79             }
80             }
81             }
82             }
83            
84 169         450 return(@possible_connections) ;
85             }
86              
87             #-----------------------------------------------------------------------------
88              
89             sub get_connections_containing
90             {
91 0     0 0 0 my($self, @elements) = @_ ;
92              
93 0         0 my %elements_to_find = map {$_ => 1} @elements ;
  0         0  
94 0         0 my @connections ;
95              
96 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
97             {
98 0 0 0     0 if(exists $elements_to_find{$connection->{CONNECTED}} || exists $elements_to_find{$connection->{CONNECTEE}})
99             {
100 0         0 push @connections, $connection;
101             }
102             }
103              
104 0         0 return(@connections) ;
105             }
106              
107             #-----------------------------------------------------------------------------
108              
109             sub delete_connections
110             {
111 0     0 0 0 my($self, @connections) = @_ ;
112              
113 0         0 my %connections_to_delete = map {$_ => 1} @connections ;
  0         0  
114              
115 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
116             {
117 0 0       0 if(exists $connections_to_delete{$connection})
118             {
119 0         0 $connection = undef ;
120             }
121             }
122              
123 0         0 @{$self->{CONNECTIONS}} = grep { defined $_} @{$self->{CONNECTIONS}} ;
  0         0  
  0         0  
  0         0  
124              
125 0         0 $self->{MODIFIED }++ ;
126             }
127              
128             #-----------------------------------------------------------------------------
129              
130             sub delete_connections_containing
131             {
132 0     0 0 0 my($self, @elements) = @_ ;
133              
134 0         0 for my $element(@elements)
135             {
136 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
137             {
138 0 0 0     0 if($connection->{CONNECTED} == $element || $connection->{CONNECTEE} == $element)
139             {
140 0         0 $connection = undef ;
141             }
142             }
143             }
144              
145 0         0 @{$self->{CONNECTIONS}} = grep { defined $_} @{$self->{CONNECTIONS}} ;
  0         0  
  0         0  
  0         0  
146              
147 0         0 $self->{MODIFIED }++ ;
148             }
149              
150             #-----------------------------------------------------------------------------
151              
152             sub is_connectee
153             {
154 0     0 0 0 my($self, $element) = @_ ;
155              
156 0         0 my $connectee = 0 ;
157              
158 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
159             {
160 0 0       0 if($connection->{CONNECTEE} == $element)
161             {
162 0         0 $connectee++ ;
163             last
164 0         0 }
165             }
166              
167 0         0 return($connectee) ;
168             }
169              
170             #-----------------------------------------------------------------------------
171              
172             sub get_connected
173             {
174 0     0 0 0 my($self, $element) = @_ ;
175              
176 0         0 my(@connected) ;
177              
178 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
179             {
180 0 0       0 if($connection->{CONNECTEE} == $element)
181             {
182 0         0 push @connected, $connection ;
183             }
184             }
185              
186 0         0 return(@connected) ;
187             }
188              
189             #-----------------------------------------------------------------------------
190              
191             sub is_connected
192             {
193 0     0 0 0 my($self, $element) = @_ ;
194              
195 0         0 my $connected = 0 ;
196              
197 0         0 for my $connection (@{$self->{CONNECTIONS}})
  0         0  
198             {
199 0 0       0 if($connection->{CONNECTED} == $element)
200             {
201 0         0 $connected++ ;
202             last
203 0         0 }
204             }
205              
206 0         0 return($connected) ;
207             }
208              
209             #-----------------------------------------------------------------------------
210              
211             sub flash_new_connections
212             {
213 169     169 0 294 my($self, @connections) = @_ ;
214              
215 169         219 push @{$self->{NEW_CONNECTIONS}}, @connections ;
  169         529  
216             }
217              
218             #-----------------------------------------------------------------------------
219              
220              
221             1 ;