lib/RDF/Service/Context.pm untested

filter: Statementstmt 0.0% 0 / 693 Branchbran 0.0% 0 / 302 Conditioncond 0.0% 0 / 158 MC/DCmcdc 0.0% 0 / 99 Subroutinesub 0.0% 0 / 58 Podpod 0.0% 0 / 50 totaltotal 0.0% 0 / 1360 CC 152 SCAR 44.9
CC
152
Cov
0%
declare_add_types
380.0
declare_arc
210.0
set_props
182.0
CRAP 89.2
1 # $Id: Context.pm,v 1.18 2000/12/21 22:04:18 aigan Exp $ -*-perl-*-
2  
3 package RDF::Service::Context;
4  
5 #=====================================================================
6 #
7 # DESCRIPTION
8 # All resources exists in a context. This is the context.
9 #
10 # AUTHOR
11 # Jonas Liljegren <jonas@paranormal.se>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2000 Jonas Liljegren. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #=====================================================================
20  
21 use strict;
22 use vars qw( $AUTOLOAD );
23 use RDF::Service::Dispatcher qw( go );
24 use RDF::Service::Constants qw( :all );
25 use RDF::Service::Cache qw( interfaces uri2id list_prefixes
26 get_unique_id id2uri debug
27 debug_start debug_end
28 $DEBUG expire time_string $Level
29 validate_context );
30 use Data::Dumper;
31 use Carp qw( confess cluck croak);
32  
33 sub new
34 {
35 my( $proto, $node, $context, $wmodel ) = @_;
36  
37 # This constructor shouls only be called from get_node, which
38 # could be called from find_node or create_node.
39  
40 my $class = ref($proto) || $proto;
41 my $self = bless [], $class;
42  
43 my $history = [];
44  
45 if( ref($proto) )
46 {
47 $context ||= $proto->[CONTEXT];
48 $node ||= $proto->[NODE]; # The same node in another context?
49 $wmodel ||= $proto->[WMODEL];
50  
51 $history = $proto->[HISTORY];
52 }
53  
54 unless( ref($history) eq 'ARRAY' )
55 {
56 confess "HOLY SHIT!!!";
57 }
58  
59 # TODO: Maby perform a deep copy of the context. At least copy
60 # each key-value pair.
61  
62 $self->[NODE] = $node or die;
63 $self->[CONTEXT] = $context or die "No context supplied";
64 $self->[WMODEL] = $wmodel
65 or confess "No WMODEL supplied by $proto\n" if $node->[RUNLEVEL];
66 $self->[MEMORY] = {};
67 $self->[HISTORY] = $history;
68 return $self;
69 }
70  
71  
72 sub AUTOLOAD
73 {
74 # The substr depends on the package length
75 #
76 $AUTOLOAD = substr($AUTOLOAD, 23);
77 return if $AUTOLOAD eq 'DESTROY';
78 debug "AUTOLOAD $AUTOLOAD\n", 2;
79  
80 &RDF::Service::Dispatcher::go(shift, $AUTOLOAD, @_);
81 }
82  
83  
84 sub name
85 {
86 my( $self ) = @_;
87 return $self->[NODE][NAME]; # not guaranteed to be defined
88 }
89  
90 sub uri
91 {
92 # This is always defined
93 $_[0]->[NODE][URISTR];
94 }
95  
96 sub model
97 {
98 my( $self ) = @_;
99 die "not implemented" if $_[0]->[NODE][MULTI];
100  
101 my $model_res = $_[0]->new( $_[0]->[NODE][MODEL] );
102  
103 if( $DEBUG )
104 {
105 unless( $model_res->is_a( NS_LS.'#Model' ) )
106 {
107 die "The model is not a model";
108 }
109 }
110  
111 return $model_res;
112  
113 # TODO: Should return a selection of models
114 }
115  
116  
117 sub get
118 {
119 $_[1] ||= NS_LD."#".&get_unique_id;
120 return get_context_by_id( $_[0], uri2id($_[1]), $_[2] );
121 }
122  
123 sub get_context_by_id
124 {
125 my( $self, $id , $ids) = @_;
126  
127 # TODO: First look for the object in the cache
128  
129 my $node = $self->[NODE];
130 $ids ||= $node->[IDS];
131 if( $DEBUG )
132 {
133 confess "IDS undefined" unless defined $ids;
134 unless( ref $RDF::Service::Cache::node->{$ids} )
135 {
136 $node->[RUNLEVEL] and
137 confess "IDS $ids not initialized\n";
138 }
139  
140 confess "id not defined" unless $id;
141 }
142  
143 my $obj = $RDF::Service::Cache::node->{$ids}{ $id };
144  
145 unless( $obj )
146 {
147 # Create an uninitialized object. Any request for the objects
148 # properties will initialize the object with the interfaces.
149  
150 $obj = $node->new_by_id($id);
151  
152 $RDF::Service::Cache::node->{$ids}{ $id } = $obj;
153 }
154  
155  
156 if( $DEBUG )
157 {
158 unless( $self->[WMODEL] or
159 $obj->[URISTR] eq NS_LD.'#The_Base_Model' )
160 {
161 confess "No WMODEL found for $node->[URISTR] ";
162 }
163  
164 unless( ref $obj eq "RDF::Service::Resource" )
165 {
166 my $uri = id2uri( $id );
167 confess "Cached $uri ($id) corrupt";
168 }
169 }
170  
171 return $self->new( $obj );
172 }
173  
174  
175 sub get_node
176 {
177 $_[1] ||= NS_LD."#".&get_unique_id;
178 return get_node_by_id( $_[0], uri2id($_[1]), $_[2] );
179 }
180  
181 sub get_node_by_id
182 {
183 my( $self, $id, $ids ) = @_;
184  
185 $ids ||= $self->[NODE][IDS];
186 my $obj = $RDF::Service::Cache::node->{$ids}{ $id };
187  
188 unless( $obj )
189 {
190 # Create an uninitialized object. Any request for the objects
191 # properties will initialize the object with the interfaces.
192  
193 $obj = $self->[NODE]->new(undef, $id, $ids);
194  
195 $RDF::Service::Cache::node->{$ids}{ $id } = $obj;
196 }
197  
198 if( $DEBUG )
199 {
200 unless( $self->[WMODEL] or
201 $obj->[URISTR] eq NS_LD.'#The_Base_Model' )
202 {
203 confess "No WMODEL found for $self->[NODE][URISTR] ";
204 }
205  
206 unless( ref $obj eq "RDF::Service::Resource" )
207 {
208 my $uri = id2uri( $id );
209 confess "Cached $uri ($id) corrupt";
210 }
211 }
212  
213 return $obj;
214 }
215  
216  
217 sub get_model
218 {
219 my( $self, $uri ) = @_;
220  
221 debug_start("get_model", ' ', $self);
222  
223 die "No uri specified" unless $uri;
224 debug " ( $uri )\n", 2;
225  
226 my $obj = $self->find_node( $uri );
227 if( $obj )
228 {
229 debug "Model existing: $uri\n", 1;
230 # Is this a model?
231 unless( $obj->is_a(NS_LS.'#Model') )
232 {
233 die "$obj->[NODE][URISTR] is not a model\n".
234 $obj->types_as_string;
235 }
236 # setting WMODEL
237 $obj->[WMODEL] = $obj;
238 }
239 else
240 {
241 debug "Model not existing. Creating it: $uri\n", 1;
242 # create_model sets WMODEL
243 $obj = $self->create_model( $uri );
244 }
245  
246 debug_end("get_model", ' ', $self);
247 return $obj;
248 }
249  
250 sub is_a
251 {
252 my( $self, $class ) = @_;
253  
254 $self->[NODE][TYPE_ALL] or $self->init_types;
255 $self->[NODE][TYPE_ALL] ||= 1;
256 return $self->is_known_as_a( $class );
257 }
258  
259 sub could_be_a
260 {
261 my( $self, $class ) = @_;
262  
263 return 1 unless $self->[NODE][TYPE_ALL];
264 return $self->is_known_as_a( $class );
265 }
266  
267 sub is_known_as_a
268 {
269 my( $self, $class ) = @_;
270  
271 $class = $self->get( $class ) unless ref $class;
272  
273 if( defined $self->[NODE][TYPE]{$class->[NODE][ID]} )
274 {
275 return 1;
276 }
277 else
278 {
279 return 0;
280 }
281 }
282  
283 sub exist_pred
284 {
285 my( $self, $pred ) = @_;
286  
287 my $pred_id;
288 if( ref $pred )
289 {
290 $pred_id = $pred->[NODE][ID];
291 }
292 else
293 {
294 $pred_id = uri2id( $pred );
295 }
296  
297 if( $self->[NODE][REV_SUBJ]{$pred_id} )
298 {
299 return 1;
300 }
301 else
302 {
303 return 0;
304 }
305 }
306  
307 sub type_orderd_list
308 {
309 my( $self, $i, $point ) = @_;
310  
311 # TODO: This should (as all the other methods) be cached and
312 # dpendencies registred.
313  
314 die "Not implemented" if $point;
315 my $node = $self->[NODE];
316  
317  
318 # We can't call level() for the resources used to define level()
319 #
320 if( $node->[URISTR] =~ /^(@{[NS_RDF]}|@{[NS_RDFS]}|@{[NS_LS]})/o )
321 {
322 my $type_uri_ref = $Schema->{$self->[NODE][URISTR]}{NS_RDF.'type'};
323  
324 return( [$self->get( $$type_uri_ref ),
325 $self->get(NS_RDFS.'Resource')] );
326 }
327  
328 debug_start("type_orderd_list", ' ', $self);
329  
330  
331 # Do we have to have all types to list the *present* defined types?
332 # $node->[TYPE_ALL] or $self->init_types;
333  
334 my @types = ();
335 my %included; # Keep track of included types
336 foreach my $type ( sort { $b->level <=> $a->level }
337 map $self->get_context_by_id($_),
338 keys %{$node->[TYPE]}
339 )
340 {
341 # Check that at least one model defines the type. Can we
342 # assume that the existence of the type (in the hash tree)
343 # implies the existence of at least one model (in the hash
344 # treee) and that the existence of a model implies that that
345 # model has the value 1, meaning that the model states the
346 # type? Yes. Assume that.
347  
348 push @types, $type unless $included{$type->[NODE][ID]};
349 $included{$type->[NODE][ID]}++;
350 }
351  
352 debug_end("type_orderd_list", ' ', $self);
353 return( \@types );
354 }
355  
356  
357  
358 # The alternative selectors:
359 #
360 # arc subj arcs
361 # arc_obj subj arcs objs
362 # arc_obj_list subj arcs objs list
363 # select_arc container subj arcs
364 # select_arc_obj container subj arcs objs
365 # type subj types
366 # select_type container subj types
367 # rev_arc obj arcs
368 # arc_subj obj arcs subjs
369 # select_rev_arc container obj arcs
370 # select_arc_subj container obj arcs subjs
371 # rev_type type objs
372 # select_rev_type container types objs
373 # li container res
374 # rev_li res container
375 # select container res
376 # rev_select res container
377  
378 sub type
379 {
380 my( $self, $point ) = @_;
381  
382 die "Not implemented" if $point;
383  
384 debug_start("type", ' ', $self);
385  
386 $self->[NODE][TYPE_ALL] or $self->init_types;
387  
388 # TODO: Insert the query in the selection, rather than the query
389 # result
390  
391 my $selection = $self->declare_selection( $self->type_orderd_list );
392  
393 debug_end("rev_type", ' ', $self);
394 return( $selection );
395 }
396  
397  
398 sub rev_type
399 {
400 my( $self, $point ) = @_;
401  
402 die "Not implemented" if $point;
403  
404 debug_start("rev_type", ' ', $self);
405  
406 $self->[NODE][REV_TYPE_ALL] or $self->init_rev_types;
407  
408 # TODO: Insert the query in the selection, rather than the query
409 # result
410  
411 my %subjs = ();
412 foreach my $subj_id ( keys %{$self->[NODE][REV_TYPE]} )
413 {
414 # This includes types from all models
415 foreach my $model_id ( keys %{$self->[NODE][REV_TYPE]{$subj_id}})
416 {
417 if( $self->[NODE][REV_TYPE]{$subj_id}{$model_id} )
418 {
419 $subjs{$subj_id} = $self->get_context_by_id( $subj_id );
420 }
421 }
422 }
423  
424 my $selection = $self->declare_selection( [values %subjs] );
425  
426 debug_end("rev_type", ' ', $self);
427 return( $selection );
428 }
429  
430  
431 sub arc
432 {
433 my( $self, $point ) = @_;
434  
435 debug_start( "arc", ' ', $self );
436  
437 unless( ref $point )
438 {
439 unless( defined $point )
440 {
441 # TODO: allow partially initialized props
442  
443 $self->[NODE][REV_SUBJ_ALL] or $self->init_rev_subjs;
444  
445 # TODO: Insert the query in the selection, rather than the
446 # query result
447 #
448 my $arcs = [];
449 foreach my $pred_id ( keys %{$self->[NODE][REV_SUBJ]} )
450 {
451 foreach my $arc_node ( @{$self->[NODE][REV_SUBJ]{$pred_id}} )
452 {
453 push @$arcs, $self->new($arc_node);
454 }
455 }
456 my $selection = $self->declare_selection( $arcs );
457  
458 debug_end("arc", ' ', $self);
459 return $selection;
460 }
461 $point = $self->get( $point );
462 }
463  
464 # Take action depending on $point
465 #
466 if( ref $point eq 'RDF::Service::Context' ) # property
467 {
468 unless( $self->[NODE][REV_SUBJ]{$point->[NODE][ID]} or
469 $self->[NODE][REV_SUBJ_ALL] )
470 {
471 $self->init_rev_subjs( $point );
472 }
473  
474  
475 # TODO: Insert the query in the selection, rather than the
476 # query result
477 #
478 my $arcs = [];
479 foreach my $arc_node (
480 @{$self->[NODE][REV_SUBJ]{$point->[NODE][ID]}}
481 )
482 {
483 push @$arcs, $self->new( $arc_node );
484 }
485 my $selection = $self->declare_selection( $arcs );
486  
487 debug_end("arc", ' ', $self);
488 return $selection;
489 }
490 else
491 {
492 die "not implemented";
493 }
494 }
495  
496 sub arc_subj
497 {
498 my( $self, $point ) = @_;
499  
500 # Default $point to be a property resource
501 #
502 unless( ref $point )
503 {
504 unless( defined $point )
505 {
506 die "Not implemented";
507 }
508 $point = $self->get( $point );
509 }
510  
511 debug_start( "arc_subj", ' ', $self );
512 debug " ( $point->[NODE][URISTR] )\n", 1;
513  
514 # Take action depending on $point
515 #
516 if( ref $point eq 'RDF::Service::Context' ) # property
517 {
518 $self->init_rev_objs( $point ) unless defined
519 $self->[NODE][REV_OBJ]{$point->[NODE][ID]};
520  
521 # TODO: Insert the query in the selection, rather than the
522 # query result
523 #
524 my $subjs = [];
525 foreach my $arc_node (
526 @{$self->[NODE][REV_OBJ]{$point->[NODE][ID]}}
527 )
528 {
529 push @$subjs, $self->new( $arc_node )->subj;
530 }
531 my $selection = $self->declare_selection( $subjs );
532  
533 debug_end("arc_subj", ' ', $self);
534 return $selection;
535 }
536 else
537 {
538 die "not implemented";
539 }
540 die "What???";
541 }
542  
543 sub arc_pred
544 {
545 my( $self, $point ) = @_;
546  
547 debug_start( "arc_pred", ' ', $self );
548  
549 if( not defined $point )
550 {
551 $self->[NODE][REV_SUBJ_ALL] or $self->init_rev_subjs;
552  
553 # TODO: Insert the query in the selection, rather than the
554 # query result
555 #
556 my $preds = [];
557 foreach my $pred_id ( keys %{$self->[NODE][REV_SUBJ]} )
558 {
559 push @$preds, $self->get_context_by_id($pred_id);
560 }
561 my $selection = $self->declare_selection( $preds );
562  
563 debug_end("arc_pred", ' ', $self);
564 return $selection;
565 }
566 else
567 {
568 die "not implemented";
569 }
570 die "What???";
571 }
572  
573 sub arc_obj
574 {
575 my( $self, $point ) = @_;
576  
577 # Default $point to be a property resource
578 #
579 unless( ref $point )
580 {
581 unless( defined $point )
582 {
583 warn "*** Failed\n";
584 warn "*** Called $self->[NODE][URISTR] with ( $point )\n";
585 croak "arc_obj ( $self->[NODE][URISTR] ) without point not implemented";
586 }
587 $point = $self->get( $point );
588 }
589  
590 debug_start( "arc_obj", ' ', $self );
591 debug " ( $point->[NODE][URISTR] )\n", 1;
592  
593 # Take action depending on $point
594 #
595 if( ref $point eq 'RDF::Service::Context' ) # property
596 {
597 unless( $self->[NODE][REV_SUBJ]{$point->[NODE][ID]} or
598 $self->[NODE][REV_SUBJ_ALL] )
599 {
600 $self->init_rev_subjs( $point );
601 }
602  
603 unless( defined $self->[NODE][REV_SUBJ]{$point->[NODE][ID]} )
604 {
605 debug_end("arc_obj", ' ', $self);
606 return $self->declare_selection( [] );
607 }
608  
609 # TODO: Insert the query in the selection, rather than the
610 # query result
611 #
612 my $objs = [];
613 foreach my $arc_node (
614 @{$self->[NODE][REV_SUBJ]{$point->[NODE][ID]}}
615 )
616 {
617 push @$objs, $self->new( $arc_node )->obj;
618 }
619 my $selection = $self->declare_selection( $objs );
620  
621 debug_end("arc_obj", ' ', $self);
622 return $selection;
623 }
624 else
625 {
626 die "not implemented";
627 }
628 die "What???";
629 }
630  
631 sub arc_obj_list
632 {
633 my( $self, $point ) = @_;
634  
635 # Default $point to be a property resource
636 #
637 unless( ref $point )
638 {
639 unless( defined $point )
640 {
641 die "Not implemented";
642 }
643 $point = $self->get( $point );
644 }
645  
646 debug_start( "arc_obj_list", ' ', $self );
647 debug " ( $point->[NODE][URISTR] )\n", 1;
648  
649 # Take action depending on $point
650 #
651 if( ref $point eq 'RDF::Service::Context' ) # property
652 {
653 unless( $self->[NODE][REV_SUBJ]{$point->[NODE][ID]} or
654 $self->[NODE][REV_SUBJ_ALL] )
655 {
656 $self->init_rev_subjs( $point );
657 }
658  
659 unless( defined $self->[NODE][REV_SUBJ]{$point->[NODE][ID]} )
660 {
661 debug_end("arc_obj_list", ' ', $self);
662 return [];
663 }
664  
665 # TODO: Insert the query in the selection, rather than the
666 # query result
667 #
668 my $objs = [];
669 foreach my $arc_node (
670 @{$self->[NODE][REV_SUBJ]{$point->[NODE][ID]}}
671 )
672 {
673 push @$objs, $self->new( $arc_node )->obj;
674 }
675  
676 debug_end("arc_obj_list", ' ', $self);
677 return $objs;
678 }
679 else
680 {
681 die "not implemented";
682 }
683 die "What???";
684 }
685  
686 sub selector
687 {
688 die "not imlemented";
689  
690 my $point;
691 if( not defined $point ) # Return all arcs
692 {
693 }
694 elsif( ref $point eq 'ARRAY' ) # Return ORed elements
695 {
696 }
697 elsif( ref $point eq 'HASH' ) # Return ANDed elements
698 {
699 }
700 elsif( ref $point eq 'RDF::Service::Context' )
701 {
702 }
703 else
704 {
705 die "Malformed entry";
706 }
707 }
708  
709  
710 sub set
711 {
712 my( $self, $types, $props ) = @_;
713  
714 # This is practicaly the same as declare_self. set() updates the
715 # data in the interfaces.
716  
717 debug_start("set", ' ', $self);
718  
719 # Should each type and property only be saved in the first best
720 # interface and not saved in the following interfaces? Yes!
721 #
722 # The types and props taken by one interface must be marked so
723 # that the next interface doesn't handle them. This could be done
724 # by modifying the arguments $types and $props to exclude those
725 # that has been taken care of.
726  
727 $self->set_types( $types, 1);
728 $self->set_props( $props, 1);
729  
730 debug_end("set", ' ', $self);
731 return $self;
732 }
733  
734 sub set_types
735 {
736 my( $self, $types, $trim, $local_changes ) = @_;
737 #
738 # Remove existing types not mentioned if $trim
739  
740 debug_start("set_types", ' ', $self);
741  
742 my $node = $self->[NODE];
743 my $model = $self->[WMODEL][NODE];
744  
745 if( $DEBUG )
746 {
747 ref $model eq "RDF::Service::Resource" or
748 confess "Bad model ($model)";
749  
750 }
751  
752 $node->[TYPE_ALL] or $self->init_types;
753 my $type_all = $node->[TYPE_ALL]; # Remember the state
754 $node->[TYPE_ALL] = 0;
755  
756  
757 my @add_types;
758 my %del_types;
759 foreach my $type ( @{$self->type_orderd_list} )
760 {
761 if( $node->[TYPE]{$type->[NODE][ID]}{$model->[ID]} )
762 {
763 $del_types{$type->[NODE][ID]} = $type;
764 }
765 }
766  
767 foreach my $type ( @$types )
768 {
769 $type = $self->get( $type ) unless ref $type;
770 if ( $del_types{ $type->[NODE][ID] } )
771 {
772 delete $del_types{ $type->[NODE][ID] };
773 }
774 else
775 {
776 push @add_types, $type;
777 }
778 }
779  
780 if ( @add_types )
781 {
782 # Will only add each type in one interface
783 $self->declare_add_types( [@add_types] );
784 unless( $local_changes )
785 {
786 $self->store_types;
787 }
788 }
789  
790 if ( $trim and %del_types )
791 {
792 # Will delete types from all interfaces
793 $self->declare_del_types( [values %del_types] );
794 $self->remove_types( [values %del_types] ) unless $local_changes;
795 }
796  
797 $self->[NODE][TYPE_ALL] = $type_all;
798  
799 debug_end("set_types", ' ', $self);
800 return $self;
801 }
802  
803 sub set_props
804 {
805 my( $self, $props, $trim, $local_changes ) = @_;
806 #
807 # Remove existing props not mentioned if $trim. Add props not yet
808 # existing. Only operate within WMODEL.
809 #
810 # $props->{$pred_uri => [ $obj ] }
811  
812 debug_start("set_props", ' ', $self);
813  
814 my $node = $self->[NODE];
815 my $model = $self->[WMODEL][NODE];
816  
817  
818 $node->[REV_SUBJ_ALL] or $self->init_rev_subjs;
819  
820 my %add_props; # $add_props->{$pred_id}[$obj]
821 my %del_props; # $del_props->{$pred_id}{$obj_id => $arc}
822  
823 # This will hold present properties in the model
824 # that does not exist in the new set of
825 # properties. Start by adding all present properties and remove
826 # the ones that exist in the new property list.
827  
828 foreach my $arc ( @{$self->arc->list} )
829 {
830 # TODO: Verify that 'eq' suffice for equality test. NOT GOOD!!
831 if( $arc->[NODE][MODEL][ID] == $model->[ID] )
832 {
833 $del_props{$arc->[NODE][PRED][ID]}{$arc->[NODE][OBJ][ID]} = $arc;
834 }
835 }
836  
837 # Foreach pred and obj
838 foreach my $pred_uri ( keys %$props )
839 {
840 foreach my $obj ( @{ $props->{$pred_uri} } )
841 {
842 if( $DEBUG )
843 {
844 if( ref $obj and $obj->[NODE][VALUE] )
845 {
846 unless( ref($obj->[NODE][VALUE]) eq 'SCALAR')
847 {
848 confess "Bad value for $obj->[NODE][URISTR] ( ".
849 ref($obj->[NODE][VALUE])." ne 'SCALAR' )";
850 }
851 }
852 }
853  
854 # Is the object a literal?
855 if( not ref $obj )
856 {
857 debug " Creating literal '$obj'\n", 1;
858  
859 # Warning. Previouslu wrote this as \$obj. It took me
860 # over a day to find this and realize that the
861 # reasigning of $obj would overwrite the created
862 # literal value, creating a self-reference
863 #
864 $obj = $self->create_literal(undef, \ "$obj");
865  
866 }
867  
868  
869 # Does this resource already have the arc?
870 my $pred_id = &uri2id($pred_uri);
871 if( $del_props{$pred_id}{$obj->[NODE][ID]} )
872 {
873 delete $del_props{$pred_id}{$obj->[NODE][ID]};
874 }
875 else
876 {
877 push @{$add_props{$pred_id}}, $obj;
878 }
879 }
880 }
881  
882 if( %del_props )
883 {
884 # Will delete props from all interfaces
885 foreach my $pred_id ( keys %del_props )
886 {
887 if( $DEBUG )
888 {
889 my $pred_uri = &id2uri($pred_id);
890 debug "Del checking $pred_uri\n", 1;
891 }
892  
893 # Remove the pred objs if other pred objs was specified,
894 # or if $trim is set, in case we remove everything not
895 # specified
896 #
897 next unless $trim or $add_props{$pred_id};
898  
899 foreach my $obj_id ( keys %{ $del_props{$pred_id} } )
900 {
901 die "not implemented" if $local_changes;
902 $del_props{$pred_id}{$obj_id}->delete_node();
903 }
904 }
905 }
906  
907 if( %add_props )
908 {
909 # Will only add each prop in one interface
910 foreach my $pred_id ( keys %add_props )
911 {
912 foreach my $obj ( @{ $add_props{$pred_id} } )
913 {
914 $self->declare_add_prop( &id2uri($pred_id), $obj );
915 }
916 }
917 unless( $local_changes or ($self->[NODE][REV_SUBJ_ALL] and
918 $self->[NODE][REV_SUBJ_ALL] == 2) )
919 {
920 $self->store_props;
921 }
922 }
923  
924 debug_end("set_props", ' ', $self);
925 return $self;
926 }
927  
928 sub create_literal
929 {
930 my( $self, $uristr, $lit_str_ref ) = @_;
931  
932 debug_start("create_literal", ' ', $self);
933 debug " Creating ($$lit_str_ref)\n", 1;
934  
935 if( $uristr )
936 {
937 # NOTE: Copied from create_model
938  
939 # This should validate the uri. If this interface can't
940 # create the URI, it will either return "try next interface"
941 # or "failed", depending on why.
942  
943 # For now: Just allow models in the local namespace
944 unless( $uristr =~ /@{[NS_LD]}/o )
945 {
946 die "Invalid namespace for literal";
947 }
948 }
949  
950 my $literal = $self->declare_literal( $lit_str_ref, $uristr );
951 $literal->store_node;
952  
953 if( $DEBUG )
954 {
955 if( $literal->[NODE][VALUE] )
956 {
957 debug "Checking literal value\n";
958 unless( ref($literal->[NODE][VALUE]) eq 'SCALAR')
959 {
960 confess "Bad value for $literal->[NODE][URISTR] ( ".
961 ref($literal->[NODE][VALUE])." ne 'SCALAR' )";
962 }
963 else
964 {
965 # warn Dumper $literal->[NODE][VALUE];
966 }
967 }
968 }
969  
970  
971 debug_end("create_literal", ' ', $self);
972  
973 # Return the literal object
974 #
975 return( $literal );
976 }
977  
978 sub set_literal
979 {
980 my( $self, $lit_str_ref ) = @_;
981  
982 debug_start("set_literal", ' ', $self);
983 debug " Change to ($$lit_str_ref)\n", 1;
984  
985 # TODO: make sure you have the right to update this literal!
986  
987 $self->declare_literal( $lit_str_ref, $self, );
988 $self->store_node;
989  
990 debug_end("set_literal", ' ', $self);
991 }
992  
993  
994 sub types_as_string
995 {
996 my( $self ) = @_;
997 #
998 # die $self->uri."--::".Dumper($self->[TYPES]);
999 my $result = "";
1000 my $type_ref = $self->[NODE][TYPE];
1001 foreach my $type_id ( sort keys %{$type_ref} )
1002 {
1003 my $type_uristr = id2uri( $type_id );
1004 $result .= "t $type_uristr\n";
1005 foreach my $model_id ( sort keys
1006 %{$type_ref->{$type_id}} )
1007 {
1008 my $model_uristr = id2uri( $model_id );
1009 my $solid = $type_ref->{$type_id}{$model_id} - 1;
1010 $result .= " m $model_uristr SOLID $solid\n";
1011 }
1012  
1013 }
1014  
1015 return $result;
1016 }
1017  
1018  
1019 sub to_string
1020 {
1021 my( $self ) = @_;
1022  
1023 # Old!
1024  
1025 my $str = "";
1026 no strict 'refs';
1027  
1028 $str.="TYPES\t: ". $self->types_as_string ."\n";
1029  
1030 foreach my $attrib (qw( IDS URISTR ID NAME LABEL VALUE FACT PREFIX MODULE_NAME ))
1031 {
1032 $self->[NODE][&{$attrib}] and $str.="$attrib\t:".
1033 $self->[NODE][&{$attrib}] ."\n";
1034 }
1035  
1036 foreach my $attrib (qw( NS MODEL ALIASFOR LANG PRED SUBJ OBJ ))
1037 {
1038 # my $dd = Data::Dumper->new([$self->[&{$attrib}]]);
1039 # $str.=Dumper($dd->Values)."\n\n\n";
1040 # $self->[&{$attrib}] and $str.="$attrib\t:".Dumper($self->[&{$attrib}])."\n";
1041 $self->[NODE][&{$attrib}] and $str.="$attrib\t:".
1042 ($self->[NODE][&{$attrib}][URISTR]||"no value")."\n";
1043 }
1044  
1045 return $str;
1046 }
1047  
1048 sub li
1049 {
1050 my( $self ) = @_;
1051  
1052 # TODO: Add support for criterions
1053  
1054 my $cnt = @{$self->[NODE][CONTENT]};
1055  
1056 if( $cnt == 1 )
1057 {
1058 return $self->[NODE][CONTENT][0];
1059 }
1060 else
1061 {
1062 die "Selection has $cnt resources, while expecting one\n";
1063 }
1064 }
1065  
1066 sub list
1067 {
1068 my( $self ) = @_;
1069  
1070 # TODO: Convert the contents to individual objects. Maby tie the
1071 # list to a list object for iteration through the list.
1072  
1073 if( $DEBUG )
1074 {
1075 my $cnt = @{$self->[NODE][CONTENT]};
1076 debug "Returning a list of $cnt resources\n", 1;
1077 }
1078  
1079 return $self->[NODE][CONTENT];
1080 }
1081  
1082  
1083 ########################################################
1084 #
1085 # Wrapper methods for the interfaces
1086 #
1087 # &RDF::Service::Dispatcher::go(shift, $AUTOLOAD, @_);
1088  
1089 # TODO: Should not mark that ALL data has been initialized if
1090 # init_types was called from a function in the process of adding new
1091 # types. This could be done with a memory of previous calls in the
1092 # CONTEXT.
1093  
1094 # NB! Expect all initiated data to be solid!
1095  
1096 # NB! Set ALL after a call to init, unless thare are additionall data
1097 # to be set.
1098  
1099 sub init_types
1100 {
1101 my( $self ) = shift;
1102 debug "GO init_types\n", 3;
1103 go($self, 'init_types', @_);
1104 $self->[NODE][TYPE_ALL] ||= 1;
1105 }
1106  
1107 sub init_rev_types
1108 {
1109 my( $self ) = shift;
1110 debug "GO init_rev_types\n", 3;
1111 go($self, 'init_rev_types', @_);
1112 $self->[NODE][REV_TYPE_ALL] ||= 1;
1113 }
1114  
1115 sub init_rev_subjs
1116 {
1117 my( $self ) = shift;
1118 debug "GO init_rev_subjs\n", 3;
1119 go($self, 'init_rev_subjs', @_);
1120 $self->[NODE][REV_SUBJ_ALL] ||= 1;
1121 }
1122  
1123 sub init_rev_objs
1124 {
1125 my( $self ) = shift;
1126 debug "GO init_rev_objs\n", 3;
1127 go($self, 'init_rev_objs', @_);
1128 $self->[NODE][REV_OBJ_ALL] ||= 1;
1129 }
1130  
1131  
1132 sub store_types
1133 {
1134 my( $self, @args ) = @_;
1135  
1136 if( $DEBUG )
1137 {
1138 warn "HISTORY --- @{$self->[HISTORY]}\n";
1139 }
1140  
1141  
1142 my $node = $self->[NODE];
1143 $node->[TYPE_ALL] or $self->init_types();
1144 debug "GO store_types\n", 3;
1145 if( go($self, 'store_types', @args) and $node->[TYPE_ALL] )
1146 {
1147 $node->[TYPE_ALL] = 2;
1148 }
1149 }
1150  
1151 sub store_props
1152 {
1153 my( $self, @args ) = @_;
1154 my $node = $self->[NODE];
1155 $node->[REV_SUBJ_ALL] or $self->init_rev_subjs();
1156 debug "GO store_props\n", 3;
1157 if( go($self, 'store_props', @args) and $node->[REV_SUBJ_ALL] )
1158 {
1159 $node->[REV_SUBJ_ALL] = 2;
1160 }
1161 }
1162  
1163 sub store
1164 {
1165 my( $self ) = @_;
1166  
1167 debug_start("store", ' ', $self);
1168  
1169 # TODO: Reset all other IDS caches (for this Resource)
1170  
1171  
1172 my $node = $self->[NODE];
1173 unless( $node->[SOLID] )
1174 {
1175 if( $DEBUG )
1176 {
1177 if( $node->[VALUE] )
1178 {
1179 debug( "Node NOT solid: ${$node->[VALUE]}\n" );
1180 }
1181 }
1182 $self->store_node;
1183 }
1184 else
1185 {
1186 if( $DEBUG )
1187 {
1188 if( $node->[VALUE] )
1189 {
1190 debug( "Node SOLID: ${$node->[VALUE]}\n" );
1191 }
1192 }
1193 }
1194  
1195 # Save types unless they are solid
1196 unless( defined $node->[TYPE_ALL] and $node->[TYPE_ALL] == 2 )
1197 {
1198 $self->store_types;
1199 }
1200  
1201 # Save props unless they are solid
1202 unless( defined $node->[REV_SUBJ_ALL] and $node->[REV_SUBJ_ALL] == 2 )
1203 {
1204 $self->store_props;
1205 }
1206  
1207  
1208 # Also store the model
1209 if( $node->[MODEL] )
1210 {
1211 debug " Is the model ($node->[URISTR]) solid?\n", 2;
1212 $self->get_context_by_id($node->[MODEL][ID])->store
1213 unless $node->[MODEL][SOLID];
1214 }
1215  
1216 # Only saves props. Not rev props.
1217  
1218 debug_end("store", ' ', $self);
1219  
1220 return( 1 );
1221 }
1222  
1223  
1224  
1225 ######################################################################
1226 #
1227 # Declaration methods should only be called from interfaces.
1228 #
1229  
1230 sub declare_del_types
1231 {
1232 my( $self, $types ) = @_;
1233  
1234 debug_start("declare_del_types", ' ', $self);
1235  
1236 my $node_type = $self->[NODE][TYPE];
1237 my $model_id = $self->[WMODEL][NODE][ID];
1238 my $id = $self->[NODE][ID];
1239  
1240 my @ids = ();
1241 if( defined $types )
1242 {
1243 @ids = map $_->[NODE][ID], @$types;
1244 }
1245 else
1246 {
1247 @ids = keys %$node_type;
1248 }
1249  
1250 foreach my $class_id ( @ids )
1251 {
1252 my $class_node = $self->get_node_by_id($class_id);
1253 debug " Checking $class_node->[URISTR]\n", 2;
1254 unless( delete $node_type->{$class_id}{$model_id} )
1255 {
1256 debug " Type defined in another model:\n", 2;
1257 foreach my $other_model_id ( keys %{$node_type->{$class_id}} )
1258 {
1259 my $other_model_node =
1260 $self->get_node_by_id($other_model_id);
1261 debug " $other_model_node->[URISTR]\n", 2;
1262 }
1263 next;
1264 }
1265  
1266 my $class_rev_type = $class_node->[REV_TYPE];
1267  
1268 debug " Removing rev_type node\n", 2;
1269 if( $DEBUG )
1270 {
1271 unless( $class_rev_type->{$id} )
1272 {
1273 unless( $class_node->[URISTR] eq NS_RDFS.'Resource' )
1274 {
1275 die " There was no rev_type to remove!!\n";
1276 }
1277 }
1278 }
1279  
1280 delete $class_rev_type->{$id}{$model_id};
1281  
1282 delete $node_type->{$class_id}
1283 unless keys %{$node_type->{$class_id}};
1284  
1285 delete $class_rev_type->{$id}
1286 unless keys %{$class_rev_type->{$id}};
1287 }
1288  
1289 debug_end("declare_del_types", ' ', $self);
1290 }
1291  
1292 sub declare_del_rev_types
1293 {
1294 my( $self, $res ) = @_;
1295  
1296 debug_start("declare_del_rev_types", ' ', $self);
1297  
1298 my $class_rev_type = $self->[NODE][REV_TYPE];
1299 my $model_id = $self->[WMODEL][NODE][ID];
1300 my $id = $self->[NODE][ID];
1301  
1302 my @ids = ();
1303 if( defined $res )
1304 {
1305 @ids = map $_->[NODE][ID], @$res;
1306 }
1307 else
1308 {
1309 @ids = keys %$class_rev_type;
1310 }
1311  
1312 foreach my $res_id ( @ids )
1313 {
1314 my $class_node = $self->get_node_by_id($res_id);
1315 debug " Checking $class_node->[URISTR]\n", 2;
1316 unless( delete $class_rev_type->{$res_id}{$model_id} )
1317 {
1318 next;
1319 }
1320  
1321 my $class_type = $class_node->[TYPE];
1322  
1323 debug " Removing type node\n", 2;
1324 if( $DEBUG )
1325 {
1326 unless( $class_type->{$id} )
1327 {
1328 die " There was no type to remove!!\n";
1329 }
1330 }
1331 delete $class_type->{$id}{$model_id};
1332  
1333 delete $class_rev_type->{$res_id}
1334 unless keys %{$class_rev_type->{$res_id}};
1335  
1336 delete $class_type->{$id}
1337 unless keys %{$class_type->{$id}};
1338 }
1339  
1340 debug_end("declare_del_rev_types", ' ', $self);
1341 }
1342  
1343 sub declare_literal
1344 {
1345 my( $self, $lit_str_ref, $lit, $types, $props, $model ) = @_;
1346 #
1347 # - $model is a resource object
1348 # - $lit (uri or node or undef)
1349 # - $lref will be a scalar ref
1350 # - $types is ref to array of type objects or undef
1351 # - $props is hash ref with remaining properties or undef
1352  
1353 # $types and $props is not done yet
1354  
1355 # $lit can be node or uristr
1356 #
1357 unless( ref $lit )
1358 {
1359 unless( defined $lit )
1360 {
1361 $lit = NS_LD."/literal/". &get_unique_id;
1362 }
1363 $lit = $self->get( $lit );
1364 }
1365  
1366 if( $DEBUG )
1367 {
1368 debug_start("declare_literal", ' ', $self );
1369 debug " ( $$lit_str_ref )\n", 1;
1370  
1371 ref $lit_str_ref eq 'SCALAR'
1372 or die "Value must be a scalar reference";
1373  
1374 if( $$lit_str_ref =~ /^RDF/ )
1375 {
1376 warn "*****";
1377 confess "Value is $$lit_str_ref";
1378 }
1379  
1380 }
1381  
1382  
1383 # TODO: Set value as property if value differ among models
1384  
1385 $model ||= $self->[WMODEL][NODE];
1386 $lit->[NODE][VALUE] = $lit_str_ref;
1387 $lit->[NODE][MODEL] = $model;
1388 $lit->[NODE][NAME] = 'Literal';
1389 $model->[REV_MODEL]{$lit->[NODE][ID]} = $lit->[NODE];
1390  
1391 $lit->declare_add_types([NS_RDFS.'Literal'], $model, 1 );
1392  
1393 debug_end("declare_literal", ' ', $self);
1394 return $lit;
1395 }
1396  
1397 sub declare_selection
1398 {
1399 my( $self, $content, $selection ) = @_;
1400  
1401  
1402 debug_start("declare_selection", ' ', $self);
1403 if( $DEBUG )
1404 {
1405 confess unless ref $content;
1406 my @con_uristr = ();
1407 foreach my $res ( @$content )
1408 {
1409 confess "$res no Resource" unless ref $res and ref $res->[NODE];
1410 push @con_uristr, $res->[NODE][URISTR];
1411 }
1412 debug " ( @con_uristr )\n";
1413 }
1414  
1415 $content ||= [];
1416 my $model = $self->[WMODEL][NODE] or
1417 die "$self->[NODE][URISTR] doesn't have a defined model";
1418  
1419 unless( ref $selection )
1420 {
1421 unless( defined $selection )
1422 {
1423 $selection = NS_LD.'/selection/'.&get_unique_id;
1424 }
1425 $selection = $self->get( $selection );
1426 }
1427 # warn "*** Selection is $selection->[NODE][URISTR]\n";
1428  
1429 my $selection_node = $selection->[NODE];
1430  
1431 $selection_node->[MODEL] = $model;
1432 $selection_node->[CONTENT] = $content;
1433 $selection_node->[NAME] = 'Selection';
1434  
1435 # TODO: Only add if this is an addition
1436 $model->[REV_MODEL]{$selection_node->[ID]} = $selection_node;
1437  
1438 $selection->declare_add_types( [NS_LS.'#Selection'] );
1439  
1440 debug_end("declare_selection", ' ', $self);
1441 return $selection;
1442 }
1443  
1444 sub declare_node
1445 {
1446 my( $self, $uri, $types, $props );
1447  
1448 die "Not done";
1449 }
1450  
1451 sub declare_self
1452 {
1453 my( $self, $types, $props ) = @_;
1454  
1455 # This is practicaly the same as set. declare_self does not store
1456 # the changes in the interfaces
1457  
1458 debug_start("declare_self", ' ', $self );
1459  
1460 # Should each type and property only be saved in the first best
1461 # interface and not saved in the following interfaces? Yes!
1462 #
1463 # The types and props taken by one interface must be marked so
1464 # that the next interface doesn't handle them. This could be done
1465 # by modifying the arguments $types and $props to exclude those
1466 # that has been taken care of.
1467  
1468 $self->set_types( $types, 0, 1 );
1469 $self->set_props( $props, 0, 1 );
1470  
1471 debug_end("declare_self", ' ', $self);
1472 return $self;
1473 }
1474  
1475  
1476 sub declare_add_types
1477 {
1478 my( $self, $types, $model, $solid ) = @_;
1479  
1480 debug_start("declare_add_types", ' ', $self );
1481  
1482 # TODO: Should it be model instead of types?
1483  
1484 # TODO: type(Resource) should be added by base init_types
1485  
1486 # The types will be listed in order from the most specific to the
1487 # most general. rdfs:Resource will allways be last. Insert
1488 # implicit items according to subClassOf.
1489  
1490 my $node = $self->[NODE];
1491 $model ||= $self->[WMODEL][NODE];
1492 $model = $self->get_node($model) unless ref $model;
1493 $solid ||= 0;
1494  
1495 if( $DEBUG )
1496 {
1497 croak "Invalid solid value: $solid" if $solid > 1;
1498 croak "types must be a list ref" unless ref $types;
1499 croak "Bad model: $model" unless
1500 ref $model eq "RDF::Service::Resource";
1501 confess "Bad node: $node" unless
1502 ref $node eq "RDF::Service::Resource";
1503 debug " in model $model->[URISTR] IDS $model->[IDS]\n";
1504 }
1505  
1506 my $model_id = $model->[ID];
1507 foreach my $type ( @$types )
1508 {
1509 # This should update the $types listref
1510 #
1511 $type = $self->get( $type ) unless ref $type;
1512  
1513 # Duplicate types in the same model will merge
1514 #
1515 # SOLID = 2, NONSOLID = 1
1516 #
1517 $node->[TYPE]{$type->[NODE][ID]}{$model_id} = 1 + $solid;
1518 $type->[NODE][REV_TYPE]{$node->[ID]}{$model_id} = 1 + $solid;
1519  
1520 if( $DEBUG )
1521 {
1522 debug(" T $type->[NODE][URISTR] ".
1523 "(IDS $type->[NODE][IDS] )\n", 2);
1524 if( $type->[NODE][MODEL] )
1525 {
1526 debug(" Model of type is ".
1527 $type->[NODE][MODEL][URISTR] .
1528 " IDS $type->[NODE][MODEL][IDS]\n", 2);
1529 }
1530 }
1531 }
1532  
1533 # TODO: Only set this if one type was added
1534 #
1535 # NB! The model include this node in REV_MODEL if it self or any
1536 # of its types belongs to the model. But the node includes the
1537 # model only if int's internal data belongs to that model.
1538 #
1539 $model->[REV_MODEL]{$node->[ID]} = $node;
1540  
1541 unless( $solid )
1542 {
1543 # Node type no longer solid. (Unsaved types)
1544 #
1545 $node->[TYPE_ALL] = 1 if $node->[TYPE_ALL];
1546 }
1547  
1548 # TODO: Separate the dynamic types to a separate init_types
1549  
1550  
1551  
1552 # TODO: Maby place in separate method
1553  
1554 # Add the implicit types for $node. This is done in a second loop
1555 # in order to resolv cyclic dependencies.
1556 # TODO: Check that this generates the right result.
1557 #
1558 my $subClassOf = $self->get(NS_RDFS.'subClassOf');
1559 foreach my $type ( @$types )
1560 {
1561 # $types has previously (in this function) been converted from
1562 # URISTR to res
1563  
1564 # NB!!! Special handling of some basic classes in order to
1565 # avoid cyclic dependencies
1566 #
1567 my $type_node = $type->[NODE];
1568 next if $type_node->[URISTR] eq NS_RDFS.'Literal';
1569 next if $type_node->[URISTR] eq NS_RDFS.'Class';
1570 next if $type_node->[URISTR] eq NS_RDFS.'Resource';
1571 next if $type_node->[URISTR] eq NS_RDF.'Statement';
1572 next if $type_node->[URISTR] eq NS_LS.'#Selection';
1573  
1574  
1575  
1576 # The class init_rev_subjs creates implicit subClassOf for
1577 # second and nth stage super classes. We only have to iterate
1578 # through the subClassOf properties of the type.
1579 #
1580 foreach my $sc ( @{$type->arc_obj_list($subClassOf)} )
1581 {
1582 # Special handling of Resource. Added below
1583 next if $sc->[NODE][URISTR] eq NS_RDFS.'Resource';
1584  
1585 # These are SOLID, since they are dynamic
1586 # TODO: What should the model be?
1587 #
1588 $node->[TYPE]{$sc->[NODE][ID]}{$model_id} = 2;
1589 $sc->[NODE][REV_TYPE]{$node->[ID]}{$model_id} = 2;
1590 # These types are dependent on the subClasOf statements
1591 }
1592 }
1593  
1594 # Add RDFS:Resource, in case not done yet
1595 #
1596 $node->[TYPE]{&uri2id(NS_RDFS.'Resource')}{uri2id(NS_RDFS)} = 2;
1597  
1598 # The jumptable must be redone now!
1599 if( $node->[JUMPTABLE] )
1600 {
1601 debug "Resetting the jumptable for ".
1602 "$node->[URISTR]: $node->[JTK]\n", 1;
1603 $node->[JTK] = '--resetted--';
1604 undef $node->[JUMPTABLE];
1605 }
1606  
1607 debug_end("declare_add_types", ' ', $self);
1608 return 1;
1609 }
1610  
1611 sub declare_add_static_literal
1612 {
1613 my( $subj, $pred, $lit_str, $arc_uristr ) = @_;
1614 #
1615 # $lit_str is a scalar ref
1616 #
1617 # The URI of a static literal represents what the value
1618 # represents. That is; the abstract property. It will never
1619 # change. (The literal static/dynamic type info is not stored)
1620  
1621 # TODO: find the literal...
1622  
1623 die "Not implemented";
1624  
1625 # $arc_uristr ||= $model.'#'.get_unique_id();
1626 # my $arc_id = uri2id( $arc_uristr );
1627 # push @{ $subj->[PROPS]{$pred->[ID]} }, [$obj->[ID],
1628 # $arc_id,
1629 # $model->[ID],
1630 # ];
1631 # return $arc_uristr;
1632 }
1633  
1634 sub declare_add_dynamic_literal
1635 {
1636 my( $subj, $pred, $lit_str_ref, $lit_uristr, $arc_uristr, $model ) = @_;
1637 #
1638 # $lit_str is a scalar ref
1639 #
1640 # The URI of a dynamic literal represents the property for the
1641 # specific subject. The literal changes content as the subjects
1642 # property changes. (The literal static/dynamic type info is not
1643 # stored)
1644  
1645 debug_start("declare_add_dynamic_literal", ' ', $subj );
1646  
1647 croak "Invalid subj" unless ref $subj;
1648 croak "No subj model" unless ref $subj->[NODE][MODEL];
1649  
1650 $pred = $subj->get( $pred ) unless ref $pred;
1651 $model ||= $subj->[WMODEL][NODE];
1652  
1653 $arc_uristr ||= NS_LD."/literal/".get_unique_id();
1654  
1655 # TODO: This is a implicit object. It's URI should be based on the
1656 # subject URI
1657 #
1658 my $lit = $subj->declare_literal( $lit_str_ref,
1659 $lit_uristr,
1660 undef,
1661 undef,
1662 $model,
1663 );
1664  
1665 my $arc = $subj->declare_add_prop( $pred, $lit, $arc_uristr, $model );
1666  
1667 debug_end("declare_add_dynamic_literal", ' ', $subj);
1668 return $arc;
1669 }
1670  
1671 sub declare_add_prop
1672 {
1673 my( $subj, $pred, $obj, $arc_uristr, $model, $solid ) = @_;
1674  
1675 $model ||= $subj->[WMODEL][NODE];
1676 $solid ||= 0;
1677  
1678 my $arc = $subj->declare_arc( $pred,
1679 $subj,
1680 $obj,
1681 $arc_uristr,
1682 $model,
1683 $solid
1684 );
1685  
1686 return $arc;
1687 }
1688  
1689 sub declare_arc
1690 {
1691 my( $self, $pred, $subj, $obj, $uristr, $model, $solid ) = @_;
1692  
1693 # It *could* be that we have two diffrent arcs with the same URI,
1694 # if they comes from diffrent models. The common case is that the
1695 # arcs with the same URI are identical. The PRED, SUBJ, OBJ slots
1696 # are used for the common case.
1697 #
1698 # TODO: Use explicit properties if the models differs.
1699 #
1700 # All models says the same thing unless the properties are
1701 # explicit.
1702  
1703 # A defined [REV_SUBJ] only means that some props has been
1704 # defined. It doesn't mean that ALL props has been defined.
1705  
1706 # A existing prop key with an undef value means that we know that
1707 # the prop doesn't exist. But a look for a nonexisting prop sould
1708 # (for now) trigger a complete initialization and set the complete
1709 # key.
1710  
1711 # The concept of "complete list" depends on other selection.
1712 # Diffrent selections will have diffrent lists. Every such
1713 # selection will be saved separately from the [REV_SUBJ] list.
1714 # It's existence guarantee that the list is complete.
1715  
1716 debug_start("declare_arc", ' ', $self);
1717  
1718 if( $DEBUG )
1719 {
1720 if( $obj->[NODE][VALUE] )
1721 {
1722 unless( ref($obj->[NODE][VALUE]) eq 'SCALAR')
1723 {
1724 confess "Bad value for $obj->[NODE][URISTR] ( ".
1725 ref($obj->[NODE][VALUE])." ne 'SCALAR' )";
1726 }
1727 }
1728 }
1729  
1730  
1731 if( $uristr ) # arc could be changed
1732 {
1733 # TODO: Check that tha agent owns the namespace
1734 # For now: Just allow models in the local namespace
1735 my $ns_l = NS_LD;
1736 unless( $uristr =~ /$ns_l/ )
1737 {
1738 confess "Invalid namespace for literal: $uristr";
1739 }
1740 }
1741 else # The arc is created
1742 {
1743 # Who will know anything about this arc? There could be
1744 # statements about it later, but not now.
1745  
1746 $uristr = NS_LD."/arc/". &get_unique_id;
1747  
1748 # TODO: Call a miniversion of add_types that knows that no other
1749 # types has been added. We should not require the setting of
1750 # types and props to initialize itself. The initialization
1751 # should be done here.
1752 }
1753  
1754 # Prioritize submitted $model
1755 #
1756 $model ||= $self->[WMODEL][NODE];
1757 $solid ||= 0;
1758  
1759  
1760 my $arc = $self->get( $uristr );
1761 my $arc_node = $arc->[NODE];
1762  
1763 $model or die "*** No WMODEL for arc $arc_node->[URISTR]\n";
1764 $arc_node->[IDS] or die "*** No IDS for arc $arc_node->[URISTR]\n";
1765  
1766  
1767  
1768 $pred = $self->get($pred) unless ref $pred;
1769 $subj = $self->get($subj) unless ref $subj;
1770 $obj = $obj->get($obj) unless ref $obj;
1771  
1772 if( $DEBUG )
1773 {
1774 unless( ref( $model ) eq "RDF::Service::Resource" )
1775 {
1776 confess "Bad model";
1777 }
1778  
1779 debug " P $pred->[NODE][URISTR]\n", 1;
1780 debug " S $subj->[NODE][URISTR]\n", 1;
1781 debug " O $obj->[NODE][URISTR]\n", 1;
1782 debug " M $model->[URISTR]\n", 1;
1783 debug " A $arc->[NODE][URISTR]\n", 1;
1784 }
1785  
1786 $arc_node->[PRED] = $pred->[NODE];
1787 $arc_node->[SUBJ] = $subj->[NODE];
1788 $arc_node->[OBJ] = $obj->[NODE];
1789 $arc_node->[MODEL] = $model;
1790  
1791 push @{ $subj->[NODE][REV_SUBJ]{$pred->[NODE][ID]} }, $arc_node;
1792 push @{ $obj->[NODE][REV_OBJ]{$pred->[NODE][ID]} }, $arc_node;
1793 push @{ $pred->[NODE][REV_PRED] }, $arc_node;
1794 $model->[REV_MODEL]{$arc_node->[ID]} = $arc_node;
1795  
1796 if( $solid )
1797 {
1798 debug "Changing SOLID to 1 for $arc_node->[URISTR] ".
1799 "IDS $arc_node->[IDS]\n", 3;
1800 $arc_node->[SOLID] = 1;
1801 }
1802 else
1803 {
1804 debug "Changing SOLID to 0 for $arc_node->[URISTR] ".
1805 "IDS $arc_node->[IDS]\n", 3;
1806 $arc_node->[SOLID] = 0;
1807 $subj->[NODE][REV_SUBJ_ALL] = 1 if $subj->[NODE][REV_SUBJ_ALL];
1808 $obj->[NODE][REV_OBJ_ALL] = 1 if $subj->[NODE][REV_OBJ_ALL];
1809 }
1810  
1811 # TODO: declare_self should only be used if a existing arc is
1812 # changed. New arc should not call declare_self since that forces
1813 # an deep initialization of itself.
1814  
1815 $arc->declare_add_types( [NS_RDF.'Statement'], NS_RDF, 1 );
1816  
1817  
1818 debug_end("declare_arc", ' ', $self);
1819 return $arc;
1820 }
1821  
1822  
1823  
1824  
1825  
1826 1;
1827  
1828  
1829 __END__