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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Sun Nov 7 13:07:53 2004 UTC (20 years ago) by wakaba
Branch: MAIN
Changes since 1.2: +5 -5 lines
File MIME type: text/plain
Daily

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 'no-undef-check' => \$Opt{no_undef_check},
11 'output-for' => \$Opt{output_for},
12 'output-local-resource' => \$Opt{output_local_resource},
13 'output-module' => \$Opt{output_module},
14 'output-resource' => \$Opt{output_resource},
15 ) or pod2usage (2);
16 if ($Opt{help}) {
17 pod2usage (0);
18 exit;
19 }
20
21 BEGIN {
22 require 'manakai/genlib.pl';
23 require 'manakai/dis.pl';
24 }
25 sub n3_literal ($) {
26 my $s = shift;
27 qq<"$s">;
28 }
29 our $State;
30 our $result = new manakai::n3;
31
32 $Opt{file_name} = shift;
33
34 $State->{DefaultFor} = $Opt{For};
35
36 my $source = dis_load_module_file (module_file_name => $Opt{file_name},
37 For => $Opt{For},
38 use_default_for => 1);
39 $State->{for_def_required}->{$State->{DefaultFor}} ||= 1;
40
41 dis_check_undef_type_and_for ()
42 unless $Opt{no_undef_check};
43
44 my $primary = $result->get_new_anon_id;
45 $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
46 if $Opt{output_module};
47 $result->add_triple ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})
48 if $Opt{output_for};
49
50 if ($Opt{output_module}) {
51 for (keys %{$State->{Module}}) {
52 my $mod = $State->{Module}->{$_};
53 if ($_ eq $mod->{URI}) {
54 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
55 ExpandedURI q<d:Module>);
56 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Name>=>
57 n3_literal $mod->{Name});
58 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=>
59 $mod->{NameURI});
60 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:ModuleGroup>=>
61 $mod->{ModuleGroup});
62 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FileName>=>
63 n3_literal $mod->{FileName})
64 if defined $mod->{FileName};
65 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=>
66 $mod->{Namespace});
67 for (@{$mod->{require_module}||[]}) {
68 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Require>=> $_);
69 }
70 for (keys %{$mod->{For}}) {
71 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_);
72 }
73 for (keys %{$mod->{ISA}}) {
74 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
75 }
76 } else {
77 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
78 }
79 }}
80
81 if ($Opt{output_for}) {
82 for (keys %{$State->{For}}) {
83 my $mod = $State->{For}->{$_};
84 if ($_ eq $mod->{URI}) {
85 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
86 ExpandedURI q<d:For>);
87 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI});
88 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=>
89 n3_literal $mod->{FullName});
90 for (keys %{$mod->{ISA}}) {
91 $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
92 }
93 for (keys %{$mod->{Implement}}) {
94 $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
95 }
96 } else {
97 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
98 }
99 }}
100
101 if ($Opt{output_resource}) {
102 sub class_to_rdf ($;%);
103 sub class_to_rdf ($;%) {
104 my ($mod, %opt) = @_;
105 return unless defined $mod->{Name};
106 if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
107 not defined $mod->{URI}) {
108 my $uri = defined $mod->{URI} ? $mod->{URI} : $result->get_new_anon_id;
109 $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
110 n3_literal $mod->{Name}) if length $mod->{Name};
111 $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
112 if defined $mod->{NameURI};
113 $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
114 $opt{parent_class_uri})
115 if defined $opt{parent_class_uri};
116 for (keys %{$mod->{Type}}) {
117 $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
118 }
119 for (keys %{$mod->{ISA}}) {
120 $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
121 }
122 for (keys %{$mod->{Implement}}) {
123 $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
124 }
125 if ($Opt{output_local_resource}) {
126 for (keys %{$mod->{Class}}) {
127 class_to_rdf ($mod->{Class}->{$_}, parent_class => $mod,
128 parent_class_uri => $uri,
129 key => $_);
130 }
131 }
132 } else { ## Alias URI
133 $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
134 }
135 }
136 for (keys %{$State->{Type}}) {
137 class_to_rdf ($State->{Type}->{$_}, key => $_);
138 }
139 }
140
141 print $result->stringify_as_xml;
142
143 package manakai::n3;
144 sub new ($) {
145 bless {triple => [], anon => 0}, shift;
146 }
147
148 sub get_new_anon_id ($) {
149 my ($self) = @_;
150 return sprintf '_:r%d', $self->{anon}++;
151 }
152
153 sub add_triple ($$$$) {
154 my ($self, $s =>$p=> $o) = @_;
155 push @{$self->{triple}}, [$s =>$p=> $o];
156 }
157
158 sub stringify ($) {
159 my ($self) = @_;
160 return join "\n", (map {"$_."} map {
161 sprintf '%s %s %s', map {
162 $_ =~ /^[_"]/ ? $_ : "<$_>"
163 } @{$_}[0, 1, 2];
164 } @{$self->{triple}}), '';
165 }
166
167 sub stringify_as_xml ($) {
168 my ($self) = @_;
169 use RDF::Notation3::XML;
170 my $notation3 = RDF::Notation3::XML->new;
171 $notation3->parse_string ($self->stringify);
172 my $xml = $notation3->get_string;
173 $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g;
174 # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//;
175 $xml;
176 }
177
178 1;
179
180 __END__
181
182 =head1 NAME
183
184 dis2rdf.pl - dis to RDF converter
185
186 =head1 SYNOPSIS
187
188 $ perl dis2rdf.pl input.dis [options...] > output.rdf
189
190 =head1 DESCRIPTION
191
192 This script generates a RDF graph from a "dis" file.
193
194 =over 4
195
196 =item I<input.dis>
197
198 The "dis" file from which a RDF graph is generated.
199
200 =item I<output.rdf>
201
202 An RDF/XML entity is outputed.
203
204 =item C<--output-module>
205
206 Show the relationship of modules.
207
208 =item C<--output-type>
209
210 Show the relationship of types.
211
212 =item C<--output-for>
213
214 Show the relationship of "for"s.
215
216 =cut
217
218 =head1 LICENSE
219
220 Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved.
221
222 This program is free software; you can redistribute it and/or
223 modify it under the same terms as Perl itself.
224
225 Note that the copyright holder(s) of this script does not claim
226 any rights for materials outputed by this script, although
227 some of its part comes from this script. The copyright
228 holder(s) of source document should define their license terms.
229
230 =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24