| 1 | use v5.14; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use Attean::Algebra; | ||
| 5 | use Attean::Expression; | ||
| 6 | use Attean::IDPQueryPlanner; | ||
| 7 | |||
| 8 | package Test::Attean::Plan::TestService 0.033 { | ||
| 9 | use Moo; | ||
| 10 | use Types::Standard qw(ConsumerOf Bool Str InstanceOf); | ||
| 11 | use Encode qw(encode encode_utf8 decode_utf8); | ||
| 12 | use Scalar::Util qw(blessed); | ||
| 13 | use URI::Escape; | ||
| 14 | use Attean::SPARQLClient; | ||
| 15 | use namespace::clean; | ||
| 16 | |||
| 17 | with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree'; | ||
| 18 | |||
| 19 | has 'plan' => (is => 'rw'); | ||
| 20 | has 'model' => (is => 'rw'); | ||
| 21 | has 'silent' => (is => 'ro', isa => Bool, default => 0); | ||
| 22 | |||
| 23 | sub plan_as_string { | ||
| 24 | my $self = shift; | ||
| 25 | my $s = $self->plan->as_string; | ||
| 26 | $s =~ s/\n/ /g; | ||
| 27 | return sprintf('TestService { ' . $s . ' }'); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub tree_attributes { return qw() }; | ||
| 31 | sub impl { | ||
| 32 | my $self = shift; | ||
| 33 | shift; # model | ||
| 34 | my $model = $self->model; | ||
| 35 | my $plan = $self->plan; | ||
| 36 | my $result = eval { $plan->impl($model, @_) }; | ||
| 37 | if ($@ and not $self->silent) { | ||
| 38 | die $@; | ||
| 39 | } | ||
| 40 | |||
| 41 | # mimic the document-scoping of bnodes from the SERVICE call | ||
| 42 | my $m = Attean::TermMap->uuid_blank_map; | ||
| 43 | return sub { | ||
| 44 | my $iter = $result->(); | ||
| 45 | return $iter->map(sub { return shift->apply_map($m) }); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | package Test::Attean::TestIDPQueryPlanner 0.033 { | ||
| 52 | use Moo; | ||
| 53 | use Data::Dumper; | ||
| 54 | use Types::Standard qw(HashRef); | ||
| 55 | use namespace::clean; | ||
| 56 | |||
| 57 | extends 'Attean::IDPQueryPlanner'; | ||
| 58 | has 'endpoints' => (is => 'rw', isa => HashRef, default => sub { +{} }); | ||
| 59 | |||
| 60 | sub register_test_endpoint { | ||
| 61 | my $self = shift; | ||
| 62 | my $endpoint = shift; | ||
| 63 | my $data = shift; | ||
| 64 | $self->endpoints->{$endpoint} = $data; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub plans_for_algebra { | ||
| 68 | my $self = shift; | ||
| 69 | my $algebra = shift; | ||
| 70 | my $model = shift; | ||
| 71 | my $active_graphs = shift; | ||
| 72 | my $default_graphs = shift; | ||
| 73 | my %args = @_; | ||
| 74 | my @children = @{ $algebra->children }; | ||
| 75 | my ($child) = $children[0]; | ||
| 76 | |||
| 77 | if ($algebra->isa('Attean::Algebra::Service')) { | ||
| 78 | my $endpoint = $algebra->endpoint->value; | ||
| 79 | # warn "Looking for endpoint $endpoint ...\n"; | ||
| 80 | if (my $data = $self->endpoints->{$endpoint}) { | ||
| 81 | # warn "... found.\n"; | ||
| 82 | my ($mock_model, $dg) = @$data; | ||
| 83 | my @plans = $self->plans_for_algebra($child, $mock_model, $active_graphs, $default_graphs, %args); | ||
| 84 | my @vars = $child->in_scope_variables; | ||
| 85 | my $silent = $algebra->silent; | ||
| 86 | my $sparql = sprintf('SELECT * WHERE { %s }', $child->as_sparql); | ||
| 87 | my $plan = Test::Attean::Plan::TestService->new( | ||
| 88 | in_scope_variables => \@vars, | ||
| 89 | plan => shift(@plans), | ||
| 90 | model => $mock_model, | ||
| 91 | silent => $silent, | ||
| 92 | ); | ||
| 93 | return $plan; | ||
| 94 | } else { | ||
| 95 | # warn "... not found.\n"; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return $self->SUPER::plans_for_algebra($algebra, $model, $active_graphs, $default_graphs, %args); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 |