| 1 | # RDF::Trine::Pattern | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | RDF::Trine::Pattern - Class for basic graph patterns | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | This document describes RDF::Trine::Pattern version 1.019 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | package RDF::Trine::Pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 68 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 68 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 68 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 68 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 68 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 68 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 68 | ▶ | use Carp qw(carp croak confess); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 68 | ▶ | use RDF::Trine::Iterator qw(smap); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 68 | ▶ | use RDF::Trine qw(iri); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 68 | our ($VERSION); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 68 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 68 | $VERSION = '1.019'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =item C<< new ( @triples ) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Returns a new BasicGraphPattern structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 991 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 991 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 991 | my @triples = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 991 | foreach my $t (@triples) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1039 | ▶ | unless (blessed($t) and $t->isa('RDF::Trine::Statement')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($t) and $t->isa("RDF::Trine::Statement")
MC/DC100%2/2
builtin::blessed($t) and $t->isa("RDF::Trine::Statement")
builtin::blessed($t)
$t->isa("RDF::Trine::Statement")
Truth table100%3/3
A and B
A= builtin::blessed($t)B= $t->isa("RDF::Trine::Statement")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | throw RDF::Trine::Error -text => "Patterns belonging to a BGP must be triples"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 989 | return bless( [ @triples ], $class ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | =item C<< construct_args >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | Returns a list of arguments that, passed to this class' constructor, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | will produce a clone of this algebra pattern. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub construct_args { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
construct_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return ($self->triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =item C<< triples >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | Returns a list of triples belonging to this BGP. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2953 | ▶ | sub triples { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
triples: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2953 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2953 | return @$self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | =item C<< type >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 25 | ▶ | sub type { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 25 | return 'BGP'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | =item C<< sse >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | Returns the SSE string for this algebra expression. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 31 | ▶ | sub sse { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 31 | my $context = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | return sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | '(bgp %s)', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 31 | join(' ', map { $_->sse( $context ) } $self->triples) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | =item C<< referenced_variables >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | Returns a list of the variable names used in this algebra expression. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 65 | ▶ | sub referenced_variables { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
referenced_variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 65 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 65 | return RDF::Trine::_uniq(map { $_->referenced_variables } $self->triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =item C<< definite_variables >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | Returns a list of the variable names that will be bound after evaluating this algebra expression. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub definite_variables { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
definite_variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | return RDF::Trine::_uniq(map { $_->definite_variables } $self->triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | =item C<< clone >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub clone { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | my $class = ref($self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | return $class->new( map { $_->clone } $self->triples ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | =item C<< bind_variables ( \%bound ) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | Returns a new pattern with variables named in %bound replaced by their corresponding bound values. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | sub bind_variables { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bind_variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | my $class = ref($self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | my $bound = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | return $class->new( map { $_->bind_variables( $bound ) } $self->triples ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =item C<< subsumes ( $statement ) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | Returns true if the pattern will subsume the $statement when matched against a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | triple store. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 5 | ▶ | sub subsumes { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
subsumes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 5 | my $st = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | my $l = Log::Log4perl->get_logger("rdf.trine.pattern"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 5 | my @triples = $self->triples; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | foreach my $t (@triples) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | ▶ | if ($t->subsumes( $st )) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 4 | $l->debug($self->sse . " \x{2292} " . $st->sse); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =item C<< merge_patterns ( @patterns ) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | Given an array of patterns, this will merge them into one. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 12 | ▶ | sub merge_patterns { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merge_patterns: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 12 | my ($class, @patterns) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 12 | my @all_triples; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 12 | foreach my $pattern (@patterns) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 12 | ▶ | unless (blessed($pattern) and $pattern->isa('RDF::Trine::Pattern')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($pattern) and $pattern->isa("RDF::Trine::Pattern")
MC/DC0%0/2
builtin::blessed($pattern) and $pattern->isa("RDF::Trine::Pattern")
builtin::blessed($pattern)
$pattern->isa("RDF::Trine::Pattern")
Truth table33%1/3
A and B
A= builtin::blessed($pattern)B= $pattern->isa("RDF::Trine::Pattern")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | throw RDF::Trine::Error -text => "Patterns to be merged must be patterns themselves"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 12 | push(@all_triples, $pattern->triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 12 | return $class->new(@all_triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | =item C<< sort_for_join_variables >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | Returns a new pattern object with the subpatterns of the referrant | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | sorted based on heuristics that ensure firstly that patterns can be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | joined on the same variable and secondly on the usual selectivity | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | (i.e. how quickly the engine can drill down to the answer) of triple | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | patterns. Calls C<< subgroup >>, C<< sort_triples >> and C<< | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | merge_patterns >> in that order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 915 | ▶ | sub sort_for_join_variables { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_for_join_variables: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 915 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 915 | ▶ | return $self if (scalar $self->triples == 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 12 | my $class = ref($self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 12 | my $l = Log::Log4perl->get_logger("rdf.trine.pattern"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 12 | $l->debug('Reordering ' . scalar $self->triples . ' triples for heuristical optimizations'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 12 | my @sorted_triple_patterns = $self->subgroup; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 12 | my @patterns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 12 | foreach my $pattern (@sorted_triple_patterns) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 12 | my $sorted = $pattern->sort_triples; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 12 | push(@patterns, $sorted); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 12 | return $class->merge_patterns(@patterns); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =item C<< subgroup >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | Splits the pattern object up in an array of pattern objects where the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | same triple patterns occur. It will group on common variables, so that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | triple patterns can be joined together is in a group together. It will | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | also group triples that have no connection to other triples in a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | group. It will then order the groups, first by number triples with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | common variables, then by number of literals, then by the total number | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | of terms that are not variables. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 12 | ▶ | sub subgroup { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
subgroup: called CC=21 SCAR=33.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 12 | my @triples = $self->triples; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 12 | my $l = Log::Log4perl->get_logger("rdf.trine.pattern"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | my %structure_counts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | my %triples_by_tid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # First, we loop the dataset to compile some numbers for the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # variables in each triple pattern. This is to break the pattern | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # into subpatterns that can be joined on the same variable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 12 | foreach my $t (@triples) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 24 | my $tid = refaddr($t); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 24 | $triples_by_tid{$tid} = $t; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 24 | my $not_variable = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 24 | foreach my $n ($t->nodes) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 80 | ▶ | if ($n->isa('RDF::Trine::Node::Variable')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 31 | my $name = $n->name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 31 | $structure_counts{ $name }{ 'name' } = $name; # TODO: Worth doing in an array? | |||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 31 | push(@{$structure_counts{$name}{'claimed_patterns'}}, $tid); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 31 | $structure_counts{ $name }{ 'common_variable_count' }++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 31 | ▶ | $structure_counts{ $name }{ 'not_variable_count' } = 0 unless ($structure_counts{ $name }{ 'not_variable_count' }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 31 | ▶ | $structure_counts{ $name }{ 'literal_count' } = 0 unless ($structure_counts{ $name }{ 'literal_count' }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 31 | foreach my $char (split(//, $n->as_string)) { # TODO: Use a more standard format | |||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 93 | $structure_counts{ $name }{ 'string_sum' } += ord($char); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 31 | foreach my $o ($t->nodes) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 99 | ▶ | unless ($o->isa('RDF::Trine::Node::Variable')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 48 | $structure_counts{ $name }{ 'not_variable_count' }++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | elsif ($o->isa('RDF::Trine::Node::Literal')) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | $structure_counts{ $name }{ 'literal_count' }++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 49 | $not_variable++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 24 | ▶ | if ($not_variable == 3) { # Then, there are no variables in the pattern | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 6 | my $name = '_no_definite'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 6 | $structure_counts{ $name }{ 'not_variable_count' } = $not_variable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 6 | $structure_counts{ $name }{ 'common_variable_count' } = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 6 | $structure_counts{ $name }{ 'literal_count' } = 0; # Doesn't mean anything now | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 6 | $structure_counts{ $name }{ 'string_sum' } = 0; # Doesn't mean anything now | |||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | push(@{$structure_counts{$name}{'claimed_patterns'}}, $tid); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Group triple subpatterns with just one triple pattern | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 12 | my $just_ones; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 12 | ▶ | while (my ($name, $data) = each(%structure_counts)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 27 | ▶ | if($data->{'common_variable_count'} <= 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 19 | $just_ones->{'common_variable_count'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 19 | $just_ones->{'string_sum'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 19 | $just_ones->{'literal_count'} += $data->{'literal_count'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 19 | $just_ones->{'not_variable_count'} += $data->{'not_variable_count'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 19 | my @claimed = @{$data->{'claimed_patterns'}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 19 | ▶ | unless (any { $_ == $claimed[0] } @{$just_ones->{'claimed_patterns'}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 14 | push(@{$just_ones->{'claimed_patterns'}}, $claimed[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 19 | delete $structure_counts{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 12 | $l->trace('Results of structural analysis: ' . Dumper(\%structure_counts)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 12 | $l->trace('Block of single-triple patterns: ' . Dumper($just_ones)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Now, sort the patterns in the order specified by first the number | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # of occurances of common variables, then the number of literals | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # and then the number of terms that are not variables | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 12 | my @sorted_patterns = sort { $b->{'common_variable_count'} <=> $a->{'common_variable_count'} | |||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | or $b->{'literal_count'} <=> $a->{'literal_count'} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | or $b->{'not_variable_count'} <=> $a->{'not_variable_count'} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | or $b->{'string_sum'} <=> $a->{'string_sum'} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$RDF::Trine::Pattern::b->{'common_variable_count'} <=> $RDF::Trine::Pattern::a->{'common_variable_count'} || $RDF::Trine::Pattern::b->{'literal_count'} <=> $RDF::Trine::Pattern::a->{'literal_count'} || $RDF::Trine::Pattern::b->{'not_variable_count'} <=> $RDF::Trine::Pattern::a->{'not_variable_count'} || $RDF::Trine::Pattern::b->{'string_sum'} <=> $RDF::Trine::Pattern::a->{'string_sum'}
$RDF::Trine::Pattern::b->{"common_variable_count"} <=> $RDF::Trine::Pattern::a->{"common_variable_count"} or $RDF::Trine::Pattern::b->{"literal_count"} <=> $RDF::Trine::Pattern::a->{"literal_count"} or $RDF::Trine::Pattern::b->{"not_variable_count"} <=> $RDF::Trine::Pattern::a->{"not_variable_count"}
$RDF::Trine::Pattern::b->{"common_variable_count"} <=> $RDF::Trine::Pattern::a->{"common_variable_count"} or $RDF::Trine::Pattern::b->{"literal_count"} <=> $RDF::Trine::Pattern::a->{"literal_count"}
MC/DC0%0/4
$RDF::Trine::Pattern::b->{'common_variable_count'} <=> $RDF::Trine::Pattern::a->{'common_variable_count'} || $RDF::Trine::Pattern::b->{'literal_count'} <=> $RDF::Trine::Pattern::a->{'literal_count'} || $RDF::Trine::Pattern::b->{'not_variable_count'} <=> $RDF::Trine::Pattern::a->{'not_variable_count'} || $RDF::Trine::Pattern::b->{'string_sum'} <=> $RDF::Trine::Pattern::a->{'string_sum'}
$RDF::Trine::Pattern::b->{"common_variable_count"} <=> $RDF::Trine::Pattern::a->{"common_variable_count"}
$RDF::Trine::Pattern::b->{"literal_count"} <=> $RDF::Trine::Pattern::a->{"literal_count"}
$RDF::Trine::Pattern::b->{"not_variable_count"} <=> $RDF::Trine::Pattern::a->{"not_variable_count"}
$RDF::Trine::Pattern::b->{'string_sum'} <=> $RDF::Trine::Pattern::a->{'string_sum'}
Truth table0%0/5
A or B or C || D
A= $RDF::Trine::Pattern::b->{"common_variable_count"} <=> $RDF::Trine::Pattern::a->{"common_variable_count"}B= $RDF::Trine::Pattern::b->{"literal_count"} <=> $RDF::Trine::Pattern::a->{"literal_count"}C= $RDF::Trine::Pattern::b->{"not_variable_count"} <=> $RDF::Trine::Pattern::a->{"not_variable_count"}D= $RDF::Trine::Pattern::b->{'string_sum'} <=> $RDF::Trine::Pattern::a->{'string_sum'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } values(%structure_counts); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 12 | push (@sorted_patterns, $just_ones); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 12 | my @sorted_triple_patterns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # Now, loop through the sorted patterns, let the one with most | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # weight first select the triples it wants to join. Within those | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # subpatterns, apply the sort order of triple pattern heuristic | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 12 | foreach my $item (@sorted_patterns) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 12 | my @triple_patterns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 12 | my $triples_left = scalar keys(%triples_by_tid); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 12 | ▶ | if ($triples_left > 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | foreach my $tid (@{$item->{'claimed_patterns'}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if (defined($triples_by_tid{$tid})) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | push(@triple_patterns, $triples_by_tid{$tid}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | delete $triples_by_tid{$tid}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $l->debug("There are $triples_left triples left"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | push(@sorted_triple_patterns, RDF::Trine::Pattern->new(@triple_patterns)); # TODO: Better way to call ourselves? | |||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 12 | $l->debug("There is a rest of $triples_left triples"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 12 | push(@sorted_triple_patterns, RDF::Trine::Pattern->new(values(%triples_by_tid))); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 12 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 12 | return @sorted_triple_patterns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =item C<< sort_triples >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | Will sort the triple patterns based on heuristics that looks at how | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | many variables the patterns have, and where they occur, see REFERENCES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | for details. Returns a new sorted pattern object. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 12 | ▶ | sub sort_triples { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_triples: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 12 | return $self->_hsp_heuristic_1_4_triple_pattern_order; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 12 | ▶ | sub _hsp_heuristic_1_4_triple_pattern_order { # Heuristic 1 and 4 of HSP | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hsp_heuristic_1_4_triple_pattern_order: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 12 | my $class = ref($self); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 12 | my @triples = @$self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 12 | ▶ | return $self if (scalar @triples == 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 12 | my %triples_by_tid; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 12 | foreach my $t (@triples) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 24 | my $tid = refaddr($t); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 24 | $triples_by_tid{$tid}{'tid'} = $tid; # TODO: Worth doing this in an array? | |||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 24 | $triples_by_tid{$tid}{'triple'} = $t; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 24 | $triples_by_tid{$tid}{'sum'} = _hsp_heuristic_triple_sum($t); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 12 | my @sorted_tids = sort { $a->{'sum'} <=> $b->{'sum'} } values(%triples_by_tid); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 12 | my @sorted_triples; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 12 | foreach my $entry (@sorted_tids) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 24 | push(@sorted_triples, $triples_by_tid{$entry->{'tid'}}->{'triple'}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 12 | return $class->new(@sorted_triples); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # The below function finds a number to aid sorting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # It takes into account Heuristic 1 and 4 of the HSP paper, see REFERENCES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # as well as that it was noted in the text that rdf:type is usually less selective. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # By assigning the integers to nodes, depending on whether they are in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # triple (subject, predicate, object), variables, rdf:type and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # literals, and sum them, they may be sorted. See code for the actual | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # values used. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # Denoting s for bound subject, p for bound predicate, a for rdf:type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # as predicate, o for bound object and l for literal object and ? for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # variable, we get the following order, most of which are identical to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # the HSP: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # spl: 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # spo: 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # sao: 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # s?l: 14 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # s?o: 16 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # ?pl: 25 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | # ?po: 27 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | # sp?: 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | # sa?: 32 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # ??l: 33 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | # ??o: 35 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | # s??: 38 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | # ?p?: 49 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | # ?a?: 51 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # ???: 57 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | # Note that this number is not intended as an estimate of selectivity, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # merely a sorting key, but further research may possibly create such | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | # numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 24 | ▶ | sub _hsp_heuristic_triple_sum { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hsp_heuristic_triple_sum: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 24 | my $t = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 24 | my $sum = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 24 | ▶ | if ($t->subject->is_variable) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 15 | $sum = 20; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 9 | $sum = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 24 | ▶ | if ($t->predicate->is_variable) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 2 | $sum += 10; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 22 | ▶ | if ($t->predicate->equal(iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'))) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 7 | $sum += 4; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 15 | $sum += 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 24 | ▶ | if ($t->object->is_variable) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 14 | $sum += 27; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } elsif ($t->object->is_literal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 3 | $sum += 3; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 7 | $sum += 5; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 24 | my $l = Log::Log4perl->get_logger("rdf.trine.pattern"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # Now a trick to get an deterministic sort order, hard to test without. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 24 | $sum *= 10000000; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 24 | foreach my $c (split(//,$t->as_string)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 1643 | $sum += ord($c); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 24 | $l->debug($t->as_string . " triple has sorting sum " . $sum); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 24 | return $sum; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 68 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||