KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ProtocolParallel.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 ProtocolParallel extends ProtocolBinaryOperator {
8
9     public ProtocolParallel( Protocol l, Protocol r ) {
10     super( "|",l,r );
11     }
12     
13     public Protocol Copy() {
14     return new ProtocolParallel( left.Copy(), right.Copy() );
15     }
16     
17     public Machine createMachine() {
18
19         machineImpl m1 = (machineImpl)left.createMachine();
20     machineImpl m2 = (machineImpl)right.createMachine();
21
22               Debug.println(2,"This is createAndParallel of ");
23     Debug.println(2,"Machine left:");
24     ((Printer)m1).Print(2);
25     Debug.println(2,"Machine right:");
26     ((Printer)m2).Print(2);
27
28         int n1,n2,n,i,j,t,e;
29     int mapping [][] = new int[m1.nextID][m2.nextID];
30         machineImpl m = new machineImpl( m1.nextID * m2.nextID);
31
32         for( i = 0; i < m1.nextID; i++ )
33         for( j = 0; j < m2.nextID; j++ ) mapping[i][j] = m.addState();
34
35     if( Debug.debug>1 ) {
36         Debug.println(2, "Mapping:" );
37         for( i = 0; i < m1.nextID; i++ )
38         for( j = 0; j < m2.nextID; j++ )
39                 Debug.println(2, "[" + i + "," + j + "] -> " + mapping[i][j]);
40     }
41
42     for( n1 = 0 ; n1< m1.nextID ; n1++ )
43         for( n2 = 0; n2 < m2.nextID; n2++ )
44         for( t = 0 ; t < EdgeFactory.getActionTokensNum(); t ++ ){
45             if( m1.states[n1][t] != null )
46             for( e = 0 ; e < m1.states[n1][t].used ; e++ )
47                  m.addEdge( mapping[n1][n2],t,mapping[ m1.states[n1][t].data[e]][n2] );
48            
49             if( m2.states[n2][t] != null )
50             for( e = 0 ; e < m2.states[n2][t].used ; e++ )
51                  m.addEdge( mapping[n1][n2],t,mapping[n1][m2.states[n2][t].data[e]] );
52         }
53
54     m.SetStart( mapping[m1.Start()][m2.Start()]);
55
56         Iterator JavaDoc k = m1.Stop().values().iterator();
57         while( k.hasNext() ) {
58             int n1i = ((Integer JavaDoc)k.next()).intValue();
59         Iterator JavaDoc l = m2.Stop().values().iterator();
60         while( l.hasNext() ) m.AddStop(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
61         }
62
63         k = m1.Update().values().iterator();
64         while( k.hasNext() ) {
65             int n1i = ((Integer JavaDoc)k.next()).intValue();
66         Iterator JavaDoc l = m2.Update().values().iterator();
67         while( l.hasNext() ) m.addUpdateEdge(mapping[n1i][((Integer JavaDoc)l.next()).intValue()]);
68         }
69
70     m.Minimize();
71
72         Debug.println(2,"createAndParallel: done - states:" + m.nextID);
73     ((Printer)m).Print(2);
74
75         return m;
76     }
77
78     public Protocol getSameType( Object JavaDoc p1, Object JavaDoc p2 ) {
79     return new ProtocolParallel( (Protocol)p1, (Protocol)p2 );
80     }
81 }
82
Popular Tags