KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ProtocolComposition.java,v 1.2 2004/05/20 14:23:53 bures Exp $ */
2
3 package SOFA.SOFAnode.Util;
4
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 class ProtocolComposition extends ProtocolBinaryOperator {
9
10     protected ActionTokenArray sync;
11
12     public ProtocolComposition( Protocol l, Protocol r, ActionTokenArray s ) {
13     super( "#",l,r );
14     sync = s;
15     }
16     
17     public Protocol Copy() {
18     return new ProtocolComposition( left.Copy(), right.Copy(), new ActionTokenArray( sync ) );
19     }
20     
21     public Machine createMachine() {
22     
23     int i,j,n1,n2,t,e,f;
24
25     Debug.println(2,"This is composition create machine");
26     Hashtable JavaDoc synchronizations = new Hashtable JavaDoc();
27     
28         machineImpl m1 = (machineImpl)left.createMachine();
29         machineImpl m2 = (machineImpl)right.createMachine();
30     
31         Debug.println( 2,"Composition create:");
32         Debug.println( 2,"First machine:");
33         ((Printer)m1).Print(2);
34         Debug.println( 2,"Second machine:");
35         ((Printer)m2).Print(2);
36
37         Debug.println(2);
38
39     int mapping [][] = new int[m1.nextID][m2.nextID];
40         machineImpl m = new machineImpl( m1.nextID * m2.nextID );
41
42         for( i = 0; i < m1.nextID; i++ )
43         for( j = 0; j < m2.nextID; j++ ) mapping[i][j] = m.addState();
44
45     if( Debug.debug>1 ) {
46         Debug.println(2, "Mapping:" );
47         for( i = 0; i < m1.nextID; i++ )
48         for( j = 0; j < m2.nextID; j++ )
49                 Debug.println(2, "[" + i + "," + j + "] -> " + mapping[i][j]);
50     }
51
52     for( n1 = 0 ; n1< m1.nextID ; n1++ )
53         for( n2 = 0; n2 < m2.nextID; n2++ )
54         for( t = 0 ; t < EdgeFactory.getActionTokensNum(); t ++ ){
55             Debug.println(3, "Working on transition "+t );
56             boolean nsynchro = !sync.containsWithEventName(t);
57             if( m1.states[n1][t] != null )
58             if( nsynchro )
59                  for( e = 0 ; e < m1.states[n1][t].used ; e++ )
60                      m.addEdge( mapping[n1][n2],t,mapping[ m1.states[n1][t].data[e]][n2] );
61             else if( m2.states[n2][ EdgeFactory.inverseActionTokens[t]] != null ) {
62                 for( e = 0 ; e < m1.states[n1][t].used ; e++ )
63                 for( f = 0 ; f < m2.states[n2][ EdgeFactory.inverseActionTokens[t]].used ; f++ )
64                     m.addEdge( mapping[n1][n2], EdgeFactory.tauActionTokens[t], mapping[m1.states[n1][t].data[e]][m2.states[n2][ EdgeFactory.inverseActionTokens[t]].data[f]]);
65                          }
66             if( nsynchro && m2.states[n2][t] != null )
67             for( e = 0 ; e < m2.states[n2][t].used ; e++ )
68                  m.addEdge( mapping[n1][n2],t,mapping[n1][m2.states[n2][t].data[e]] );
69         }
70
71     m.SetStart( mapping[m1.Start()][m2.Start()]);
72
73         Iterator JavaDoc k = m1.Stop().values().iterator();
74         while( k.hasNext() ) {
75             int n1i = ((Integer JavaDoc)k.next()).intValue();
76         Iterator JavaDoc l = m2.Stop().values().iterator();
77         while( l.hasNext() ) m.AddStop(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
78         }
79
80         k = m1.Update().values().iterator();
81         while( k.hasNext() ) {
82             int n1i = ((Integer JavaDoc)k.next()).intValue();
83         Iterator JavaDoc l = m2.Update().values().iterator();
84         while( l.hasNext() ) m.addUpdateEdge(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
85         }
86
87     m.Minimize();
88
89         Debug.println(2, "Composition create done:");
90         ((Printer)m).Print(2);
91         Debug.println(2);
92         return m;
93     }
94     
95     public boolean Restrict( ActionTokenArray a ) {
96     ActionTokenArray h = new ActionTokenArray( a );
97     for( int i = 0 ; i< sync.size; i++ )
98         h.tokens[i] = sync.tokens[i] || h.tokens[i];
99     Debug.print( 3, "Restriction passed to arguments: "+ h.toList() );
100     return super.Restrict( h );
101     }
102
103     public Protocol getSameType( Object JavaDoc p1, Object JavaDoc p2 ) {
104     return new ProtocolComposition( (Protocol)p1, (Protocol)p2, new ActionTokenArray( sync ) );
105     }
106 }
107
108
Popular Tags