File: parser-trig.t

package info (click to toggle)
librdf-trine-perl 1.015-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,188 kB
  • ctags: 1,059
  • sloc: perl: 17,584; makefile: 30; sql: 20
file content (195 lines) | stat: -rw-r--r-- 6,378 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use Test::More tests => 14;
use Test::Exception;
use FindBin qw($Bin);
use File::Spec;
use Data::Dumper;
use utf8;
binmode( \*STDOUT, ':utf8' );
binmode( \*STDERR, ':utf8' );

use RDF::Trine qw(iri blank literal);
use RDF::Trine::Parser;

################################################################################
# Log::Log4perl::init( \q[
# 	log4perl.category.rdf.trine.model          = TRACE, Screen
# 	
# 	log4perl.appender.Screen         = Log::Log4perl::Appender::Screen
# 	log4perl.appender.Screen.stderr  = 0
# 	log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
# ] );
################################################################################

my $parser	= RDF::Trine::Parser::TriG->new();
isa_ok( $parser, 'RDF::Trine::Parser::TriG' );

{
	my $model = RDF::Trine::Model->temporary_model;
	my $trig	= <<'END';
# TriG Example Document 2
@prefix ex: <http://www.example.org/vocabulary#> .
@prefix : <http://www.example.org/exampleDocument#> .
:G1 = { :Monica a ex:Person ;
                 ex:name "Monica Murphy" ;      
                 ex:homepage <http://www.monicamurphy.org> ;
                 ex:email <mailto:monica@monicamurphy.org> ;
                 ex:hasSkill ex:Management ,
                             ex:Programming . } .
END
	$parser->parse_into_model(undef, $trig, $model);
	
	is( $model->size, 6, 'expected model (triple) size after nquads parse' );
	is( $model->count_statements(undef, undef, undef, undef), 6, 'expected 6 count ffff' );
	is( $model->count_statements(iri('http://www.example.org/exampleDocument#Monica'), undef, undef, undef), 6, 'expected 2 count bfff' );
	is( $model->count_statements(iri('b')), 0, 'expected 0 count bff' );
	is( $model->count_statements(iri('b'), undef, undef, undef), 0, 'expected 0 count bfff' );
	is( $model->count_statements(undef, iri('http://www.example.org/vocabulary#hasSkill')), 2, 'expected 2 count fbf' );
	is( $model->count_statements(undef, iri('http://www.example.org/vocabulary#hasSkill'), undef, undef), 2, 'expected 2 count fbff' );
	is( $model->count_statements(undef, undef, undef, iri('http://www.example.org/exampleDocument#G1')), 6, 'expected 6 count fffb' );
}

{
	my $model = RDF::Trine::Model->temporary_model;
	my $trig	= <<'END';
# TriG Example Document 1
# This document encodes three graphs.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix swp: <http://www.w3.org/2004/03/trix/swp-1/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http://www.example.org/vocabulary#> .
@prefix : <http://www.example.org/exampleDocument#> .
:G1 { :Monica ex:name "Monica Murphy" .      
      :Monica ex:homepage <http://www.monicamurphy.org> .
      :Monica ex:email <mailto:monica@monicamurphy.org> .
      :Monica ex:hasSkill ex:Management }

:G2 { :Monica rdf:type ex:Person .
      :Monica ex:hasSkill ex:Programming }

:G3 { :G1 swp:assertedBy _:w1 .
      _:w1 swp:authority :Chris .
      _:w1 dc:date "2003-10-02"^^xsd:date .   
      :G2 swp:quotedBy _:w2 .
      :G3 swp:assertedBy _:w2 .
      _:w2 dc:date "2003-09-03"^^xsd:date .
      _:w2 swp:authority :Chris .
      :Chris rdf:type ex:Person .  
      :Chris ex:email <mailto:chris@bizer.de> }
END
	$parser->parse_into_model(undef, $trig, $model);
	
	{
		my $iter	= $model->get_contexts;
		my %expect	= (
			'<http://www.example.org/exampleDocument#G1>'	=> 1,
			'<http://www.example.org/exampleDocument#G2>'	=> 1,
			'<http://www.example.org/exampleDocument#G3>'	=> 1,
		);
		my %got;
		while (my $c = $iter->next) {
			$got{ $c->as_string }++;
		}
		is_deeply( \%got, \%expect, 'expected graph names' );
	}
	
	{
		my $iter	= $model->get_statements( undef, undef, undef, undef );
		my %expect	= (
			'<http://www.example.org/exampleDocument#G1>'	=> 4,
			'<http://www.example.org/exampleDocument#G2>'	=> 2,
			'<http://www.example.org/exampleDocument#G3>'	=> 9,
		);
		my %got;
		while (my $st = $iter->next) {
			$got{ $st->context->as_string }++;
		}
		is_deeply( \%got, \%expect, 'expected statement counts per graph name' );
	}
}

{
	my $model = RDF::Trine::Model->temporary_model;
	my $trig	= <<'END';
# TriG Example Document 3
# This document contains a default graph and two named graphs.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
# default graph
    { 
      <http://example.org/bob> dc:publisher "Bob" . 
      <http://example.org/alice> dc:publisher "Alice" .
    }

<http://example.org/bob> 
    { 
       _:a foaf:name "Bob" . 
       _:a foaf:mbox <mailto:bob@oldcorp.example.org> .
    }
 
<http://example.org/alice>
    { 
       _:a foaf:name "Alice" . 
       _:a foaf:mbox <mailto:alice@work.example.org> .
    }
END
	$parser->parse_into_model(undef, $trig, $model);
	
	{
		my $iter	= $model->get_contexts;
		my %expect	= (
			'<http://example.org/bob>'	=> 1,
			'<http://example.org/alice>'	=> 1,
		);
		my %got;
		while (my $c = $iter->next) {
			$got{ $c->as_string }++;
		}
		is_deeply( \%got, \%expect, 'expected graph names' );
	}
	
	{
		my $iter	= $model->get_statements( undef, undef, undef, undef );
		my %expect	= (
			'(nil)'	=> 2,
			'<http://example.org/bob>'	=> 2,
			'<http://example.org/alice>'	=> 2,
		);
		my %got;
		while (my $st = $iter->next) {
			$got{ $st->context->as_string }++;
		}
		is_deeply( \%got, \%expect, 'expected statement counts per graph name' );
	}
}

{
	my $model = RDF::Trine::Model->temporary_model;
	my $trig	= <<'END';
# TriG Example Document 4
# This document contains a default graph and one named graphs with non-ASCII chars.
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix ex: <http://www.example.org/vocabulary#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
# default graph
    { 
      <http://example.org/bob> dc:publisher "Bob" . 
      <http://example.org/alice> dc:publisher "Alice" .
    }

<http://example.org/bob> 
    { 
       _:a foaf:name "Bob" . 
       _:a ex:likes "Blåbærsyltetøy"@no .
    }

END
	$parser->parse_into_model(undef, $trig, $model);
	my $iter = $model->get_statements( undef,
					   RDF::Trine::Node::Resource->new('http://www.example.org/vocabulary#likes'),
					   undef, undef );
	my $st = $iter->next;
	is($st->object->literal_value, 'Blåbærsyltetøy', "Finding UTF-8 string");
}