| 1 | #!/usr/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 | 1 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use vars qw/ $opt_h $opt_b $opt_u $opt_d $opt_v $opt_s $opt_i $opt_F $opt_o/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | getopts('hudvsFibo:'); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # usage | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | if ( &check_odo() or $opt_h or @ARGV == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
&check_odo() or $opt_h or @ARGV == 0
&check_odo() or $opt_h
MC/DC0%0/3
&check_odo() or $opt_h or @ARGV == 0
&check_odo()
$opt_h
@ARGV == 0
Truth table25%1/4
A or B or C
A= &check_odo()B= $opt_hC= @ARGV == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | 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 | 0 | exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub check_odo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_odo: called CC=3 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | eval "require PLUTO"; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | print STDOUT | |||||||||||||||||||||||||||||||||||||||||
| 44 | "Module PLUTO not installed and is required for this script.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | print STDOUT "Should I proceed? [n] "; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $tmp = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $tmp =~ s/\s//g; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | exit() unless $tmp =~ /y/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # ----------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | use OWL::Base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | use OWL::Utils; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | use OWL2Perl; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | use ODO::Parser::XML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | use ODO::Graph::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use ODO::Ontology::OWL::Lite; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | use ODO::Graph::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use ODO::Node; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $LOG->level('INFO') if $opt_v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | $LOG->level('DEBUG') if $opt_d; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | sub say { print @_, "\n"; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
say: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my %imports_added; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | say "Output is going to $opt_o\n" if $opt_o; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | say "Using SAX parser $OWLCFG::XML_PARSER" if defined $OWLCFG::XML_PARSER and $opt_v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $OWLCFG::XML_PARSER and $main::opt_v
MC/DC0%0/2
defined $OWLCFG::XML_PARSER and $main::opt_v
defined $OWLCFG::XML_PARSER
$main::opt_v
Truth table33%1/3
A and B
A= defined $OWLCFG::XML_PARSERB= $main::opt_v
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # make sure that owl2perl-install was run! | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | unless ($opt_o) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless (defined ($OWLCFG::GENERATORS_OUTDIR)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | 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 | 0 | my $tmp = <STDIN>; $tmp =~ s/\s//g; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | exit() unless $tmp =~ /y/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | foreach my $arg (@ARGV) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | say 'Generating perl modules for: ' . $arg; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | my $GRAPH_schema = ODO::Graph::Simple->Memory(); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | my $GRAPH_source_data = ODO::Graph::Simple->Memory(); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | if ($opt_u) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | say 'Downloading OWL file'; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $owl = OWL::Utils::getHttpRequestByURL($arg); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my ( $statements, $imports ) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | ODO::Parser::XML->parse( | ||||||||||||||||||||||||||||||||||||||||||
| 98 | $owl, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | base_uri => $arg, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $GRAPH_schema->add($statements); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $imports_added{$arg} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | # process imports | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($opt_i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | foreach my $i (@$imports) { | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $i =~ s/#*$//gi; | |||||||||||||||||||||||||||||||||||||||||
| 108 | # skip imports we have already processed | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | next if $imports_added{$i}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | &process_import( $GRAPH_schema, $i ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | say "Parsing schema file: $arg\n"; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 2 | my $base_uri = undef; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | if ($opt_b) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | print STDOUT "Please specify the base uri for $arg: "; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $tmp = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $tmp =~ s/\s//g; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | chomp($tmp); | |||||||||||||||||||||||||||||||||||||||||
| 121 | # strip # from end if it exists | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $tmp =~ s/#*$//gi; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $base_uri = $tmp; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | my ( $statements, $imports ) = ODO::Parser::XML->parse_file($arg, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | base_uri => $base_uri, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||||||||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | $GRAPH_schema->add($statements); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | if ($opt_i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | foreach my $i (@$imports) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $i =~ s/#*$//gi; | |||||||||||||||||||||||||||||||||||||||||
| 133 | # skip imports we have already processed | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | next if $imports_added{$i}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | &process_import( $GRAPH_schema, $i ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # create the 'stuff' | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | say('Aggregating ontologies ...') if $opt_v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | 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 | 2 | ▶ | my $owl2perl = OWL2Perl->new(force=>(defined $opt_F ? 1 : 0), outdir=>$opt_o); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | ▶ | if ($opt_s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $code = ''; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $owl2perl->generate_datatypes( $SCHEMA, \$code ); | |||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | print STDOUT $code; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | $owl2perl->generate_datatypes($SCHEMA); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub process_import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_import: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my ($GRAPH_schema, $import) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $import =~ s/#*$//gi; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | say ("\tProcessing import $import"); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $owl = OWL::Utils::getHttpRequestByURL($import); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my ( $statements, $imports ) = ODO::Parser::XML->parse($owl, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | base_uri => $import, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | sax_parser => defined $OWLCFG::XML_PARSER ? $OWLCFG::XML_PARSER : undef | ||||||||||||||||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | $GRAPH_schema->add($statements); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | foreach my $i (@$imports) { | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $i =~ s/#*$//gi; | |||||||||||||||||||||||||||||||||||||||||
| 173 | # skip imports we have already processed | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | next if $imports_added{$i}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | $imports_added{$i} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | &process_import( $GRAPH_schema, $i); | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | say 'Done.'; | |||||||||||||||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||||||||||||||||