notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
svn commit: r1251 - in development/laboratory/planet/components/transitionmachine: . api api/src api/src/main api/src/main/net api/src/main/net/dpml api/src/main/net/dpml/transitionmachine basic basic/src basic/src/main basic/src/main/net basic/src/main/net/dpml basic/src/main/net/dpml/planet basic/src/main/net/dpml/planet/transitionmachine basic/src/main/net/dpml/planet/transitionmachine/basic demo demo/trafficlight demo/trafficlight/src demo/trafficlight/src/main demo/trafficlight/src/main/net demo/trafficlight/src/main/net/dpml demo/trafficlight/src/main/net/dpml/planet demo/trafficlight/src/main/net/dpml/planet/demo demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight
- From: niclas AT netcompartner.com
- To: notify-dpml AT lists.ibiblio.org
- Subject: svn commit: r1251 - in development/laboratory/planet/components/transitionmachine: . api api/src api/src/main api/src/main/net api/src/main/net/dpml api/src/main/net/dpml/transitionmachine basic basic/src basic/src/main basic/src/main/net basic/src/main/net/dpml basic/src/main/net/dpml/planet basic/src/main/net/dpml/planet/transitionmachine basic/src/main/net/dpml/planet/transitionmachine/basic demo demo/trafficlight demo/trafficlight/src demo/trafficlight/src/main demo/trafficlight/src/main/net demo/trafficlight/src/main/net/dpml demo/trafficlight/src/main/net/dpml/planet demo/trafficlight/src/main/net/dpml/planet/demo demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight
- Date: Mon, 20 Dec 2004 23:42:04 +0100
Author: niclas
Date: Mon Dec 20 23:42:03 2004
New Revision: 1251
Added:
development/laboratory/planet/components/transitionmachine/
development/laboratory/planet/components/transitionmachine/api/
development/laboratory/planet/components/transitionmachine/api/src/
development/laboratory/planet/components/transitionmachine/api/src/main/
development/laboratory/planet/components/transitionmachine/api/src/main/net/
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/State.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/Transition.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionEvent.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionListener.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionMachine.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionRule.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/basic/
development/laboratory/planet/components/transitionmachine/basic/src/
development/laboratory/planet/components/transitionmachine/basic/src/main/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/CommandRule.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/StateImpl.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionImpl.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionMachineImpl.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/demo/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/Ticker.java
(contents, props changed)
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/TrafficLight.java
(contents, props changed)
Log:
A first cut of a type of state machine.
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/State.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/State.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+import java.util.Locale;
+import java.io.Serializable;
+
+/**
+ * This interface represents each State internal in the TransitionMachine.
+ * <p/>
+ * Each StateImpl is defined as a Metro component, and has an Identity
(machine readable), a Name
+ * and a Description, which are both human-readable in the caller's Locale.
+ * <p/>
+ * The Name and Description is read from the java.util.ResourceBundle named
+ * "net.dpml.transitionmachine.Resources", and can be either a properties
file, or a
+ * net.dpml.i18n.BundleHandler.
+ * <p/>
+ * For each "identity", i.e. each StateImpl, you need to have two resource
strings, with the same
+ * name as the identity, prepended with ".name" and ".description"
respectively. Example;
+ * <p/>
+ * <code><pre>
+ * normal.name=Normal
+ * normal.description=This is the Normal state, where it is waiting for a
command.
+ * print.name=Printing
+ * print.description=In this state, the printing rendering is in progress.
It will leave \
+ * this state when all data has been delivered to the output buffers.
+ * error.name=Failure
+ * error.description=Something has gone wrong and it is set into an error
state. Reset command
+ * required.
+ * </pre></code>
+ */
+public interface State extends Serializable
+{
+
+ /**
+ * Return the Identity of this State.
+ * <p/>
+ * The Identity is the machine-readable name of the State, and should
not be used for any
+ * human-representation other than for developers.
+ *
+ * @return The Identity of this State.
+ */
+ String getIdentity();
+
+ /**
+ * Return the human-readable name of this State.
+ *
+ * @param locale The java.util.Locale to return the name in.
+ *
+ * @return A human-readable name in the requested Locale, or the default
Locale if null is
+ * passed in as the locale argument.
+ */
+ String getName( Locale locale );
+
+ /**
+ * Return the human-readable description of this State.
+ *
+ * @param locale The java.util.Locale to return the description in.
+ *
+ * @return A human-readable description in the requested Locale, or the
default Locale if null
+ * is passed in as the locale argument.
+ */
+ String getDescription( Locale locale );
+
+}
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/Transition.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/Transition.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+import java.util.Locale;
+import java.io.Serializable;
+
+/**
+ * This class represents each Transition coupling between the States in the
TransitionMachine.
+ * <p/>
+ * Each StateImpl is defined as a Metro component, and has an Identity
(machine readable), a Name
+ * and a Description, which are both human-readable in the caller's Locale.
+ * <p/>
+ * The Name and Description is read from the java.util.ResourceBundle named
+ * "net.dpml.transitionmachine.Resources", and can be either a properties
file, or a
+ * net.dpml.i18n.BundleHandler.
+ * <p/>
+ * For each "identity", i.e. each StateImpl, you need to have two resource
strings, with the same
+ * name as the identity, prepended with ".name" and ".description"
respectively. Example;
+ * <p/>
+ * <code><pre>
+ * reset.name=Reset
+ * reset.description=Resets Error conditions back to Normal state.
+ * startprint.name=Start Printing
+ * startprint.description=Printing process is started.
+ * finishedprint.name=Print Finished
+ * finishedprint.description=Printing has finished, and it will return to
normal state.
+ * </pre></code>
+ *
+ * @metro.service type="net.dpml.transitionmachine.Transition" version="1.0"
+ */
+public interface Transition extends Serializable
+{
+ TransitionRule getTransitionRule();
+
+ State getBeginState();
+
+ State getEndState();
+
+ String getIdentity();
+
+ String getName( Locale locale );
+
+ String getDescription( Locale locale );
+
+ void addTransitionListener( TransitionListener listener );
+
+ void removeTransitionListener( TransitionListener listener );
+}
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionEvent.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionEvent.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+import java.util.Date;
+import java.io.Serializable;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+
+/**
+ * TransitionEvent is sent whenever a Transition occurs.
+ */
+public class TransitionEvent
+ implements Serializable
+{
+ static final long serialVersionUID = 1L;
+
+ private Transition m_transition;
+ private TransitionMachine m_transitionMachine;
+ private Object m_source;
+ private Date m_TransitionDate;
+
+ /**
+ * Returns the Transition instance that has just been transited.
+ *
+ * @return the Transition instance.
+ */
+ public Transition getTransition()
+ {
+ return m_transition;
+ }
+
+ /**
+ * Returns the TransitionMachine from where the Transition took place.
+ *
+ * @return the TransitionMachine instance in which the Transition
occurred.
+ */
+ public TransitionMachine getTransitionMachine()
+ {
+ return m_transitionMachine;
+ }
+
+ /**
+ * Returns the Object that triggered the Transition from outside the
TransitionMachine.
+ *
+ * @return the Object that was passed as the <code>source</code>
argument in the
+ * <code>TransitionMachine.trigger()</code> method.
+ */
+ public Object getSource()
+ {
+ return m_source;
+ }
+
+ /**
+ * Returns the time of the Transition.
+ * <p/>
+ * For absolute clarity, the timestamp is done in this class'
constructor, and implementations
+ * are expected to instantiate an instance of this class, within a
millisecond of the Transition
+ * itself.
+ *
+ * @return the time when the Transition was made.
+ */
+ public Date getTransitionDate()
+ {
+ return m_TransitionDate;
+ }
+
+ /** Normal constructor.
+ * <p/>
+ * NOTE: Only TransitionMachine implementations should be instantiating
TransitionEvents.
+ *
+ * @param source The source that is passed to the
<code>TransitionMachine.trigger()</code> method.
+ * @param transition The Transition that was performed.
+ * @param transitionMachine The TransitionMachine instance where the
Transition was performed.
+ */
+ public TransitionEvent(
+ Object source, Transition transition, TransitionMachine
transitionMachine )
+ {
+ m_transition = transition;
+ m_transitionMachine = transitionMachine;
+ m_source = source;
+ m_TransitionDate = new Date();
+ }
+
+ /** Overridden equals() method.
+ *
+ * Two TransitionEvent instances can be equal() even though they are not
the same JVM instance,
+ * by the means of Serialization. By overriding the equals() and
hashCode() methods we ensure
+ * that TransitionEvents can be serialized and yet compared properly.
+ * @param other the object to compare against.
+ * @return true if this TransitionEvent is the exact same event as the
argument <code>other</code>.
+ */
+ public boolean equals( Object other )
+ {
+ if( this == other )
+ {
+ return true;
+ }
+ if( !( other instanceof TransitionEvent ) )
+ {
+ return false;
+ }
+
+ final TransitionEvent event = (TransitionEvent) other;
+
+ if( !m_source.equals( event.m_source ) )
+ {
+ return false;
+ }
+ if( !m_TransitionDate.equals( event.m_TransitionDate ) )
+ {
+ return false;
+ }
+ if( !m_transitionMachine.equals( event.m_transitionMachine ) )
+ {
+ return false;
+ }
+ if( !m_transition.equals( event.m_transition ) )
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /** Overridden hashCode() method.
+ *
+ * Two TransitionEvent instances can be equal() even though they are
not the same JVM instance,
+ * by the means of Serialization. By overriding the equals() and
hashCode() methods we ensure
+ * that TransitionEvents can be serialized and yet compared properly.
+ *
+ * @return a hash value.
+ */
+ public int hashCode()
+ {
+ int result;
+ result = m_transition.hashCode();
+ result = 29 * result + m_transitionMachine.hashCode();
+ result = 29 * result + m_source.hashCode();
+ result = 29 * result + m_TransitionDate.hashCode();
+ return result;
+ }
+
+ // Serialization overrides.
+
+ /** This method is used by the object serialization framework, and is
accessed through
+ * reflection. See the Serialization specification for details.
+ * @param in The ObjectInputStream to read the serialized values of this
instance from.
+ * @throws IOException when an underlying IO problem occurs.
+ * @throws ClassNotFoundException if the Class referenced in the
ObjectInputStream is not available
+ * to the ClassLoader of this Class.
+ */
+ private void readObject( ObjectInputStream in )
+ throws IOException, ClassNotFoundException
+ {
+ m_transition = (Transition) in.readObject();
+ m_TransitionDate = (Date) in.readObject();
+ Object placeholder = in.readObject();
+ if( placeholder instanceof TransitionMachine )
+ m_transitionMachine = (TransitionMachine) placeholder;
+ placeholder = in.readObject();
+ if( ! (placeholder instanceof Boolean ) )
+ m_source = placeholder;
+ }
+
+ /** This method is used by the object serialization framework, and is
accessed through
+ * reflection. See the Serialization specification for details.
+ * @param out The ObjectOutputStream to write this object to.
+ * @throws IOException when an underlying IO problem occurs.
+ */
+ private void writeObject( ObjectOutputStream out )
+ throws IOException
+ {
+ out.writeObject( m_transition );
+ out.writeObject( m_TransitionDate );
+ if( m_transitionMachine instanceof Serializable )
+ {
+ out.writeObject( m_transitionMachine );
+ }
+ else
+ out.writeObject( Boolean.FALSE );
+ if( m_source instanceof Serializable )
+ out.writeObject( m_source );
+ else
+ out.writeObject( Boolean.FALSE );
+ }
+
+}
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionListener.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionListener.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+/**
+ * The TransitionListener register itself with the TransitionMachine, and
normally performs something
+ * upon a Transition occurring.
+ * <p/>
+ * Only events for Transitions that has been registered at the
TransitionMachine will be fired to the
+ * TransitionListener. If all events are requested, a null argument should
have been passed to the
+ * <code>TransitionMachine.addTransitionListener()</code> method. </p>
+ */
+public interface TransitionListener
+{
+ void transition( TransitionEvent event );
+}
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionMachine.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionMachine.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+import java.util.Collection;
+
+/** The TransitionMachine interface.
+ * <p/>
+ * A TransitionMachine is essentially equal to a StateMachine, except that
it is Transition centric
+ * instead of State centric. You can find this by examining the methods
below.
+ * <p/>
+ * The TransitionMachine abstraction assumes that it is completely
re-programmable on the fly,
+ * but requires that the client <code>stop()</code> the TransitionMachine
prior to changing its
+ * setup. This leaves the configuration method open, not only to the
implementation itself, but
+ * external setup/configuration tools.
+ * <p/>
+ *
+ * @metro.service type="net.dpml.transitionmachine.TransitionMachine"
version="1.0"
+ */
+public interface TransitionMachine
+{
+
+ final String BUNDLE_NAME = "net.dpml.transitionmachine.Resources";
+
+ /**
+ * Trigger a change in the TransitionMachine.
+ * <p/>
+ * The
+ * @param commandString The command string to be evaluated.
+ * @param source The object that will be marked as the source of the
Transition in events fired.
+ * This object is preferred to be <code>java.io.Serializable</code>, to
work properly in a
+ * distributed environment.
+ */
+ void trigger( Object source, String commandString );
+
+ void addTransition( Transition transition );
+
+ void addTransitions( Collection transitions );
+
+ void removeTransition( Transition transition );
+
+ void removeAllTransitions();
+
+ void addTransitionListener( TransitionListener listener, String
transitionId );
+
+ void removeTransitionListener( TransitionListener listener, String
transitionId );
+
+ void removeTransitionListener( TransitionListener listener );
+
+ void start();
+
+ void stop();
+
+ void reset( State initialState );
+}
Added:
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionRule.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/api/src/main/net/dpml/transitionmachine/TransitionRule.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.transitionmachine;
+
+import java.io.Serializable;
+
+/**
+ * A TransitionRule is associated with a Transition.
+ * <p/>
+ * The TransitionMachine will pass the commandString to each TransitionRule,
in priority order, and
+ * perform the Transition of the first TransitionRule that returns
<code>true</code> when the
+ * <code>isTransition</code> method is called. A lower priority number
indicates higher priority,
+ * i.e. the TransitionRule list is sorted in numerical order and iterated
from low towards higher
+ * numbers. TransitionRule extends java.lang.Comparable and SHOULD use
<code>getPriority()</code>
+ * to compare with, even if more elaborate priority schemes are employed.
+ * </p>
+ *
+ * @metro.service type="net.dpml.transitionmachine.TransitionRule"
version="1.0"
+ */
+public interface TransitionRule extends Comparable, Serializable
+{
+
+ /**
+ * Returns whether the command string corresponds to this transition
rule.
+ *
+ * @param commandString An command string passed to the
TransitionMachine.
+ *
+ * @return true if the Transition defined by this TransitionRule should
be performed, false
+ * otherwise.
+ */
+ boolean isTransition( String commandString );
+
+ int getPriority();
+}
Added:
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/CommandRule.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/CommandRule.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.planet.transitionmachine.basic;
+
+import net.dpml.transitionmachine.TransitionRule;
+import net.dpml.parameters.Parameters;
+import net.dpml.parameters.ParameterException;
+
+/**
+ * The CommandRule is a plain rule, that checks if the Command string is of
a particular and
+ * exact value.
+ * <p/>
+ * The generic CommandRule should be enough for most situations, unless
dynamically user
+ * re-programmable statemachines are needed.
+ *
+ * @metro.component name="CommandRule" lifestyle="singleton"
collection="hard"
+ * @metro.service type="net.dpml.transitionmachine.TransitionRule"
+ */
+public class CommandRule
+ implements TransitionRule
+{
+ private String m_command;
+ private int m_priority;
+ private boolean m_ignoreCase;
+
+ public CommandRule( Parameters params )
+ throws ParameterException
+ {
+ this( params.getParameter( "command" ),
+ params.getParameterAsInteger( "priority", 100 ),
+ params.getParameterAsBoolean( "ignore-case", false ));
+ }
+
+ public CommandRule( String command, int priority, boolean ignorecase )
+ {
+ m_command = command;
+ m_priority = priority;
+ m_ignoreCase = ignorecase;
+ }
+
+ /**
+ * Returns whether the command string corresponds to this transition
rule.
+ *
+ * @param commandString An command string passed to the
TransitionMachine.
+ *
+ * @return true if the Transition defined by this TransitionRule should
be performed, false
+ * otherwise.
+ */
+ public boolean isTransition( String commandString )
+ {
+ if( m_ignoreCase )
+ {
+ return m_command.equalsIgnoreCase( commandString );
+ }
+ return m_command.equals( commandString );
+ }
+
+ public int getPriority()
+ {
+ return m_priority;
+ }
+
+ /**
+ * Compares this object with the specified object for order. Returns a
negative integer, zero,
+ * or a positive integer as this object is less than, equal to, or
greater than the specified
+ * object.<p>
+ * <p/>
+ * In the foregoing description, the notation
<tt>sgn(</tt><i>expression</i><tt>)</tt>
+ * designates the mathematical <i>signum</i> function, which is defined
to return one of
+ * <tt>-1</tt>, <tt>0</tt>, or <tt>1</tt> according to whether the value
of <i>expression</i> is
+ * negative, zero or positive.
+ * <p/>
+ * The implementor must ensure <tt>sgn(x.compareTo(y)) ==
-sgn(y.compareTo(x))</tt> for all
+ * <tt>x</tt> and <tt>y</tt>. (This implies that
<tt>x.compareTo(y)</tt> must throw an
+ * exception iff <tt>y.compareTo(x)</tt> throws an exception.)<p>
+ * <p/>
+ * The implementor must also ensure that the relation is transitive:
<tt>(x.compareTo(y)>0
+ * && y.compareTo(z)>0)</tt> implies
<tt>x.compareTo(z)>0</tt>.<p>
+ * <p/>
+ * Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt>
implies that
+ * <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for all
<tt>z</tt>.<p>
+ * <p/>
+ * It is strongly recommended, but <i>not</i> strictly required that
<tt>(x.compareTo(y)==0) ==
+ * (x.equals(y))</tt>. Generally speaking, any class that implements
the <tt>Comparable</tt>
+ * interface and violates this condition should clearly indicate this
fact. The recommended
+ * language is "Note: this class has a natural ordering that is
inconsistent with equals."
+ *
+ * @param o the Object to be compared.
+ *
+ * @return a negative integer, zero, or a positive integer as this
object is less than, equal
+ * to, or greater than the specified object.
+ *
+ * @throws ClassCastException if the specified object's type prevents it
from being compared to
+ * this Object.
+ */
+ public int compareTo( Object o )
+ {
+ if( o instanceof TransitionRule )
+ {
+ TransitionRule rule = (TransitionRule) o;
+ int p = rule.getPriority();
+ if( p == m_priority )
+ return 0;
+ if( p > m_priority )
+ return -1;
+ else
+ return 1;
+ }
+ return -1;
+ }
+}
Added:
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/StateImpl.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/StateImpl.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.planet.transitionmachine.basic;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+import net.dpml.transitionmachine.State;
+import net.dpml.transitionmachine.TransitionMachine;
+
+/**
+ * This class represents each StateImpl internal in the TransitionMachine.
+ * <p/>
+ * <p/>
+ * Each StateImpl is defined as a Metro component, and has an Identity
(machine readable), a Name
+ * and a Description, which are both human-readable in the caller's Locale.
</p>
+ * <p/>
+ * The Name and Description is read from the java.util.ResourceBundle named
+ * "net.dpml.transitionmachine.Resources", and can be either a properties
file, or a
+ * net.dpml.i18n.BundleHandler. </p>
+ * <p/>
+ * For each "identity", i.e. each StateImpl, you need to have two resource
strings, with the same
+ * name as the identity, prepended with ".name" and ".description"
respectively. Example; </p>
+ * <code><pre>
+ * normal.name=Normal
+ * normal.description=This is the Normal state, where it is waiting for a
command.
+ * print.name=Printing
+ * print.description=In this state, the printing rendering is in progress.
It will leave \
+ * this state when all data has been delivered to the output buffers.
+ * error.name=Failure
+ * error.description=Something has gone wrong and it is set into an error
state. Reset command
+ * required.
+ * </pre></code>
+ */
+public class StateImpl implements State
+{
+
+ private String m_identity;
+
+ public StateImpl( Parameters params )
+ throws ParameterException
+ {
+ m_identity = params.getParameter( "identity" );
+ }
+
+ public String getIdentity()
+ {
+ return m_identity;
+ }
+
+ public String getName( Locale locale )
+ {
+ ResourceBundle bundle = ResourceBundle.getBundle(
TransitionMachine.BUNDLE_NAME, locale );
+ return bundle.getString( m_identity + ".name" );
+ }
+
+ public String getDescription( Locale locale )
+ {
+ ResourceBundle bundle = ResourceBundle.getBundle(
TransitionMachine.BUNDLE_NAME, locale );
+ return bundle.getString( m_identity + ".description" );
+ }
+}
Added:
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionImpl.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionImpl.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,189 @@
+
+package net.dpml.planet.transitionmachine.basic;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.ArrayList;
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+import net.dpml.service.ServiceException;
+import net.dpml.service.ServiceManager;
+import net.dpml.transitionmachine.Transition;
+import net.dpml.transitionmachine.State;
+import net.dpml.transitionmachine.TransitionMachine;
+import net.dpml.transitionmachine.TransitionRule;
+import net.dpml.transitionmachine.TransitionListener;
+
+/**
+ * This class represents each StateImpl internal in the TransitionMachine.
+ * <p/>
+ * <p/>
+ * Each StateImpl is defined as a Metro component, and has an Identity
(machine readable), a Name
+ * and a Description, which are both human-readable in the caller's Locale.
</p>
+ * <p/>
+ * The Name and Description is read from the java.util.ResourceBundle named
+ * "net.dpml.transitionmachine.Resources", and can be either a properties
file, or a
+ * net.dpml.i18n.BundleHandler. </p>
+ * <p/>
+ * For each "identity", i.e. each StateImpl, you need to have two resource
strings, with the same
+ * name as the identity, prepended with ".name" and ".description"
respectively. Example; </p>
+ * <code><pre>
+ * reset.name=Reset
+ * reset.description=Resets Error conditions back to Normal state.
+ * startprint.name=Start Printing
+ * startprint.description=Printing process is started.
+ * finishedprint.name=Print Finished
+ * finishedprint.description=Printing has finished, and it will return to
normal state.
+ * </pre></code>
+ *
+ * @metro.component name="transition" lifestyle="transient" collection="hard"
+ */
+public class TransitionImpl implements Transition
+{
+ private State m_beginState;
+ private State m_endState;
+ private String m_Identity;
+ private TransitionRule m_Rule;
+ private ArrayList m_listeners;
+
+ /**
+ * @param man The ServiceManager for this component.
+ * @param params Parameters for this component.
+ *
+ * @throws ParameterException if a "identity" parameter is not
specified, which is used as key
+ * in ResourceBundle lookups.
+ * @throws ServiceException if the "begin" and "end" StateImpl
dependencies can not be
+ * satisfied.
+ * @metro.dependency key="begin" type="net.dpml.transitionmachine.State"
+ * @metro.dependency key="end" type="net.dpml.transitionmachine.State"
+ * @metro.dependency key="sm"
type="net.dpml.transitionmachine.TransitionMachine"
+ * @metro.dependency key="rule"
type="net.dpml.transitionmachine.TransitionRule"
+ */
+ public TransitionImpl( ServiceManager man, Parameters params )
+ throws ParameterException, ServiceException
+ {
+ String identity = params.getParameter( "identity" );
+ m_Identity = identity;
+ State beginState = (State) man.lookup( "begin" );
+ m_beginState = beginState;
+ State endState = (State) man.lookup( "end" );
+ m_endState = endState;
+ TransitionMachine sm = (TransitionMachine) man.lookup( "sm" );
+ sm.addTransition( this );
+ m_Rule = (TransitionRule) man.lookup( "rule" );
+
+ m_listeners = new ArrayList();
+ }
+
+ /**
+ * Non-Metro constructor.
+ *
+ * @param beginState The StateImpl from which this Transition starts at.
+ * @param endState The StateImpl to where this Transition leads to.
+ * @param identity The Identity of this Transition.
+ */
+ public TransitionImpl( State beginState, State endState, String identity
)
+ {
+ if( beginState == null )
+ {
+ throw new NullPointerException( "Null arguments not allowed:
beginState" );
+ }
+ if( endState == null )
+ {
+ throw new NullPointerException( "Null arguments not allowed:
endState" );
+ }
+ if( identity == null )
+ {
+ throw new NullPointerException( "Null arguments not allowed:
identity" );
+ }
+ m_beginState = beginState;
+ m_endState = endState;
+ m_Identity = identity;
+ }
+
+ public TransitionRule getTransitionRule()
+ {
+ return m_Rule;
+ }
+
+ public State getBeginState()
+ {
+ return m_beginState;
+ }
+
+ public State getEndState()
+ {
+ return m_endState;
+ }
+
+ public String getIdentity()
+ {
+ return m_Identity;
+ }
+
+ public String getName( Locale locale )
+ {
+ ResourceBundle bundle = ResourceBundle.getBundle(
TransitionMachine.BUNDLE_NAME, locale );
+ return bundle.getString( m_Identity + ".name" );
+ }
+
+ public String getDescription( Locale locale )
+ {
+ ResourceBundle bundle = ResourceBundle.getBundle(
TransitionMachine.BUNDLE_NAME, locale );
+ return bundle.getString( m_Identity + ".description" );
+ }
+
+ public void addTransitionListener( TransitionListener listener )
+ {
+ synchronized( m_listeners )
+ {
+ m_listeners.add( listener );
+ }
+ }
+
+ public void removeTransitionListener( TransitionListener listener )
+ {
+ synchronized( m_listeners )
+ {
+ m_listeners.add( listener );
+ }
+ }
+
+ public boolean equals( Object o )
+ {
+ if( this == o )
+ {
+ return true;
+ }
+ if( !( o instanceof TransitionImpl ) )
+ {
+ return false;
+ }
+
+ final TransitionImpl transition = (TransitionImpl) o;
+
+ if( !m_beginState.equals( transition.m_beginState ) )
+ {
+ return false;
+ }
+ if( !m_endState.equals( transition.m_endState ) )
+ {
+ return false;
+ }
+ if( !m_Identity.equals( transition.m_Identity ) )
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int result;
+ result = m_beginState.hashCode();
+ result = ( result >>> 11 ) + m_endState.hashCode();
+ result = ( result >>> 17 ) + m_Identity.hashCode();
+ return result;
+ }
+}
Added:
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionMachineImpl.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/basic/src/main/net/dpml/planet/transitionmachine/basic/TransitionMachineImpl.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 20, 2004
+ *
+ * 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 net.dpml.planet.transitionmachine.basic;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.TreeSet;
+import java.util.WeakHashMap;
+import net.dpml.activity.Startable;
+import net.dpml.transitionmachine.State;
+import net.dpml.transitionmachine.Transition;
+import net.dpml.transitionmachine.TransitionListener;
+import net.dpml.transitionmachine.TransitionMachine;
+import net.dpml.transitionmachine.TransitionRule;
+
+/**
+ * Basic TransitionMachine implementation.
+ *
+ * @metro.component name="basic-transitionmachine" lifestyle="singleton"
collection="hard"
+ * @metro.service type="net.dpml.transitionmachine.TransitionMachine"
+ */
+public class TransitionMachineImpl
+ implements TransitionMachine, Startable
+{
+ private HashMap m_transitions;
+ private WeakHashMap m_crossIndex;
+ private boolean m_running;
+ private State m_currentState;
+
+ public TransitionMachineImpl()
+ {
+ m_transitions = new HashMap();
+ m_crossIndex = new WeakHashMap();
+ }
+
+ /**
+ * Trigger a change in the TransitionMachine.
+ *
+ * @param commandString The command string to be evaluated.
+ */
+ public void trigger( String commandString )
+ {
+ synchronized( this )
+ {
+ Collection bucket = (Collection) m_crossIndex.get(
m_currentState );
+ Iterator iterator = bucket.iterator();
+ while( iterator.hasNext() )
+ {
+ Transition transition = (Transition) iterator.next();
+ TransitionRule rule = transition.getTransitionRule();
+ if( rule.isTransition( commandString ) )
+ {
+ m_currentState = transition.getEndState();
+ break;
+ }
+ }
+ }
+ }
+
+ public void addTransition( Transition transition )
+ {
+ if( m_running )
+ throw new IllegalStateException(
+ "Unable to modify the State Machine while it is running."
+ );
+ synchronized( this )
+ {
+ String id = transition.getIdentity();
+ m_transitions.put( id, transition );
+ State begin = transition.getBeginState();
+ Collection bucket = (Collection) m_crossIndex.remove( begin );
+ if( bucket == null )
+ bucket = new TreeSet();
+ bucket.add( transition );
+ m_crossIndex.put( begin, bucket );
+ }
+ }
+
+ public void addTransitions( Collection transitions )
+ {
+ Iterator iterator = transitions.iterator();
+ while( iterator.hasNext() )
+ {
+ Transition transition = (Transition) iterator.next();
+ removeTransition( transition );
+ }
+ }
+
+ public void removeTransition( Transition transition )
+ {
+ if( m_running )
+ throw new IllegalStateException(
+ "Unable to modify the State Machine while it is running."
+ );
+ synchronized( this )
+ {
+ String id = transition.getIdentity();
+ m_transitions.remove( id );
+ State begin = transition.getBeginState();
+ Collection bucket = (Collection) m_crossIndex.remove( begin );
+ bucket.remove( transition );
+ if( bucket.size() > 0 )
+ m_crossIndex.put( begin, bucket );
+ }
+ }
+
+ public void removeAllTransitions()
+ {
+ synchronized( this )
+ {
+ m_transitions.clear();
+ m_crossIndex.clear();
+ }
+ }
+
+ public void addTransitionListener( TransitionListener listener, String
transitionId )
+ {
+ synchronized( this )
+ {
+ Transition t = (Transition) m_transitions.get( transitionId );
+ t.addTransitionListener( listener );
+ }
+ }
+
+ public void removeTransitionListener( TransitionListener listener,
String transitionId )
+ {
+ synchronized( this )
+ {
+ Transition t = (Transition) m_transitions.get( transitionId );
+ t.removeTransitionListener( listener );
+ }
+ }
+
+ public void removeTransitionListener( TransitionListener listener )
+ {
+ Iterator iter = m_transitions.keySet().iterator();
+ while( iter.hasNext() )
+ {
+ Transition transition = (Transition) iter.next();
+ transition.removeTransitionListener( listener );
+ }
+ }
+
+ public void start()
+ {
+ m_running = true;
+ }
+
+ public void stop()
+ {
+ m_running = false;
+ }
+
+ public void reset( State initialState )
+ {
+ synchronized( this )
+ {
+ m_currentState = initialState;
+ }
+ }
+}
Added:
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/Ticker.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/Ticker.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 21, 2004
+ *
+ * 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 net.dpml.planet.demo.trafficlight;
+
+import net.dpml.transitionmachine.TransitionEvent;
+
+/**
+ * TODO: automatically generated. Description required.
+ *
+ * @metro.component name="Ticker" lifestyle="singleton" collection="hard"
+ * @metro.service type="net.dpml.transitionmachine.TransitionListener"
version="1.0"
+ */
+public class Ticker
+ implements net.dpml.transitionmachine.TransitionListener, Runnable
+{
+
+ public void transition( TransitionEvent event )
+ {
+
+ }
+}
Added:
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/TrafficLight.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/transitionmachine/demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight/TrafficLight.java
Mon Dec 20 23:42:03 2004
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2004 Niclas Hedhman.
+ * Created: Dec 21, 2004
+ *
+ * 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 net.dpml.planet.demo.trafficlight;
+
+import java.util.ArrayList;
+import java.io.Serializable;
+import net.dpml.activity.Startable;
+import net.dpml.parameters.Parameters;
+import net.dpml.service.ServiceException;
+import net.dpml.service.ServiceManager;
+import net.dpml.transitionmachine.Transition;
+import net.dpml.transitionmachine.TransitionMachine;
+import net.dpml.transitionmachine.State;
+
+/**
+ * This TrafficLight implements a traffic light, by cycling the states
according to the selected
+ * system, either 'international' or 'old swedish'.
+ * <p/>
+ * <p/>
+ * This demo shows how to change the transitions in order to achieve a
different sequence.
+ * <p/>
+ *
+ * @metro.component name="TrafficLight" lifestyle="singleton"
collection="hard"
+ */
+public class TrafficLight
+ implements Runnable, Startable, Serializable
+{
+
+ private TransitionMachine m_transitionMachine;
+ private ArrayList m_internationalTransitions;
+ private ArrayList m_swedishTransitions;
+ private Thread m_thread;
+ private boolean m_run;
+ private State m_red;
+
+ /**
+ * @param man ServiceManager servicing this Part.
+ *
+ * @throws ServiceException if any of the required dependencies can not
be satisfied.
+ * @metro.dependency type="net.dpml.transitionmachine.TransitionMachine"
key="tm"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="red-to-green"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="green-to-yellow"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="yellow-to-red"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="red-to-redyellow"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="redyellow-to-green"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="green-to-greenyellow"
+ * @metro.dependency type="net.dpml.transitionmachine.Transition"
key="greenyellow-to-red"
+ */
+ public TrafficLight( ServiceManager man, Parameters params )
+ throws ServiceException
+ {
+ m_transitionMachine = (TransitionMachine) man.lookup( "tm" );
+
+ m_internationalTransitions = new ArrayList();
+ m_swedishTransitions = new ArrayList();
+
+ Transition t = (Transition) man.lookup( "red-to-green" );
+ m_red = t.getBeginState();
+ m_internationalTransitions.add( t );
+ t = (Transition) man.lookup( "yellow-to-red" );
+ m_internationalTransitions.add( t );
+ t = (Transition) man.lookup( "green-to-yellow" );
+ m_internationalTransitions.add( t );
+
+ t = (Transition) man.lookup( "red-to-redyellow" );
+ m_swedishTransitions.add( t );
+ t = (Transition) man.lookup( "redyellow-to-green" );
+ m_swedishTransitions.add( t );
+ t = (Transition) man.lookup( "green-to-greenyellow" );
+ m_swedishTransitions.add( t );
+ t = (Transition) man.lookup( "greenyellow-to-red" );
+ m_swedishTransitions.add( t );
+
+ international();
+ }
+
+ private TrafficLight()
+ {
+ // Constructor required by Serialization.
+ }
+
+ /**
+ * Sets international sequencing system.
+ */
+ public void international()
+ {
+ m_transitionMachine.stop();
+ m_transitionMachine.removeAllTransitions();
+ m_transitionMachine.addTransitions( m_internationalTransitions );
+ m_transitionMachine.start();
+ }
+
+ /**
+ * Sets international sequencing system.
+ */
+ public void swedish()
+ {
+ m_transitionMachine.stop();
+ m_transitionMachine.removeAllTransitions();
+ m_transitionMachine.addTransitions( m_swedishTransitions );
+ m_transitionMachine.start();
+ }
+
+ public void start()
+ {
+ m_thread = new Thread( this );
+ m_thread.start();
+ }
+
+ public void stop()
+ {
+ m_run = false;
+ m_thread.interrupt();
+ }
+
+ public void run()
+ {
+ m_run = true;
+ String command = "tick";
+ m_transitionMachine.reset( m_red );
+ try
+ {
+ while( m_run )
+ {
+ synchronized( this )
+ {
+ wait();
+ m_transitionMachine.trigger( this, command );
+ }
+ }
+ } catch( InterruptedException e )
+ {
+ }
+ }
+}
- svn commit: r1251 - in development/laboratory/planet/components/transitionmachine: . api api/src api/src/main api/src/main/net api/src/main/net/dpml api/src/main/net/dpml/transitionmachine basic basic/src basic/src/main basic/src/main/net basic/src/main/net/dpml basic/src/main/net/dpml/planet basic/src/main/net/dpml/planet/transitionmachine basic/src/main/net/dpml/planet/transitionmachine/basic demo demo/trafficlight demo/trafficlight/src demo/trafficlight/src/main demo/trafficlight/src/main/net demo/trafficlight/src/main/net/dpml demo/trafficlight/src/main/net/dpml/planet demo/trafficlight/src/main/net/dpml/planet/demo demo/trafficlight/src/main/net/dpml/planet/demo/trafficlight, niclas, 12/20/2004
Archive powered by MHonArc 2.6.24.