| 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.041 | ||
| 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 ATTRIBUTES | ||
| 42 | |||
| 43 | =over 4 | ||
| 44 | |||
| 45 | =item dataset_graphs | ||
| 46 | |||
| 47 | A hash mapping the string C<value> of each C<Attean::API::IRI> graph name in | ||
| 48 | the dataset to that IRI object. | ||
| 49 | |||
| 50 | =back | ||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | package Attean::DatasetModel 0.041 { | ||
| 59 | use Moo; | ||
| 60 | use Scalar::Util qw(blessed reftype); | ||
| 61 | use Types::Standard qw(ConsumerOf HashRef); | ||
| 62 | use namespace::clean; | ||
| 63 | |||
| 64 | has 'model' => ( | ||
| 65 | is => 'ro', | ||
| 66 | does => 'Attean::API::Model', | ||
| 67 | required => 1, | ||
| 68 | handles => [qw()], | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'dataset_graphs' => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => HashRef[ConsumerOf['Attean::API::IRI']], | ||
| 74 | default => sub { +{} } | ||
| 75 | ); | ||
| 76 | |||
| 77 | around 'BUILDARGS' => sub { | ||
| 78 | my $orig = shift; | ||
| 79 | my $class = shift; | ||
| 80 | my %args = @_; | ||
| 81 | |||
| 82 | my $graphs = delete $args{ 'graphs' }; | ||
| 83 | my %graphs = map { $_->value => $_ } grep { blessed($_) and $_->does('Attean::API::IRI') } @{ $graphs || [] }; | ||
| 84 | $args{ 'dataset_graphs' } = \%graphs; | ||
| 85 | |||
| 86 | unless (exists $args{ 'model' }) { | ||
| 87 | $args{ 'model' } = Attean->temporary_model; | ||
| 88 | } | ||
| 89 | |||
| 90 | return $class->$orig(%args); | ||
| 91 | }; | ||
| 92 | |||
| 93 | =item C<< get_quads ( $subject, $predicate, $object, $graph ) >> | ||
| 94 | |||
| 95 | Returns an L<Attean::API::Iterator> for quads in the model that match the | ||
| 96 | supplied C<< $subject >>, C<< $predicate >>, C<< $object >>, and C<< $graph >>. | ||
| 97 | Any of these terms may be undefined or a L<Attean::API::Variable> object, in | ||
| 98 | which case that term will be considered as a wildcard for the purposes of | ||
| 99 | matching. | ||
| 100 | |||
| 101 | The returned iterator conforms to both L<Attean::API::Iterator> and | ||
| 102 | L<Attean::API::QuadIterator>. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub get_quads { | ||
| 107 | my $self = shift; | ||
| 108 | my @nodes = @_[0..3]; | ||
| 109 | my $quad = $nodes[3]; | ||
| 110 | if (defined($quad) and reftype($quad) eq 'ARRAY') { | ||
| 111 | my @graphs = @$quad; | ||
| 112 | my @ok = grep { exists $self->dataset_graphs->{$_->value} } @graphs; | ||
| 113 | return $self->model->get_quads( @nodes[0..2], \@ok ); | ||
| 114 | } elsif (blessed($quad) and not($quad->isa('Attean::API::Variable'))) { | ||
| 115 | if (exists($self->dataset_graphs->{ $quad->value })) { | ||
| 116 | return $self->model->get_statements( @_ ); | ||
| 117 | } else { | ||
| 118 | return Attean::ListIterator->new( values => [], item_type => 'Attean::API::Quad' ); | ||
| 119 | } | ||
| 120 | } else { | ||
| 121 | my @iters; | ||
| 122 | if (not(defined($quad)) or $quad->isa('Attean::API::Variable')) { | ||
| 123 | foreach my $g (values %{ $self->dataset_graphs }) { | ||
| 124 | push(@iters, $self->model->get_quads( @nodes[0..2], $g )); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | my %seen; | ||
| 128 | my $code = sub { | ||
| 129 | while (1) { | ||
| 130 | return unless scalar(@iters); | ||
| 131 | my $st = $iters[0]->next; | ||
| 132 | if ($st) { | ||
| 133 | if ($seen{ $st->as_string }++) { | ||
| 134 | next; | ||
| 135 | } | ||
| 136 | return $st; | ||
| 137 | } else { | ||
| 138 | shift(@iters); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | }; | ||
| 142 | return Attean::IteratorSequence->new( iterators => \@iters, item_type => 'Attean::API::Quad' ); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | =item C<< count_quads ( $s, $p, $o, $g ) >> | ||
| 147 | |||
| 148 | Returns the count of quads matching the supplied pattern. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub count_quads { | ||
| 153 | my $self = shift; | ||
| 154 | my $iter = $self->get_quads(@_); | ||
| 155 | my $count = 0; | ||
| 156 | while ($iter->next) { | ||
| 157 | $count++; | ||
| 158 | } | ||
| 159 | return $count; | ||
| 160 | } | ||
| 161 | |||
| 162 | =item C<< count_quads_estimate ( $s, $p, $o, $g ) >> | ||
| 163 | |||
| 164 | Returns an estimated count of the quads matching the supplied pattern. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub count_quads_estimate { | ||
| 169 | my $self = shift; | ||
| 170 | return $self->count_quads(@_); | ||
| 171 | } | ||
| 172 | |||
| 173 | =item C<< get_graphs >> | ||
| 174 | |||
| 175 | Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects for all | ||
| 176 | the graphs defined in the dataset. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub get_graphs { | ||
| 181 | my $self = shift; | ||
| 182 | my @graphs = values %{ $self->dataset_graphs }; | ||
| 183 | return Attean::ListIterator->new( values => \@graphs, item_type => 'Attean::API::Term' ); | ||
| 184 | } | ||
| 185 | |||
| 186 | =item C<< size >> | ||
| 187 | |||
| 188 | Returns the number of quads in the dataset. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub size { | ||
| 193 | my $self = shift; | ||
| 194 | return $self->count_quads(); | ||
| 195 | } | ||
| 196 | |||
| 197 | with 'Attean::API::Model'; | ||
| 198 | } | ||
| 199 | |||
| 200 | package Attean::MutableDatasetModel 0.041 { | ||
| 201 | use Moo; | ||
| 202 | use Scalar::Util qw(blessed reftype); | ||
| 203 | use Types::Standard qw(ConsumerOf HashRef); | ||
| 204 | use namespace::clean; | ||
| 205 | |||
| 206 | extends 'Attean::DatasetModel'; | ||
| 207 | |||
| 208 | has 'model' => ( | ||
| 209 | is => 'ro', | ||
| 210 | does => 'Attean::API::MutableModel', | ||
| 211 | required => 1, | ||
| 212 | handles => [qw(remove_quad)], | ||
| 213 | ); | ||
| 214 | |||
| 215 | sub add_quad { | ||
| 216 | my $self = shift; | ||
| 217 | my $quad = shift; | ||
| 218 | |||
| 219 | my $g = $quad->graph; | ||
| 220 | $self->model->add_quad($quad); | ||
| 221 | $self->dataset_graphs->{ $g->value } = $g; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub create_graph { | ||
| 225 | my $self = shift; | ||
| 226 | my $g = shift; | ||
| 227 | |||
| 228 | $self->dataset_graphs->{ $g->value } = $g; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub drop_graph { | ||
| 232 | my $self = shift; | ||
| 233 | my $g = shift; | ||
| 234 | |||
| 235 | $self->model->drop_graph($g); | ||
| 236 | delete $self->dataset_graphs->{ $g->value }; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub clear_graph { | ||
| 240 | my $self = shift; | ||
| 241 | my $g = shift; | ||
| 242 | |||
| 243 | $self->model->clear_graph($g); | ||
| 244 | } | ||
| 245 | |||
| 246 | with 'Attean::API::MutableModel'; | ||
| 247 | } | ||
| 248 | |||
| 249 | 1; | ||
| 250 | |||
| 251 | __END__ |