/[suikacvs]/messaging/manakai/bin/dis2rdf.pl
Suika

Contents of /messaging/manakai/bin/dis2rdf.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations) (download)
Tue Nov 23 13:20:33 2004 UTC (20 years ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +0 -0 lines
File MIME type: text/plain
Error occurred while calculating annotation data.
FILE REMOVED
Daily

1 #!/usr/bin/perl -w
2 use strict;
3 use Message::Util::QName::Filter {
4 d => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->,
5 dis2pm => q<http://suika.fam.cx/~wakaba/archive/2004/11/8/dis2pm#>,
6 DOMCore => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>,
7 DOMMain => q<http://suika.fam.cx/~wakaba/archive/2004/dom/main#>,
8 lang => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#>,
9 Perl => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#Perl-->,
10 license => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/license#>,
11 ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,
12 owl => q<http://www.w3.org/2002/07/owl#>,
13 rdf => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>,
14 rdfs => q<http://www.w3.org/2000/01/rdf-schema#>,
15 };
16
17 use Getopt::Long;
18 use Pod::Usage;
19 my %Opt;
20 GetOptions (
21 'for=s' => \$Opt{For},
22 'help' => \$Opt{help},
23 'undef-check!' => \$Opt{no_undef_check},
24 'output-anon-resource!' => \$Opt{output_anon_resource},
25 'output-as-n3' => \$Opt{output_as_n3},
26 'output-as-xml' => \$Opt{output_as_xml},
27 'output-for!' => \$Opt{output_for},
28 'output-local-resource!' => \$Opt{output_local_resource},
29 'output-module!' => \$Opt{output_module},
30 'output-only-in-module=s' => \$Opt{output_resource_pattern},
31 'output-perl!' => \$Opt{output_prop_perl},
32 'output-perl-member-pattern=s' => \$Opt{output_perl_member_pattern},
33 'output-resource!' => \$Opt{output_resource},
34 'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern},
35 'output-root-anon-resource!' => $Opt{output_root_anon_resource},
36 ) or pod2usage (2);
37 if ($Opt{help}) {
38 pod2usage (0);
39 exit;
40 }
41 if ($Opt{output_as_n3} and $Opt{output_as_xml}) {
42 pod2usage (2);
43 exit;
44 }
45 $Opt{file_name} = shift;
46 $Opt{output_resource_pattern} ||= qr/./;
47 $Opt{output_resource_uri_pattern} ||= qr/./;
48 $Opt{output_root_anon_resource} = $Opt{output_anon_resource}
49 unless defined $Opt{output_anon_resource};
50 $Opt{output_as_xml} = 1 unless $Opt{output_as_n3};
51 $Opt{output_anon_resource} = 1 unless defined $Opt{output_anon_resource};
52 $Opt{output_local_resource} = 1 unless defined $Opt{output_local_resource};
53 $Opt{no_undef_check} = defined $Opt{no_undef_check}
54 ? $Opt{no_undef_check} ? 0 : 1 : 0;
55 $Opt{output_perl_member_pattern} ||= qr/./;
56
57 BEGIN {
58 require 'manakai/genlib.pl';
59 require 'manakai/dis.pl';
60 }
61 sub n3_literal ($) {
62 my $s = shift;
63 impl_err ("Literal value not defined") unless defined $s;
64 qq<"$s">;
65 }
66 our $State;
67 our $result = new manakai::n3;
68
69 $State->{DefaultFor} = $Opt{For};
70
71 my $source = dis_load_module_file (module_file_name => $Opt{file_name},
72 For => $Opt{For},
73 use_default_for => 1);
74 $State->{for_def_required}->{$State->{DefaultFor}} ||= 1;
75
76 dis_check_undef_type_and_for () unless $Opt{no_undef_check};
77
78 if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) {
79 dis_perl_init ($source, For => $State->{DefaultFor});
80 }
81
82 my $primary = $result->get_new_anon_id (Name => 'boot');
83 $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
84 if $Opt{output_module};
85 $result->add_triple
86 ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})
87 if $Opt{output_for};
88
89 if ($Opt{output_module}) {
90 for (keys %{$State->{Module}}) {
91 my $mod = $State->{Module}->{$_};
92 if ($_ eq $mod->{URI}) {
93 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
94 ExpandedURI q<d:Module>);
95 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Name>=>
96 n3_literal $mod->{Name});
97 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=>
98 $mod->{NameURI});
99 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:ModuleGroup>=>
100 $mod->{ModuleGroup});
101 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FileName>=>
102 n3_literal $mod->{FileName})
103 if defined $mod->{FileName};
104 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=>
105 $mod->{Namespace});
106 for (@{$mod->{require_module}||[]}) {
107 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Require>=> $_);
108 }
109 for (keys %{$mod->{For}}) {
110 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_);
111 }
112 for (@{$mod->{ISA}}) {
113 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
114 }
115 if ($Opt{output_prop_perl}) {
116 $result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:packageName>=>
117 n3_literal $mod->{ExpandedURI q<dis2pm:packageName>})
118 if defined $mod->{ExpandedURI q<dis2pm:packageName>};
119 if ($Opt{output_resource}) {
120 for (values %{$mod->{ExpandedURI q<dis2pm:package>}}) {
121 my $uri = defined $_->{URI}
122 ? $_->{URI}
123 : ($_->{ExpandedURI q<d:anonID>}
124 ||= $result->get_new_anon_id (Name => $_->{Name}));
125 $result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:package>=>
126 $uri);
127 }
128 }
129 }
130 } else {
131 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
132 }
133 }}
134
135 if ($Opt{output_for}) {
136 for (keys %{$State->{For}}) {
137 my $mod = $State->{For}->{$_};
138 if ($_ eq $mod->{URI}) {
139 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
140 ExpandedURI q<d:For>);
141 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI});
142 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=>
143 n3_literal $mod->{FullName})
144 if defined $mod->{FullName};
145 for (@{$mod->{ISA}}) {
146 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
147 }
148 for (@{$mod->{Implement}}) {
149 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
150 }
151 } else {
152 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
153 }
154 }}
155
156 if ($Opt{output_resource}) {
157 sub class_to_rdf ($;%);
158 sub class_to_rdf ($;%) {
159 my ($mod, %opt) = @_;
160 return unless defined $mod->{Name};
161 return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
162 return if $Opt{output_prop_perl} and
163 $mod->{ExpandedURI q<dis2pm:type>} and
164 {
165 ExpandedURI q<ManakaiDOM:DOMAttribute> => 1,
166 ExpandedURI q<ManakaiDOM:DOMMethod> => 1,
167 }->{$mod->{ExpandedURI q<dis2pm:type>}} and
168 $mod->{Name} and
169 $mod->{Name} !~ /$Opt{output_perl_member_pattern}/;
170 if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
171 not defined $mod->{URI}) {
172 return if defined $mod->{URI} and
173 $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
174 return if not defined $mod->{URI} and not $Opt{output_anon_resource};
175 my $uri = defined $mod->{URI}
176 ? $mod->{URI}
177 : ($mod->{ExpandedURI q<d:anonID>}
178 ||= $result->get_new_anon_id (Name => $mod->{Name}));
179 $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
180 n3_literal $mod->{Name}) if length $mod->{Name};
181 $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
182 if defined $mod->{NameURI};
183 $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
184 $opt{parent_class_uri})
185 if defined $opt{parent_class_uri};
186 if ($Opt{output_module}) {
187 $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
188 $mod->{parentModule});
189 }
190 for (keys %{$mod->{Type}}) {
191 $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
192 }
193 for (@{$mod->{ISA}}) {
194 $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
195 }
196 for (@{$mod->{Implement}}) {
197 $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
198 }
199 if ($Opt{output_for}) {
200 for (keys %{$mod->{For}}) {
201 $result->add_triple ($uri =>ExpandedURI q<d:For>=> $_);
202 }
203 }
204 for (@{$mod->{hasResource}||[]}) {
205 my $ruri = defined $_->{URI}
206 ? $_->{URI}
207 : ($_->{ExpandedURI q<d:anonID>}
208 ||= $result->get_new_anon_id (Name => $_->{Name}));
209 $result->add_triple ($uri =>ExpandedURI q<d:hasResource>=> $ruri);
210 }
211 if ($Opt{output_prop_perl}) {
212 for my $prop ([ExpandedURI q<dis2pm:packageName>],
213 [ExpandedURI q<dis2pm:ifPackagePrefix>],
214 [ExpandedURI q<dis2pm:methodName>],
215 [ExpandedURI q<dis2pm:paramName>],
216 [ExpandedURI q<dis2pm:constGroupName>],
217 [ExpandedURI q<dis2pm:constName>],
218 [ExpandedURI q<ManakaiDOM:isRedefining>,
219 ExpandedURI q<DOMMain:boolean>],
220 [ExpandedURI q<ManakaiDOM:isForInternal>,
221 ExpandedURI q<DOMMain:boolean>],
222 [ExpandedURI q<d:Read>, ExpandedURI q<DOMMain:boolean>],
223 [ExpandedURI q<d:Write>,
224 ExpandedURI q<DOMMain:boolean>],
225 [ExpandedURI q<dis2pm:undefable>,
226 ExpandedURI q<DOMMain:boolean>]) {
227 $result->add_triple ($uri =>$prop->[0]=>
228 n3_literal $mod->{$prop->[0]})
229 if defined $mod->{$prop->[0]};
230 }
231 for my $prop ([ExpandedURI q<d:Type>],
232 [ExpandedURI q<d:actualType>]) {
233 $result->add_triple ($uri =>$prop->[0]=> $mod->{$prop->[0]})
234 if defined $mod->{$prop->[0]};
235 }
236 for my $prop ([ExpandedURI q<dis2pm:getter>],
237 [ExpandedURI q<dis2pm:setter>],
238 [ExpandedURI q<dis2pm:return>]) {
239 my $oo = $mod->{$prop->[0]};
240 if ($oo and defined $oo->{Name}) {
241 my $o = defined $oo->{URI}
242 ? $oo->{URI}
243 : ($oo->{ExpandedURI q<d:anonID>}
244 ||= $result->get_new_anon_id (Name => $oo->{Name}));
245 $result->add_triple ($uri =>$prop->[0]=> $o)
246 }
247 }
248 for my $p (ExpandedURI q<dis2pm:method>,
249 ExpandedURI q<dis2pm:constGroup>,
250 ExpandedURI q<dis2pm:const>) {
251 for my $v (values %{$mod->{$p}||{}}) {
252 my $ruri = defined $v->{URI}
253 ? $v->{URI}
254 : ($v->{ExpandedURI q<d:anonID>}
255 ||= $result->get_new_anon_id (Name => $v->{Name}));
256 $result->add_triple ($uri =>$p=> $ruri);
257 }
258 }
259 if ($mod->{ExpandedURI q<dis2pm:type>} eq
260 ExpandedURI q<ManakaiDOM:DOMMethod>) {
261 $result->add_triple
262 ($uri =>ExpandedURI q<dis2pm:param>=>
263 my $p = $result->get_new_anon_id (Name => 'param'));
264 $result->add_triple ($uri =>ExpandedURI q<rdf:type>=>
265 ExpandedURI q<rdf:Seq>);
266 my $i = 0;
267 for (@{$mod->{ExpandedURI q<dis2pm:param>}||[]}) {
268 my $ruri = defined $_->{URI}
269 ? $_->{URI}
270 : ($_->{ExpandedURI q<d:anonID>}
271 ||= $result->get_new_anon_id (Name => $_->{Name}));
272 $result->add_triple ($p =>(ExpandedURI q<rdf:_>).++$i=> $ruri);
273 }
274 }
275 }
276 if ($Opt{output_local_resource}) {
277 for (keys %{$mod->{Resource}}) {
278 class_to_rdf ($mod->{Resource}->{$_}, %opt, parent_class => $mod,
279 parent_class_uri => $uri,
280 key => $_);
281 }
282 }
283 } else { ## Alias URI
284 return unless $opt{key} =~ /$Opt{output_resource_uri_pattern}/ or
285 $mod->{URI} =~ /$Opt{output_resource_uri_pattern}/;
286 $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
287 }
288 }
289 for (sort keys %{$State->{Type}}) {
290 next if not $Opt{output_root_anon_resource} and
291 not defined $State->{Type}->{$_}->{URI};
292 class_to_rdf ($State->{Type}->{$_}, key => $_);
293 }
294 }
295
296 if ($Opt{output_as_xml}) {
297 print $result->stringify_as_xml;
298 } else {
299 print $result->stringify;
300 }
301
302 package manakai::n3;
303 sub new ($) {
304 bless {triple => [], anon => 0}, shift;
305 }
306
307 sub get_new_anon_id ($;%) {
308 my ($self, %opt) = @_;
309 my $s = $opt{Name} ? $opt{Name} : '';
310 return sprintf '_:r%d%s', $self->{anon}++, $s;
311 }
312
313 sub add_triple ($$$$) {
314 my ($self, $s =>$p=> $o) = @_;
315 main::impl_err ("Subject undefined") unless defined $s;
316 main::impl_err ("Property undefined") unless defined $p;
317 main::impl_err ("Object undefined") unless defined $o;
318 push @{$self->{triple}}, [$s =>$p=> $o];
319 }
320
321 sub stringify ($) {
322 my ($self) = @_;
323 return join "\n", (map {"$_."} map {
324 sprintf '%s %s %s', map {
325 $_ =~ /^[_"]/ ? $_ : "<$_>"
326 } @{$_}[0, 1, 2];
327 } @{$self->{triple}}), '';
328 }
329
330 sub stringify_as_xml ($) {
331 my ($self) = @_;
332 use RDF::Notation3::XML;
333 my $notation3 = RDF::Notation3::XML->new;
334 my $n3 = $self->stringify;
335 my $rdf_ = ExpandedURI q<rdf:_>;
336 $n3 =~ s{$rdf_}{ExpandedURI q<rdf:XXXX__dummy__XXXX>}ge;
337 $notation3->parse_string ($n3);
338 my $xml = $notation3->get_string;
339 $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g;
340 $xml =~ s/XXXX__dummy__XXXX/_/g;
341 # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//;
342 $xml;
343 }
344
345 1;
346
347 __END__
348
349 =head1 NAME
350
351 dis2rdf.pl - dis to RDF converter
352
353 =head1 SYNOPSIS
354
355 $ perl dis2rdf.pl input.dis [options...] > output.rdf
356
357 =head1 DESCRIPTION
358
359 This script generates a RDF graph from a "dis" file.
360
361 =over 4
362
363 =item I<input.dis>
364
365 The "dis" file from which a RDF graph is generated.
366
367 =item I<output.rdf>
368
369 An RDF/XML entity is outputed.
370
371 =item C<--output-module>
372
373 Show the relationship of modules.
374
375 =item C<--output-type>
376
377 Show the relationship of types.
378
379 =item C<--output-for>
380
381 Show the relationship of "for"s.
382
383 =cut
384
385 =head1 LICENSE
386
387 Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
388
389 This program is free software; you can redistribute it and/or
390 modify it under the same terms as Perl itself.
391
392 Note that the copyright holder(s) of this script does not claim
393 any rights for materials outputed by this script, although
394 some of its part comes from this script. The copyright
395 holder(s) of source document should define their license terms.
396
397 =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24