notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
svn commit: r1562 - in development/laboratory/planet/components/fsm: . api/src/main/net/dpml/fsm basic/src/main/net/dpml/fsm/basic basic/src/test/net/dpml/fsm/basic/test demo/trafficlight demo/trafficlight/src/main/net/dpml/fsm demo/trafficlight/src/main/net/dpml/fsm/trafficlight
- From: niclas AT netcompartner.com
- To: notify-dpml AT lists.ibiblio.org
- Subject: svn commit: r1562 - in development/laboratory/planet/components/fsm: . api/src/main/net/dpml/fsm basic/src/main/net/dpml/fsm/basic basic/src/test/net/dpml/fsm/basic/test demo/trafficlight demo/trafficlight/src/main/net/dpml/fsm demo/trafficlight/src/main/net/dpml/fsm/trafficlight
- Date: Sun, 23 Jan 2005 13:45:32 +0100
Author: niclas
Date: Sun Jan 23 13:45:32 2005
New Revision: 1562
Added:
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/Resouces.properties
(contents, props changed)
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/trafficlight/Timer.java
(contents, props changed)
Removed:
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/trafficlight/TrafficLight.java
Modified:
development/laboratory/planet/components/fsm/api/src/main/net/dpml/fsm/TransitionEvent.java
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateComponent.java
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateMachineImpl.java
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/TransitionComponent.java
development/laboratory/planet/components/fsm/basic/src/test/net/dpml/fsm/basic/test/StateTestCase.java
development/laboratory/planet/components/fsm/demo/trafficlight/build.xml
development/laboratory/planet/components/fsm/index.xml
Log:
Forgot to support 'null' in the StateMachine.addTransitionListener() and also
simplifying the TrafficLight example.
Modified:
development/laboratory/planet/components/fsm/api/src/main/net/dpml/fsm/TransitionEvent.java
==============================================================================
---
development/laboratory/planet/components/fsm/api/src/main/net/dpml/fsm/TransitionEvent.java
(original)
+++
development/laboratory/planet/components/fsm/api/src/main/net/dpml/fsm/TransitionEvent.java
Sun Jan 23 13:45:32 2005
@@ -179,10 +179,14 @@
m_TransitionDate = (Date) in.readObject();
Object placeholder = in.readObject();
if( placeholder instanceof StateMachine )
+ {
m_stateMachine = (StateMachine) placeholder;
+ }
placeholder = in.readObject();
if( ! (placeholder instanceof Boolean ) )
+ {
m_source = placeholder;
+ }
}
/** This method is used by the object serialization framework, and is
@@ -204,11 +208,18 @@
out.writeObject( m_stateMachine );
}
else
+ {
out.writeObject( Boolean.FALSE );
+ }
+
if( m_source instanceof Serializable )
+ {
out.writeObject( m_source );
+ }
else
+ {
out.writeObject( Boolean.FALSE );
+ }
}
}
Modified:
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateComponent.java
==============================================================================
---
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateComponent.java
(original)
+++
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateComponent.java
Sun Jan 23 13:45:32 2005
@@ -26,7 +26,8 @@
import net.dpml.lang.NullArgumentException;
-import net.dpml.parameters.Parameters;
+import net.dpml.context.Context;
+import net.dpml.context.ContextException;
/**
* This class represents each State internal in the StateMachine.
@@ -54,10 +55,13 @@
public class StateComponent extends StateImpl
implements State
{
- public StateComponent( Parameters params )
- throws NullArgumentException
+ /**
+ * @metro.entry key="urn:metro:name"
+ */
+ public StateComponent( Context ctx )
+ throws ContextException
{
- super( params.getParameter( "identity", null ) );
+ super( (String) ctx.get( "urn:metro:name" ) );
}
}
Modified:
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateMachineImpl.java
==============================================================================
---
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateMachineImpl.java
(original)
+++
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/StateMachineImpl.java
Sun Jan 23 13:45:32 2005
@@ -91,6 +91,7 @@
bucket = new TreeSet();
bucket.add( transition );
m_crossIndex.put( begin, bucket );
+System.out.println( m_crossIndex );
}
}
@@ -136,8 +137,20 @@
{
synchronized( this )
{
- Transition t = (Transition) m_transitions.get( transitionId );
- t.addTransitionListener( listener );
+ if( transitionId != null )
+ {
+ Transition t = (Transition) m_transitions.get( transitionId
);
+ t.addTransitionListener( listener );
+ }
+ else
+ {
+ Iterator list = m_transitions.values().iterator();
+ while( list.hasNext() )
+ {
+ Transition t = (Transition) list.next();
+ t.addTransitionListener( listener );
+ }
+ }
}
}
@@ -145,8 +158,20 @@
{
synchronized( this )
{
- Transition t = (Transition) m_transitions.get( transitionId );
- t.removeTransitionListener( listener );
+ if( transitionId != null )
+ {
+ Transition t = (Transition) m_transitions.get( transitionId
);
+ t.removeTransitionListener( listener );
+ }
+ else
+ {
+ Iterator list = m_transitions.values().iterator();
+ while( list.hasNext() )
+ {
+ Transition t = (Transition) list.next();
+ t.removeTransitionListener( listener );
+ }
+ }
}
}
Modified:
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/TransitionComponent.java
==============================================================================
---
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/TransitionComponent.java
(original)
+++
development/laboratory/planet/components/fsm/basic/src/main/net/dpml/fsm/basic/TransitionComponent.java
Sun Jan 23 13:45:32 2005
@@ -67,7 +67,7 @@
* type="java.lang.String"
* @metro.dependency key="begin" type="net.dpml.fsm.State"
* @metro.dependency key="end" type="net.dpml.fsm.State"
- * @metro.dependency key="fsm" type="net.dpml.fsm.StateMachine"
+ * @metro.dependency key="fsm" type="net.dpml.fsm.StateMachine"
* @metro.dependency key="rule" type="net.dpml.fsm.TransitionRule"
*/
public TransitionComponent( ServiceManager man, Context ctx, Logger
logger )
@@ -80,6 +80,7 @@
State endState = (State) man.lookup( "end" );
m_endState = endState;
StateMachine sm = (StateMachine) man.lookup( "fsm" );
+System.out.println( "ADDING!!!" );
sm.addTransition( this );
m_rule = (TransitionRule) man.lookup( "rule" );
Modified:
development/laboratory/planet/components/fsm/basic/src/test/net/dpml/fsm/basic/test/StateTestCase.java
==============================================================================
---
development/laboratory/planet/components/fsm/basic/src/test/net/dpml/fsm/basic/test/StateTestCase.java
(original)
+++
development/laboratory/planet/components/fsm/basic/src/test/net/dpml/fsm/basic/test/StateTestCase.java
Sun Jan 23 13:45:32 2005
@@ -32,7 +32,8 @@
import net.dpml.lang.NullArgumentException;
-import net.dpml.parameters.impl.DefaultParameters;
+import net.dpml.context.Context;
+import net.dpml.context.ContextException;
public class StateTestCase extends TestCase
{
@@ -51,10 +52,9 @@
public void testComponentConstructor()
throws Exception
{
- DefaultParameters params = new DefaultParameters();
- params.setParameter( "identity", "test-identity" );
+ Context ctx = new TestContext( "test-identity" );
- StateComponent state1 = new StateComponent( params );
+ StateComponent state1 = new StateComponent( ctx );
StateImpl state2 = new StateImpl( "test-identity" );
assertEquals( "equals not same for Component", state1, state2 );
}
@@ -69,16 +69,6 @@
{
// expected!
}
-
- try
- {
- DefaultParameters params = new DefaultParameters();
- StateComponent state1 = new StateComponent( params );
- fail( "NullArgumentException was expected for Component
constructor, but not thrown." );
- } catch( NullArgumentException e )
- {
- // expected !
- }
}
public void testLocale()
@@ -146,4 +136,23 @@
ois.close();
assertEquals( "serialization", state1, state2 );
}
+
+ private class TestContext
+ implements Context
+ {
+ private String m_name;
+
+ TestContext( String name )
+ {
+ m_name = name;
+ }
+
+ public Object get( Object key )
+ throws ContextException
+ {
+ if( key.equals( "urn:metro:name" ) )
+ return m_name;
+ throw new ContextException( "Context entry not found: '" + key +
"'" );
+ }
+ }
}
Modified:
development/laboratory/planet/components/fsm/demo/trafficlight/build.xml
==============================================================================
--- development/laboratory/planet/components/fsm/demo/trafficlight/build.xml
(original)
+++ development/laboratory/planet/components/fsm/demo/trafficlight/build.xml
Sun Jan 23 13:45:32 2005
@@ -13,105 +13,47 @@
<target name="build" depends="standard.build" >
<x:block name="trafficlight" >
- <x:component name="runner"
class="net.dpml.fsm.trafficlight.TrafficLight" />
-
- <x:component name="machine"
class="net.dpml.fsm.basic.StateMachineImpl" activation="false" >
+ <x:component name="timer" class="net.dpml.fsm.trafficlight.Timer" >
<x:dependencies>
- <x:dependency key="red-to-green" source="transition-red-to-green"
/>
- <x:dependency key="green-to-yellow"
source="transition-green-to-yellow" />
- <x:dependency key="yellow-to-red"
source="transition-yellow-to-red" />
- <x:dependency key="red-to-redyellow"
source="transition-red-to-redyellow" />
- <x:dependency key="redyellow-to-green"
source="transition-redyellow-to-green" />
- <x:dependency key="green-to-greenyellow"
source="transition-green-to-greenyellow" />
- <x:dependency key="greenyellow-to-red"
source="transition-greenyellow-to-red" />
+ <x:dependency key="initial-state" source="state-red" />
</x:dependencies>
</x:component>
- <x:component name="rule" class="net.dpml.fsm.basic.CommandRule" >
- <x:parameters>
- <x:parameter name="command" value="tick" />
- </x:parameters>
- </x:component>
+ <x:component name="machine"
class="net.dpml.fsm.basic.StateMachineImpl" activation="true"/>
- <x:component name="state-red" class="net.dpml.fsm.basic.StateImpl" >
+ <x:component name="rule"
class="net.dpml.fsm.basic.CommandRuleComponent" activation="true">
<x:parameters>
- <x:parameter name="identity" value="state-red" />
+ <x:parameter name="command" value="next" />
</x:parameters>
</x:component>
- <x:component name="state-redyellow"
class="net.dpml.fsm.basic.StateImpl" >
- <x:parameters>
- <x:parameter name="identity" value="state-redyellow" />
- </x:parameters>
- </x:component>
+ <x:component name="state-red"
class="net.dpml.fsm.basic.StateComponent" />
- <x:component name="state-yellow" class="net.dpml.fsm.basic.StateImpl" >
- <x:parameters>
- <x:parameter name="identity" value="state-yellow" />
- </x:parameters>
- </x:component>
+ <x:component name="state-yellow"
class="net.dpml.fsm.basic.StateComponent" />
- <x:component name="state-green" class="net.dpml.fsm.basic.StateImpl" >
- <x:parameters>
- <x:parameter name="identity" value="state-green" />
- </x:parameters>
- </x:component>
+ <x:component name="state-green"
class="net.dpml.fsm.basic.StateComponent" />
- <x:component name="state-greenyellow"
class="net.dpml.fsm.basic.StateImpl" >
- <x:parameters>
- <x:parameter name="identity" value="state-greenyellow" />
- </x:parameters>
- </x:component>
-
- <x:component name="transition-red-to-green"
class="net.dpml.fsm.basic.TransitionComponent" >
+ <x:component name="transition-red-to-green"
class="net.dpml.fsm.basic.TransitionComponent" activation="true">
<x:dependencies>
<x:dependency key="begin" source="state-red" />
<x:dependency key="end" source="state-green" />
</x:dependencies>
</x:component>
- <x:component name="transition-red-to-redyellow"
class="net.dpml.fsm.basic.TransitionComponent" >
- <x:dependencies>
- <x:dependency key="begin" source="state-red" />
- <x:dependency key="end" source="state-redyellow" />
- </x:dependencies>
- </x:component>
-
- <x:component name="transition-yellow-to-red"
class="net.dpml.fsm.basic.TransitionComponent" >
+ <x:component name="transition-yellow-to-red"
class="net.dpml.fsm.basic.TransitionComponent" activation="true">
<x:dependencies>
<x:dependency key="begin" source="state-yellow" />
<x:dependency key="end" source="state-red" />
</x:dependencies>
</x:component>
- <x:component name="transition-green-to-yellow"
class="net.dpml.fsm.basic.TransitionComponent" >
+ <x:component name="transition-green-to-yellow"
class="net.dpml.fsm.basic.TransitionComponent" activation="true">
<x:dependencies>
<x:dependency key="begin" source="state-green" />
<x:dependency key="end" source="state-yellow" />
</x:dependencies>
</x:component>
- <x:component name="transition-green-to-greenyellow"
class="net.dpml.fsm.basic.TransitionComponent" >
- <x:dependencies>
- <x:dependency key="begin" source="state-green" />
- <x:dependency key="end" source="state-greenyellow" />
- </x:dependencies>
- </x:component>
-
- <x:component name="transition-greenyellow-to-red"
class="net.dpml.fsm.basic.TransitionComponent" >
- <x:dependencies>
- <x:dependency key="begin" source="state-greenyellow" />
- <x:dependency key="end" source="state-red" />
- </x:dependencies>
- </x:component>
-
- <x:component name="transition-redyellow-to-green"
class="net.dpml.fsm.basic.TransitionComponent" >
- <x:dependencies>
- <x:dependency key="begin" source="state-redyellow" />
- <x:dependency key="end" source="state-green" />
- </x:dependencies>
- </x:component>
-
</x:block>
</target>
</project>
Added:
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/Resouces.properties
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/Resouces.properties
Sun Jan 23 13:45:32 2005
@@ -0,0 +1,18 @@
+
+state-red.name=Red
+state-red.description=Red signal means "stop".
+
+state-yellow.name=Yellow
+state-yellow.description=Yellow signal means "go faster" :o)
+
+state-green.name=Green
+state-green.description=Green signal means "go".
+
+transition-red-to-green.name=GoingToGreen
+transition-red-to-green.description=Signal changing from Red to Green.
+
+transition-green-to-yellow.name=GoingToGreen
+transition-green-to-yellow.name=GoingToGreen
+
+transition-yellow-to-red.name=GoingToGreen
+transition-yellow-to-red.name=GoingToGreen
Added:
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/trafficlight/Timer.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/fsm/demo/trafficlight/src/main/net/dpml/fsm/trafficlight/Timer.java
Sun Jan 23 13:45:32 2005
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2004-2005 Niclas Hedhman.
+ *
+ * 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.fsm.trafficlight;
+
+import java.io.Serializable;
+
+import java.text.SimpleDateFormat;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Locale;
+
+import net.dpml.activity.Startable;
+
+import net.dpml.parameters.Parameters;
+
+import net.dpml.service.ServiceException;
+import net.dpml.service.ServiceManager;
+
+import net.dpml.fsm.State;
+import net.dpml.fsm.StateMachine;
+import net.dpml.fsm.Transition;
+import net.dpml.fsm.TransitionEvent;
+import net.dpml.fsm.TransitionListener;
+
+/**
+ * @metro.component name="timer" lifestyle="singleton" collection="hard"
+ */
+public class Timer
+ implements Runnable, Startable, TransitionListener
+{
+ private StateMachine m_stateMachine;
+ private State m_initial;
+ private int m_nextWait;
+ private Thread m_thread;
+ private boolean m_run;
+
+ /**
+ * @param man ServiceManager servicing this Part.
+ *
+ * @throws ServiceException if any of the required dependencies can not
be satisfied.
+ * @metro.dependency type="net.dpml.fsm.StateMachine" key="fsm"
+ * @metro.dependency type="net.dpml.fsm.State" key="initial-state"
+ */
+ public Timer( ServiceManager man )
+ throws ServiceException
+ {
+ System.out.println( "Creating Trafficlight." );
+ m_stateMachine = (StateMachine) man.lookup( "fsm" );
+ m_initial = (State) man.lookup( "initial-state" );
+ m_stateMachine.addTransitionListener( this, null );
+ }
+
+ public void start()
+ {
+ System.out.println( "Starting Trafficlight." );
+ m_thread = new Thread( this );
+ m_thread.start();
+ }
+
+ public void stop()
+ {
+ System.out.println( "Stopping Trafficlight." );
+ m_run = false;
+ m_thread.interrupt();
+ }
+
+ public void run()
+ {
+ m_run = true;
+ String command = "next";
+ m_stateMachine.reset( m_initial );
+ m_nextWait = 5000;
+ try
+ {
+ while( m_run )
+ {
+ synchronized( this )
+ {
+ wait( m_nextWait );
+ m_nextWait = 100;
+ m_stateMachine.trigger( this, command );
+ }
+ }
+ } catch( InterruptedException e )
+ {
+ }
+ }
+
+ public void transition( TransitionEvent event )
+ {
+ Transition transition = event.getTransition();
+ Date date = event.getTransitionDate();
+ State state = transition.getEndState();
+ SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss" );
+ ClassLoader cl = getClass().getClassLoader();
+ String stateName = state.getName( Locale.getDefault(), cl );
+ System.out.println( sdf.format( date ) + " : " + stateName );
+
+ String id = state.getIdentity();
+ if( id.equals( "state-red" ) )
+ m_nextWait = 5000;
+ if( id.equals( "state-yellow" ) )
+ m_nextWait = 1000;
+ if( id.equals( "state-red" ) )
+ m_nextWait = 5000;
+ }
+}
Modified: development/laboratory/planet/components/fsm/index.xml
==============================================================================
--- development/laboratory/planet/components/fsm/index.xml (original)
+++ development/laboratory/planet/components/fsm/index.xml Sun Jan 23
13:45:32 2005
@@ -28,7 +28,6 @@
<group>dpml/fsm</group>
<name>dpml-fsm</name>
<version>1.0.0</version>
- <status>SNAPSHOT</status>
<type>module</type>
</info>
<plugins>
@@ -44,7 +43,6 @@
<group>dpml/fsm</group>
<name>dpml-fsm-api</name>
<version>1.0.0</version>
- <status>SNAPSHOT</status>
</info>
<dependencies>
</dependencies>
@@ -55,10 +53,9 @@
<project basedir="basic">
<info>
- <group>dpml/fsmm</group>
+ <group>dpml/fsm</group>
<name>dpml-fsm-basic</name>
<version>1.0.0</version>
- <status>SNAPSHOT</status>
</info>
<dependencies>
<include key="dpml-activity-api" />
@@ -81,9 +78,9 @@
<group>dpml/fsm/demo</group>
<name>dpml-trafficlight</name>
<version>1.0.0</version>
- <status>SNAPSHOT</status>
</info>
<dependencies>
+ <include key="dpml-fsm-api" />
<include key="dpml-fsm-basic" />
</dependencies>
<plugins>
- svn commit: r1562 - in development/laboratory/planet/components/fsm: . api/src/main/net/dpml/fsm basic/src/main/net/dpml/fsm/basic basic/src/test/net/dpml/fsm/basic/test demo/trafficlight demo/trafficlight/src/main/net/dpml/fsm demo/trafficlight/src/main/net/dpml/fsm/trafficlight, niclas, 01/23/2005
Archive powered by MHonArc 2.6.24.