/usr/share/perl5/Attean/API/Plan.pm is in libattean-perl 0.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | use v5.14;
use warnings;
use utf8;
=head1 NAME
Attean::API::Plan - Query plan
=head1 VERSION
This document describes Attean::API::Plan version 0.019
=head1 DESCRIPTION
The Attean::API::Plan role defines a common API for all query plans.
=head1 ATTRIBUTES
=over 4
=item C<< cost >>
=item C<< distinct >>
=item C<< item_type >>
=item C<< in_scope_variables >>
=item C<< ordered >>
=back
=head1 REQUIRED METHODS
The following methods are required by the L<Attean::API::Plan> role:
=over 4
=item C<< impl( $model ) >>
Returns a code reference that when called (without arguments), returns an
L<Attean::API::Iterator> object.
=back
=head1 METHODS
=over 4
=item C<< has_cost >>
=cut
use Type::Tiny::Role;
package Attean::API::Plan 0.019 {
use Scalar::Util qw(blessed);
use Types::Standard qw(ArrayRef CodeRef Str Object InstanceOf Bool Num Int);
use Moo::Role;
has 'cost' => (is => 'rw', isa => Int, predicate => 'has_cost');
has 'distinct' => (is => 'rw', isa => Bool, required => 1, default => 0);
has 'item_type' => (is => 'ro', isa => Str, required => 1, default => 'Attean::API::Result');
has 'in_scope_variables' => (is => 'ro', isa => ArrayRef[Str], required => 1);
has 'ordered' => (is => 'ro', isa => ArrayRef, required => 1, default => sub { [] });
requires 'impl';
requires 'plan_as_string';
=item C<< as_string >>
Returns a tree-structured string representation of this plan, including children.
=cut
sub as_string {
my $self = shift;
my $string = '';
$self->walk( prefix => sub {
my $a = shift;
my $level = shift;
my $parent = shift;
my $indent = ' ' x $level;
my @flags;
push(@flags, 'distinct') if ($a->distinct);
if (scalar(@{ $a->ordered })) {
my @orders;
foreach my $c (@{ $a->ordered }) {
my $dir = $c->ascending ? "↑" : "↓";
my $s = $dir . $c->expression->as_string;
push(@orders, $s);
}
push(@flags, "order: " . join('; ', @orders));
}
if (defined(my $cost = $a->cost)) {
push(@flags, "cost: $cost");
}
$string .= "-$indent " . $a->plan_as_string($level);
if (scalar(@flags)) {
$string .= ' (' . join(' ', @flags) . ")";
}
$string .= "\n";
});
return $string;
}
=item C<< evaluate( $model ) >>
Evaluates this plan and returns the resulting iterator.
=cut
sub evaluate {
my $self = shift;
my $impl = $self->impl(@_);
return $impl->();
}
=item C<< in_scope_variables_union( @plans ) >>
Returns the set union of C<< in_scope_variables >> of the given plan objects.
=cut
sub in_scope_variables_union {
my @plans = grep { blessed($_) } @_;
my %vars = map { $_ => 1 } map { @{ $_->in_scope_variables } } @plans;
return keys %vars;
}
=item C<< subplans_of_type_are_variable_connected( $type ) >>
Returns true if the subpatterns of the given C<< $type >> are all connected
through their C<< in_scope_variables >>, false otherwise (implying a cartesian
product if the connecting plans perform some form of join.
=cut
sub subplans_of_type_are_variable_connected {
my $self = shift;
my @types = @_;
my @c = $self->subpatterns_of_type(@types);
return $self->_plans_are_variable_connected(@c);
}
=item C<< children_are_variable_connected( $type ) >>
Returns true if the children of this plan are all connected
through their C<< in_scope_variables >>, false otherwise (implying a cartesian
product if this plan performs some form of join.
=cut
sub children_are_variable_connected {
my $self = shift;
my @c = @{ $self->children };
return $self->_plans_are_variable_connected(@c);
}
sub _plans_are_variable_connected {
# TODO: In the worst case, this is going to run in O(n^2) in the number
# of children. Better indexing of the children by variables can speed
# this up.
my $self = shift;
my @c = @_;
# warn "===========================\n";
# foreach my $c (@c) {
# warn $c->as_string;
# }
return 1 unless (scalar(@c));
my %vars_by_child;
foreach my $i (0 .. $#c) {
my $c = $c[$i];
foreach my $var (@{ $c->in_scope_variables }) {
$vars_by_child{$i}{$var}++;
}
}
#
my @remaining = keys %vars_by_child;
return 1 unless (scalar(@remaining));
my $current = shift(@remaining);
# warn 'Starting with ' . $c[$current]->as_string;
my %seen_vars = %{ $vars_by_child{$current} };
LOOP: while (scalar(@remaining)) {
foreach my $i (0 .. $#remaining) {
my $candidate = $remaining[$i];
my @candidate_vars = keys %{ $vars_by_child{$candidate} };
foreach my $var (@candidate_vars) {
if (exists $seen_vars{ $var }) {
foreach my $var (@candidate_vars) {
$seen_vars{$var}++;
}
# warn "connected with $var: " . $c[$candidate]->as_string;
splice(@remaining, $i, 1);
next LOOP;
}
}
}
# warn 'Not fully connected';
return 0;
}
# warn 'Fully connected';
return 1;
}
}
package Attean::API::BindingSubstitutionPlan 0.019 {
use Moo::Role;
with 'Attean::API::Plan';
requires 'substitute_impl'; # $code = $plan->impl($model, $binding);
sub impl {
my $self = shift;
my $model = shift;
my $b = Attean::Result->new();
return $self->substitute_impl($model, $b);
}
}
package Attean::API::UnionScopeVariablesPlan 0.019 {
use Moo::Role;
with 'Attean::API::Plan';
around 'BUILDARGS' => sub {
my $orig = shift;
my $class = shift;
my %args = @_;
my @vars = Attean::API::Plan->in_scope_variables_union( @{ $args{children} } );
if (exists $args{in_scope_variables}) {
Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
}
$args{in_scope_variables} = [@vars];
return $orig->( $class, %args );
};
}
package Attean::API::Plan::Join 0.019 {
use Types::Standard qw(CodeRef);
use Types::Standard qw(ArrayRef Str ConsumerOf Bool);
use Moo::Role;
with 'Attean::API::Plan', 'Attean::API::BinaryQueryTree';
with 'Attean::API::UnionScopeVariablesPlan';
has 'join_variables' => (is => 'ro', isa => ArrayRef[Str], required => 1);
has 'anti' => (is => 'ro', isa => Bool, default => 0); # is this an anti-join
has 'left' => (is => 'ro', isa => Bool, default => 0); # is this a left, outer-join
# if this is a left, outer-join, this is the filter expression that acts as part of the join operation (see the SPARQL semantics for LeftJoin for more details)
has 'expression' => (is => 'ro', isa => ConsumerOf['Attean::API::Expression'], required => 0, default => sub { Attean::ValueExpression->new( value => Attean::Literal->true ) });
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/attean/issues>.
=head1 SEE ALSO
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2018 Gregory Todd Williams.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|