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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by wakaba, Sun Nov 7 13:07:53 2004 UTC revision 1.4 by wakaba, Mon Nov 8 07:23:30 2004 UTC
# Line 8  GetOptions ( Line 8  GetOptions (
8    'for=s' => \$Opt{For},    'for=s' => \$Opt{For},
9    'help' => \$Opt{help},    'help' => \$Opt{help},
10    'no-undef-check' => \$Opt{no_undef_check},    'no-undef-check' => \$Opt{no_undef_check},
11      'output-as-n3' => \$Opt{output_as_n3},
12      'output-as-xml' => \$Opt{output_as_xml},
13    'output-for' => \$Opt{output_for},    'output-for' => \$Opt{output_for},
14    'output-local-resource' => \$Opt{output_local_resource},    'output-local-resource' => \$Opt{output_local_resource},
15    'output-module' => \$Opt{output_module},    'output-module' => \$Opt{output_module},
16      'output-only-in-module=s' => \$Opt{output_resource_pattern},
17      'output-prop-perl' => \$Opt{output_prop_perl},
18    'output-resource' => \$Opt{output_resource},    'output-resource' => \$Opt{output_resource},
19      'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern},
20  ) or pod2usage (2);  ) or pod2usage (2);
21  if ($Opt{help}) {  if ($Opt{help}) {
22    pod2usage (0);    pod2usage (0);
23    exit;    exit;
24  }  }
25    if ($Opt{output_as_n3} and $Opt{output_as_xml}) {
26      pod2usage (2);
27      exit;
28    }
29    $Opt{output_as_xml} = 1 unless $Opt{output_as_n3};
30    
31  BEGIN {  BEGIN {
32  require 'manakai/genlib.pl';  require 'manakai/genlib.pl';
# Line 30  our $State; Line 40  our $State;
40  our $result = new manakai::n3;  our $result = new manakai::n3;
41    
42  $Opt{file_name} = shift;  $Opt{file_name} = shift;
43    $Opt{output_resource_pattern} ||= qr/.+/;
44    $Opt{output_resource_uri_pattern} ||= qr/.+/;
45    
46  $State->{DefaultFor} = $Opt{For};  $State->{DefaultFor} = $Opt{For};
47    
# Line 41  $State->{for_def_required}->{$State->{De Line 53  $State->{for_def_required}->{$State->{De
53  dis_check_undef_type_and_for ()  dis_check_undef_type_and_for ()
54    unless $Opt{no_undef_check};    unless $Opt{no_undef_check};
55    
56  my $primary = $result->get_new_anon_id;  if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) {
57      dis_perl_init ($source, For => $State->{DefaultFor});
58    }
59    
60    my $primary = $result->get_new_anon_id (Name => 'boot');
61  $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})  $result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module})
62                  if $Opt{output_module};                  if $Opt{output_module};
63  $result->add_triple ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})  $result->add_triple ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor})
# Line 70  for (keys %{$State->{Module}}) { Line 86  for (keys %{$State->{Module}}) {
86      for (keys %{$mod->{For}}) {      for (keys %{$mod->{For}}) {
87        $result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_);        $result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_);
88      }      }
89      for (keys %{$mod->{ISA}}) {      for (@{$mod->{ISA}}) {
90        $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);        $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
91      }      }
92        if ($Opt{output_prop_perl}) {
93          $result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:packageName>=>
94                               n3_literal $mod->{ExpandedURI q<dis2pm:packageName>})
95            if defined $mod->{ExpandedURI q<dis2pm:packageName>};
96        }
97    } else {    } else {
98      $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});      $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
99    }    }
# Line 81  for (keys %{$State->{Module}}) { Line 102  for (keys %{$State->{Module}}) {
102  if ($Opt{output_for}) {  if ($Opt{output_for}) {
103  for (keys %{$State->{For}}) {  for (keys %{$State->{For}}) {
104    my $mod = $State->{For}->{$_};    my $mod = $State->{For}->{$_};
105      next unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
106    if ($_ eq $mod->{URI}) {    if ($_ eq $mod->{URI}) {
107      $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>      $result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=>
108                           ExpandedURI q<d:For>);                           ExpandedURI q<d:For>);
109      $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI});      $result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI});
110      $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=>      $result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=>
111                           n3_literal $mod->{FullName});                           n3_literal $mod->{FullName});
112      for (keys %{$mod->{ISA}}) {      $result->add_triple ($mod->{URI} =>ExpandedURI q<d:parentModule>=>
113                             $mod->{parentModule});
114        for (@{$mod->{ISA}}) {
115        $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);        $result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_);
116      }      }
117      for (keys %{$mod->{Implement}}) {      for (@{$mod->{Implement}}) {
118        $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);        $result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_);
119      }      }
120    } else {    } else {
# Line 103  if ($Opt{output_resource}) { Line 127  if ($Opt{output_resource}) {
127    sub class_to_rdf ($;%) {    sub class_to_rdf ($;%) {
128      my ($mod, %opt) = @_;      my ($mod, %opt) = @_;
129      return unless defined $mod->{Name};      return unless defined $mod->{Name};
130        return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/;
131      if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or      if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or
132          not defined $mod->{URI}) {          not defined $mod->{URI}) {
133        my $uri = defined $mod->{URI} ? $mod->{URI} : $result->get_new_anon_id;        return if defined $mod->{URI} and
134                    $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/;
135          my $uri = defined $mod->{URI}
136                           ? $mod->{URI}
137                           : $result->get_new_anon_id (Name => $mod->{Name});
138        $result->add_triple ($uri =>ExpandedURI q<d:Name>=>        $result->add_triple ($uri =>ExpandedURI q<d:Name>=>
139                             n3_literal $mod->{Name}) if length $mod->{Name};                             n3_literal $mod->{Name}) if length $mod->{Name};
140        $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})        $result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI})
# Line 113  if ($Opt{output_resource}) { Line 142  if ($Opt{output_resource}) {
142        $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>        $result->add_triple ($uri =>ExpandedURI q<d:parentResource>=>
143                             $opt{parent_class_uri})                             $opt{parent_class_uri})
144          if defined $opt{parent_class_uri};          if defined $opt{parent_class_uri};
145          $result->add_triple ($uri =>ExpandedURI q<d:parentModule>=>
146                               $mod->{parentModule});
147        for (keys %{$mod->{Type}}) {        for (keys %{$mod->{Type}}) {
148          $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);          $result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_);
149        }        }
150        for (keys %{$mod->{ISA}}) {        for (@{$mod->{ISA}}) {
151          $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);          $result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_);
152        }        }
153        for (keys %{$mod->{Implement}}) {        for (@{$mod->{Implement}}) {
154          $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);          $result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_);
155        }        }
156        if ($Opt{output_local_resource}) {        if ($Opt{output_local_resource}) {
157          for (keys %{$mod->{Class}}) {          for (keys %{$mod->{Resource}}) {
158            class_to_rdf ($mod->{Class}->{$_}, parent_class => $mod,            class_to_rdf ($mod->{Resource}->{$_}, parent_class => $mod,
159                          parent_class_uri => $uri,                          parent_class_uri => $uri,
160                          key => $_);                          key => $_);
161          }          }
162        }        }
163      } else { ## Alias URI      } else { ## Alias URI
164        $result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI});        return if $opt{key} !~ /$Opt{output_resource_uri_pattern}/;
165          $result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI});
166      }      }
167    }    }
168    for (keys %{$State->{Type}}) {    for (sort keys %{$State->{Type}}) {
169      class_to_rdf ($State->{Type}->{$_}, key => $_);      class_to_rdf ($State->{Type}->{$_}, key => $_);
170    }    }
171  }  }
172    
173  print $result->stringify_as_xml;  if ($Opt{output_as_xml}) {
174      print $result->stringify_as_xml;
175    } else {
176      print $result->stringify;
177    }
178    
179  package manakai::n3;  package manakai::n3;
180  sub new ($) {  sub new ($) {
181    bless {triple => [], anon => 0}, shift;    bless {triple => [], anon => 0}, shift;
182  }  }
183    
184  sub get_new_anon_id ($) {  sub get_new_anon_id ($;%) {
185    my ($self) = @_;    my ($self, %opt) = @_;
186    return sprintf '_:r%d', $self->{anon}++;    my $s = $opt{Name} ? $opt{Name} : '';
187      return sprintf '_:r%d%s', $self->{anon}++, $s;
188  }  }
189    
190  sub add_triple ($$$$) {  sub add_triple ($$$$) {

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24