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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations) (download)
Sat Nov 20 11:12:50 2004 UTC (20 years ago) by wakaba
Branch: MAIN
Changes since 1.5: +45 -4 lines
File MIME type: text/plain
Message::Util::QName::General: Now Message::Markup::XML::QName does not need; Message::Util::QName::Filter: New module; Message::Util::DOM: daily

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.5 'output-prop-perl!' => \$Opt{output_prop_perl},
37     'output-resource!' => \$Opt{output_resource},
38 wakaba 1.4 'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern},
39 wakaba 1.1 ) or pod2usage (2);
40     if ($Opt{help}) {
41     pod2usage (0);
42     exit;
43     }
44 wakaba 1.4 if ($Opt{output_as_n3} and $Opt{output_as_xml}) {
45     pod2usage (2);
46     exit;
47     }
48     $Opt{output_as_xml} = 1 unless $Opt{output_as_n3};
49 wakaba 1.5 $Opt{output_anon_resource} = 1 unless defined $Opt{output_anon_resource};
50     $Opt{output_local_resource} = 1 unless defined $Opt{output_local_resource};
51     $Opt{no_undef_check} = $Opt{no_undef_check} ? 0 : 1;
52 wakaba 1.1
53     BEGIN {
54     require 'manakai/genlib.pl';
55     require 'manakai/dis.pl';
56     }
57     sub n3_literal ($) {
58     my $s = shift;
59 wakaba 1.5 impl_err ("Literal value not defined") unless defined $s;
60 wakaba 1.1 qq<"$s">;
61     }
62     our $State;
63     our $result = new manakai::n3;
64    
65     $Opt{file_name} = shift;
66 wakaba 1.4 $Opt{output_resource_pattern} ||= qr/.+/;
67     $Opt{output_resource_uri_pattern} ||= qr/.+/;
68 wakaba 1.1
69     $State->{DefaultFor} = $Opt{For};
70 wakaba 1.2
71 wakaba 1.1 my $source = dis_load_module_file (module_file_name => $Opt{file_name},
72 wakaba 1.2 For => $Opt{For},
73 wakaba 1.1 use_default_for => 1);
74     $State->{for_def_required}->{$State->{DefaultFor}} ||= 1;
75    
76 wakaba 1.2 dis_check_undef_type_and_for ()
77     unless $Opt{no_undef_check};
78 wakaba 1.1
79 wakaba 1.4 if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) {
80     dis_perl_init ($source, For => $State->{DefaultFor});
81     }
82    
83     my $primary = $result->get_new_anon_id (Name => 'boot');
84 wakaba 1.1 $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
85     if $Opt{output_module};
86     $result->add_triple ($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 wakaba 1.2 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 wakaba 1.1 n3_literal $mod->{Name});
97 wakaba 1.2 $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 wakaba 1.1 n3_literal $mod->{FileName})
103     if defined $mod->{FileName};
104 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=>
105     $mod->{Namespace});
106 wakaba 1.1 for (@{$mod->{require_module}||[]}) {
107 wakaba 1.2 $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 wakaba 1.4 for (@{$mod->{ISA}}) {
113 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
114 wakaba 1.1 }
115 wakaba 1.4 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 wakaba 1.6 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 wakaba 1.4 }
130 wakaba 1.1 } else {
131 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
132 wakaba 1.1 }
133     }}
134    
135     if ($Opt{output_for}) {
136     for (keys %{$State->{For}}) {
137     my $mod = $State->{For}->{$_};
138 wakaba 1.2 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 wakaba 1.5 n3_literal $mod->{FullName})
144     if defined $mod->{FullName};
145 wakaba 1.4 for (@{$mod->{ISA}}) {
146 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
147 wakaba 1.1 }
148 wakaba 1.4 for (@{$mod->{Implement}}) {
149 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
150 wakaba 1.1 }
151     } else {
152 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
153 wakaba 1.1 }
154     }}
155    
156 wakaba 1.3 if ($Opt{output_resource}) {
157 wakaba 1.2 sub class_to_rdf ($;%);
158     sub class_to_rdf ($;%) {
159     my ($mod, %opt) = @_;
160     return unless defined $mod->{Name};
161 wakaba 1.4 return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
162 wakaba 1.2 if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
163     not defined $mod->{URI}) {
164 wakaba 1.4 return if defined $mod->{URI} and
165     $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
166 wakaba 1.5 return if not defined $mod->{URI} and not $Opt{output_anon_resource};
167 wakaba 1.4 my $uri = defined $mod->{URI}
168     ? $mod->{URI}
169 wakaba 1.5 : ($mod->{ExpandedURI q<d:anonID>}
170     ||= $result->get_new_anon_id (Name => $mod->{Name}));
171 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
172     n3_literal $mod->{Name}) if length $mod->{Name};
173     $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
174     if defined $mod->{NameURI};
175 wakaba 1.3 $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
176 wakaba 1.2 $opt{parent_class_uri})
177     if defined $opt{parent_class_uri};
178 wakaba 1.4 $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
179     $mod->{parentModule});
180 wakaba 1.2 for (keys %{$mod->{Type}}) {
181     $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
182     }
183 wakaba 1.4 for (@{$mod->{ISA}}) {
184 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
185     }
186 wakaba 1.4 for (@{$mod->{Implement}}) {
187 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
188     }
189 wakaba 1.5 for (keys %{$mod->{For}}) {
190     $result->add_triple ($uri =>ExpandedURI q<d:For>=> $_);
191     }
192     for (@{$mod->{hasResource}||[]}) {
193     my $ruri = defined $_->{URI}
194     ? $_->{URI}
195     : ($_->{ExpandedURI q<d:anonID>}
196     ||= $result->get_new_anon_id (Name => $_->{Name}));
197     $result->add_triple ($uri =>ExpandedURI q<d:hasResource>=> $ruri);
198     }
199     if ($Opt{output_prop_perl}) {
200 wakaba 1.6 for my $prop ([ExpandedURI q<dis2pm:packageName>],
201     [ExpandedURI q<dis2pm:ifPackagePrefix>],
202     [ExpandedURI q<dis2pm:methodName>],
203     [ExpandedURI q<ManakaiDOM:isRedefining>,
204     ExpandedURI q<DOMMain:boolean>],
205     [ExpandedURI q<ManakaiDOM:isForInternal>,
206     ExpandedURI q<DOMMain:boolean>]) {
207     $result->add_triple ($uri =>$prop->[0]=>
208     n3_literal $mod->{$prop->[0]})
209     if defined $mod->{$prop->[0]};
210     }
211     for (values %{$mod->{ExpandedURI q<dis2pm:method>}||{}}) {
212     my $ruri = defined $_->{URI}
213     ? $_->{URI}
214     : ($_->{ExpandedURI q<d:anonID>}
215     ||= $result->get_new_anon_id (Name => $_->{Name}));
216     $result->add_triple ($uri =>ExpandedURI q<dis2pm:method>=> $ruri);
217 wakaba 1.5 }
218     }
219 wakaba 1.3 if ($Opt{output_local_resource}) {
220 wakaba 1.4 for (keys %{$mod->{Resource}}) {
221     class_to_rdf ($mod->{Resource}->{$_}, parent_class => $mod,
222 wakaba 1.2 parent_class_uri => $uri,
223     key => $_);
224     }
225     }
226     } else { ## Alias URI
227 wakaba 1.4 return if $opt{key} !~ /$Opt{output_resource_uri_pattern}/;
228     $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
229 wakaba 1.1 }
230     }
231 wakaba 1.4 for (sort keys %{$State->{Type}}) {
232 wakaba 1.2 class_to_rdf ($State->{Type}->{$_}, key => $_);
233     }
234     }
235 wakaba 1.1
236 wakaba 1.4 if ($Opt{output_as_xml}) {
237     print $result->stringify_as_xml;
238     } else {
239     print $result->stringify;
240     }
241 wakaba 1.1
242     package manakai::n3;
243     sub new ($) {
244     bless {triple => [], anon => 0}, shift;
245     }
246    
247 wakaba 1.4 sub get_new_anon_id ($;%) {
248     my ($self, %opt) = @_;
249     my $s = $opt{Name} ? $opt{Name} : '';
250     return sprintf '_:r%d%s', $self->{anon}++, $s;
251 wakaba 1.1 }
252    
253     sub add_triple ($$$$) {
254     my ($self, $s =>$p=> $o) = @_;
255 wakaba 1.5 main::impl_err ("Subject undefined") unless defined $s;
256     main::impl_err ("Property undefined") unless defined $p;
257     main::impl_err ("Object undefined") unless defined $o;
258 wakaba 1.1 push @{$self->{triple}}, [$s =>$p=> $o];
259     }
260    
261     sub stringify ($) {
262     my ($self) = @_;
263     return join "\n", (map {"$_."} map {
264     sprintf '%s %s %s', map {
265     $_ =~ /^[_"]/ ? $_ : "<$_>"
266     } @{$_}[0, 1, 2];
267     } @{$self->{triple}}), '';
268     }
269    
270     sub stringify_as_xml ($) {
271     my ($self) = @_;
272     use RDF::Notation3::XML;
273     my $notation3 = RDF::Notation3::XML->new;
274     $notation3->parse_string ($self->stringify);
275 wakaba 1.2 my $xml = $notation3->get_string;
276     $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g;
277     # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//;
278     $xml;
279 wakaba 1.1 }
280    
281     1;
282    
283     __END__
284    
285     =head1 NAME
286    
287     dis2rdf.pl - dis to RDF converter
288    
289     =head1 SYNOPSIS
290    
291     $ perl dis2rdf.pl input.dis [options...] > output.rdf
292    
293     =head1 DESCRIPTION
294    
295     This script generates a RDF graph from a "dis" file.
296    
297     =over 4
298    
299     =item I<input.dis>
300    
301     The "dis" file from which a RDF graph is generated.
302    
303     =item I<output.rdf>
304    
305     An RDF/XML entity is outputed.
306    
307     =item C<--output-module>
308    
309     Show the relationship of modules.
310    
311     =item C<--output-type>
312    
313     Show the relationship of types.
314    
315     =item C<--output-for>
316    
317     Show the relationship of "for"s.
318    
319     =cut
320    
321     =head1 LICENSE
322    
323     Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
324    
325     This program is free software; you can redistribute it and/or
326     modify it under the same terms as Perl itself.
327    
328     Note that the copyright holder(s) of this script does not claim
329     any rights for materials outputed by this script, although
330     some of its part comes from this script. The copyright
331     holder(s) of source document should define their license terms.
332    
333     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24