commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ra...@apache.org
Subject svn commit: r381907 - in /jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env: StopWatch.java stopwatch.xml
Date Wed, 01 Mar 2006 05:12:23 GMT
Author: rahul
Date: Tue Feb 28 21:12:20 2006
New Revision: 381907

URL: http://svn.apache.org/viewcvs?rev=381907&view=rev
Log:
Towards BZ 38274

[scxml] Write Use Case for Stand-alone Application Modelling a Stop Watch

A SCXML driven stop watch. Thanks to:

 * Stephen Colebourne, the state machine and stopwatch usecase is inspired by Stephen's slides
for his Javapolis talk '05 (New and Upcoming in Jakarta Commons)

 * Tim O'Brien, for the nudge

Added:
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
  (with props)

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java?rev=381907&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
Tue Feb 28 21:12:20 2006
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.scxml.env;
+
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.commons.scxml.env.AbstractStateMachine;
+
+/**
+ * A SCXML document driven stop watch.
+ *
+ * Using SCXML makes the StopWatch class simplistic; you are neither
+ * managing the stopwatch "lifecycle" nor coding any "transitions",
+ * that information is pulled in straight from the behavioral model
+ * of the stop watch, which is encapsulated in the SCXML document
+ * the constructor points to (which in turn may be generated straight
+ * from the UML model).
+ */
+public class StopWatch extends AbstractStateMachine {
+
+    /** The events for the stop watch. */
+    public static final String EVENT_START = "watch.start",
+        EVENT_STOP = "watch.stop", EVENT_SPLIT = "watch.split",
+        EVENT_UNSPLIT = "watch.unsplit", EVENT_RESET = "watch.reset";
+
+    /** The fragments of the elapsed time. */
+    private int hr, min, sec, fract;
+    /** The fragments of the display time. */
+    private int dhr, dmin, dsec, dfract;
+    /** The stopwatch "split" (display freeze). */
+    private boolean split;
+    /** The Timer to keep time. */
+    private Timer timer;
+    /** The display decorations. */
+    private static final String DELIM = ":", DOT = ".", EMPTY = "", ZERO = "0";
+
+    public StopWatch() {
+        super(StopWatch.class.getClassLoader().
+            getResource("org/apache/commons/scxml/env/stopwatch.xml"));
+    }
+
+    // Each method below is the activity corresponding to a state in the
+    // SCXML document (see class constructor for pointer to the document).
+    public void reset() {
+        hr = min = sec = fract = dhr = dmin = dsec = dfract = 0;
+        split = false;
+    }
+
+    public void running() {
+        split = false;
+        if (timer == null) {
+            timer = new Timer(true);
+            timer.scheduleAtFixedRate(new TimerTask() {
+                public void run() {
+                    increment();
+                }
+            }, 100, 100);
+        }
+    }
+
+    public void paused() {
+        split = true;
+    }
+
+    public void stopped() {
+        timer.cancel();
+        timer = null;
+    }
+
+    public String getDisplay() {
+        String padhr = dhr > 9 ? EMPTY : ZERO;
+        String padmin = dmin > 9 ? EMPTY : ZERO;
+        String padsec = dsec > 9 ? EMPTY : ZERO;
+        return new StringBuffer().append(padhr).append(dhr).append(DELIM).
+            append(padmin).append(dmin).append(DELIM).append(padsec).
+            append(dsec).append(DOT).append(dfract).toString();
+    }
+
+    // used by the demonstration (see StopWatchDisplay usecase)
+    public String getCurrentState() {
+        Set states = getEngine().getCurrentStatus().getStates();
+        return ((org.apache.commons.scxml.model.State) states.iterator().
+            next()).getId();
+    }
+
+    private void increment() {
+        if (fract < 9) {
+            fract++;
+        } else {
+            fract = 0;
+            if (sec < 59) {
+                sec++;
+            } else {
+                sec = 0;
+                if (min < 59) {
+                    min++;
+                } else {
+                    min = 0;
+                    if (hr < 99) {
+                        hr++;
+                    } else {
+                        hr = 0; //wrap
+                    }
+                }
+            }
+        }
+        if (!split) {
+            dhr = hr;
+            dmin = min;
+            dsec = sec;
+            dfract = fract;
+        }
+    }
+
+}
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/StopWatch.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml?rev=381907&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
Tue Feb 28 21:12:20 2006
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<!--
+   Copyright 2006 The Apache Software Foundation
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<scxml xmlns="http://www.w3.org/2005/07/SCXML"
+       version="1.0"
+       initialstate="reset">
+
+    <state id="reset">
+       <transition event="watch.start">
+           <target next="running"/>
+       </transition>
+    </state>
+
+    <state id="running">
+		<transition event="watch.split">
+            <target next="paused"/>
+        </transition>
+        <transition event="watch.stop">
+            <target next="stopped"/>
+        </transition>
+    </state>
+
+    <state id="paused">
+       <transition event="watch.unsplit">
+           <target next="running"/>
+       </transition>
+        <transition event="watch.stop">
+            <target next="stopped"/>
+        </transition>
+    </state>
+
+    <state id="stopped">
+       <transition event="watch.reset">
+           <target next="reset"/>
+       </transition>
+    </state>
+
+</scxml>
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/env/stopwatch.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message