Skip to content

Instantly share code, notes, and snippets.

@kasei
Last active March 31, 2016 16:15
Show Gist options
  • Save kasei/7e8ecb0134870caaf122237dc9af75e4 to your computer and use it in GitHub Desktop.
Save kasei/7e8ecb0134870caaf122237dc9af75e4 to your computer and use it in GitHub Desktop.
Attean algebra pattern matching
#!/usr/bin/env perl
use v5.14;
use Attean;
use Attean::Algebra qw(match);
my $algebra = Attean->get_parser('SPARQL')->parse(<<"END");
SELECT * WHERE {
?s ?p ?o
FILTER(ISLITERAL(?o))
}
END
say $algebra->as_string;
# - Query
# - Project { ?o ?s ?p }
# - Filter { ISLITERAL(?o) }
# - BGP { ?s ?p ?o . }
# Now, try to match the algebra against the pattern Query(Project(*)), and pull
# out the child of the Project.
my $pattern = Attean::Algebra::Query->new(
children => [
Attean::Algebra::Project->new(
children => [
# if we successfully match the pattern, store this part of the
# algebra tree with the name 'graphpattern'
match('graphpattern'),
],
# Only 'children' are matched, so this is here only to stop the
# Attean::Algebra::Project constructor from complaining about
# missing build args.
variables => [],
)
]
);
if (my $matches = $algebra->match($pattern)) {
say "The child algebra of Project:";
say $matches->{'graphpattern'}->as_string;
# The child algebra of Project:
# - Filter { ISLITERAL(?o) }
# - BGP { ?s ?p ?o . }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment