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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Tue Nov 23 13:20:33 2004 UTC (20 years ago) by wakaba
Branch: MAIN
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     if ($Opt{output_resource}) {
131     sub class_to_rdf ($;%);
132     sub class_to_rdf ($;%) {
133     my ($mod, %opt) = @_;
134     return unless defined $mod->{Name};
135     return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
136     return if $Opt{output_prop_perl} and
137     $mod->{ExpandedURI q<dis2pm:type>} and
138     {
139     ExpandedURI q<ManakaiDOM:DOMAttribute> => 1,
140     ExpandedURI q<ManakaiDOM:DOMMethod> => 1,
141     }->{$mod->{ExpandedURI q<dis2pm:type>}} and
142     $mod->{Name} and
143     $mod->{Name} !~ /$Opt{output_perl_member_pattern}/;
144     if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
145     not defined $mod->{URI}) {
146     return if defined $mod->{URI} and
147     $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
148     return if not defined $mod->{URI} and not $Opt{output_anon_resource};
149     my $uri = defined $mod->{URI}
150     ? $mod->{URI}
151     : ($mod->{ExpandedURI q<d:anonID>}
152     ||= $result->get_new_anon_id (Name => $mod->{Name}));
153     $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
154     n3_literal $mod->{Name}) if length $mod->{Name};
155     $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
156     if defined $mod->{NameURI};
157     $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
158     $opt{parent_class_uri})
159     if defined $opt{parent_class_uri};
160     if ($Opt{output_module}) {
161     $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
162     $mod->{parentModule});
163     }
164     for (keys %{$mod->{Type}}) {
165     $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
166     }
167     for (@{$mod->{ISA}}) {
168     $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
169     }
170     for (@{$mod->{Implement}}) {
171     $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
172     }
173     if ($Opt{output_for}) {
174     for (keys %{$mod->{For}}) {
175     $result->add_triple ($uri =>ExpandedURI q<d:For>=> $_);
176     }
177     }
178     for (@{$mod->{hasResource}||[]}) {
179     my $ruri = defined $_->{URI}
180     ? $_->{URI}
181     : ($_->{ExpandedURI q<d:anonID>}
182     ||= $result->get_new_anon_id (Name => $_->{Name}));
183     $result->add_triple ($uri =>ExpandedURI q<d:hasResource>=> $ruri);
184     }
185     if ($Opt{output_prop_perl}) {
186     for my $prop ([ExpandedURI q<dis2pm:packageName>],
187     [ExpandedURI q<dis2pm:ifPackagePrefix>],
188     [ExpandedURI q<dis2pm:methodName>],
189     [ExpandedURI q<dis2pm:paramName>],
190     [ExpandedURI q<dis2pm:constGroupName>],
191     [ExpandedURI q<dis2pm:constName>],
192     [ExpandedURI q<ManakaiDOM:isRedefining>,
193     ExpandedURI q<DOMMain:boolean>],
194     [ExpandedURI q<ManakaiDOM:isForInternal>,
195     ExpandedURI q<DOMMain:boolean>],
196     [ExpandedURI q<d:Read>, ExpandedURI q<DOMMain:boolean>],
197     [ExpandedURI q<d:Write>,
198     ExpandedURI q<DOMMain:boolean>],
199     [ExpandedURI q<dis2pm:undefable>,
200     ExpandedURI q<DOMMain:boolean>]) {
201     $result->add_triple ($uri =>$prop->[0]=>
202     n3_literal $mod->{$prop->[0]})
203     if defined $mod->{$prop->[0]};
204     }
205     for my $prop ([ExpandedURI q<d:Type>],
206     [ExpandedURI q<d:actualType>]) {
207     $result->add_triple ($uri =>$prop->[0]=> $mod->{$prop->[0]})
208     if defined $mod->{$prop->[0]};
209     }
210     for my $prop ([ExpandedURI q<dis2pm:getter>],
211     [ExpandedURI q<dis2pm:setter>],
212     [ExpandedURI q<dis2pm:return>]) {
213     my $oo = $mod->{$prop->[0]};
214     if ($oo and defined $oo->{Name}) {
215     my $o = defined $oo->{URI}
216     ? $oo->{URI}
217     : ($oo->{ExpandedURI q<d:anonID>}
218     ||= $result->get_new_anon_id (Name => $oo->{Name}));
219     $result->add_triple ($uri =>$prop->[0]=> $o)
220     }
221     }
222     for my $p (ExpandedURI q<dis2pm:method>,
223     ExpandedURI q<dis2pm:constGroup>,
224     ExpandedURI q<dis2pm:const>) {
225     for my $v (values %{$mod->{$p}||{}}) {
226     my $ruri = defined $v->{URI}
227     ? $v->{URI}
228     : ($v->{ExpandedURI q<d:anonID>}
229     ||= $result->get_new_anon_id (Name => $v->{Name}));
230     $result->add_triple ($uri =>$p=> $ruri);
231     }
232     }
233     if ($mod->{ExpandedURI q<dis2pm:type>} eq
234     ExpandedURI q<ManakaiDOM:DOMMethod>) {
235     $result->add_triple
236     ($uri =>ExpandedURI q<dis2pm:param>=>
237     my $p = $result->get_new_anon_id (Name => 'param'));
238     $result->add_triple ($uri =>ExpandedURI q<rdf:type>=>
239     ExpandedURI q<rdf:Seq>);
240     my $i = 0;
241     for (@{$mod->{ExpandedURI q<dis2pm:param>}||[]}) {
242     my $ruri = defined $_->{URI}
243     ? $_->{URI}
244     : ($_->{ExpandedURI q<d:anonID>}
245     ||= $result->get_new_anon_id (Name => $_->{Name}));
246     $result->add_triple ($p =>(ExpandedURI q<rdf:_>).++$i=> $ruri);
247     }
248     }
249     }
250     if ($Opt{output_local_resource}) {
251     for (keys %{$mod->{Resource}}) {
252     class_to_rdf ($mod->{Resource}->{$_}, %opt, parent_class => $mod,
253     parent_class_uri => $uri,
254     key => $_);
255     }
256     }
257     } else { ## Alias URI
258     return unless $opt{key} =~ /$Opt{output_resource_uri_pattern}/ or
259     $mod->{URI} =~ /$Opt{output_resource_uri_pattern}/;
260     $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
261     }
262     }
263     for (sort keys %{$State->{Type}}) {
264     next if not $Opt{output_root_anon_resource} and
265     not defined $State->{Type}->{$_}->{URI};
266     class_to_rdf ($State->{Type}->{$_}, key => $_);
267     }
268     }
269    
270     if ($Opt{output_as_xml}) {
271     print $result->stringify_as_xml;
272     } else {
273     print $result->stringify;
274     }
275    
276     package manakai::n3;
277     sub new ($) {
278     bless {triple => [], anon => 0}, shift;
279     }
280    
281     sub get_new_anon_id ($;%) {
282     my ($self, %opt) = @_;
283     my $s = $opt{Name} ? $opt{Name} : '';
284     return sprintf '_:r%d%s', $self->{anon}++, $s;
285     }
286    
287     sub add_triple ($$$$) {
288     my ($self, $s =>$p=> $o) = @_;
289     main::impl_err ("Subject undefined") unless defined $s;
290     main::impl_err ("Property undefined") unless defined $p;
291     main::impl_err ("Object undefined") unless defined $o;
292     push @{$self->{triple}}, [$s =>$p=> $o];
293     }
294    
295     sub stringify ($) {
296     my ($self) = @_;
297     return join "\n", (map {"$_."} map {
298     sprintf '%s %s %s', map {
299     $_ =~ /^[_"]/ ? $_ : "<$_>"
300     } @{$_}[0, 1, 2];
301     } @{$self->{triple}}), '';
302     }
303    
304     sub stringify_as_xml ($) {
305     my ($self) = @_;
306     use RDF::Notation3::XML;
307     my $notation3 = RDF::Notation3::XML->new;
308     my $n3 = $self->stringify;
309     my $rdf_ = ExpandedURI q<rdf:_>;
310     $n3 =~ s{$rdf_}{ExpandedURI q<rdf:XXXX__dummy__XXXX>}ge;
311     $notation3->parse_string ($n3);
312     my $xml = $notation3->get_string;
313     $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g;
314     $xml =~ s/XXXX__dummy__XXXX/_/g;
315     # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//;
316     $xml;
317     }
318    
319     __END__
320    
321     =head1 NAME
322    
323     cdis2rdf - cdis to RDF converter
324    
325     =head1 SYNOPSIS
326    
327     perl cdis2rdf.pl input.cdis [options...] > output.rdf
328     perl cdis2rdf.pl --help
329    
330     =head1 DESCRIPTION
331    
332     The C<cdis2rdf> utility generates a RDF graph from a compiled
333     "dis" file. The graph describes relationship of module, "For" or
334     resource defined in the dis files. The RDF data outputed are able
335     to be used with other utilities that support RDF.
336    
337     =head2 OPTIONS
338    
339     =over 4
340    
341     =item I<input.cdis>
342    
343     A compiled "dis" file from which a RDF graph is generated.
344    
345     =item I<output.rdf>
346    
347     A file to which the RDF data generated is saved.
348    
349     =item C<--output-anon-resource> (default) / C<--nooutput-anon-resource>
350    
351     Set whether anonymous resources are outputed.
352    
353     =item C<--output-as-n3>
354    
355     Set to output the graph in RDF/Notation3 format.
356    
357     =item C<--output-as-xml> (default)
358    
359     Set to output the graph in RDF/XML format. Note that the
360     L<RDF::Notation3::XML> Perl module is used to generate the XML entity.
361    
362     =item C<--help>
363    
364     Show the help message.
365    
366     =item C<--output-for> / C<--nooutput-for> (default)
367    
368     Set whether relationships of "For" URI references are outputed.
369    
370     =item C<--output-local-resource> (default) / C<--nooutput-local-resource>
371    
372     Set whether local resources (resources that do have the locally-scoped
373     name but do not have the global name) are outputed.
374    
375     =item C<--output-only-in-module=I<pattern>> (default: C<.>)
376    
377     A regex filter that is applied to URI references of module names.
378     This filter is applied to defining-modules of resources (not modules themselves).
379    
380     =item C<--output-module> / C<--nooutput-module> (default)
381    
382     Set whehter relationships of modules are outputed.
383    
384     =item C<--output-perl> / C<--nooutput-perl> (default)
385    
386     Set whether "For"-Perl specific properties are outputed.
387    
388     =item C<--output-perl-member-pattern=I<pattern>> (default: C<.>)
389    
390     A regex filter that is applied to URI references of Perl
391     package members such as methods and constant values.
392    
393     =item C<--output-resource> / C<--nooutput-resource> (default)
394    
395     Set whether relationships of resources are outputed.
396    
397     =item C<--output-resource-uri-pattern=I<pattern>> (default: C<.>)
398    
399     A regex filter that is applied to URI references of
400     resources.
401    
402     =item C<--output-root-anon-resource> / C<--nooutput-root-anon-resource> (default: same as C<--output-anon-resource> / C<--nooutput-anon-resource>)
403    
404     Set whether anonymous resources that are direct children of modules.
405    
406     =cut
407    
408     =head1 LICENSE
409    
410     Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
411    
412     This program is free software; you can redistribute it and/or
413     modify it under the same terms as Perl itself.
414    
415     Note that the copyright holder(s) of this script does not claim
416     any rights for materials outputed by this script, although
417     some of its part comes from this script. The copyright
418     holder(s) of source document should define their license terms.
419    
420     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24