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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Wed Nov 24 12:00:13 2004 UTC (20 years ago) by wakaba
Branch: MAIN
Changes since 1.1: +17 -6 lines
File MIME type: text/plain
Daily

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24