Scripting Java Applications with Sleep 

by Raphael Mudge



Listing One (vote.sl - VoteApp Sleep Script)



3   candidate "George"

4   {

5      addVoteFor("Raphael");    # guess who will win?

6      updateResults("George");

7   }

8

9   candidate "Howard"

10  {

11     updateResults("Howard");

12  }

13

14  candidate "Raphael"

15  {

16     updateResults("Raphael");

17  }





Listing Two (vote.sl - continued)



21  sub updateResults

22  {

23     $votes = getVotesFor($1);

24

25     if (-iswinning $1)

26     {

27        Display("Your candidate is winning with $votes vote(s)");

28     }

29     else

30     {

31        $winner = getWinner();

32        Display("$1 is losing to $winner with $votes vote(s)");

33     }

34  }





Listing Three (vote.sl - continued)



36  sub getWinner

37  {

38     foreach $candidate (keys(%results))

39     {

40        if (-iswinning $candidate)

41        {

42           return $candidate;

43        }

44     }

45

46     return $null;

47  }





Listing Four (VoteScript.java)



8   public class VoteScript implements Loadable

9   {

10     private VoteApp        dialog;

11     private ScriptLoader   loader;

12     private ScriptInstance script;

13     private HashMap        candidates = new HashMap();

  ...

20     public void init()

21     {

22        loader = new ScriptLoader();

23        loader.addGlobalBridge(this);

24

25        try

26        {

27           script = loader.loadScript("vote.sl");

28           script.runScript();

29        }

30        catch (Exception ex)

31        {





Listing Five  (VoteScript.java)



8   public class VoteScript implements Loadable

9   {

  ...

20     public void init()

21     {

22        loader = new ScriptLoader();

23        loader.addGlobalBridge(this);





Listing Six (VoteScript.java)



37  public boolean scriptLoaded(ScriptInstance script)

38  {

39     Hashtable environment = script.getScriptEnvironment().getEnvironment();

 ...

47     environment.put("&Display", new Display());





Listing Seven  (VoteScript.java)



63     private class Display implements Function

64     {

65        public Scalar evaluate(String funcName, ScriptInstance script,

                                 Stack arguments)

66        {

67           String message = BridgeUtilities.getString(arguments, "eh?!?");

68           dialog.setDisplay(message);

69           return SleepUtils.getEmptyScalar();

70        }

71     }





Listing Eight (VoteScript.java)



8   public class VoteScript implements Loadable

9   {

  ...

13     private HashMap        candidates = new HashMap();

  ...

98     private class SetupCandidate implements Environment

99     {

100       public void bindFunction(ScriptInstance script, String keyword,

                                   String funcName, Block body)

101       {

102          candidates.put(funcName, body);

103          dialog.addCandidate(funcName);

104       }

105    }





Listing Nine (VoteScript.java)



57     public void candidateVotedFor(String name)

58     {

59        Block code = (Block)candidates.get(name);

60        SleepUtils.runCode(code, script.getScriptEnvironment());

61     }





Listing Ten (VoteScript.java)



37   public boolean scriptLoaded(ScriptInstance script)

38   {

39     Hashtable environment = script.getScriptEnvironment().getEnvironment();

40

41     environment.put("candidate",  new SetupCandidate());





Listing Eleven  (VoteScript.java)



89     private class IsWinning implements Predicate

90     {

91        public boolean decide(String condition, ScriptInstance script,

                                Stack arguments)

92        {

93           String candidate = BridgeUtilities.getString(arguments, "none");

94           return dialog.isWinning(candidate);

95        }

96     }





Listing Twelve  (VoteScript.java)



37   public boolean scriptLoaded(ScriptInstance script)

38   {

39     Hashtable environment = script.getScriptEnvironment().getEnvironment();

  ...

42     environment.put("-iswinning", new IsWinning());





Listing Thirteen (VoteScript.java)



37  public boolean scriptLoaded(ScriptInstance script)

38  {

39    Hashtable environment = script.getScriptEnvironment().getEnvironment();

  ...

49    HashMap results = dialog.getAllVotes();

50    script.getScriptVariables().putScalar("%results",

                    SleepUtils.getHashWrapper(results));

