KickJava   Java API By Example, From Geeks To Geeks.

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


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