Attean/DatasetModel.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 178 CC 8 SCAR 31.6
CC
8
Cov
0%
get_quads
72.0
count_quads
2.0
count_quads_estimate
2.0
CRAP 23.5
1 use v5.14;
2 use warnings;
3  
4 =head1 NAME
5  
6 Attean::DatasetModel - RDF model that restricts the dataset of a sub-model
7  
8 =head1 VERSION
9  
10 This document describes Attean::DatasetModel version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $model = Attean::DatasetModel->new( model => $model, graphs => \@named_graphs );
17  
18 =head1 DESCRIPTION
19  
20 The Attean::DatasetModel class represents a model that is backed by a sub-model.
21 It restricts the available named graphs of the sub-model to only those
22 configured during construction.
23  
24 This class conforms to the L<Attean::API::Model> role.
25  
26 The Attean::DatasetModel constructor requires two named arguments:
27  
28 =over 4
29  
30 =item model
31  
32 A L<Attean::API::Model> object representing the sub-model.
33  
34 =item graphs
35  
36 An array reference of L<Attean::API::IRI> objects, enumerating the acceptable
37 named graphs that can be queried.
38  
39 =back
40  
41 =head1 METHODS
42  
43 =over 4
44  
45 =cut
46  
47 package Attean::DatasetModel 0.040 {
48 use Moo;
49 use Scalar::Util qw(blessed reftype);
50 use Types::Standard qw(ConsumerOf HashRef);
51 use namespace::clean;
52  
53 has 'model' => (
54 is => 'ro',
55 does => 'Attean::API::Model',
56 required => 1,
57 handles => [qw()],
58 );
59
60 has 'dataset_graphs' => (
61 is => 'ro',
62 isa => HashRef[ConsumerOf['Attean::API::IRI']],
63 default => sub { +{} }
64 );
65  
66 around 'BUILDARGS' => sub {
67 my $orig = shift;
68 my $class = shift;
69 my %args = @_;
70
71 my $graphs = delete $args{ 'graphs' };
72 my %graphs = map { $_->value => $_ } grep { blessed($_) and $_->does('Attean::API::IRI') } @{ $graphs || [] };
73 $args{ 'dataset_graphs' } = \%graphs;
74
75 unless (exists $args{ 'model' }) {
76 $args{ 'model' } = Attean->temporary_model;
77 }
78
79 return $class->$orig(%args);
80 };
81
82 =item C<< get_quads ( $subject, $predicate, $object, $graph ) >>
83  
84 Returns an L<Attean::API::Iterator> for quads in the model that match the
85 supplied C<< $subject >>, C<< $predicate >>, C<< $object >>, and C<< $graph >>.
86 Any of these terms may be undefined or a L<Attean::API::Variable> object, in
87 which case that term will be considered as a wildcard for the purposes of
88 matching.
89  
90 The returned iterator conforms to both L<Attean::API::Iterator> and
91 L<Attean::API::QuadIterator>.
92  
93 =cut
94  
95 sub get_quads {
96 my $self = shift;
97 my @nodes = @_[0..3];
98 my $quad = $nodes[3];
99 if (defined($quad) and reftype($quad) eq 'ARRAY') {
100 my @graphs = @$quad;
101 my @ok = grep { exists $self->dataset_graphs->{$_->value} } @graphs;
102 return $self->model->get_quads( @nodes[0..2], \@ok );
103 } elsif (blessed($quad) and not($quad->isa('Attean::API::Variable'))) {
104 if (exists($self->dataset_graphs->{ $quad->value })) {
105 return $self->model->get_statements( @_ );
106 } else {
107 return Attean::ListIterator->new( values => [], item_type => 'Attean::API::Quad' );
108 }
109 } else {
110 my @iters;
111 if (not(defined($quad)) or $quad->isa('Attean::API::Variable')) {
112 foreach my $g (values %{ $self->dataset_graphs }) {
113 push(@iters, $self->model->get_quads( @nodes[0..2], $g ));
114 }
115 }
116 my %seen;
117 my $code = sub {
118 while (1) {
119 return unless scalar(@iters);
120 my $st = $iters[0]->next;
121 if ($st) {
122 if ($seen{ $st->as_string }++) {
123 next;
124 }
125 return $st;
126 } else {
127 shift(@iters);
128 }
129 }
130 };
131 return Attean::IteratorSequence->new( iterators => \@iters, item_type => 'Attean::API::Quad' );
132 }
133 }
134  
135 sub count_quads {
136 my $self = shift;
137 my $iter = $self->get_quads(@_);
138 my $count = 0;
139 while ($iter->next) {
140 $count++;
141 }
142 return $count;
143 }
144
145 sub count_quads_estimate {
146 my $self = shift;
147 return $self->count_quads(@_);
148 }
149
150 sub get_graphs {
151 my $self = shift;
152 my @graphs = values %{ $self->dataset_graphs };
153 return Attean::ListIterator->new( values => \@graphs, item_type => 'Attean::API::Term' );
154 }
155
156 sub size {
157 my $self = shift;
158 return $self->count_quads();
159 }
160
161 with 'Attean::API::Model';
162 }
163  
164 package Attean::MutableDatasetModel 0.040 {
165 use Moo;
166 use Scalar::Util qw(blessed reftype);
167 use Types::Standard qw(ConsumerOf HashRef);
168 use namespace::clean;
169
170 extends 'Attean::DatasetModel';
171  
172 has 'model' => (
173 is => 'ro',
174 does => 'Attean::API::MutableModel',
175 required => 1,
176 handles => [qw(remove_quad)],
177 );
178
179 sub add_quad {
180 my $self = shift;
181 my $quad = shift;
182
183 my $g = $quad->graph;
184 $self->model->add_quad($quad);
185 $self->dataset_graphs->{ $g->value } = $g;
186 }
187
188 sub create_graph {
189 my $self = shift;
190 my $g = shift;
191
192 $self->dataset_graphs->{ $g->value } = $g;
193 }
194
195 sub drop_graph {
196 my $self = shift;
197 my $g = shift;
198
199 $self->model->drop_graph($g);
200 delete $self->dataset_graphs->{ $g->value };
201 }
202
203 sub clear_graph {
204 my $self = shift;
205 my $g = shift;
206
207 $self->model->clear_graph($g);
208 }
209
210 with 'Attean::API::MutableModel';
211 }
212  
213 1;
214  
215 __END__