KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > ProtocolAlternative


1 /* $Id: ProtocolAlternative.java,v 1.2 2004/05/20 14:23:53 bures Exp $ */
2
3 package SOFA.SOFAnode.Util;
4
5 import java.util.Iterator JavaDoc;
6
7 class ProtocolAlternative extends ProtocolBinaryOperator {
8
9     public ProtocolAlternative( Protocol l, Protocol r ) {
10     super( "+",l,r );
11     }
12     
13     public Protocol Copy() {
14     return new ProtocolAlternative( left.Copy(), right.Copy() );
15     }
16     
17     public Machine createMachine() {
18         Debug.println(3, "Alternative create machine start:");
19     
20     machineImpl m1 = (machineImpl)left.createMachine();
21     machineImpl m2 = (machineImpl)right.createMachine();
22     
23     if( m1 == null ) return m2; // if m1 is null, return m2 or null
24
if( m2 == null ) return m1; // m1 is not not and m2 is null
25

26     Debug.println(3, "Alternative first machine:" );
27     ((Printer)m1).Print(3);
28     Debug.println(3, "Alternative second machine:" );
29     ((Printer)m2).Print(3);
30          
31     boolean start_is_stop = m2.Stop().containsKey( new Integer JavaDoc(m2.Start()));
32     int map[] = m1.AddStates( m2.states, m2.nextID ); // add all states form m2 to result
33
m1.Merge(m1.Start(), map[m2.Start()] ); // connect following states to result start
34
for( Iterator JavaDoc i = m2.Stop().values().iterator(); i.hasNext(); )
35         m1.AddStop( map[ ((Integer JavaDoc)i.next()).intValue()] ); // add finish states from m2
36

37     for( Iterator JavaDoc i = m2.Update().values().iterator(); i.hasNext(); )
38         m1.addUpdateEdge( map[ ((Integer JavaDoc)i.next()).intValue()] ); // add finish states from m2
39

40 // m1.RemoveState( m2.Start() ); // remove m2 starting state
41

42     if( start_is_stop ) m1.AddStop( m1.Start() );
43
44     m1.Minimize();
45
46         Debug.println(3, "Alternative create machine finished:");
47     ((Printer)m1).Print(3);
48     
49     return m1;
50     }
51
52     public Protocol getSameType( Object JavaDoc p1, Object JavaDoc p2 ) {
53     return new ProtocolAlternative( (Protocol)p1, (Protocol)p2 );
54     }
55 }
56
57
Popular Tags