Commits

Konstantin Baierer committed 30d6558

finally working export_to_web test, started RDF::Trine::Serializer::SparqlUpdateInsert

Comments (0)

Files changed (7)

         # 'Some::Module'  => '1.23',
         # 'Other::Module' => '>= 1.2, != 1.5, < 2.0',
     },
+    build_requires => {
+        'RDF::Endpoint' => '0.04',
+    },
 );
 $build->create_build_script;

lib/MooseX/Semantic/Role/RdfExport.pm

 package MooseX::Semantic::Role::RdfExport;
 use Moose::Role;
+use RDF::Trine::Serializer::SparqlUpdateInsert;
 use RDF::Trine qw(iri);
 use RDF::Trine::Namespace qw(rdf);
 use LWP::UserAgent;
     
     ### XXX: It would be handy if there were an application/sparql-update
     ###      serializer for Trine.
+    ### kb Tue Nov 29 03:55:55 CET 2011
+    #   started sparqlu_insert serializer
     my $ser = $self->_get_serializer(%opts);
     my ($type) = $ser->media_types;
     

lib/MooseX/Semantic/Test/MockServer.pm

-#=======================================================================
-#         FILE:  MockServer.pm
-#  DESCRIPTION:  
-#       AUTHOR:  Konstantin Baierer (kba), konstantin.baierer@gmail.com
-#      CREATED:  11/28/2011 07:11:09 PM
-#    COPYRIGHT:  Artistic License 2.0
-#=======================================================================
-package MooseX::Semantic::Test::MockServer;
-use Moose;
-use Data::Dumper;
-use HTTP::Daemon;
-use HTTP::Status;
-use LWP::Simple;
-
-# my $RUN_PID = 0;
-has RUN_PID => (is => 'rw', default => 0);
-
-sub run {
-    my $self = shift;
-    # return if $pid != 0;
-
-    my $d = HTTP::Daemon->new(
-        LocalPort => 54321,
-    ) || do {
-        warn "$@";
-        return;
-    };
-    # my $pid = fork();
-    # if ($pid == 0) {
-        # # $self->RUN_PID($pid);
-        # return;
-    # }
-    # $self->RUN_PID($pid);
-
-    # warn  "Started mock server @ ", $d->url, " .";
-    CONN:
-    while (my $c = $d->accept ) {
-        while ( my $r = $c->get_request ) {
-            if ($r->uri eq '/shutdown') {
-                warn "shutting down server";
-                $c->close;
-                exit;
-                # last CONN;
-            }
-            if ( $r->method =~ 'PUT|POST' ) {
-                $c->send_status_line( RC_OK );
-            }
-        }
-        $c->close;
-        undef($c);
-    }
-    # wait;
-}
-
-sub kill {
-    # my $self = shift;
-    get('localhost:54321/shutdown');
-    # kill 1, $self->RUN_PID;
-    # $self->RUN_PID(0);
-    # wait();# $RUN_PID ;
-    # wait;
-    # exit;
-}
-
-
-1;

lib/RDF/Trine/Serializer/SparqlUpdateInsert.pm

+# RDF::Trine::Serializer::SparqlUpdateInsert
+# -----------------------------------------------------------------------------
+
+=head1 NAME
+
+RDF::Trine::Serializer::NTriples - N-Triples Serializer
+
+=head1 VERSION
+
+This document describes RDF::Trine::Serializer::NTriples version 0.136
+
+=head1 SYNOPSIS
+
+ use RDF::Trine::Serializer::NTriples;
+ my $serializer	= RDF::Trine::Serializer::NTriples->new();
+
+=head1 DESCRIPTION
+
+The RDF::Trine::Serializer::NTriples class provides an API for serializing RDF
+graphs to the N-Triples syntax.
+
+=head1 METHODS
+
+Beyond the methods documented below, this class inherits methods from the
+L<RDF::Trine::Serializer> class.
+
+=over 4
+
+=cut
+package RDF::Trine::Serializer::SparqlUpdateInsert;
+
+use strict;
+use warnings;
+no warnings 'redefine';
+use base qw(RDF::Trine::Serializer);
+
+use URI;
+use Carp;
+use Data::Dumper;
+use Scalar::Util qw(blessed);
+
+use RDF::Trine::Node;
+use RDF::Trine::Statement;
+use RDF::Trine::Error qw(:try);
+
+######################################################################
+
+our ($VERSION);
+BEGIN {
+	$VERSION	= '0.136';
+	$RDF::Trine::Serializer::serializer_names{ 'sparqluinsert' }	= __PACKAGE__;
+	$RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/Submission/SPARQL-Update/' }	= __PACKAGE__;
+	foreach my $type (qw(application/sparql-update)) {
+		$RDF::Trine::Serializer::media_types{ $type }	= __PACKAGE__;
+	}
+}
+
+######################################################################
+
+=item C<< new >>
+
+Returns a new N-Triples serializer object.
+
+=cut
+
+sub new {
+	my $class	= shift;
+	my %args	= @_;
+	my $self = bless( {}, $class);
+	return $self;
+}
+
+=item C<< serialize_model_to_file ( $fh, $model ) >>
+
+Serializes the C<$model> to N-Triples, printing the results to the supplied
+filehandle C<<$fh>>.
+
+=cut
+
+sub serialize_model_to_file {
+	my $self	= shift;
+	my $file	= shift;
+	my $model	= shift;
+	my $st		= RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
+	my $pat		= RDF::Trine::Pattern->new( $st );
+	my $stream	= $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
+	my $iter	= $stream->as_statements( qw(s p o) );
+	while (my $st = $iter->next) {
+		print {$file} $self->statement_as_string( $st );
+	}
+}
+
+=item C<< serialize_model_to_string ( $model ) >>
+
+Serializes the C<$model> to N-Triples, returning the result as a string.
+
+=cut
+
+sub serialize_model_to_string {
+	my $self	= shift;
+	my $model	= shift;
+	my $st		= RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
+	my $pat		= RDF::Trine::Pattern->new( $st );
+	my $stream	= $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
+	my $iter	= $stream->as_statements( qw(s p o) );
+	
+	my $string	= "INSERT DATA {";;
+	while (my $st = $iter->next) {
+		my @nodes	= $st->nodes;
+		$string		.= $self->statement_as_string( $st );
+	}
+
+    $string .= "}";
+	return $string;
+}
+
+=item C<< serialize_iterator_to_file ( $file, $iter ) >>
+
+Serializes the iterator to N-Triples, printing the results to the supplied
+filehandle C<<$fh>>.
+
+=cut
+
+sub serialize_iterator_to_file {
+	my $self	= shift;
+	my $file	= shift;
+	my $iter	= shift;
+	while (my $st = $iter->next) {
+		print {$file} $self->statement_as_string( $st );
+	}
+}
+
+=item C<< serialize_iterator_to_string ( $iter ) >>
+
+Serializes the iterator to N-Triples, returning the result as a string.
+
+=cut
+
+sub serialize_iterator_to_string {
+	my $self	= shift;
+	my $iter	= shift;
+	my $string	= '';
+	while (my $st = $iter->next) {
+		my @nodes	= $st->nodes;
+		$string		.= $self->statement_as_string( $st );
+	}
+	return $string;
+}
+
+sub _serialize_bounded_description {
+	my $self	= shift;
+	my $model	= shift;
+	my $node	= shift;
+	my $seen	= shift || {};
+	return '' if ($seen->{ $node->sse }++);
+	my $iter	= $model->get_statements( $node, undef, undef );
+	my $string	= '';
+	while (my $st = $iter->next) {
+		my @nodes	= $st->nodes;
+		$string		.= $self->statement_as_string( $st );
+		if ($nodes[2]->isa('RDF::Trine::Node::Blank')) {
+			$string	.= $self->_serialize_bounded_description( $model, $nodes[2], $seen );
+		}
+	}
+	return $string;
+}
+
+=item C<< statement_as_string ( $st ) >>
+
+Returns a string with the supplied RDF::Trine::Statement object serialized as
+N-Triples, ending in a DOT and newline.
+
+=cut
+
+sub statement_as_string {
+	my $self	= shift;
+	my $st		= shift;
+	my @nodes	= $st->nodes;
+	my $to_return .= join(' ', map { $_->as_ntriples } @nodes[0..2]) . " .\n";
+    return $to_return;
+}
+
+=item C<< serialize_node ( $node ) >>
+
+Returns a string containing the N-Triples serialization of C<< $node >>.
+
+=cut
+
+sub serialize_node {
+	my $self	= shift;
+	my $node	= shift;
+	return $node->as_ntriples;
+}
+
+1;
+
+__END__
+
+=back
+
+=head1 SEE ALSO
+
+L<http://www.w3.org/TR/rdf-testcases/#ntriples>
+
+=head1 AUTHOR
+
+Gregory Todd Williams  C<< <gwilliams@cpan.org> >>
+
+=head1 COPYRIGHT
+
+Copyright (c) 2006-2010 Gregory Todd Williams. This
+program is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut

t/config/rdf_endpoint.yaml

+store: 'Memory'
+endpoint: 
+  endpoint_path: '/'
+  update: 1
+  load_data: 1
+  html: 
+    resource_links: 1    # turn resources into links in HTML query result pages
+    embed_images: 0    # display foaf:Images as images in HTML query result pages
+    image_width: 200  # with 'embed_images' scale images to this width
+  service_description: 
+      default: 1    # generate dataset description of the default graph
+      named_graphs: 1    # generate dataset description of the available named graphs

t/rdf_export/mock_server.t

-use Test::More skip_all => 'bad solution as of now'; 
-use Test::Moose;
-use Data::Dumper;
-use MooseX::Semantic::Test::MockServer;
-use MooseX::Semantic::Test::Person;
-
-my $server = MooseX::Semantic::Test::MockServer->new;
-my $pid = fork;
-if ($pid) {
-    $server->run;
-}
-
-my $p = MooseX::Semantic::Test::Person->new(
-    'name' => 'ABC',
-);
-my $resp =
-  $p->export_to_web( PUT => 'http://localhost:54321', format => 'turtle' );
-my $in_turtle = $p->export_to_string( format => 'turtle' );
-
-is($resp->request->content, $in_turtle, 'Server received correct serialization');
-is($resp->request->method, 'PUT', 'Server was requested using PUT');
-
-# HTTP::Request
-
-done_testing;
-$server->kill;
-# exit;

t/rdf_export/rdf_endpoint.t

+use Test::More; 
+use Test::Moose;
+use Data::Dumper;
+use RDF::Endpoint;
+use LWP::Protocol::PSGI;
+use Config::JFDI;
+use MooseX::Semantic::Test::Person;
+my $endpoint_config = Config::JFDI->open( name => "RDF::Endpoint", path => 't/config') or die "Couldn't find config";
+my $model = RDF::Trine::Model->temporary_model;
+my $end     = RDF::Endpoint->new( $model, $endpoint_config );
+my $end_app = sub {
+    my $env 	= shift;
+    my $req 	= Plack::Request->new($env);
+    my $resp	= $end->run( $req );
+	return $resp->finalize;
+};
+
+# XXX this is pure awesomeness for testing
+LWP::Protocol::PSGI->register($end_app);
+
+my $p = MooseX::Semantic::Test::Person->new(
+    'name' => 'ABC',
+);
+
+my $in_sparu = $p->export_to_string( format => 'sparqlu_insert' );
+# warn Dumper $in_sparu;
+
+my $model_size_old = $end->{model}->size;
+is( $model_size_old, 0, 'Start with empty model');
+my $resp = $p->export_to_web( POST => "http://localhost", format => 'sparqlu_insert' );
+is( $resp->status_line, "200 OK", 'export was accepted by RDF::Endpoint');
+is( $resp->request->method, 'POST', 'Server was requested using POST');
+my $model_size_new = $end->{model}->size;
+is($model_size_new, 2, 'Model now contains exported rdf data.');
+
+is($resp->request->content, $in_sparu, 'Server received correct serialization');
+
+done_testing;