/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
- /**
- * AmbientTalk/2 Project
- * NATSeconds.java created on Apr 14, 2008
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.signals.natives;
- import java.util.Timer;
- import java.util.TimerTask;
- import edu.vub.at.exceptions.InterpreterException;
- import edu.vub.at.objects.ATNumber;
- import edu.vub.at.objects.grammar.ATSymbol;
- import edu.vub.at.objects.natives.NATByRef;
- import edu.vub.at.objects.natives.NATMethodInvocation;
- import edu.vub.at.objects.natives.NATNumber;
- import edu.vub.at.objects.natives.NATTable;
- import edu.vub.at.objects.natives.NATText;
- import edu.vub.at.objects.natives.grammar.AGSymbol;
- /**
- * Seconds is the builtin behavior representing the number of seconds that have elapsed since the
- * AmbientTalk virtual machine has been started.
- *
- * @author smostinc
- */
- public class NATSeconds extends NATBehavior {
- private static final ATSymbol _TICK_ = AGSymbol.jAlloc("tick");
- private static final TimerSource _TIMER_ = new TimerSource();
-
- private static final ThreadLocal _SECONDS_ = new ThreadLocal() {
- protected Object initialValue() {
- try {
- return new NATSeconds(_TIMER_, new SecondsConsumer());
- } catch (InterpreterException e) {
- throw new RuntimeException(e);
- }
- };
- };
-
- public static NATSeconds instance() {
- return (NATSeconds) _SECONDS_.get();
- }
-
- /**
- * TimerSource is a natively implemented event source which generates time tick events.
- * The Event Source is shared between all actors on a single virtual machine, yet all
- * actors have a separate seconds object, to represent the time. Due to synchronization
- * when propagating events, the timer cannot cause concurrency within a single actor.
- *
- * @author smostinc
- */
- private static class TimerSource extends NATEventSource {
-
- private Timer timer_;
- public TimerSource() {
- super(0);
-
- timer_ = new Timer();
- TimerTask task_ = new TimerTask() {
- public void run() {
- try {
- propagateNewMessage(new NATMethodInvocation(
- _TICK_,
- NATTable.EMPTY,
- NATTable.EMPTY));
- } catch (InterpreterException e) {
- // Failed to construct a <-tick() message
- e.printStackTrace();
- }
- };
- };
-
- timer_.schedule(task_, 1000, 1000);
- }
- }
-
- /**
- * <code>
- * def SecondsConsumer := object: {
- * def seconds := 0;
- * def tick() {
- * seconds := seconds.inc();
- * }
- * }
- * </code>
- */
- public static class SecondsConsumer extends NATByRef {
- ATNumber seconds_ = NATNumber.ZERO;
-
- public ATNumber base_tick() throws InterpreterException {
- seconds_ = seconds_.base_inc();
- return seconds_;
- }
-
- public NATText meta_print() throws InterpreterException {
- return NATText.atValue("<object:"+this.hashCode()+">");
- }
- }
-
- NATSeconds(TimerSource theTimer, SecondsConsumer theConsumer) throws InterpreterException {
- super(theTimer, NATNumber.ZERO, theConsumer);
- }
- }