#!/usr/bin/perl -w use strict; use Getopt::Long; use Pod::Usage; my %Opt; GetOptions ( 'for=s' => \$Opt{For}, 'help' => \$Opt{help}, 'no-undef-check' => \$Opt{no_undef_check}, 'output-as-n3' => \$Opt{output_as_n3}, 'output-as-xml' => \$Opt{output_as_xml}, 'output-for' => \$Opt{output_for}, 'output-local-resource' => \$Opt{output_local_resource}, 'output-module' => \$Opt{output_module}, 'output-only-in-module=s' => \$Opt{output_resource_pattern}, 'output-prop-perl' => \$Opt{output_prop_perl}, 'output-resource' => \$Opt{output_resource}, 'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern}, ) or pod2usage (2); if ($Opt{help}) { pod2usage (0); exit; } if ($Opt{output_as_n3} and $Opt{output_as_xml}) { pod2usage (2); exit; } $Opt{output_as_xml} = 1 unless $Opt{output_as_n3}; BEGIN { require 'manakai/genlib.pl'; require 'manakai/dis.pl'; } sub n3_literal ($) { my $s = shift; qq<"$s">; } our $State; our $result = new manakai::n3; $Opt{file_name} = shift; $Opt{output_resource_pattern} ||= qr/.+/; $Opt{output_resource_uri_pattern} ||= qr/.+/; $State->{DefaultFor} = $Opt{For}; my $source = dis_load_module_file (module_file_name => $Opt{file_name}, For => $Opt{For}, use_default_for => 1); $State->{for_def_required}->{$State->{DefaultFor}} ||= 1; dis_check_undef_type_and_for () unless $Opt{no_undef_check}; if (dis_uri_for_match (ExpandedURI q, $State->{DefaultFor})) { dis_perl_init ($source, For => $State->{DefaultFor}); } my $primary = $result->get_new_anon_id (Name => 'boot'); $result->add_triple ($primary =>ExpandedURI q=> $State->{module}) if $Opt{output_module}; $result->add_triple ($primary =>ExpandedURI q => $State->{DefaultFor}) if $Opt{output_for}; if ($Opt{output_module}) { for (keys %{$State->{Module}}) { my $mod = $State->{Module}->{$_}; if ($_ eq $mod->{URI}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> ExpandedURI q); $result->add_triple ($mod->{URI} =>ExpandedURI q=> n3_literal $mod->{Name}); $result->add_triple ($mod->{URI} =>ExpandedURI q=> $mod->{NameURI}); $result->add_triple ($mod->{URI} =>ExpandedURI q=> $mod->{ModuleGroup}); $result->add_triple ($mod->{URI} =>ExpandedURI q=> n3_literal $mod->{FileName}) if defined $mod->{FileName}; $result->add_triple ($mod->{URI} =>ExpandedURI q=> $mod->{Namespace}); for (@{$mod->{require_module}||[]}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> $_); } for (keys %{$mod->{For}}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> $_); } for (@{$mod->{ISA}}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> $_); } if ($Opt{output_prop_perl}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> n3_literal $mod->{ExpandedURI q}) if defined $mod->{ExpandedURI q}; } } else { $result->add_triple ($_ =>ExpandedURI q=> $mod->{URI}); } }} if ($Opt{output_for}) { for (keys %{$State->{For}}) { my $mod = $State->{For}->{$_}; next unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/; if ($_ eq $mod->{URI}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> ExpandedURI q); $result->add_triple ($mod->{URI} =>ExpandedURI q=> $mod->{URI}); $result->add_triple ($mod->{URI} =>ExpandedURI q=> n3_literal $mod->{FullName}); $result->add_triple ($mod->{URI} =>ExpandedURI q=> $mod->{parentModule}); for (@{$mod->{ISA}}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> $_); } for (@{$mod->{Implement}}) { $result->add_triple ($mod->{URI} =>ExpandedURI q=> $_); } } else { $result->add_triple ($_ =>ExpandedURI q=> $mod->{URI}); } }} if ($Opt{output_resource}) { sub class_to_rdf ($;%); sub class_to_rdf ($;%) { my ($mod, %opt) = @_; return unless defined $mod->{Name}; return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/; if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or not defined $mod->{URI}) { return if defined $mod->{URI} and $mod->{URI} !~ /$Opt{output_resource_uri_pattern}/; my $uri = defined $mod->{URI} ? $mod->{URI} : $result->get_new_anon_id (Name => $mod->{Name}); $result->add_triple ($uri =>ExpandedURI q=> n3_literal $mod->{Name}) if length $mod->{Name}; $result->add_triple ($uri =>ExpandedURI q=> $mod->{NameURI}) if defined $mod->{NameURI}; $result->add_triple ($uri =>ExpandedURI q=> $opt{parent_class_uri}) if defined $opt{parent_class_uri}; $result->add_triple ($uri =>ExpandedURI q=> $mod->{parentModule}); for (keys %{$mod->{Type}}) { $result->add_triple ($uri =>ExpandedURI q=> $_); } for (@{$mod->{ISA}}) { $result->add_triple ($uri =>ExpandedURI q=> $_); } for (@{$mod->{Implement}}) { $result->add_triple ($uri =>ExpandedURI q=> $_); } if ($Opt{output_local_resource}) { for (keys %{$mod->{Resource}}) { class_to_rdf ($mod->{Resource}->{$_}, parent_class => $mod, parent_class_uri => $uri, key => $_); } } } else { ## Alias URI return if $opt{key} !~ /$Opt{output_resource_uri_pattern}/; $result->add_triple ($opt{key} =>ExpandedURI q=> $mod->{URI}); } } for (sort keys %{$State->{Type}}) { class_to_rdf ($State->{Type}->{$_}, key => $_); } } if ($Opt{output_as_xml}) { print $result->stringify_as_xml; } else { print $result->stringify; } package manakai::n3; sub new ($) { bless {triple => [], anon => 0}, shift; } sub get_new_anon_id ($;%) { my ($self, %opt) = @_; my $s = $opt{Name} ? $opt{Name} : ''; return sprintf '_:r%d%s', $self->{anon}++, $s; } sub add_triple ($$$$) { my ($self, $s =>$p=> $o) = @_; push @{$self->{triple}}, [$s =>$p=> $o]; } sub stringify ($) { my ($self) = @_; return join "\n", (map {"$_."} map { sprintf '%s %s %s', map { $_ =~ /^[_"]/ ? $_ : "<$_>" } @{$_}[0, 1, 2]; } @{$self->{triple}}), ''; } sub stringify_as_xml ($) { my ($self) = @_; use RDF::Notation3::XML; my $notation3 = RDF::Notation3::XML->new; $notation3->parse_string ($self->stringify); my $xml = $notation3->get_string; $xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g; # $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//; $xml; } 1; __END__ =head1 NAME dis2rdf.pl - dis to RDF converter =head1 SYNOPSIS $ perl dis2rdf.pl input.dis [options...] > output.rdf =head1 DESCRIPTION This script generates a RDF graph from a "dis" file. =over 4 =item I The "dis" file from which a RDF graph is generated. =item I An RDF/XML entity is outputed. =item C<--output-module> Show the relationship of modules. =item C<--output-type> Show the relationship of types. =item C<--output-for> Show the relationship of "for"s. =cut =head1 LICENSE Copyright 2004 Wakaba . All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Note that the copyright holder(s) of this script does not claim any rights for materials outputed by this script, although some of its part comes from this script. The copyright holder(s) of source document should define their license terms. =cut