blib/lib/Logic/Relational/Variable.pm

filter: Statementstmt 100% 17 / 17 Branchbran 100% 2 / 2 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 5 / 5 totaltotal 94.5% 35 / 37 CC 4 SCAR 5.9
CC
4
Cov
96%
new
3.0
freshen
2.0
BEGIN
1.0
CRAP 1.8
« lib/Logic/Relational/ChangeEvent.pm ↑ Summary lib/Logic/Relational/StdLib.pm »
1 package Logic::Relational::Variable;
2  
3 36 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Logic::Relational::Variable - Represents a logical variable in the logic engine.
9  
10 =head1 SYNOPSIS
11  
12 use Logic::Relational::Variable;
13 my $var = Logic::Relational::Variable->new(name => 'X');
14 say $var->id; # unique integer ID
15 say $var->name; # X
16  
17 =head1 DESCRIPTION
18  
19 C<Logic::Relational::Variable> represents a logical variable. Each variable
20 has a unique internal ID to distinguish it from other variables, even if they share
21 the same display name.
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 Constructor. Takes C<name> (optional) and C<id> (optional). If C<id> is omitted,
28 a unique integer ID is allocated from a global counter.
29  
30 =head2 id
31  
32 Returns the unique integer ID.
33  
34 =head2 name
35  
36 Returns the display name of the variable.
37  
38 =head2 as_string
39  
40 Returns a string representation of the variable, formatting it with its name and ID.
41  
42 =head2 freshen
43  
44 Takes a variable mapping hash reference. If this variable's ID is not present
45 in the mapping, a new variable with the same name and a fresh ID is created
46 and stored in the mapping. Returns the mapped variable.
47  
48 =cut
49  
50 36 our $NEXT_ID = 1;
51  
52 1429 ▶sub new ( $class, %args ) {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
53 1429 ▶ my $id = $args{id} // $NEXT_ID++;
Condition50%1/2
$args{"id"} // $Logic::Relational::Variable::NEXT_ID++
l!l
01429
MC/DC0%0/1
$args{"id"} // $Logic::Relational::Variable::NEXT_ID++
$args{"id"}
Truth table50%1/2
A
A= $args{"id"}
Aresult
11
00
54 1429 ▶ my $name = $args{name} // "var_$id";
Condition100%2/2
$args{"name"} // "var_$id"
l!l
14281
MC/DC100%1/1
$args{"name"} // "var_$id"
$args{"name"}
Truth table100%2/2
A
A= $args{"name"}
Aresult
11
00
55 1429 return bless { id => $id, name => $name }, $class;
56 }
57  
58 12042 ▶sub id ($self) {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
59 12042 return $self->{id};
60 }
61  
62 682 ▶sub name ($self) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
63 682 return $self->{name};
64 }
65  
66 6 ▶sub as_string ($self) {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
67 6 return "_" . $self->{name} . "_" . $self->{id};
68 }
69  
70 1060 ▶sub freshen ( $self, $var_map ) {
Subroutine freshen: called CC=2 SCAR=6.9
Pod documented
71 1060 my $id = $self->{id};
72 1060 ▶ if ( !exists $var_map->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $var_map->{$id}) 509 551 1060
73 509 $var_map->{$id} = ref($self)->new( name => $self->{name} );
74 }
75 1060 return $var_map->{$id};
76 }
77  
78 36 1;
« lib/Logic/Relational/ChangeEvent.pm ↑ Summary lib/Logic/Relational/StdLib.pm »