| 1 | # RDF::Trine::Model::StatementFilter | ||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||
| 3 | |||||||||||
| 4 | =head1 NAME | ||||||||||
| 5 | |||||||||||
| 6 | RDF::Trine::Model::StatementFilter - Model for filtering statements based on a user-specified criteria | ||||||||||
| 7 | |||||||||||
| 8 | =head1 VERSION | ||||||||||
| 9 | |||||||||||
| 10 | This document describes RDF::Trine::Model::StatementFilter version 1.019 | ||||||||||
| 11 | |||||||||||
| 12 | =head1 METHODS | ||||||||||
| 13 | |||||||||||
| 14 | Beyond the methods documented below, this class inherits methods from the | ||||||||||
| 15 | L<RDF::Trine::Model> class. | ||||||||||
| 16 | |||||||||||
| 17 | =over 4 | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | package RDF::Trine::Model::StatementFilter; | ||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use base qw(RDF::Trine::Model); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | use RDF::Trine::Node; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use RDF::Trine::Pattern; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use RDF::Trine::Iterator qw(sgrep); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 1 | our ($VERSION); | |||||||||
| 35 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | $VERSION = '1.019'; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | ################################################################################ | ||||||||||
| 40 | |||||||||||
| 41 | =item C<< new ( $store ) >> | ||||||||||
| 42 | |||||||||||
| 43 | Returns a new statement-filter model. | ||||||||||
| 44 | |||||||||||
| 45 | =cut | ||||||||||
| 46 | |||||||||||
| 47 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | 1 | my $class = shift; | |||||||||
| 49 | 1 | my $self = $class->SUPER::new( @_ ); | |||||||||
| 50 | 1 | $self->{rules} = []; | |||||||||
| 51 | 1 | return $self; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | =item C<< count_statements ($subject, $predicate, $object) >> | ||||||||||
| 55 | |||||||||||
| 56 | Returns a count of all the statements matching the specified subject, | ||||||||||
| 57 | predicate and objects. Any of the arguments may be undef to match any value. | ||||||||||
| 58 | |||||||||||
| 59 | =cut | ||||||||||
| 60 | |||||||||||
| 61 | 8 | ▶ | sub count_statements { | ||||||||
|
Subroutine
count_statements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 8 | my $self = shift; | |||||||||
| 63 | 8 | my $s = shift; | |||||||||
| 64 | 8 | my $p = shift; | |||||||||
| 65 | 8 | my $o = shift; | |||||||||
| 66 | 8 | my $c = shift; | |||||||||
| 67 | |||||||||||
| 68 | 8 | my $count = 0; | |||||||||
| 69 | 8 | my $i = $self->get_statements( $s, $p, $o, $c ); | |||||||||
| 70 | 8 | ▶ | while (my $s = $i->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 24 | $count++; | |||||||||
| 72 | } | ||||||||||
| 73 | 8 | return $count; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | =item C<< get_statements ($subject, $predicate, $object [, $context] ) >> | ||||||||||
| 77 | |||||||||||
| 78 | Returns a stream object of all statements matching the specified subject, | ||||||||||
| 79 | predicate and objects from all of the rdf stores. Any of the arguments may be | ||||||||||
| 80 | undef to match any value. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 14 | ▶ | sub get_statements { | ||||||||
|
Subroutine
get_statements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 85 | 14 | my $self = shift; | |||||||||
| 86 | 14 | my $s = shift; | |||||||||
| 87 | 14 | my $p = shift; | |||||||||
| 88 | 14 | my $o = shift; | |||||||||
| 89 | 14 | my $c = shift; | |||||||||
| 90 | |||||||||||
| 91 | 14 | ▶ | my $stream = sgrep { $self->apply_rules($_) } $self->SUPER::get_statements( $s, $p, $o, $c ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 92 | 14 | return $stream; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =item C<< get_pattern ( $bgp [, $context] ) >> | ||||||||||
| 96 | |||||||||||
| 97 | Returns a stream object of all bindings matching the specified graph pattern. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | 10 | ▶ | sub get_pattern { | ||||||||
|
Subroutine
get_pattern: called CC=11 SCAR=35.6
Pod
documented
| |||||||||||
| 102 | 10 | my $self = shift; | |||||||||
| 103 | 10 | my $bgp = shift; | |||||||||
| 104 | 10 | my $context = shift; | |||||||||
| 105 | 10 | my %args = @_; | |||||||||
| 106 | |||||||||||
| 107 | 10 | ▶ | if (my $o = $args{ orderby }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 5 | my @ordering = @$o; | |||||||||
| 109 | 5 | ▶ | while (my ($col, $dir) = splice( @ordering, 0, 2, () )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 111 | 7 | ▶ | unless ($dir =~ /^(ASC|DESC)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 1 | throw RDF::Trine::Error -text => 'Direction must be ASC or DESC in get_pattern call'; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 9 | my @rules = $self->rules; | |||||||||
| 118 | 9 | ▶ | if (@rules) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | ▶ | my (@triples) = ($bgp->isa('RDF::Trine::Statement')) ? $bgp : $bgp->triples; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | ▶ | unless (@triples) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | throw RDF::Trine::Error::CompilationError -text => 'Cannot call get_pattern() with empty pattern'; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | my @streams; | |||||||||
| 125 | 0 | foreach my $triple (@triples) { | |||||||||
| 126 | 0 | my @vars = map { $_->name } grep { $_->isa('RDF::Trine::Node::Variable') } $triple->nodes; | |||||||||
| 127 | 0 | ▶ | Carp::confess "not a statement object: " . Dumper($triple) unless ($triple->isa('RDF::Trine::Statement')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | my $stream = $self->get_statements( $triple->nodes, $context ); | |||||||||
| 129 | 0 | my $binds = $stream->as_bindings( $triple->nodes )->project( @vars ); | |||||||||
| 130 | 0 | push(@streams, $binds); | |||||||||
| 131 | } | ||||||||||
| 132 | 0 | ▶ | if (@streams) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | ▶ | while (@streams > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | my $a = shift(@streams); | |||||||||
| 135 | 0 | my $b = shift(@streams); | |||||||||
| 136 | 0 | unshift(@streams, RDF::Trine::Iterator::Bindings->join_streams( $a, $b )); | |||||||||
| 137 | } | ||||||||||
| 138 | } else { | ||||||||||
| 139 | 0 | push(@streams, RDF::Trine::Iterator::Bindings->new([{}], [])); | |||||||||
| 140 | } | ||||||||||
| 141 | 0 | my $stream = shift(@streams); | |||||||||
| 142 | 0 | return $stream; | |||||||||
| 143 | } else { | ||||||||||
| 144 | 9 | return $self->SUPER::get_pattern( $bgp, $context, %args ); | |||||||||
| 145 | } | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | =item C<< apply_rules ( $statement ) >> | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 31 | ▶ | sub apply_rules { | ||||||||
|
Subroutine
apply_rules: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 153 | 31 | my $self = shift; | |||||||||
| 154 | 31 | my $st = shift; | |||||||||
| 155 | 31 | my @rules = $self->rules; | |||||||||
| 156 | 31 | foreach my $rule (@rules) { | |||||||||
| 157 | 2 | ▶ | return 0 unless ($rule->( $st )); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | } | ||||||||||
| 159 | 30 | return 1; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | =item C<< rules >> | ||||||||||
| 163 | |||||||||||
| 164 | Returns a list of all rules in the inferencing model. | ||||||||||
| 165 | |||||||||||
| 166 | =cut | ||||||||||
| 167 | |||||||||||
| 168 | 40 | ▶ | sub rules { | ||||||||
|
Subroutine
rules: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 169 | 40 | my $self = shift; | |||||||||
| 170 | 40 | return @{ $self->{rules} }; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | =item C<< add_rule ( \&rule ) >> | ||||||||||
| 174 | |||||||||||
| 175 | Adds a rule to the inferencing model. The rule should be a CODE reference that, | ||||||||||
| 176 | when passed a statement object, will return true if the statement should be | ||||||||||
| 177 | allowed in the model, false if it should be filtered out. | ||||||||||
| 178 | |||||||||||
| 179 | =cut | ||||||||||
| 180 | |||||||||||
| 181 | 1 | ▶ | sub add_rule { | ||||||||
|
Subroutine
add_rule: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 182 | 1 | my $self = shift; | |||||||||
| 183 | 1 | my $rule = shift; | |||||||||
| 184 | 1 | ▶ | throw RDF::Trine::Error -text => "Filter must be a CODE reference" unless (reftype($rule) eq 'CODE'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 185 | 1 | push( @{ $self->{rules} }, $rule ); | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 1 | 1; | |||||||||
| 189 | |||||||||||
| 190 | __END__ | ||||||||||