KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ProtocolAdjustment.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 ProtocolAdjustment extends ProtocolBinaryOperator {
8
9     protected ActionTokenArray sync;
10
11     public ProtocolAdjustment( Protocol l, Protocol r, ActionTokenArray s ) {
12     super( "@",l,r );
13     sync = s;
14     }
15     
16     public Protocol Copy() {
17     return new ProtocolAdjustment( left.Copy(), right.Copy(), new ActionTokenArray( sync ) );
18     }
19
20     public Machine createMachine() {
21         machineImpl m1 = (machineImpl)left.createMachine();
22         machineImpl m2 = (machineImpl)right.createMachine();
23
24         Debug.println( 2,"Adjustment create:");
25         Debug.println( 2,"First machine:");
26         ((Printer)m1).Print(2);
27         Debug.println( 2,"Second machine:");
28         ((Printer)m2).Print(2);
29
30         Debug.println(2);
31
32         int n1,n2,n,i,j,t,e,f;
33
34     int mapping [][] = new int[m1.nextID][m2.nextID];
35         machineImpl m = new machineImpl(m1.nextID * m2.nextID);
36
37         for( i = 0; i < m1.nextID; i++ ) {
38         for( j = 0; j < m2.nextID; j++ )
39         mapping[i][j] = m.addState();
40         Debug.print(2,i+" ");
41     }
42
43     Debug.println(2);
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             boolean nsynchro = !sync.containsWithEventName(t);
56             if( m1.states[n1][t] != null )
57             if( nsynchro )
58                  for( e = 0 ; e < m1.states[n1][t].used ; e++ )
59                      m.addEdge( mapping[n1][n2],t,mapping[ m1.states[n1][t].data[e]][n2] );
60             else if( m2.states[n2][t] != null ) {
61                 for( e = 0 ; e < m1.states[n1][t].used ; e++ )
62                 for( f = 0 ; f < m2.states[n2][t].used ; f++ )
63                     m.addEdge( mapping[n1][n2],t, mapping[m1.states[n1][t].data[e]][m2.states[n2][t].data[f]]);
64                          }
65             if( nsynchro && m2.states[n2][t] != null )
66             for( e = 0 ; e < m2.states[n2][t].used ; e++ )
67                  m.addEdge( mapping[n1][n2],t,mapping[n1][m2.states[n2][t].data[e]] );
68         }
69
70     m.SetStart( mapping[m1.Start()][m2.Start()]);
71
72         Iterator JavaDoc k = m1.Stop().values().iterator();
73         while( k.hasNext() ) {
74             int n1i = ((Integer JavaDoc)k.next()).intValue();
75         Iterator JavaDoc l = m2.Stop().values().iterator();
76         while( l.hasNext() ) m.AddStop(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
77         }
78
79         k = m1.Update().values().iterator();
80         while( k.hasNext() ) {
81             int n1i = ((Integer JavaDoc)k.next()).intValue();
82         Iterator JavaDoc l = m2.Update().values().iterator();
83         while( l.hasNext() ) m.addUpdateEdge(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
84         }
85
86     m.Minimize();
87
88         Debug.println(2, "Adjustment create done:");
89         ((Printer)m).Print(2);
90         Debug.println(2);
91
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++ ) h.tokens[i] = h.tokens[i] || sync.tokens[i];
98     return super.Restrict( h );
99     }
100
101     public Protocol getSameType( Object JavaDoc p1, Object JavaDoc p2 ) {
102     return new ProtocolAdjustment( (Protocol)p1, (Protocol)p2, new ActionTokenArray( sync ) );
103     }
104 }
105
106
Popular Tags