1. Toby Inkster
  2. p5-rdf-trinex-functions

Commits

Toby Inkster  committed 23a8030

allow "graph" option to parse

  • Participants
  • Parent commits efc9984
  • Branches default
  • Tags 0.004

Comments (0)

Files changed (4)

File lib/RDF/TrineX/Functions.pm

View file
 
 BEGIN {
 	$RDF::TrineX::Functions::AUTHORITY = 'cpan:TOBYINK';
-	$RDF::TrineX::Functions::VERSION   = '0.003';
+	$RDF::TrineX::Functions::VERSION   = '0.004';
 }
 
 use Carp qw< croak >;
 use IO::Detect 0.003 qw< is_filehandle is_filename is_fileuri >;
+use PerlX::Maybe qw< maybe >;
 use RDF::NS::Trine;
 use RDF::Trine qw< store >;
 use RDF::Trine::Namespace qw< rdf rdfs owl xsd >;
 	
 	my ($thing, %opts) = @_;
 	
-	my $model  = delete($opts{into}) // delete($opts{model});
+	my $model  = delete($opts{into})   // delete($opts{model});
 	my $base   = delete($opts{base});
 	my $parser = delete($opts{parser}) // delete($opts{type})  // delete($opts{as}) // delete($opts{using});
-	
+	my $graph  = delete($opts{graph})  // delete($opts{context});
+
+	# Normalise $graph.
+	#
+	$graph = iri($graph) if defined $graph;
+
 	if (blessed($thing) && $thing->isa('RDF::Trine::Store')
 	or  blessed($thing) && $thing->isa('RDF::Trine::Model'))
 	{
 		return model($thing) unless $model;
 		
 		$thing->as_stream->each(sub {
-			$model->add_statement($_[0]);
+			my ($s, $p, $o) = shift->nodes;
+			$model->add_statement(
+				$graph
+					? statement($s, $p, $o, $graph)
+					: statement($s, $p, $o)
+			);
 		});
 		return $model;
 	}
 		}
 		elsif (not ref $parser and $parser eq 'RDF::Trine::Parser')
 		{
-			RDF::Trine::Parser->parse_url_into_model("$thing", $model);
+			RDF::Trine::Parser->parse_url_into_model(
+				"$thing",
+				$model,
+				maybe context => $graph,
+			);
 			return $model;
 		}
 		else
 			# UA string consistent with RDF::Trine::Parser
 			my $ua   = LWP::UserAgent->new(agent => "RDF::Trine/$RDF::Trine::VERSION");
 			my $resp = $ua->get("$thing");
-			$parser->parse_into_model(("$base"//"$thing"), $resp->decoded_content, $model);
+			$parser->parse_into_model(
+				("$base"//"$thing"),
+				$resp->decoded_content,
+				$model,
+				maybe context => $graph,
+			);
 			return $model;
 		}
 	}
 	if (is_filename $thing)
 	{
 		$base //= URI::file->new_abs("$thing");
-		$parser->parse_file_into_model("$base", "$thing", $model);
+		$parser->parse_file_into_model(
+			"$base",
+			"$thing",
+			$model,
+			maybe context => $graph,
+		);
 		return $model;
 	}
 	
 	#
 	if (is_filehandle $thing)
 	{
-		$parser->parse_file_into_model("$base", $thing, $model);
+		$parser->parse_file_into_model(
+			"$base",
+			$thing,
+			$model,
+			maybe context => $graph,
+		);
 		return $model;
 	}
 	
 	croak "No parser provided for parsing" unless blessed $parser;
-	$parser->parse_into_model("$base", $thing, $model);
+	$parser->parse_into_model(
+		"$base",
+		$thing,
+		$model,
+		maybe context => $graph,
+	);
 	
 	return $model;
 }
 object to use; the C<type> option can be used instead to provide a media
 type hint. The C<base> option provides the base URI. The C<model> option
 can be used to tell this function to parse into an existing model rather
-than returning a new one.
+than returning a new one. The C<graph> option may be used to provide a graph
+URI.
 
 C<into> is an alias for C<model>; C<type>, C<using> and C<as> are
-aliases for C<parser>.
+aliases for C<parser>; C<context> is an alias for C<graph>.
 
 Examples:
 

File meta/changes.pret

View file
 		item "Use IO::Detect to simplify the parse function."^^Update;
 		item "The parse function now accepts unblessed 'data:' URIs."^^Addition;
 	].
+
+`RDF-TrineX-Functions 0.004 cpan:TOBYINK`
+	issued    2012-07-11;
+	changeset [
+		item "The parse function now accepts a 'graph' option."^^Addition;
+	].

File meta/makefile.pret

View file
 	requires           p`URI::file`;
 	requires           p`Carp`;
 	requires           p`Sub::Exporter`;
+	requires           p`PerlX::Maybe`;
 	test_requires      p`Test::More 0.61`;
 	.
 

File t/02parse_and_serialise.t

View file
-use Test::More tests => 5;
+use Test::More tests => 10;
 
 use RDF::Trine;
 use RDF::TrineX::Functions
+	iri       => {},
 	parse     => {},
 	serialize => { -type => RDF::Trine::Serializer::NTriples::Canonical->new };
 
 		is($out, $expected, "serialize OK");
 	}
 }
+
+my $model = parse();
+isa_ok $model => 'RDF::Trine::Model';
+
+parse(
+	$in{data},
+	as    => 'NTriples',
+	into  => $model,
+	graph => 'http://example.net/g1',
+	base  => 'http://example.net/',
+);
+
+parse(
+	$in{data},
+	as    => 'NTriples',
+	into  => $model,
+	graph => iri('http://example.net/g1'),
+	base  => 'http://example.net/',
+);
+
+parse(
+	$in{data},
+	as    => 'NTriples',
+	into  => $model,
+	graph => 'http://example.net/g2',
+	base  => 'http://example.net/',
+);
+
+is $model->count_statements((undef)x4), 2, "model is correct size"
+	or note serialize($model, as => 'NQuads');
+
+my %expected = (
+	1 => 1,
+	2 => 1,
+	3 => 0,
+);
+for (qw(1 2 3))
+{
+	is(
+		$model->count_statements((undef)x3, iri("http://example.net/g$_")),
+		$expected{$_},
+		"graph http://example.net/g$_",
+	);
+}
+