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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations) (download)
Sun Nov 21 05:17:32 2004 UTC (20 years ago) by wakaba
Branch: MAIN
Changes since 1.6: +61 -12 lines
File MIME type: text/plain
Method parameter supported

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24