Examples

Cell Designs and Messages

In this example we show the basics of how a CellSpeak program is built.

We create a group, called 'Club' with two types of cells: a president and members. To run the program we just have to create the president-cell, who wil then create a number of members and send some messages to these memmbers.











(1) In order to show some output, we also create a an output window - from a design MenuWindow which is part of the Editor group in the system package. We pass this cell - called Screen - in the program also as a parameter to the members that are created, but we also could have sent this cell to system with the request to set it as the standard output. In other examples this is how we do it.






(2) Note that when we create the members that are part of the 'club', we do not keep an explicit cell-reference to each. In many cases it is desirable to have an explicit reference to the cells created, but here the 'president' will simply send the messages to 'all', a keyword that stands for all children created by a cell.






(3) The 'president' sends a message to all children, but expects only one to reply to the message. The first available child will handle the message and reply. This is useful for example for load-balancing.






















































(4) The default action for the 'president' is that he passes on any unhandled message to his children (the members in this case).






(5) Keep is a handy short-form for creating a variable of the same name and type as the parameter and copying the parameter value to that variable.

(6) If a function or handler is just one expression, you can use the '=>' notation






















(7) This how delivery/non-delivery notifications are formed: the suffix DN or NDN is added to the original message.

01 Cells Designs and Messages.celsrc
-- The groups that are used in this source file 
use Windows, Editor, Math, Strings

-- Designs that belong together are defined in a group 
group Club

-- The design of the President of the club
design President is

	cell Screen							
	int  MemberCount=0					
	int  NumberOfFriends=0				
	utf8 Names[] = ["Marc","Tom","Joe","Karen","Stacey"]
	cell Secretary

	-- The design has a constructor. It is called when the cell is created.
	constructor is

		-- Create the output cell
(1)		Screen = create MenuWindow("Presidential Screen",[0,0,900,750])
	
		-- We output a simple message to the window
		Screen <- Println("The President will now create the members.")

		-- 'Name' is a loop variable, and does not have to be declared.
		for each Name in Names do
		
			-- Print a message to the user
			Screen <- Println("Creating member [Name].")
			
			-- Cells are created with the 'create' keyword.
(2)			create Member( Name, Screen )	
		end
		
		-- A private member - only replies to the president.
		Secretary = create private Member( "Secretary" , Screen)
					
		-- all is a keyword and stands for 'all children cells'
		all <- AreYouPresent
		
		-- send the id of a private cell
		all <- ThisIsMySecretary( Secretary)
		
		-- Send a message that only needs to be handled by one cell
(3)		all <!- GetVolunteer
		
		-- Send a priority message
		all <*- DoThisFirst
		
		-- Send a message to myself to test default behaviour (see below)
		self <- TestDefaultBehaviour
		
		-- test an interface with a supported message
		all <- Greetings.HowAreYou
		
		-- test an interface with an unsupported message
		all <- Greetings.GetLost
		
		-- Send a generally unsupported message
		all <!- HowAboutThis
		
		-- Send another unsupported message
		all <- Bluetooth.Connect
		
		-- just some more info to the user
		Screen <- Println("\nThe President has finished.\n")
	end

	-- We can also declare variables here
	int x = 5 , y = 7
	on Present(ansi Name) do	

		-- The president counts the number of members that are responding
		MemberCount += 1
		
		-- ..and gives feedback to the user. 
		Screen <- Println("[Name] has responded and is present")
		
		-- ..and the President sends two messages in one line.
		sender <- "Do you want to be my friend ?", AddThisForMe( x, y)
		
		-- change the numbers for the next member
		x *= 2
		y += 5	

	end

	on Result(ansi Name, int a, int b,int Sum) do
	
		-- When the president receives the result, he prints the result
		Screen <- Println("[Name] says that [a] + [b] = [Sum]")
	end

	on IamYourFriend do
	
		-- The president counts his friends 
		NumberOfFriends += 1
		
		-- ..and gives some feedback to the user
		Screen <- Println("I have now [NumberOfFriends] friends")
	end
	
	on Volunteer(ansi Name) do
		
		-- Print the name of the volunteer
		Screen <- Println("[Name] volunteers, hurray !")	
		
	end
	
	-- our default behaviour : pass the message to all the children
(4)	on ? => all <- (same)
	
end -- president

---------------------------------------------------------------------
-- The design for the Members - simple, no constructor nor destructor 
design Member(ansi Name, cell Screen) is
	
	-- keep the parameters as permanent data
(5)	keep Name, Screen
	
	-- A simple function to report the reception of a message
(6)	function Feedback => Screen <- Println("\t[Name]: received [same]")
	
	-- A simple reply - 'sender' is a keyword
	on AreYouPresent do
		sender <- Present( Name )
		Feedback()
	end

	-- This message has two integer parameters
	on AddThisForMe(int a, int b) => sender <- Result(Name, a,b, a+b)

	-- Messages names can be strings eg to contain special characters
	on "Do you want to be my friend ?" do	
		sender <- IamYourFriend
		Feedback()
	end
	
	-- A handler to test the unique message feature
	on GetVolunteer => sender <- Volunteer( Name )
	
	-- A message that is sent with high priority
	on DoThisFirst => Feedback()
	
	-- Secretary is private - will result in a non-delivery
	on ThisIsMySecretary(cell Secretary) => Secretary <+- AreYouPresent
	
(7)	on AreYouPresent.DN => Feedback()
	
	on AreYouPresent.NDN => Feedback()
	
	on TestDefaultBehaviour => Feedback()
	
	interface Greetings is
	
		on HowAreYou => Feedback()
		
		-- Default behaviour for the Greetings interface"
		on ? => Screen <- Println("\t[Name]: [same] is not supported.")
	
	interface end
	
	-- Bluetooth interface is not supported"
	on Bluetooth.? => Screen <- Println("\t[Name]: No Bluetooth yet !")
	
	-- Reply for all other messages."	
	on ? => Screen <- Println("\t[Name]: [same] is not supported.")

end