/[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 - (hide 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 wakaba 1.1 #!/usr/bin/perl -w
2     use strict;
3 wakaba 1.6 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 wakaba 1.1
22     use Getopt::Long;
23     use Pod::Usage;
24     my %Opt;
25     GetOptions (
26     'for=s' => \$Opt{For},
27     'help' => \$Opt{help},
28 wakaba 1.5 'undef-check!' => \$Opt{no_undef_check},
29     'output-anon-resource!' => \$Opt{output_anon_resource},
30 wakaba 1.4 'output-as-n3' => \$Opt{output_as_n3},
31     'output-as-xml' => \$Opt{output_as_xml},
32 wakaba 1.5 'output-for!' => \$Opt{output_for},
33     'output-local-resource!' => \$Opt{output_local_resource},
34     'output-module!' => \$Opt{output_module},
35 wakaba 1.4 'output-only-in-module=s' => \$Opt{output_resource_pattern},
36 wakaba 1.7 'output-perl!' => \$Opt{output_prop_perl},
37     'output-perl-member-pattern=s' => \$Opt{output_perl_member_pattern},
38 wakaba 1.5 'output-resource!' => \$Opt{output_resource},
39 wakaba 1.4 'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern},
40 wakaba 1.7 'output-root-anon-resource!' => $Opt{output_root_anon_resource},
41 wakaba 1.1 ) or pod2usage (2);
42     if ($Opt{help}) {
43     pod2usage (0);
44     exit;
45     }
46 wakaba 1.4 if ($Opt{output_as_n3} and $Opt{output_as_xml}) {
47     pod2usage (2);
48     exit;
49     }
50 wakaba 1.7 $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 wakaba 1.4 $Opt{output_as_xml} = 1 unless $Opt{output_as_n3};
56 wakaba 1.5 $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 wakaba 1.7 $Opt{output_perl_member_pattern} ||= qr/./;
60 wakaba 1.1
61     BEGIN {
62     require 'manakai/genlib.pl';
63     require 'manakai/dis.pl';
64     }
65     sub n3_literal ($) {
66     my $s = shift;
67 wakaba 1.5 impl_err ("Literal value not defined") unless defined $s;
68 wakaba 1.1 qq<"$s">;
69     }
70     our $State;
71     our $result = new manakai::n3;
72    
73    
74     $State->{DefaultFor} = $Opt{For};
75 wakaba 1.2
76 wakaba 1.1 my $source = dis_load_module_file (module_file_name => $Opt{file_name},
77 wakaba 1.2 For => $Opt{For},
78 wakaba 1.1 use_default_for => 1);
79     $State->{for_def_required}->{$State->{DefaultFor}} ||= 1;
80    
81 wakaba 1.2 dis_check_undef_type_and_for ()
82     unless $Opt{no_undef_check};
83 wakaba 1.1
84 wakaba 1.4 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 wakaba 1.1 $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
90     if $Opt{output_module};
91 wakaba 1.7 $result->add_triple
92     ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})
93 wakaba 1.1 if $Opt{output_for};
94    
95     if ($Opt{output_module}) {
96     for (keys %{$State->{Module}}) {
97     my $mod = $State->{Module}->{$_};
98 wakaba 1.2 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 wakaba 1.1 n3_literal $mod->{Name});
103 wakaba 1.2 $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 wakaba 1.1 n3_literal $mod->{FileName})
109     if defined $mod->{FileName};
110 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=>
111     $mod->{Namespace});
112 wakaba 1.1 for (@{$mod->{require_module}||[]}) {
113 wakaba 1.2 $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 wakaba 1.4 for (@{$mod->{ISA}}) {
119 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
120 wakaba 1.1 }
121 wakaba 1.4 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 wakaba 1.6 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 wakaba 1.4 }
136 wakaba 1.1 } else {
137 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
138 wakaba 1.1 }
139     }}
140    
141     if ($Opt{output_for}) {
142     for (keys %{$State->{For}}) {
143     my $mod = $State->{For}->{$_};
144 wakaba 1.2 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 wakaba 1.5 n3_literal $mod->{FullName})
150     if defined $mod->{FullName};
151 wakaba 1.4 for (@{$mod->{ISA}}) {
152 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
153 wakaba 1.1 }
154 wakaba 1.4 for (@{$mod->{Implement}}) {
155 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
156 wakaba 1.1 }
157     } else {
158 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
159 wakaba 1.1 }
160     }}
161    
162 wakaba 1.3 if ($Opt{output_resource}) {
163 wakaba 1.2 sub class_to_rdf ($;%);
164     sub class_to_rdf ($;%) {
165     my ($mod, %opt) = @_;
166     return unless defined $mod->{Name};
167 wakaba 1.4 return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
168 wakaba 1.7 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 wakaba 1.2 if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
177     not defined $mod->{URI}) {
178 wakaba 1.4 return if defined $mod->{URI} and
179     $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
180 wakaba 1.5 return if not defined $mod->{URI} and not $Opt{output_anon_resource};
181 wakaba 1.4 my $uri = defined $mod->{URI}
182     ? $mod->{URI}
183 wakaba 1.5 : ($mod->{ExpandedURI q<d:anonID>}
184     ||= $result->get_new_anon_id (Name => $mod->{Name}));
185 wakaba 1.2 $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 wakaba 1.3 $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
190 wakaba 1.2 $opt{parent_class_uri})
191     if defined $opt{parent_class_uri};
192 wakaba 1.7 if ($Opt{output_module}) {
193     $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
194     $mod->{parentModule});
195     }
196 wakaba 1.2 for (keys %{$mod->{Type}}) {
197     $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
198     }
199 wakaba 1.4 for (@{$mod->{ISA}}) {
200 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
201     }
202 wakaba 1.4 for (@{$mod->{Implement}}) {
203 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
204     }
205 wakaba 1.7 if ($Opt{output_for}) {
206     for (keys %{$mod->{For}}) {
207     $result->add_triple ($uri =>ExpandedURI q<d:For>=> $_);
208     }
209 wakaba 1.5 }
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 wakaba 1.6 for my $prop ([ExpandedURI q<dis2pm:packageName>],
219     [ExpandedURI q<dis2pm:ifPackagePrefix>],
220     [ExpandedURI q<dis2pm:methodName>],
221 wakaba 1.7 [ExpandedURI q<dis2pm:paramName>],
222 wakaba 1.6 [ExpandedURI q<ManakaiDOM:isRedefining>,
223     ExpandedURI q<DOMMain:boolean>],
224     [ExpandedURI q<ManakaiDOM:isForInternal>,
225 wakaba 1.7 ExpandedURI q<DOMMain:boolean>],
226     [ExpandedURI q<d:Read>, ExpandedURI q<DOMMain:boolean>],
227     [ExpandedURI q<d:Write>,
228 wakaba 1.6 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 wakaba 1.7 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 wakaba 1.6 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 wakaba 1.5 }
245 wakaba 1.7 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 wakaba 1.5 }
262 wakaba 1.3 if ($Opt{output_local_resource}) {
263 wakaba 1.4 for (keys %{$mod->{Resource}}) {
264 wakaba 1.7 class_to_rdf ($mod->{Resource}->{$_}, %opt, parent_class => $mod,
265 wakaba 1.2 parent_class_uri => $uri,
266     key => $_);
267     }
268     }
269     } else { ## Alias URI
270 wakaba 1.7 return unless $opt{key} =~ /$Opt{output_resource_uri_pattern}/ or
271     $mod->{URI} =~ /$Opt{output_resource_uri_pattern}/;
272 wakaba 1.4 $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
273 wakaba 1.1 }
274     }
275 wakaba 1.4 for (sort keys %{$State->{Type}}) {
276 wakaba 1.7 next if not $Opt{output_root_anon_resource} and
277     not defined $State->{Type}->{$_}->{URI};
278 wakaba 1.2 class_to_rdf ($State->{Type}->{$_}, key => $_);
279     }
280     }
281 wakaba 1.1
282 wakaba 1.4 if ($Opt{output_as_xml}) {
283     print $result->stringify_as_xml;
284     } else {
285     print $result->stringify;
286     }
287 wakaba 1.1
288     package manakai::n3;
289     sub new ($) {
290     bless {triple => [], anon => 0}, shift;
291     }
292    
293 wakaba 1.4 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 wakaba 1.1 }
298    
299     sub add_triple ($$$$) {
300     my ($self, $s =>$p=> $o) = @_;
301 wakaba 1.5 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 wakaba 1.1 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 wakaba 1.7 my $n3 = $self->stringify;
321     my $rdf_ = ExpandedURI q<rdf:_>;
322     $n3 =~ s/$rdf_/data:,dummy_/g;
323     $notation3->parse_string ($n3);
324 wakaba 1.2 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 wakaba 1.1 }
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