| 1 | package Class::OWL::MOP; | ||
| 2 | 6 | ▶ | use base qw(Class::MOP::Class); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use Data::Dumper; | ||
| 5 | |||
| 6 | sub from_rdf { | ||
| 7 | my ($self,$uri,$rdf) = @_; | ||
| 8 | |||
| 9 | die "Inconsistent type" | ||
| 10 | unless $rdf->exists($uri,'rdf:type',$self->_type); | ||
| 11 | |||
| 12 | Class::OWL->from_rdf($uri,$rdf); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub new_instance { | ||
| 16 | my $self = shift; | ||
| 17 | my ($uri,$rdf); | ||
| 18 | |||
| 19 | # (uri,rdf) | ||
| 20 | # (uri) | ||
| 21 | # (rdf) | ||
| 22 | # (rdf,uri) | ||
| 23 | |||
| 24 | $uri = shift unless ref $_[0]; | ||
| 25 | $rdf = shift if ref $_[0] && $_[0]->isa("RDF::Helper"); | ||
| 26 | $uri = shift unless $uri; | ||
| 27 | |||
| 28 | $rdf = Class::OWL->new_model() unless $rdf; | ||
| 29 | $uri = $rdf->new_bnode() unless $uri; | ||
| 30 | return Class::OWL->new_instance($rdf,$self->_type => $uri,@_); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub accessor { | ||
| 34 | my ($mop,$o,$a,$v) = @_; | ||
| 35 | |||
| 36 | my $attr = $mop->find_attribute_by_name('$'.$a); | ||
| 37 | |||
| 38 | unless ($attr) { | ||
| 39 | warn caller(); | ||
| 40 | die "No such attribute $a on $o"; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (defined $v) { | ||
| 44 | if ($v) { | ||
| 45 | $attr->set_value($o,$v); | ||
| 46 | } else { | ||
| 47 | $attr->clear_value($o); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | $v = $attr->get_value($o); | ||
| 51 | |||
| 52 | return undef unless defined $v; | ||
| 53 | |||
| 54 | if (wantarray) { | ||
| 55 | return ref $v eq 'ARRAY' ? @$v : ($v); | ||
| 56 | } else { | ||
| 57 | return ref $v eq 'ARRAY' ? $v->[0] : $v; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | package Class::OWL::Property; | ||
| 62 | use base qw(Class::MOP::Attribute); | ||
| 63 | use Data::Dumper; | ||
| 64 | |||
| 65 | sub _resource { | ||
| 66 | $_[0]->{_resource}; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _cardinality { | ||
| 70 | $_[0]->{_cardinality} = $_[1] if exists $_[1]; | ||
| 71 | $_[0]->{_cardinality}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _domain { | ||
| 75 | $_[0]->{_domain} = $_[1] if exists $_[1]; | ||
| 76 | $_[0]->{_domain}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _range { | ||
| 80 | $_[0]->{_range} = $_[1] if exists $_[1]; | ||
| 81 | $_[0]->{_range}; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub single_valued { | ||
| 85 | $_[0]->max_cardinality == 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub max_cardinality { | ||
| 89 | $_[0]->{_cardinality}->[1]; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub min_cardinality { | ||
| 93 | $_[0]->{_cardinality}->[0]; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub restrict { | ||
| 97 | my ($p,$property_data,$rdf) = @_; | ||
| 98 | $p->{_cardinality} = [0,undef] unless $p->{_cardinality}; | ||
| 99 | if ($rdf->exists($p->{_resource},'rdf:type','owl:FunctionalProperty')) | ||
| 100 | { | ||
| 101 | $p->{_cardinality} = [0,1]; | ||
| 102 | } | ||
| 103 | $p->{_cardinality} = [0,$property_data->{'owl:cardinality'}] | ||
| 104 | if $property_data->{'owl:cardinality'}; | ||
| 105 | $p->{_cardinality} = [$property_data->{'owl:minCardinality'},$property_data->{'owl:maxCardinality'}] | ||
| 106 | if $property_data->{'owl:minCardinality'} || $property_data->{'owl:maxCardinality'}; | ||
| 107 | |||
| 108 | $p->{_domain} = $property_data->{'rdfs:domain'} | ||
| 109 | if $property_data->{'rdfs:domain'}; | ||
| 110 | $p->{_range} = $property_data->{'rdfs:range'} | ||
| 111 | if $property_data->{'rdfs:range'}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _accessor_info { | ||
| 115 | my ($self,$name) = @_; | ||
| 116 | |||
| 117 | return { | ||
| 118 | $name => sub { my ($o,$v) = @_; $o->meta->accessor($o,$name,$v) } | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub new { | ||
| 123 | my ( $class, $resource, $property_data, $rdf ) = @_; | ||
| 124 | |||
| 125 | my $value = undef; | ||
| 126 | my $name = Class::OWL::_get_name($resource); | ||
| 127 | die "Malformed resource $resource" unless $name; | ||
| 128 | my $p = bless Class::MOP::Attribute->new( | ||
| 129 | '$'.$name => ( | ||
| 130 | accessor => Class::OWL::Property->_accessor_info($name), | ||
| 131 | init_arg => ':' . $name, | ||
| 132 | default => $value, | ||
| 133 | ) | ||
| 134 | ), $class; | ||
| 135 | $p->{_resource} = $resource; | ||
| 136 | $p->restrict($property_data,$rdf); | ||
| 137 | |||
| 138 | $p; | ||
| 139 | } | ||
| 140 | |||
| 141 | package Class::OWL; | ||
| 142 | |||
| 143 | use version; $VERSION = qv('0.0.6'); | ||
| 144 | |||
| 145 | use warnings; | ||
| 146 | use strict; | ||
| 147 | use Carp; | ||
| 148 | |||
| 149 | use RDF::Helper; | ||
| 150 | use Class::MOP; | ||
| 151 | |||
| 152 | #use Class::OWL::MOP::Class; | ||
| 153 | use Class::MOP::Attribute; | ||
| 154 | |||
| 155 | use LWP::Simple qw(get); | ||
| 156 | |||
| 157 | use XML::CommonNS qw(RDF RDFS OWL); | ||
| 158 | |||
| 159 | use Data::Dumper; | ||
| 160 | |||
| 161 | my %CONFIG = ( | ||
| 162 | Namespaces => { | ||
| 163 | rdf => "$RDF", | ||
| 164 | rdfs => "$RDFS", | ||
| 165 | owl => "$OWL", | ||
| 166 | }, | ||
| 167 | ExpandQNames => 1, | ||
| 168 | ); | ||
| 169 | |||
| 170 | my $DEBUG = 0; | ||
| 171 | sub debug($) { return unless $DEBUG; print STDERR @_, "\n" } | ||
| 172 | |||
| 173 | sub import { | ||
| 174 | my $class = shift; | ||
| 175 | my %opt = @_; | ||
| 176 | |||
| 177 | if ( $opt{debug} ) { $DEBUG = 1; } | ||
| 178 | |||
| 179 | if ( $opt{namespaces} ) { | ||
| 180 | $CONFIG{Namespaces} = | ||
| 181 | { %{ $CONFIG{Namespaces} }, %{ $opt{namespaces} }, }; | ||
| 182 | } | ||
| 183 | |||
| 184 | if ( $opt{url} ) { | ||
| 185 | $CONFIG{Namespaces}{'#default'} ||= $opt{url}; | ||
| 186 | $class->parse_url( $opt{package} || __PACKAGE__, $opt{url} ); | ||
| 187 | } | ||
| 188 | |||
| 189 | if ( $opt{file} ) { | ||
| 190 | $class->parse_url( $opt{package} || __PACKAGE__, $opt{file} ); | ||
| 191 | } | ||
| 192 | |||
| 193 | if ( $opt{owl} ) { | ||
| 194 | $class->parse_rdfxml($opt{package} || __PACKAGE__,$opt{owl}); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _assert_triple { | ||
| 199 | my ($rdf,$subject,$predicate,$object) = @_; | ||
| 200 | |||
| 201 | if (ref $object eq 'ARRAY') { | ||
| 202 | foreach my $v (@{$object}) { | ||
| 203 | _assert_triple($rdf,$subject,$predicate,$v); | ||
| 204 | } | ||
| 205 | } elsif (ref $object) { | ||
| 206 | $rdf->assert_resource( $subject, $predicate,$object->_resource ); | ||
| 207 | Class::OWL->to_rdf($object,$rdf); | ||
| 208 | } else { | ||
| 209 | $rdf->assert_literal( $subject, $predicate, $object ); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | sub to_rdf($) { | ||
| 214 | my ( $self, $i, $rdf ) = @_; | ||
| 215 | $rdf = $self->new_model() unless $rdf; | ||
| 216 | foreach my $t (@{$i->_type()}) { | ||
| 217 | $rdf->assert_resource( $i->_resource, 'rdf:type', $t ); | ||
| 218 | } | ||
| 219 | foreach my $attr ( $i->meta->compute_all_applicable_attributes() ) { | ||
| 220 | next if $attr->name eq '$_resource' || $attr->name eq '$_model' || $attr->name eq '$_type'; | ||
| 221 | next unless $attr->has_value($i); | ||
| 222 | _assert_triple($rdf,$i->_resource,$attr->_resource,$attr->get_value($i)); | ||
| 223 | } | ||
| 224 | return $rdf; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub _phash { | ||
| 228 | my ($rdf,$subject) = @_; | ||
| 229 | my %h; | ||
| 230 | foreach my $stmt ($rdf->get_statements($subject)) { | ||
| 231 | $h{_r_name($stmt->[1])} = _r_name($stmt->[2]); | ||
| 232 | } | ||
| 233 | \%h; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub from_rdf { | ||
| 237 | my ($self,$subject,$rdf) = @_; | ||
| 238 | |||
| 239 | unless (ref $rdf) | ||
| 240 | { | ||
| 241 | my $xml = $rdf; | ||
| 242 | $rdf = $self->new_model(); | ||
| 243 | $rdf->include_rdfxml( xml => $xml ); | ||
| 244 | } | ||
| 245 | |||
| 246 | $subject = $rdf->new_resource($subject) unless ref $subject; | ||
| 247 | my $instance_data = $rdf->property_hash($subject); | ||
| 248 | my $type = $instance_data->{'rdf:type'}; | ||
| 249 | my $o = Class::OWL->new_instance($rdf,$type => $subject); | ||
| 250 | my $m = $o->meta; | ||
| 251 | |||
| 252 | foreach my $stmt ($rdf->get_statements($subject)) { | ||
| 253 | next if $stmt->[1]->as_string eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; | ||
| 254 | my $p = Class::OWL->owl_property($stmt->[1]); | ||
| 255 | die "Unknown property type "._r_name($stmt->[1]) unless $p; | ||
| 256 | my $attr = $m->find_attribute_by_name($p->name); | ||
| 257 | die "Unknown attribute ".$p->name unless $attr; | ||
| 258 | |||
| 259 | my $value; | ||
| 260 | if ($stmt->[2]->is_literal) | ||
| 261 | { | ||
| 262 | $value = $stmt->[2]->literal_value; | ||
| 263 | } | ||
| 264 | else | ||
| 265 | { | ||
| 266 | $value = $self->from_rdf($stmt->[2],$rdf); | ||
| 267 | } | ||
| 268 | |||
| 269 | CASE: { | ||
| 270 | $attr->single_valued and do { | ||
| 271 | $attr->set_value($o,$value); | ||
| 272 | },last CASE; | ||
| 273 | |||
| 274 | !$attr->max_cardinality || $attr->max_cardinality > 1 and do { | ||
| 275 | my $v = $attr->get_value($o) || []; | ||
| 276 | push(@{$v},$value); | ||
| 277 | $attr->set_value($o,$v); | ||
| 278 | },last CASE; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | $o; | ||
| 283 | } | ||
| 284 | |||
| 285 | our %class; | ||
| 286 | our %attribute; | ||
| 287 | |||
| 288 | sub _r_name { | ||
| 289 | my $r = ref $_[0] ? $_[0] : RDF::Helper->new_resource($_[0]); | ||
| 290 | $r->is_blank ? $r->blank_identifier : $r->uri->as_string; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _f { | ||
| 294 | ref $_[0] eq 'ARRAY' ? $_[0]->[0] : $_[0]; # XXX should be the most significant class! | ||
| 295 | } | ||
| 296 | |||
| 297 | sub owl_class { | ||
| 298 | my $self = shift; | ||
| 299 | my $u = _f($_[0]); | ||
| 300 | my $name = _r_name($u); | ||
| 301 | die "Malformed resource $u ", caller() unless $name; | ||
| 302 | $class{$name} = $_[1] if $_[1]; | ||
| 303 | return $class{$name}; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub owl_property { | ||
| 307 | my $self = shift; | ||
| 308 | my $u = _f($_[0]); | ||
| 309 | my $name = _r_name($u); | ||
| 310 | die "Malformed resource $u", caller() unless $name; | ||
| 311 | $attribute{$name} = $_[1] if $_[1]; | ||
| 312 | return $attribute{$name}; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub new_instance { | ||
| 316 | my ( $self, $rdf, $type, $subject, @params ) = @_; | ||
| 317 | #warn caller(); | ||
| 318 | #warn $self; | ||
| 319 | #warn $rdf; | ||
| 320 | #warn $type; | ||
| 321 | #warn $subject; | ||
| 322 | |||
| 323 | my $c = $self->owl_class($type); | ||
| 324 | die "No such class $type" unless $c; | ||
| 325 | debug "New instance of "._r_name(_f($type))." -> ".$c->name; | ||
| 326 | my $o = $c->new_object(@params); | ||
| 327 | $o->_resource($subject) if $subject; | ||
| 328 | $o->_model($rdf); | ||
| 329 | $o->_type([_f($type)]); | ||
| 330 | $o; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub new_model { return RDF::Helper->new(%CONFIG); } | ||
| 334 | |||
| 335 | sub _for_type { | ||
| 336 | my ( $rdf, $type, $sub) = @_; | ||
| 337 | if ( $rdf->exists( undef, undef, $type ) ) { | ||
| 338 | for my $s ( $rdf->get_statements(undef, 'rdf:type', $type ) ) { | ||
| 339 | $sub->( $s->[0], $rdf->property_hash($s->[0]), $rdf ); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | sub _for_statements { | ||
| 345 | my ( $rdf, $subject, $predicate, $sub ) = @_; | ||
| 346 | foreach my $stmt ( $rdf->get_statements( $subject, $predicate ) ) { | ||
| 347 | $sub->( $rdf, $stmt->[2] ); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | sub parse_url { | ||
| 352 | my ( $self, $package, $url ) = @_; | ||
| 353 | ( my $uri = $url ) =~ s/\.rdf$//; | ||
| 354 | my $rdfxml = get($url); | ||
| 355 | return $self->parse_rdfxml($package,$rdfxml); | ||
| 356 | } | ||
| 357 | |||
| 358 | sub parse_rdfxml { | ||
| 359 | my ( $self, $package, $rdfxml ) = @_; | ||
| 360 | |||
| 361 | my $rdf = $self->new_model(); | ||
| 362 | $rdf->include_rdfxml( xml => $rdfxml ); | ||
| 363 | #print $rdf->serialize( filename => '/tmp/model.n3', format => 'ntriples' ); | ||
| 364 | if ( $rdf->exists( undef, 'rdf:type', 'owl:Class' ) ) { | ||
| 365 | _parse_classes($package,$rdf); | ||
| 366 | _parse_properties($package,$rdf); | ||
| 367 | _parse_inheritance($package,$rdf); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | sub _get_name { return ( $_[0] =~ /[#\/]([^#\/]+)$/ )[0] } | ||
| 372 | |||
| 373 | sub _parse_properties { | ||
| 374 | my ($package,$rdf) = @_; | ||
| 375 | _for_type( | ||
| 376 | $rdf, | ||
| 377 | 'http://www.w3.org/2002/07/owl#ObjectProperty', | ||
| 378 | sub { | ||
| 379 | my ( $resource, $property_data, $rdf ) = @_; | ||
| 380 | _create_property( $resource, $property_data, $rdf ); | ||
| 381 | } | ||
| 382 | ); | ||
| 383 | _for_type( | ||
| 384 | $rdf, | ||
| 385 | 'http://www.w3.org/2002/07/owl#DatatypeProperty', | ||
| 386 | sub { | ||
| 387 | my ( $resource, $property_data,$rdf ) = @_; | ||
| 388 | _create_property( $resource, $property_data, $rdf ); | ||
| 389 | } | ||
| 390 | ); | ||
| 391 | } | ||
| 392 | |||
| 393 | sub _create_property { | ||
| 394 | my ( $resource, $property_data, $rdf ) = @_; | ||
| 395 | |||
| 396 | my $property = Class::OWL::Property->new(_r_name($resource),$property_data,$rdf); | ||
| 397 | Class::OWL->owl_property( $resource, $property ); | ||
| 398 | debug "Created property " | ||
| 399 | . $property->_resource . " as " | ||
| 400 | . $property->name; | ||
| 401 | |||
| 402 | my $domain = $property_data->{'rdfs:domain'}; | ||
| 403 | if ($domain) | ||
| 404 | { | ||
| 405 | my $domain_class = Class::OWL->owl_class($domain); | ||
| 406 | die "No such domain class $domain" unless $domain_class; | ||
| 407 | $domain_class->add_attribute($property); | ||
| 408 | debug "Added property " | ||
| 409 | . $property->name | ||
| 410 | . " to class " | ||
| 411 | . $domain_class->name; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | sub _parse_classes { | ||
| 416 | my ($package,$rdf) = @_; | ||
| 417 | |||
| 418 | # create Thing | ||
| 419 | _create_class('Class::OWL',RDF::Helper->new_resource('http://www.w3.org/2002/07/owl#Thing')); | ||
| 420 | |||
| 421 | # create All other classes | ||
| 422 | _for_type( | ||
| 423 | $rdf, | ||
| 424 | 'http://www.w3.org/2002/07/owl#Class', | ||
| 425 | sub { | ||
| 426 | my $resource = shift; | ||
| 427 | _create_class($package, $resource); | ||
| 428 | } | ||
| 429 | ); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _package { | ||
| 433 | my ($name,$package) = @_; | ||
| 434 | return $name ? $package . "::" . $name : $name; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub _class_name { | ||
| 438 | my ($resource,$package) = @_; | ||
| 439 | my $name = _get_name(_r_name($resource)); | ||
| 440 | return _package($name,$package); | ||
| 441 | } | ||
| 442 | |||
| 443 | sub _create_class { | ||
| 444 | my ( $package, $resource, $name) = @_; | ||
| 445 | $name = _class_name($resource,$package) unless $name; | ||
| 446 | |||
| 447 | |||
| 448 | my $class; | ||
| 449 | |||
| 450 | if ($name) { | ||
| 451 | $class = Class::OWL::MOP->create($name); | ||
| 452 | $class->name(); | ||
| 453 | } | ||
| 454 | else { | ||
| 455 | $class = Class::OWL::MOP->create_anon_class; | ||
| 456 | $name = $class->name; | ||
| 457 | } | ||
| 458 | |||
| 459 | debug "Create class from "._r_name($resource)." as $name"; | ||
| 460 | |||
| 461 | for my $attr ( _create_attribute( '_type' => $resource )) | ||
| 462 | { | ||
| 463 | $class->meta->add_attribute($attr); | ||
| 464 | } | ||
| 465 | |||
| 466 | for my $attr (_create_attribute( '_resource'), _create_attribute('_type'), _create_attribute('_model')) | ||
| 467 | { | ||
| 468 | $class->add_attribute($attr); | ||
| 469 | } | ||
| 470 | |||
| 471 | $class->add_method('new_model' => sub { | ||
| 472 | Class::OWL->new_model(); | ||
| 473 | }); | ||
| 474 | |||
| 475 | $class->add_method('new' => sub { | ||
| 476 | my $class = shift; | ||
| 477 | my ($rdf,$uri); | ||
| 478 | |||
| 479 | $uri = shift unless ref $_[0]; | ||
| 480 | $rdf = shift if ref $_[0] && $_[0]->isa("RDF::Helper"); | ||
| 481 | $uri = shift unless $uri; | ||
| 482 | |||
| 483 | $rdf = Class::OWL->new_model() unless $rdf; | ||
| 484 | $uri = $rdf->new_bnode() unless $uri; | ||
| 485 | |||
| 486 | my %args = @_; | ||
| 487 | |||
| 488 | my $o = $class->meta->new_instance($rdf,$uri); | ||
| 489 | foreach my $attr (keys %args) { | ||
| 490 | $o->meta->accessor($o,$attr,$args{$attr}) | ||
| 491 | } | ||
| 492 | $o; | ||
| 493 | }); | ||
| 494 | |||
| 495 | $class->add_method('_rdf' => sub { | ||
| 496 | Class::OWL->to_rdf($_[0]); | ||
| 497 | }); | ||
| 498 | |||
| 499 | $class->_type($resource); | ||
| 500 | Class::OWL->owl_class($resource,$class); | ||
| 501 | |||
| 502 | return $name, $class; | ||
| 503 | } | ||
| 504 | |||
| 505 | sub _create_attribute { | ||
| 506 | my ( $name, $value ) = @_; | ||
| 507 | if ( ref $value ) { | ||
| 508 | $value = sub { $value }; | ||
| 509 | } | ||
| 510 | my %config = ( | ||
| 511 | default => $value, | ||
| 512 | accessor => $name, | ||
| 513 | ); | ||
| 514 | return Class::MOP::Attribute->new( '$' . $name => %config ); | ||
| 515 | } | ||
| 516 | |||
| 517 | sub _unwrap_list { | ||
| 518 | my ($rdf,$lst,$i) = @_; | ||
| 519 | |||
| 520 | foreach my $s ($rdf->get_statements($i,'http://www.w3.org/1999/02/22-rdf-syntax-ns#first')) | ||
| 521 | { | ||
| 522 | push(@{$lst},$s->[2]); | ||
| 523 | } | ||
| 524 | |||
| 525 | foreach my $s ($rdf->get_statements($i,'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest')) | ||
| 526 | { | ||
| 527 | _unwrap_list($rdf,$lst,$s->[2]); | ||
| 528 | } | ||
| 529 | |||
| 530 | return $lst; | ||
| 531 | } | ||
| 532 | |||
| 533 | sub _parse_inheritance { | ||
| 534 | my ($package,$rdf) = @_; | ||
| 535 | for my $c ( values %class ) { | ||
| 536 | |||
| 537 | _for_statements( $rdf, $c->_type, 'rdfs:subClassOf', sub { | ||
| 538 | my ( $rdf, $instance ) = @_; | ||
| 539 | return | ||
| 540 | unless $rdf->exists( $instance, 'rdf:type', 'owl:Restriction' ) | ||
| 541 | || $rdf->exists( $instance, 'rdf:type', 'owl:Class' ); | ||
| 542 | |||
| 543 | my $is_restriction = $rdf->exists( $instance, 'rdf:type', 'owl:Restriction' ); | ||
| 544 | |||
| 545 | my $class_data = $rdf->property_hash($instance); | ||
| 546 | |||
| 547 | my $class = Class::OWL->owl_class($instance); | ||
| 548 | unless ( $class ) { | ||
| 549 | my $name = undef; | ||
| 550 | if ($is_restriction) | ||
| 551 | { | ||
| 552 | my $property = $class_data->{'owl:onProperty'}; | ||
| 553 | my $attr = Class::OWL->owl_property($property); | ||
| 554 | $name = "RestrictionOn".ucfirst(substr($attr->name,1))."::"._r_name($instance); | ||
| 555 | } | ||
| 556 | my ( $actual_name, $aclass ) = _create_class($package,$instance,_package($name,$package)); | ||
| 557 | } | ||
| 558 | $class = Class::OWL->owl_class($instance); | ||
| 559 | debug $class->name . " is a superclass of " . $c->name; | ||
| 560 | |||
| 561 | if ($is_restriction) { | ||
| 562 | # move the attribute from the base domain, restrict it and add it to the restriction | ||
| 563 | my $property = $class_data->{'owl:onProperty'}; | ||
| 564 | my $attr = Class::OWL->owl_property($property); | ||
| 565 | debug Dumper($class_data); | ||
| 566 | $c->remove_attribute($attr->name) if $c->has_attribute($attr->name); | ||
| 567 | $attr->restrict($class_data,$rdf); | ||
| 568 | $class->add_attribute($attr); | ||
| 569 | } | ||
| 570 | |||
| 571 | $c->superclasses( $c->superclasses(), $class->name ); | ||
| 572 | }); | ||
| 573 | |||
| 574 | _for_statements( $rdf, $c->_type, 'owl:unionOf', sub { | ||
| 575 | my ($rdf, $instance ) = @_; | ||
| 576 | |||
| 577 | my $classes = _unwrap_list($rdf,[],$instance); | ||
| 578 | foreach my $resource (@{$classes}) | ||
| 579 | { | ||
| 580 | my $class_data = $rdf->property_hash($resource); | ||
| 581 | my $class = Class::OWL->owl_class($resource); | ||
| 582 | unless ( $class ) { | ||
| 583 | my ( $actual_name, $aclass ) = _create_class($package,$resource); | ||
| 584 | Class::OWL->owl_class($resource,$aclass); | ||
| 585 | } | ||
| 586 | debug $class->name . " is part of the union that is " . $c->name; | ||
| 587 | $class->superclasses($class->superclasses(),$c->name); | ||
| 588 | } | ||
| 589 | }); | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | # || $rdf->exists( $instance, 'rdf:type', 'owl:unionOf' ) | ||
| 594 | # || $rdf->exists( $instance, 'rdf:type', 'owl:allValuesFrom') | ||
| 595 | |||
| 596 | 1; # Magic true value required at end of module | ||
| 597 | __END__ | ||