Skip to content

Instantly share code, notes, and snippets.

@sinclairtarget
Last active February 21, 2022 15:18
Show Gist options
  • Save sinclairtarget/6364cd521010d28ee24dd41ab3d61a96 to your computer and use it in GitHub Desktop.
Save sinclairtarget/6364cd521010d28ee24dd41ab3d61a96 to your computer and use it in GitHub Desktop.
Simula Demonstration
Simulation Begin
! We will use this as input to Normal() below ;
Integer seed;
! Utility method for logging messages along with the current simulation
time ;
Procedure log(message); Text message; Begin
OutFix(Time, 2, 0);
OutText(": " & message);
OutImage;
End;
! This is a regular class, used to represent a fishing rod that you can
wait in line for ;
Class FishingRod; Begin
Ref (Head) queue; ! Head is a system class representing a linked list ;
Boolean inUse;
Procedure request; Begin
If inUse Then Begin
Wait(queue); ! This suspends the waiting process ;
queue.First.out;
End;
inUse := True;
End;
Procedure surrender; Begin
inUse := False;
Activate queue.First; ! Start/resume the process ;
End;
queue :- New Head;
End;
Ref (FishingRod) rod;
! The below line declares the class and what we would now call a
constructor ;
Process Class Villager(villagerName); Text villagerName; Begin
! These are local to the operating rule, but are basically our object
fields / instance attributes ;
Integer health;
Real start;
Real finish;
health := 100;
While True Do Begin
! Villagers get hungry after 60 minutes or so, give or take a
standard deviation of 20 minutes ;
Hold(Normal(60, 20, seed));
start := Time;
log(villagerName & " is hungry and requests the fishing rod.");
rod.request; ! Wait for the fishing rod ;
finish := Time;
! If a villager has to wait too long, that villager loses health ;
If finish - start > 5 Then Begin
log(villagerName & " went hungry waiting for the rod.");
health := health - (finish - start) * 2;
If health < 0 Then Begin
log(villagerName & " starved to death waiting for the rod.");
rod.surrender;
Passivate; ! Suspend the process indefinitely ;
End;
End;
! It takes around 12 minutes to catch a fish ;
log(villagerName & " is now fishing.");
Hold(Normal(12, 4, seed));
log(villagerName & " has caught a fish.");
rod.surrender;
End;
End;
rod :- New FishingRod;
Activate New Villager("John");
Activate New Villager("Jane");
Activate New Villager("Betty");
Activate New Villager("Sam");
! Let's wait a full day of simulated time to see what happens ;
Hold(60 * 24);
End;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment