| 1 | # RDF::Trine::Parser::LineProtocol | ||
| 2 | # ----------------------------------------------------------------------------- | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | RDF::Trine::Parser::LineProtocol - RDF LineProtocol Parser | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | This document describes RDF::Trine::Parser::LineProtocol version 1.019 | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use RDF::Trine::Parser::LineProtocol; | ||
| 15 | my $serializer = RDF::Trine::Parser::LineProtocol->new(); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | The RDF::Trine::Parser::LineProtocol class provides | ||
| 20 | A line-based protocol for querying and updating triple/quad stores. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | package RDF::Trine::Parser::LineProtocol; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use Scalar::Util qw(blessed); | ||
| 33 | use base qw(RDF::Trine::Parser::RDFPatch); | ||
| 34 | |||
| 35 | ###################################################################### | ||
| 36 | |||
| 37 | our ($VERSION); | ||
| 38 | BEGIN { | ||
| 39 | $VERSION = '1.019'; | ||
| 40 | } | ||
| 41 | |||
| 42 | ###################################################################### | ||
| 43 | |||
| 44 | =item C<< handle_op ( $op, $handler, $lineno ) >> | ||
| 45 | |||
| 46 | Handles the RDF::Trine::Parser::RDFPatch::Op operation object. | ||
| 47 | For 'A'dd operations, the C<< $handler >> callback is called with the RDF statement. | ||
| 48 | Otherwise an exception is thrown. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub handle_op { | ||
| 53 | my $self = shift; | ||
| 54 | my $op = shift; | ||
| 55 | my $handler = shift; | ||
| 56 | my $lineno = shift; | ||
| 57 | my $opid = $op->op; | ||
| 58 | if ($opid eq 'F') { | ||
| 59 | # no-op | ||
| 60 | } else { | ||
| 61 | return $self->SUPER::handle_op( $op, $handler, $lineno ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | =item C<< parse_line ( $line, $base ) >> | ||
| 66 | |||
| 67 | Parses the Line Protocol string C<< $line >> and returns the corresponding | ||
| 68 | L<RDF::Trine::Parser::RDFPatch::Op> object. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub parse_line { | ||
| 73 | my $self = shift; | ||
| 74 | my $line = shift; | ||
| 75 | my $base = shift; | ||
| 76 | |||
| 77 | my ($op, $tail) = split(/ /, $line, 2); | ||
| 78 | my $p = RDF::Trine::Parser::Turtle->new( 'map' => $self->{namespaces} ); | ||
| 79 | if ($op =~ /^[QF]$/) { | ||
| 80 | my @nodes; | ||
| 81 | foreach my $pos (1,2,3,4) { | ||
| 82 | if ($tail =~ /^\s*U\b/) { | ||
| 83 | substr($tail, 0, $+[0], ''); | ||
| 84 | my $v = RDF::Trine::Node::Variable->new("v$pos"); | ||
| 85 | $self->{last}[$pos] = $v; | ||
| 86 | push(@nodes, $v); | ||
| 87 | } elsif ($tail =~ /^\s*R\b/) { | ||
| 88 | substr($tail, 0, $+[0], ''); | ||
| 89 | my $node = $self->{last}[$pos]; | ||
| 90 | unless (blessed($node)) { | ||
| 91 | throw RDF::Trine::Error -text => "Use of non-existent `R`epeated term"; | ||
| 92 | } | ||
| 93 | push(@nodes, $node); | ||
| 94 | } elsif ($tail =~ /^\s*[.]/) { | ||
| 95 | last; | ||
| 96 | } else { | ||
| 97 | my $token; | ||
| 98 | my $n = $p->parse_node($tail, $base, token => \$token); | ||
| 99 | $self->{last}[$pos] = $n; | ||
| 100 | push(@nodes, $n); | ||
| 101 | my $len = $token->column; | ||
| 102 | substr($tail, 0, $len, ''); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | my @args; | ||
| 107 | if (scalar(@nodes) == 3) { | ||
| 108 | @args = RDF::Trine::Statement->new(@nodes); | ||
| 109 | } elsif (scalar(@nodes) == 4) { | ||
| 110 | @args = RDF::Trine::Statement::Quad->new(@nodes); | ||
| 111 | } else { | ||
| 112 | @args = @nodes; | ||
| 113 | } | ||
| 114 | |||
| 115 | return RDF::Trine::Parser::RDFPatch::Op->new( $op, @args ); | ||
| 116 | } else { | ||
| 117 | return $self->SUPER::parse_line( $line, $base ); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =item C<< execute_line ( $line, $model, $out ) >> | ||
| 122 | |||
| 123 | Parses the Line Protocol string C<< $line >> and executes the parsed operation using | ||
| 124 | the supplied C<< $model >>. If the operation returns results (in the case of a | ||
| 125 | Query operation), they are printed to the C<< $out >> file handle. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub execute_line { | ||
| 130 | my $self = shift; | ||
| 131 | my $line = shift; | ||
| 132 | my $model = shift; | ||
| 133 | my $out = shift; | ||
| 134 | |||
| 135 | return unless ($line =~ /^\S/); | ||
| 136 | if ($line =~ /[?]/) { | ||
| 137 | print $out $model->as_string; | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | |||
| 141 | my $op = $self->parse_line($line); | ||
| 142 | return unless ($op and blessed($op)); | ||
| 143 | my $map = $self->namespace_map; | ||
| 144 | if ($op->op =~ /^[AD]$/) { | ||
| 145 | $op->execute( $model ); | ||
| 146 | } elsif ($op->op eq 'Q') { | ||
| 147 | my $iter = $op->execute( $model ); | ||
| 148 | my $s = RDF::Trine::Serializer::Turtle->new( namespaces => $map ); | ||
| 149 | while (my $st = $iter->next) { | ||
| 150 | my @nodes = $st->nodes; | ||
| 151 | print $out join(' ', 'A', map { $s->serialize_node($_) } @nodes) . " .\n"; | ||
| 152 | } | ||
| 153 | print $out "F .\n"; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ |