| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # | ||
| 3 | # Generate perl modules from OWL files. | ||
| 4 | # | ||
| 5 | # $Id: owl2perl-generate-modules.pl,v 1.70 2010-02-11 18:16:44 ubuntu Exp $ | ||
| 6 | # Contact: Edward Kawas <edward.kawas+owl2perl@gmail.com> | ||
| 7 | # ----------------------------------------------------------- | ||
| 8 | # some command-line options | ||
| 9 | use Getopt::Std; | ||
| 10 | use vars qw/ $opt_h $opt_b $opt_u $opt_d $opt_v $opt_s $opt_i $opt_F $opt_o/; | ||
| 11 | getopts('hudvsFibo:'); | ||
| 12 | |||
| 13 | # usage | ||
| 14 | if ( &check_odo() or $opt_h or @ARGV == 0 ) { | ||
| 15 | print STDOUT <<'END_OF_USAGE'; | ||
| 16 | Generate perl modules from OWL files. | ||
| 17 | Usage: [-vdsib] [-o outdir] owl-class-file | ||
| 18 | [-vdsi] [-o outdir] -u owl-class-url | ||
| 19 | |||
| 20 | -u ... owl is from url | ||
| 21 | -s ... show generated code on STDOUT | ||
| 22 | (no file is created, disabled when no data type name given) | ||
| 23 | |||
| 24 | -b ... option to specify the base uri for the owl document (you will be prompted) | ||
| 25 | |||
| 26 | -i ... follow owl import statements | ||
| 27 | |||
| 28 | -v ... verbose | ||
| 29 | -d ... debug | ||
| 30 | -h ... help | ||
| 31 | |||
| 32 | Note: This script requires that the PERL module ODO, from IBM Semantic Layered | ||
| 33 | Research Platform be installed on your workstation! ODO is available on CPAN | ||
| 34 | as PLUTO. | ||
| 35 | |||
| 36 | END_OF_USAGE | ||
| 37 | exit(0); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub check_odo { | ||
| 41 | eval "require PLUTO"; | ||
| 42 | if ($@) { | ||
| 43 | print STDOUT | ||
| 44 | "Module PLUTO not installed and is required for this script.\n"; | ||
| 45 | print STDOUT "Should I proceed? [n] "; | ||
| 46 | my $tmp = <STDIN>; | ||
| 47 | $tmp =~ s/\s//g; | ||
| 48 | exit() unless $tmp =~ /y/i; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | # ----------------------------------------------------------- | ||
| 53 | use strict; | ||
| 54 | use warnings; | ||
| 55 | |||
| 56 | use OWL::Base; | ||
| 57 | use OWL::Utils; | ||
| 58 | use OWL2Perl; | ||
| 59 | |||
| 60 | use ODO::Parser::XML; | ||
| 61 | use ODO::Graph::Simple; | ||
| 62 | use ODO::Ontology::OWL::Lite; | ||
| 63 | use ODO::Graph::Simple; | ||
| 64 | use ODO::Node; | ||
| 65 | |||
| 66 | use Data::Dumper; | ||
| 67 | $LOG->level('INFO') if $opt_v; | ||
| 68 | $LOG->level('DEBUG') if $opt_d; | ||
| 69 | sub say { print @_, "\n"; } | ||
| 70 | |||
| 71 | my %imports_added; | ||
| 72 | say "Output is going to $opt_o\n" if $opt_o; | ||
| 73 | |||
| 74 | say "Using SAX parser $OWLCFG::XML_PARSER" if defined $OWLCFG::XML_PARSER and $opt_v; | ||
| 75 | |||
| 76 | # make sure that owl2perl-install was run! | ||
| 77 | unless ($opt_o) { | ||
| 78 | unless (defined ($OWLCFG::GENERATORS_OUTDIR)) { | ||
| 79 | print STDOUT <<EOT; | ||
| 80 | Unable to detect that you have run the 'owl2perl-install.pl' script! | ||
| 81 | This only has to be done once. Shall I proceed [n]? | ||
| 82 | EOT | ||
| 83 | my $tmp = <STDIN>; $tmp =~ s/\s//g; | ||
| 84 | exit() unless $tmp =~ /y/i; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | if (@ARGV) { | ||
| 89 | foreach my $arg (@ARGV) { | ||
| 90 | say 'Generating perl modules for: ' . $arg; | ||
| 91 | my $GRAPH_schema = ODO::Graph::Simple->Memory(); | ||
| 92 | my $GRAPH_source_data = ODO::Graph::Simple->Memory(); | ||
| 93 | if ($opt_u) { | ||
| 94 | say 'Downloading OWL file'; | ||
| 95 | my $owl = OWL::Utils::getHttpRequestByURL($arg); | ||
| 96 | my ( $statements, $imports ) = | ||
| 97 | ODO::Parser::XML->parse( | ||
| 98 | $owl, | ||
| 99 | base_uri => $arg, | ||
| 100 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||
| 101 | ); | ||
| 102 | $GRAPH_schema->add($statements); | ||
| 103 | $imports_added{$arg} = 1; | ||
| 104 | # process imports | ||
| 105 | if ($opt_i) { | ||
| 106 | foreach my $i (@$imports) { | ||
| 107 | $i =~ s/#*$//gi; | ||
| 108 | # skip imports we have already processed | ||
| 109 | next if $imports_added{$i}; | ||
| 110 | &process_import( $GRAPH_schema, $i ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } else { | ||
| 114 | say "Parsing schema file: $arg\n"; | ||
| 115 | my $base_uri = undef; | ||
| 116 | if ($opt_b) { | ||
| 117 | print STDOUT "Please specify the base uri for $arg: "; | ||
| 118 | my $tmp = <STDIN>; | ||
| 119 | $tmp =~ s/\s//g; | ||
| 120 | chomp($tmp); | ||
| 121 | # strip # from end if it exists | ||
| 122 | $tmp =~ s/#*$//gi; | ||
| 123 | $base_uri = $tmp; | ||
| 124 | } | ||
| 125 | my ( $statements, $imports ) = ODO::Parser::XML->parse_file($arg, | ||
| 126 | base_uri => $base_uri, | ||
| 127 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||
| 128 | ); | ||
| 129 | $GRAPH_schema->add($statements); | ||
| 130 | if ($opt_i) { | ||
| 131 | foreach my $i (@$imports) { | ||
| 132 | $i =~ s/#*$//gi; | ||
| 133 | # skip imports we have already processed | ||
| 134 | next if $imports_added{$i}; | ||
| 135 | &process_import( $GRAPH_schema, $i ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | # create the 'stuff' | ||
| 141 | say('Aggregating ontologies ...') if $opt_v; | ||
| 142 | my $SCHEMA = | ||
| 143 | ODO::Ontology::OWL::Lite->new( | ||
| 144 | graph => $GRAPH_source_data, | ||
| 145 | schema_graph => $GRAPH_schema, | ||
| 146 | schemaName => '', | ||
| 147 | verbose => $opt_v | ||
| 148 | ); | ||
| 149 | # instantiate OWL2Perl object, set force, outdir, SCHEMA then call generate_datatypes | ||
| 150 | my $owl2perl = OWL2Perl->new(force=>(defined $opt_F ? 1 : 0), outdir=>$opt_o); | ||
| 151 | if ($opt_s) { | ||
| 152 | my $code = ''; | ||
| 153 | $owl2perl->generate_datatypes( $SCHEMA, \$code ); | ||
| 154 | print STDOUT $code; | ||
| 155 | } else { | ||
| 156 | $owl2perl->generate_datatypes($SCHEMA); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub process_import { | ||
| 162 | my ($GRAPH_schema, $import) = @_; | ||
| 163 | $import =~ s/#*$//gi; | ||
| 164 | say ("\tProcessing import $import"); | ||
| 165 | my $owl = OWL::Utils::getHttpRequestByURL($import); | ||
| 166 | my ( $statements, $imports ) = ODO::Parser::XML->parse($owl, | ||
| 167 | base_uri => $import, | ||
| 168 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||
| 169 | ); | ||
| 170 | $GRAPH_schema->add($statements); | ||
| 171 | foreach my $i (@$imports) { | ||
| 172 | $i =~ s/#*$//gi; | ||
| 173 | # skip imports we have already processed | ||
| 174 | next if $imports_added{$i}; | ||
| 175 | $imports_added{$i} = 1; | ||
| 176 | &process_import( $GRAPH_schema, $i); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | say 'Done.'; | ||
| 181 | __END__ |