/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/signals/natives/NATSeconds.java

http://ambienttalk.googlecode.com/ · Java · 128 lines · 61 code · 15 blank · 52 comment · 0 complexity · d80f746c7349c5f0e143a798c1105567 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATSeconds.java created on Apr 14, 2008
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.signals.natives;
  29. import java.util.Timer;
  30. import java.util.TimerTask;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.objects.ATNumber;
  33. import edu.vub.at.objects.grammar.ATSymbol;
  34. import edu.vub.at.objects.natives.NATByRef;
  35. import edu.vub.at.objects.natives.NATMethodInvocation;
  36. import edu.vub.at.objects.natives.NATNumber;
  37. import edu.vub.at.objects.natives.NATTable;
  38. import edu.vub.at.objects.natives.NATText;
  39. import edu.vub.at.objects.natives.grammar.AGSymbol;
  40. /**
  41. * Seconds is the builtin behavior representing the number of seconds that have elapsed since the
  42. * AmbientTalk virtual machine has been started.
  43. *
  44. * @author smostinc
  45. */
  46. public class NATSeconds extends NATBehavior {
  47. private static final ATSymbol _TICK_ = AGSymbol.jAlloc("tick");
  48. private static final TimerSource _TIMER_ = new TimerSource();
  49. private static final ThreadLocal _SECONDS_ = new ThreadLocal() {
  50. protected Object initialValue() {
  51. try {
  52. return new NATSeconds(_TIMER_, new SecondsConsumer());
  53. } catch (InterpreterException e) {
  54. throw new RuntimeException(e);
  55. }
  56. };
  57. };
  58. public static NATSeconds instance() {
  59. return (NATSeconds) _SECONDS_.get();
  60. }
  61. /**
  62. * TimerSource is a natively implemented event source which generates time tick events.
  63. * The Event Source is shared between all actors on a single virtual machine, yet all
  64. * actors have a separate seconds object, to represent the time. Due to synchronization
  65. * when propagating events, the timer cannot cause concurrency within a single actor.
  66. *
  67. * @author smostinc
  68. */
  69. private static class TimerSource extends NATEventSource {
  70. private Timer timer_;
  71. public TimerSource() {
  72. super(0);
  73. timer_ = new Timer();
  74. TimerTask task_ = new TimerTask() {
  75. public void run() {
  76. try {
  77. propagateNewMessage(new NATMethodInvocation(
  78. _TICK_,
  79. NATTable.EMPTY,
  80. NATTable.EMPTY));
  81. } catch (InterpreterException e) {
  82. // Failed to construct a <-tick() message
  83. e.printStackTrace();
  84. }
  85. };
  86. };
  87. timer_.schedule(task_, 1000, 1000);
  88. }
  89. }
  90. /**
  91. * <code>
  92. * def SecondsConsumer := object: {
  93. * def seconds := 0;
  94. * def tick() {
  95. * seconds := seconds.inc();
  96. * }
  97. * }
  98. * </code>
  99. */
  100. public static class SecondsConsumer extends NATByRef {
  101. ATNumber seconds_ = NATNumber.ZERO;
  102. public ATNumber base_tick() throws InterpreterException {
  103. seconds_ = seconds_.base_inc();
  104. return seconds_;
  105. }
  106. public NATText meta_print() throws InterpreterException {
  107. return NATText.atValue("<object:"+this.hashCode()+">");
  108. }
  109. }
  110. NATSeconds(TimerSource theTimer, SecondsConsumer theConsumer) throws InterpreterException {
  111. super(theTimer, NATNumber.ZERO, theConsumer);
  112. }
  113. }