kasei / attean

A Perl Semantic Web Framework
19 stars 10 forks source link

Expected query type but got (undef) #143

Closed VladimirAlexiev closed 5 years ago

VladimirAlexiev commented 5 years ago

The following query as per https://metacpan.org/pod/Attean::SimpleQueryEvaluator

use Attean;

my $store = Attean->get_store('Memory')->new();
my $model = Attean::TripleModel->new( store => $store );
my $algebra = Attean->get_parser('SPARQL')->parse(<>);  ###
my $e = Attean::SimpleQueryEvaluator->new( model => $model );
my $iter = $e->evaluate( $algebra, Attean::IRI->new('http://example.org/') );

gets Expected query type but got (undef) at query.pl line 8.

The query on stdin is

prefix wd: <http://www.wikidata.org/entity/> 

select * {
  bind(wd:Q156578 as ?co)
  bind(strafter(str(?co),str(wd:)) as ?WD)
  {
    bind(?WD as ?WD1)
  } union {
    bind(?WD as ?WD2)
  }
}
kasei commented 5 years ago

@VladimirAlexiev – There are a few things wrong here. The primary issue is that you are only passing one line of STDIN with <> to the SPARQL parser. You need to either join all the input lines into a string, or change the input record separator variable $/. A few extra use statements, and a default graph to use for Attean::SimpleQueryEvaluator gives me a script that runs as expected:

use Attean;
use Attean::RDF qw(iri);
use Attean::SimpleQueryEvaluator;

my $store = Attean->get_store('Memory')->new();
my $model = Attean::TripleModel->new( store => $store );
local($/);
my $algebra = Attean->get_parser('SPARQL')->parse(<>);  ###
my $e = Attean::SimpleQueryEvaluator->new( model => $model, default_graph => iri('') );
my $iter = $e->evaluate( $algebra, Attean::IRI->new('http://example.org/') );
while (my $r = $iter->next) {
    say $r->as_string;
}

output:

{WD="Q156578", co=<http://www.wikidata.org/entity/Q156578>}
{WD="Q156578", co=<http://www.wikidata.org/entity/Q156578>}
VladimirAlexiev commented 5 years ago

thanks!!