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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Wed Nov 3 11:49:39 2004 UTC (20 years ago) by wakaba
Branch: MAIN
File MIME type: text/plain
dis2rdf: New; dis2pm: New version under development

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24