| 1 | # $Id: Service.pm,v 1.22 2000/12/21 22:04:18 aigan Exp $ -*-perl-*- | ||
| 2 | |||
| 3 | package RDF::Service; | ||
| 4 | |||
| 5 | #===================================================================== | ||
| 6 | # | ||
| 7 | # DESCRIPTION | ||
| 8 | # Creates the Service resource | ||
| 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 5.006; | ||
| 23 | use RDF::Service::Constants qw( :rdf :namespace :context ); | ||
| 24 | use RDF::Service::Cache qw( get_unique_id uri2id debug debug_start | ||
| 25 | debug_end time_string $DEBUG); | ||
| 26 | use RDF::Service::Resource; | ||
| 27 | use RDF::Service::Context; | ||
| 28 | use Data::Dumper; | ||
| 29 | |||
| 30 | our $VERSION = 0.04; | ||
| 31 | |||
| 32 | sub new | ||
| 33 | { | ||
| 34 | my( $class, $uristr ) = @_; | ||
| 35 | |||
| 36 | # This will (then implemented) create a session context resource. | ||
| 37 | # This resource will hold a orderd list of connected interfaces. | ||
| 38 | # A signature will be generated from this order. Every created | ||
| 39 | # object will be marked with this signature. The object will be | ||
| 40 | # cached and can be used again if a object with the same signature | ||
| 41 | # is creating the same object. | ||
| 42 | |||
| 43 | # This means that all the objects functions and information is | ||
| 44 | # only dependent on the order of the connected interfaces. This | ||
| 45 | # also means that for each resource there could be one object for | ||
| 46 | # each signature. They are the same resource, but viewed from | ||
| 47 | # diffrent contexts, they appear diffrent in regard to the result | ||
| 48 | # of queries, etc. | ||
| 49 | |||
| 50 | # Many queries will probably give the same answer for diffrent | ||
| 51 | # signatures, but that could not be guaranteed. This construction | ||
| 52 | # is based on the asumption that there will be many agents using | ||
| 53 | # the same signature. That is: the same object will be reused more | ||
| 54 | # times than not. For futher optimization, some data could be | ||
| 55 | # shared between diffrent objects for the same resource. Data | ||
| 56 | # could be marked as static or dynamic and new objects could point | ||
| 57 | # to the data for the previous object, if its marked as static. | ||
| 58 | |||
| 59 | # Interfaces connected after the creation of an object will not | ||
| 60 | # change the objects signature. The objects signature is a read | ||
| 61 | # only value. The first interface connection parameter is taken | ||
| 62 | # to be the URI of the source accessed by the interface. This URI | ||
| 63 | # is a part of the signature. (That is, the connection string to a | ||
| 64 | # database, or similar.) | ||
| 65 | |||
| 66 | # All context/client-dependent functions should use the session | ||
| 67 | # object methods or send it in as a parameter. This construction | ||
| 68 | # is a compromise to shorten the method calls in most cases, so | ||
| 69 | # that we doesn't have to tag along the context varaible evrywhere | ||
| 70 | # and so that we still can reuse the cached resource objects | ||
| 71 | # between more than one session. | ||
| 72 | |||
| 73 | |||
| 74 | # Initialize the level indicator | ||
| 75 | $RDF::Service::Cache::Level = 0; | ||
| 76 | |||
| 77 | debug_start("new RDF::Service"); | ||
| 78 | |||
| 79 | if( $uristr ) | ||
| 80 | { | ||
| 81 | # Must have a Service URI as recognized by the Base find_node | ||
| 82 | |||
| 83 | my $pattern = "^".NS_LD."/service/[^/#]+\$"; | ||
| 84 | unless( $uristr =~ m/$pattern/o ) | ||
| 85 | { | ||
| 86 | die "Invalid namespace for Service: $uristr"; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | # Every service object is unique | ||
| 92 | # | ||
| 93 | $uristr = NS_LD."/service/".&get_unique_id; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | # The service object is not stored in any interface. The base | ||
| 98 | # interface init_types function states that all resources matching | ||
| 99 | # a specific pattern are Service objects. That is needed since | ||
| 100 | # the resources acts as models for other models stored in other | ||
| 101 | # interfaces. But here we state the types for the newly created | ||
| 102 | # Service object. | ||
| 103 | |||
| 104 | # Declare the types for the service. Do it the low-level way. We | ||
| 105 | # can not call declare_add_typews() since that calles init_props() | ||
| 106 | # for the classes. | ||
| 107 | |||
| 108 | |||
| 109 | my $so = RDF::Service::Resource->new($uristr); | ||
| 110 | $so->[RUNLEVEL] = 0; # Startup runlevel | ||
| 111 | my $s = RDF::Service::Context->new( $so, {} ); | ||
| 112 | |||
| 113 | &_bootstrap( $s ); | ||
| 114 | |||
| 115 | debug_end("new RDF::Service"); | ||
| 116 | |||
| 117 | return $s; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _bootstrap | ||
| 121 | { | ||
| 122 | my( $s ) = @_; | ||
| 123 | # | ||
| 124 | # Connect the base interface. | ||
| 125 | |||
| 126 | debug "Bootstrap\n", 1; | ||
| 127 | |||
| 128 | my $node = $s->[NODE]; | ||
| 129 | |||
| 130 | my $base_model = $s->get_node(NS_LD.'#The_Base_Model'); | ||
| 131 | # $base_model->[REV_MODEL]{$node->[ID]} = $node; # Try without... | ||
| 132 | $base_model->[TYPE_ALL] = 1; | ||
| 133 | debug "Changing SOLID to 1 for $base_model->[URISTR] ". | ||
| 134 | "IDS $base_model->[IDS]\n", 3; | ||
| 135 | $base_model->[SOLID] = 1; # nonchanging | ||
| 136 | |||
| 137 | $s->[WMODEL] = $s; | ||
| 138 | $node->[MODEL] = $base_model; | ||
| 139 | $node->[TYPE] = {}; | ||
| 140 | $node->[INTERFACES] = []; | ||
| 141 | debug "Changing SOLID to 0 for $node->[URISTR] ". | ||
| 142 | "IDS $node->[IDS]\n", 3; | ||
| 143 | $node->[SOLID] = 0; | ||
| 144 | |||
| 145 | my $module = "RDF::Service::Interface::Base::V01"; | ||
| 146 | |||
| 147 | my $file = $module; | ||
| 148 | $file =~ s!::!/!g; | ||
| 149 | $file .= ".pm"; | ||
| 150 | require "$file"; | ||
| 151 | |||
| 152 | { | ||
| 153 | no strict 'refs'; | ||
| 154 | # my $base_interface_uri = &{$module."::_construct_interface_uri"}( $module ); | ||
| 155 | # my $base_interface = $s->get( $base_interface_uri ); | ||
| 156 | |||
| 157 | # NB! The session node is used as the interface. It will be | ||
| 158 | # replaced during the connection. | ||
| 159 | # | ||
| 160 | debug_start( "connect", 0, $s ); | ||
| 161 | &{$module."::connect"}( $s, $s->[NODE], $module ); | ||
| 162 | debug_end( "connect", 0, $s ); | ||
| 163 | } | ||
| 164 | |||
| 165 | # Node has now been updated | ||
| 166 | # | ||
| 167 | $node = $s->[NODE]; | ||
| 168 | |||
| 169 | # The IDS of $s is now defined; update $s->[WMODEL]. We do not | ||
| 170 | # have to do the same thing for $node, since it doesn't is used as | ||
| 171 | # an object above. | ||
| 172 | # | ||
| 173 | $base_model->[IDS] = $node->[IDS]; | ||
| 174 | $base_model->[JUMPTABLE] = undef; | ||
| 175 | |||
| 176 | # Save node in cache! | ||
| 177 | # | ||
| 178 | # $RDF::Service::Cache::node->{$node->[IDS]}{ $node->[ID] } = $node; | ||
| 179 | # warn "*** Setting cache($s->[IDS], $s->[ID] ) to $s\n"; | ||
| 180 | |||
| 181 | # Add the type | ||
| 182 | # | ||
| 183 | $s->declare_add_types( [NS_LS.'#Service'] ); | ||
| 184 | |||
| 185 | $node->[RUNLEVEL] = 1; # Normal runlevel | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | 1; | ||
| 190 |