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
use Windows, Editor, Math, Strings
group Club
design President is
cell Screen
int MemberCount=0
int NumberOfFriends=0
utf8 Names[] = ["Marc","Tom","Joe","Karen","Stacey"]
cell Secretary
constructor is
(1) Screen = create MenuWindow("Presidential Screen",[0,0,900,750])
Screen <- Println("The President will now create the members.")
for each Name in Names do
Screen <- Println("Creating member [Name].")
(2) create Member( Name, Screen )
end
Secretary = create private Member( "Secretary" , Screen)
all <- AreYouPresent
all <- ThisIsMySecretary( Secretary)
(3) all <!- GetVolunteer
all <*- DoThisFirst
self <- TestDefaultBehaviour
all <- Greetings.HowAreYou
all <- Greetings.GetLost
all <!- HowAboutThis
all <- Bluetooth.Connect
Screen <- Println("\nThe President has finished.\n")
end
int x = 5 , y = 7
on Present(ansi Name) do
MemberCount += 1
Screen <- Println("[Name] has responded and is present")
sender <- "Do you want to be my friend ?", AddThisForMe( x, y)
x *= 2
y += 5
end
on Result(ansi Name, int a, int b,int Sum) do
Screen <- Println("[Name] says that [a] + [b] = [Sum]")
end
on IamYourFriend do
NumberOfFriends += 1
Screen <- Println("I have now [NumberOfFriends] friends")
end
on Volunteer(ansi Name) do
Screen <- Println("[Name] volunteers, hurray !")
end
(4) on ? => all <- (same)
end
design Member(ansi Name, cell Screen) is
(5) keep Name, Screen
(6) function Feedback => Screen <- Println("\t[Name]: received [same]")
on AreYouPresent do
sender <- Present( Name )
Feedback()
end
on AddThisForMe(int a, int b) => sender <- Result(Name, a,b, a+b)
on "Do you want to be my friend ?" do
sender <- IamYourFriend
Feedback()
end
on GetVolunteer => sender <- Volunteer( Name )
on DoThisFirst => Feedback()
on ThisIsMySecretary(cell Secretary) => Secretary <+- AreYouPresent
(7) on AreYouPresent.DN => Feedback()
on AreYouPresent.NDN => Feedback()
on TestDefaultBehaviour => Feedback()
interface Greetings is
on HowAreYou => Feedback()
on ? => Screen <- Println("\t[Name]: [same] is not supported.")
interface end
on Bluetooth.? => Screen <- Println("\t[Name]: No Bluetooth yet !")
on ? => Screen <- Println("\t[Name]: [same] is not supported.")
end