| 1 | # | ||
| 2 | # The contents of this file are subject to the Mozilla Public | ||
| 3 | # License Version 1.1 (the "License"); you may not use this file | ||
| 4 | # except in compliance with the License. You may obtain a copy of | ||
| 5 | # the License at http://www.mozilla.org/MPL/ | ||
| 6 | # | ||
| 7 | # Software distributed under the License is distributed on an "AS | ||
| 8 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
| 9 | # implied. See the License for the specific language governing | ||
| 10 | # rights and limitations under the License. | ||
| 11 | # | ||
| 12 | # The Original Code is the RDF::Core module | ||
| 13 | # | ||
| 14 | # The Initial Developer of the Original Code is Ginger Alliance Ltd. | ||
| 15 | # Portions created by Ginger Alliance are | ||
| 16 | # Copyright (C) 2001 Ginger Alliance Ltd. | ||
| 17 | # All Rights Reserved. | ||
| 18 | # | ||
| 19 | # Contributor(s): | ||
| 20 | # | ||
| 21 | # Alternatively, the contents of this file may be used under the | ||
| 22 | # terms of the GNU General Public License Version 2 or later (the | ||
| 23 | # "GPL"), in which case the provisions of the GPL are applicable | ||
| 24 | # instead of those above. If you wish to allow use of your | ||
| 25 | # version of this file only under the terms of the GPL and not to | ||
| 26 | # allow others to use your version of this file under the MPL, | ||
| 27 | # indicate your decision by deleting the provisions above and | ||
| 28 | # replace them with the notice and other provisions required by | ||
| 29 | # the GPL. If you do not delete the provisions above, a recipient | ||
| 30 | # may use your version of this file under either the MPL or the | ||
| 31 | # GPL. | ||
| 32 | # | ||
| 33 | |||
| 34 | package RDF::Core::Function; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | #require RDF::Core::Query; | ||
| 40 | use Carp; | ||
| 41 | |||
| 42 | use RDF::Core::Constants qw(:rdfs); | ||
| 43 | |||
| 44 | |||
| 45 | sub new { | ||
| 46 | my ($pkg, %options) = @_; | ||
| 47 | $pkg = ref $pkg || $pkg; | ||
| 48 | my $self = {}; | ||
| 49 | $self->{_options} = \%options; | ||
| 50 | $self->{_functions} = {self=>\&self, | ||
| 51 | subclass=>\&subclass, | ||
| 52 | subproperty=>\&subproperty, | ||
| 53 | member=>\&member, | ||
| 54 | }; | ||
| 55 | bless $self, $pkg; | ||
| 56 | } | ||
| 57 | sub getOptions { | ||
| 58 | my $self = shift; | ||
| 59 | return $self->{_options}; | ||
| 60 | } | ||
| 61 | sub getFunctions { | ||
| 62 | my $self = shift; | ||
| 63 | return $self->{_functions}; | ||
| 64 | } | ||
| 65 | ############################################################ | ||
| 66 | |||
| 67 | sub self { | ||
| 68 | my ($self, $subject, $params) = @_; | ||
| 69 | my $retVal = []; | ||
| 70 | my $predicates = []; | ||
| 71 | $retVal = defined $subject ? [$subject] : [$params->[0]]; | ||
| 72 | return ($retVal,$predicates); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub subclass { | ||
| 76 | my ($self, $subject, $params) = @_; | ||
| 77 | my $predicates = []; | ||
| 78 | |||
| 79 | croak "Function subclass expects one parameter." | ||
| 80 | unless @$params == 1; | ||
| 81 | croak "Subject parameter not allowed for function subclass." if $subject; | ||
| 82 | my @subClasses; | ||
| 83 | my $pred = $self->getOptions->{Factory}->newResource(RDFS_SUBCLASS_OF); | ||
| 84 | my $enum = $self->getOptions->{Schema}->getStmts(undef,$pred,$params->[0]); | ||
| 85 | while (my $st = $enum->getNext) { | ||
| 86 | push @subClasses, $st->getSubject; | ||
| 87 | my @sub = $self->subclass(undef, [$st->getSubject]); | ||
| 88 | push @subClasses, @{$sub[0]}; | ||
| 89 | } | ||
| 90 | $enum->close; | ||
| 91 | |||
| 92 | return (\@subClasses, $predicates); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub subproperty { | ||
| 96 | my ($self, $subject, $params) = @_; | ||
| 97 | my $retVal= []; | ||
| 98 | my $predicates = []; | ||
| 99 | |||
| 100 | croak "Function subproperty expects one parameter." | ||
| 101 | unless @$params == 1; | ||
| 102 | my @subProperties; | ||
| 103 | my $pred = $self->getOptions->{Factory}->newResource(RDFS_SUBPROPERTY_OF); | ||
| 104 | my $enum = $self->getOptions->{Schema}->getStmts(undef,$pred,$params->[0]); | ||
| 105 | while (my $st = $enum->getNext) { | ||
| 106 | push @subProperties, $st->getSubject; | ||
| 107 | my @sub = $self->subproperty(undef, [$st->getSubject]); | ||
| 108 | push @subProperties, @{$sub[0]}; | ||
| 109 | } | ||
| 110 | $enum->close; | ||
| 111 | if ($subject) { | ||
| 112 | unless ($subject->isLiteral) { | ||
| 113 | foreach my $property (@subProperties) { | ||
| 114 | my $enum = $self->getOptions->{Data}->getStmts($subject, | ||
| 115 | $property, | ||
| 116 | undef); | ||
| 117 | while (my $st = $enum->getNext) { | ||
| 118 | push @$retVal, $st->getObject; | ||
| 119 | push @$predicates, $st->getPredicate; | ||
| 120 | } | ||
| 121 | $enum->close; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } else { | ||
| 125 | $retVal = \@subProperties; | ||
| 126 | } | ||
| 127 | |||
| 128 | return ($retVal, $predicates); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub member { | ||
| 132 | my ($self, $subject, $params) = @_; | ||
| 133 | my @retVal; | ||
| 134 | my @predicates; | ||
| 135 | my @members; | ||
| 136 | my @sorted_members; | ||
| 137 | |||
| 138 | if ($subject && !$subject->isLiteral) { | ||
| 139 | my $enum = $self->getOptions->{Data}-> | ||
| 140 | getStmts($subject,undef,undef); | ||
| 141 | while (my $st = $enum->getNext) { | ||
| 142 | if ($st->getPredicate->getURI =~ /\#\_(\d+)$/) { | ||
| 143 | push @members, [$st->getObject,$st->getPredicate,$1]; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | @sorted_members = sort { $a->[2] <=> $b->[2] } @members; | ||
| 147 | } | ||
| 148 | foreach (@sorted_members) { | ||
| 149 | push @retVal, $_->[0]; | ||
| 150 | push @predicates, $_->[1]; | ||
| 151 | } | ||
| 152 | return (\@retVal, \@predicates); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | 1; | ||
| 159 | __END__ |