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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Fri Nov 19 14:12:30 2004 UTC (20 years ago) by wakaba
Branch: MAIN
Changes since 1.4: +36 -11 lines
File MIME type: text/plain
Daily (Perl class & interface package name implemented)

1 wakaba 1.1 #!/usr/bin/perl -w
2     use strict;
3    
4     use Getopt::Long;
5     use Pod::Usage;
6     my %Opt;
7     GetOptions (
8     'for=s' => \$Opt{For},
9     'help' => \$Opt{help},
10 wakaba 1.5 'undef-check!' => \$Opt{no_undef_check},
11     'output-anon-resource!' => \$Opt{output_anon_resource},
12 wakaba 1.4 'output-as-n3' => \$Opt{output_as_n3},
13     'output-as-xml' => \$Opt{output_as_xml},
14 wakaba 1.5 'output-for!' => \$Opt{output_for},
15     'output-local-resource!' => \$Opt{output_local_resource},
16     'output-module!' => \$Opt{output_module},
17 wakaba 1.4 'output-only-in-module=s' => \$Opt{output_resource_pattern},
18 wakaba 1.5 'output-prop-perl!' => \$Opt{output_prop_perl},
19     'output-resource!' => \$Opt{output_resource},
20 wakaba 1.4 'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern},
21 wakaba 1.1 ) or pod2usage (2);
22     if ($Opt{help}) {
23     pod2usage (0);
24     exit;
25     }
26 wakaba 1.4 if ($Opt{output_as_n3} and $Opt{output_as_xml}) {
27     pod2usage (2);
28     exit;
29     }
30     $Opt{output_as_xml} = 1 unless $Opt{output_as_n3};
31 wakaba 1.5 $Opt{output_anon_resource} = 1 unless defined $Opt{output_anon_resource};
32     $Opt{output_local_resource} = 1 unless defined $Opt{output_local_resource};
33     $Opt{no_undef_check} = $Opt{no_undef_check} ? 0 : 1;
34 wakaba 1.1
35     BEGIN {
36     require 'manakai/genlib.pl';
37     require 'manakai/dis.pl';
38     }
39     sub n3_literal ($) {
40     my $s = shift;
41 wakaba 1.5 impl_err ("Literal value not defined") unless defined $s;
42 wakaba 1.1 qq<"$s">;
43     }
44     our $State;
45     our $result = new manakai::n3;
46    
47     $Opt{file_name} = shift;
48 wakaba 1.4 $Opt{output_resource_pattern} ||= qr/.+/;
49     $Opt{output_resource_uri_pattern} ||= qr/.+/;
50 wakaba 1.1
51     $State->{DefaultFor} = $Opt{For};
52 wakaba 1.2
53 wakaba 1.1 my $source = dis_load_module_file (module_file_name => $Opt{file_name},
54 wakaba 1.2 For => $Opt{For},
55 wakaba 1.1 use_default_for => 1);
56     $State->{for_def_required}->{$State->{DefaultFor}} ||= 1;
57    
58 wakaba 1.2 dis_check_undef_type_and_for ()
59     unless $Opt{no_undef_check};
60 wakaba 1.1
61 wakaba 1.4 if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) {
62     dis_perl_init ($source, For => $State->{DefaultFor});
63     }
64    
65     my $primary = $result->get_new_anon_id (Name => 'boot');
66 wakaba 1.1 $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
67     if $Opt{output_module};
68     $result->add_triple ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})
69     if $Opt{output_for};
70    
71     if ($Opt{output_module}) {
72     for (keys %{$State->{Module}}) {
73     my $mod = $State->{Module}->{$_};
74 wakaba 1.2 if ($_ eq $mod->{URI}) {
75     $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
76     ExpandedURI q<d:Module>);
77     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Name>=>
78 wakaba 1.1 n3_literal $mod->{Name});
79 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=>
80     $mod->{NameURI});
81     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:ModuleGroup>=>
82     $mod->{ModuleGroup});
83     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FileName>=>
84 wakaba 1.1 n3_literal $mod->{FileName})
85     if defined $mod->{FileName};
86 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=>
87     $mod->{Namespace});
88 wakaba 1.1 for (@{$mod->{require_module}||[]}) {
89 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Require>=> $_);
90     }
91     for (keys %{$mod->{For}}) {
92     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_);
93     }
94 wakaba 1.4 for (@{$mod->{ISA}}) {
95 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
96 wakaba 1.1 }
97 wakaba 1.4 if ($Opt{output_prop_perl}) {
98     $result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:packageName>=>
99     n3_literal $mod->{ExpandedURI q<dis2pm:packageName>})
100     if defined $mod->{ExpandedURI q<dis2pm:packageName>};
101     }
102 wakaba 1.1 } else {
103 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
104 wakaba 1.1 }
105     }}
106    
107     if ($Opt{output_for}) {
108     for (keys %{$State->{For}}) {
109     my $mod = $State->{For}->{$_};
110 wakaba 1.2 if ($_ eq $mod->{URI}) {
111     $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
112     ExpandedURI q<d:For>);
113     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI});
114     $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=>
115 wakaba 1.5 n3_literal $mod->{FullName})
116     if defined $mod->{FullName};
117 wakaba 1.4 for (@{$mod->{ISA}}) {
118 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
119 wakaba 1.1 }
120 wakaba 1.4 for (@{$mod->{Implement}}) {
121 wakaba 1.2 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
122 wakaba 1.1 }
123     } else {
124 wakaba 1.2 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
125 wakaba 1.1 }
126     }}
127    
128 wakaba 1.3 if ($Opt{output_resource}) {
129 wakaba 1.2 sub class_to_rdf ($;%);
130     sub class_to_rdf ($;%) {
131     my ($mod, %opt) = @_;
132     return unless defined $mod->{Name};
133 wakaba 1.4 return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
134 wakaba 1.2 if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
135     not defined $mod->{URI}) {
136 wakaba 1.4 return if defined $mod->{URI} and
137     $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
138 wakaba 1.5 return if not defined $mod->{URI} and not $Opt{output_anon_resource};
139 wakaba 1.4 my $uri = defined $mod->{URI}
140     ? $mod->{URI}
141 wakaba 1.5 : ($mod->{ExpandedURI q<d:anonID>}
142     ||= $result->get_new_anon_id (Name => $mod->{Name}));
143 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
144     n3_literal $mod->{Name}) if length $mod->{Name};
145     $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
146     if defined $mod->{NameURI};
147 wakaba 1.3 $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
148 wakaba 1.2 $opt{parent_class_uri})
149     if defined $opt{parent_class_uri};
150 wakaba 1.4 $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
151     $mod->{parentModule});
152 wakaba 1.2 for (keys %{$mod->{Type}}) {
153     $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
154     }
155 wakaba 1.4 for (@{$mod->{ISA}}) {
156 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
157     }
158 wakaba 1.4 for (@{$mod->{Implement}}) {
159 wakaba 1.2 $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
160     }
161 wakaba 1.5 for (keys %{$mod->{For}}) {
162     $result->add_triple ($uri =>ExpandedURI q<d:For>=> $_);
163     }
164     for (@{$mod->{hasResource}||[]}) {
165     my $ruri = defined $_->{URI}
166     ? $_->{URI}
167     : ($_->{ExpandedURI q<d:anonID>}
168     ||= $result->get_new_anon_id (Name => $_->{Name}));
169     $result->add_triple ($uri =>ExpandedURI q<d:hasResource>=> $ruri);
170     }
171     if ($Opt{output_prop_perl}) {
172     for my $prop (ExpandedURI q<dis2pm:packageName>,
173     ExpandedURI q<dis2pm:ifPackagePrefix>) {
174     $result->add_triple ($mod->{URI} =>$prop=> n3_literal $mod->{$prop})
175     if defined $mod->{$prop};
176     }
177     }
178 wakaba 1.3 if ($Opt{output_local_resource}) {
179 wakaba 1.4 for (keys %{$mod->{Resource}}) {
180     class_to_rdf ($mod->{Resource}->{$_}, parent_class => $mod,
181 wakaba 1.2 parent_class_uri => $uri,
182     key => $_);
183     }
184     }
185     } else { ## Alias URI
186 wakaba 1.4 return if $opt{key} !~ /$Opt{output_resource_uri_pattern}/;
187     $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
188 wakaba 1.1 }
189     }
190 wakaba 1.4 for (sort keys %{$State->{Type}}) {
191 wakaba 1.2 class_to_rdf ($State->{Type}->{$_}, key => $_);
192     }
193     }
194 wakaba 1.1
195 wakaba 1.4 if ($Opt{output_as_xml}) {
196     print $result->stringify_as_xml;
197     } else {
198     print $result->stringify;
199     }
200 wakaba 1.1
201     package manakai::n3;
202     sub new ($) {
203     bless {triple => [], anon => 0}, shift;
204     }
205    
206 wakaba 1.4 sub get_new_anon_id ($;%) {
207     my ($self, %opt) = @_;
208     my $s = $opt{Name} ? $opt{Name} : '';
209     return sprintf '_:r%d%s', $self->{anon}++, $s;
210 wakaba 1.1 }
211    
212     sub add_triple ($$$$) {
213     my ($self, $s =>$p=> $o) = @_;
214 wakaba 1.5 main::impl_err ("Subject undefined") unless defined $s;
215     main::impl_err ("Property undefined") unless defined $p;
216     main::impl_err ("Object undefined") unless defined $o;
217 wakaba 1.1 push @{$self->{triple}}, [$s =>$p=> $o];
218     }
219    
220     sub stringify ($) {
221     my ($self) = @_;
222     return join "\n", (map {"$_."} map {
223     sprintf '%s %s %s', map {
224     $_ =~ /^[_"]/ ? $_ : "<$_>"
225     } @{$_}[0, 1, 2];
226     } @{$self->{triple}}), '';
227     }
228    
229     sub stringify_as_xml ($) {
230     my ($self) = @_;
231     use RDF::Notation3::XML;
232     my $notation3 = RDF::Notation3::XML->new;
233     $notation3->parse_string ($self->stringify);
234 wakaba 1.2 my $xml = $notation3->get_string;
235     $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g;
236     # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//;
237     $xml;
238 wakaba 1.1 }
239    
240     1;
241    
242     __END__
243    
244     =head1 NAME
245    
246     dis2rdf.pl - dis to RDF converter
247    
248     =head1 SYNOPSIS
249    
250     $ perl dis2rdf.pl input.dis [options...] > output.rdf
251    
252     =head1 DESCRIPTION
253    
254     This script generates a RDF graph from a "dis" file.
255    
256     =over 4
257    
258     =item I<input.dis>
259    
260     The "dis" file from which a RDF graph is generated.
261    
262     =item I<output.rdf>
263    
264     An RDF/XML entity is outputed.
265    
266     =item C<--output-module>
267    
268     Show the relationship of modules.
269    
270     =item C<--output-type>
271    
272     Show the relationship of types.
273    
274     =item C<--output-for>
275    
276     Show the relationship of "for"s.
277    
278     =cut
279    
280     =head1 LICENSE
281    
282     Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
283    
284     This program is free software; you can redistribute it and/or
285     modify it under the same terms as Perl itself.
286    
287     Note that the copyright holder(s) of this script does not claim
288     any rights for materials outputed by this script, although
289     some of its part comes from this script. The copyright
290     holder(s) of source document should define their license terms.
291    
292     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24