Advertisement
kjetilk

SPARQL+LDF cost test

Jan 28th, 2016
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.94 KB | None | 0 0
  1.  
  2. use v5.14;
  3. use autodie;
  4. use utf8;
  5. use Test::Modern;
  6.  
  7. use CHI;
  8.  
  9. use Attean;
  10. use Attean::RDF;
  11. use AtteanX::QueryPlanner::Cache::LDF;
  12. use AtteanX::Store::Memory;
  13. #use Carp::Always;
  14. use Data::Dumper;
  15. use AtteanX::Store::SPARQL;
  16. use AtteanX::Store::LDF;
  17. use AtteanX::Model::SPARQLCache::LDF;
  18. use Log::Any::Adapter;
  19. Log::Any::Adapter->set($ENV{LOG_ADAPTER} || 'Stderr', category => qr/AtteanX::QueryPlanner::Cache AtteanX::Model::SPARQLCache::LDF/) if ($ENV{TEST_VERBOSE});
  20.  
  21. my $cache = CHI->new( driver => 'Memory', global => 1 );
  22.  
  23. my $p   = AtteanX::QueryPlanner::Cache::LDF->new;
  24. isa_ok($p, 'Attean::QueryPlanner');
  25. isa_ok($p, 'AtteanX::QueryPlanner::Cache::LDF');
  26. does_ok($p, 'Attean::API::CostPlanner');
  27.  
  28. package TestLDFCreateStore {
  29.         use Moo;
  30.         with 'Test::Attean::Store::LDF::Role::CreateStore';
  31. };
  32.  
  33. my $test = TestLDFCreateStore->new;
  34.  
  35.  
  36. {
  37.  
  38.     my $sparqlstore = Attean->get_store('SPARQL')->new('endpoint_url' => iri('http://test.invalid/sparql'));
  39.     isa_ok($sparqlstore, 'AtteanX::Store::SPARQL');
  40.  
  41.     my $graph = iri('http://test.invalid/graph');
  42.     my $t       = triple(variable('s'), iri('p'), literal('1'));
  43.     my $u       = triple(variable('s'), iri('p'), variable('o'));
  44.     my $v       = triple(variable('s'), iri('q'), blank('xyz'));
  45.     my $w       = triple(variable('a'), iri('b'), iri('c'));
  46.     my $x       = triple(variable('s'), iri('q'), iri('a'));
  47.     my $y       = triple(variable('o'), iri('b'), literal('2'));
  48.     my $z       = triple(variable('a'), iri('c'), variable('s'));
  49.     my $s       = triple(iri('a'), variable('p'), variable('o'));
  50.  
  51.     my $ldfstore    = $test->create_store(triples => [$t,$u,$v,$w,$x,$y,$z,$s]);
  52.  
  53.     isa_ok($ldfstore, 'AtteanX::Store::LDF');
  54.     my $model   = AtteanX::Model::SPARQLCache::LDF->new( store => $sparqlstore,
  55.                                                                           ldf_store => $ldfstore,
  56.                                                                           cache => $cache );
  57.     isa_ok($model, 'AtteanX::Model::SPARQLCache::LDF');
  58.     isa_ok($model, 'AtteanX::Model::SPARQLCache');
  59.     isa_ok($model, 'AtteanX::Model::SPARQL');
  60.  
  61.  
  62.  
  63.         $cache->set('?v001 <p> "1" .', ['<http://example.org/foo>', '<http://example.org/bar>']);
  64.         $cache->set('?v001 <p> "dahut" .', ['<http://example.com/foo>', '<http://example.com/bar>']);
  65.         $cache->set('?v001 <dahut> "1" .', ['<http://example.org/dahut>']);
  66.         $cache->set('?v002 <p> ?v001 .', {'<http://example.org/foo>' => ['<http://example.org/bar>'],
  67.                                                      '<http://example.com/foo>' => ['<http://example.org/baz>', '<http://example.org/foobar>']});
  68.         $cache->set('?v001 <p> "dahut" .', ['<http://example.com/foo>', '<http://example.com/bar>']);
  69.         $cache->set('?v002 <dahut> ?v001 .', {'<http://example.org/dahut>' => ['"Foobar"']});
  70.  
  71.     subtest '2-triple BGP with join variable with cache one cached' => sub {
  72.         my $bgp     = Attean::Algebra::BGP->new(triples => [$t, $x]);
  73.         my @plans   = $p->plans_for_algebra($bgp, $model, [$graph]);
  74.         is(scalar @plans, 5, 'Got 5 plans');
  75.         foreach my $plan (@plans){
  76.             print "\n". $plan->as_string;
  77.         }
  78.         # The first two plans should be the "best", containing a HashJoin over
  79.         # a Table and a LDF.
  80.         foreach my $plan (@plans[0..1]) {
  81.             does_ok($plan, 'Attean::API::Plan::Join', 'First 2 plans are joins');
  82.             my @tables  = $plan->subpatterns_of_type('Attean::Plan::Table');
  83.             is(scalar(@tables), 1, 'First 2 plans contain 1 table sub-plan');
  84.             my @ldfs    = $plan->subpatterns_of_type('AtteanX::Store::LDF::Plan::Triple');
  85.             is(scalar(@ldfs), 1, 'First 2 plans contain 1 table sub-plan');
  86.         }
  87.  
  88.         my $plan = $plans[0];
  89.        
  90.         # sorting the strings should result in the Attean::Plan::Table being first
  91.         my @children    = sort { "$a" cmp "$b" } @{$plan->children};
  92.         foreach my $cplan (@children) {
  93.             does_ok($cplan, 'Attean::API::Plan', 'Each child of 2-triple BGP');
  94.         }
  95.        
  96.         my ($table,$ldfplan)    = @children;
  97.         isa_ok($table, 'Attean::Plan::Table', 'Should join on Table first');
  98.         isa_ok($ldfplan, 'AtteanX::Store::LDF::Plan::Triple', 'Then on LDF triple');
  99.         is($ldfplan->plan_as_string, 'LDFTriple { ?s, <q>, <a> }', 'Child plan OK');
  100.     };
  101. }
  102.     done_testing;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement