2 name: A Stem Cell Cookbook
4 title: A few questions about a simple sub
6 *: What can you do with this sub?
7 *: Can it be networked?
8 *: Can it receive messages?
9 *: Can it send messages?
16 return "Hello world!\n";
21 title: A few questions about a simple sub: Answers
23 *: It can be networked under Stem
24 *: It can receive Stem messages
25 *: It can send Stem messages
26 *: No coding changes need to be made
33 return "Hello world!\n";
38 title: Loading World1 into Stem
40 *: Stem configuration files load modules and register Cells
41 *: This configuration loads the console module and the World1 module
42 *: This is YAML (yaml.org) format and other formats are supported
43 *: Start it with: run_stem worlds.stem
56 *: Stem is a message passing, event driven system
57 *: Stem Cells are Perl objects that are registered
58 *: Cells can be Perl classes or instantiated objects
59 *: Cells can send and receive messages.
60 *: Cell classes are loaded and Cells are created via Stem configuration files
61 *: Stem Hubs (processes) can support many active Cells
62 *: Stem Hubs can be connected in networks on one or more systems
63 *: Any Cell can send a message to any other reachable Cell
67 title: Stem message delivery
69 *: Messages are objects with address, content and related fields
70 *: The 'to' address of a message is used to identify the destination
72 *: The destination Cell is invoked by a method and the message is
74 *: The method to be called is determined by the message type and command
75 *: A 'foo' command message is delivered to method 'foo_cmd'
76 *: A 'bar' type message is delivered to method 'bar_in'
77 *: Command methods can optionaly return data which is sent back in in
84 *: Stem Addresses are name triplets: Hub, Cell, Target
85 *: The Hub is the name of Stem process
86 *: The Cell is the registered name of the Stem object or class
87 *: The target is the unique address of a cloned Cell
88 *: The Cell part of an address is required and the Hub and Target are optional
89 *: Addresses are written in string form in Stem configuration or from
100 title: Adding class level data
102 *: We add class level data and a method to change it
103 *: The file lexical variable $name stores the planet's name
104 *: The method 'name_cmd' can set that name from the data in a message
105 *: A 'name' command message can be sent from the console or anywhere
106 *: The configuration file doesn't change other except for the name of the
113 my $name = 'UNKNOWN' ;
116 <blue>return "Hello world from $name\n";</font>
121 my( $class, $msg ) = @_ ;
123 my $data = $msg->data() ;
125 return unless $data ;
134 title: A basic object level Cell
136 *: This module has an attribute specification so we can construct an object
137 *: If no planet name is passed to the constructor, it will be named 'X'
138 *: It also has a constructor method new() that is called from the configuration
139 *: The hello_cmd method now returns the object data in the planet attribute
146 <blue>my $attr_spec = [
154 my ( $class ) = shift ;
155 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
161 <blue>my( $self ) = @_ ;
163 return "Hello world from $self->{'planet'}\n" ;</font>
168 title: Configuration for object level cells
170 *: We add a 'name' field which is the cell part of the address for this
172 *: We add an 'args' field whose values are passed to the new() method
173 *: The object is constructed and registered with the selected 'name'
174 *: The initial value of the planet name can be set in the arguments
175 *: We created two object cells here using the same class but the first
176 uses the default planet name of 'X' and the second is named 'venus'
194 title: Changing object data
196 *: All that is needed is a name_cmd method very similar to the one in World2
197 *: It just changes the value in the cell itself
198 *: The configuration file needs to only change the class and cell names
213 my ( $class ) = shift ;
214 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
222 return "Hello world from $self->{'planet'}\n" ;
227 my ( $self, $msg ) = @_ ;
229 my $data = $msg->data() ;
231 return unless $data ;
233 $self->{'planet'} = ${$data} ;
243 title: Cloning object cells
245 *: Cloned Cells are similar to sessions or state objects but are much
246 simpler to create and manage
247 *: Object Cells that use the cloning services of Stem::Cell are called
249 *: All cloned Cells are owned by the Parent cell
250 *: When a parent Cell is triggered (via a message or internal call), it
251 copies and registers the clone with a unique target address
252 *: The Stem::Cell module is inherited and it handles the 'cell_trigger'
254 *: The specification must include a Stem::Cell class attribute
256 Note that it has its own default
257 *: A callback to the 'triggered_cell' method is made in a newly cloned cell
262 <blue>use base 'Stem::Cell' ;</font>
272 'name' => 'cell_attr',
273 'class' => 'Stem::Cell',
278 my ( $class ) = shift ;
279 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
287 return "Hello world from $self->{'planet'}\n" ;
294 $self->{'planet'} = $self->cell_get_args( 'planet' ) || 'pluto' ;
302 my ( $self, $msg ) = @_ ;
304 my $data = $msg->data() ;
306 return unless $data ;
308 $self->{'planet'} = ${$data} ;
315 title: Cloning cell configuration
317 *: This is the similar to the World4 configuration but we added the
318 'cell_attr' attribute and set its 'cloneable' flag to true