KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: EdgeFactory.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
7 /**
8  * The factory class for creating new and copying the old edges.
9  *
10  * @author Stanislav Visnovsky
11  * @version 2.0.0
12 */

13 class EdgeFactory {
14
15
16 // size should be 3*n+2, since for every event we need emitting, absorbing and internal
17
// index 0 is for NULL, index 1 is for [UPDATE]
18

19     public static ActionToken ActionTokens[];
20     public static int inverseActionTokens[];
21     public static int tauActionTokens[];
22
23     private static Hashtable JavaDoc idxActionTokens;
24
25     private static int nextindex;
26
27     private static int size;
28
29     public static int getActionTokensNum() {
30         return nextindex;
31     }
32
33     public static void initActionTokens() {
34         Debug.println( 2, "Initializing action tokens");
35         ActionTokens = new ActionToken[16];
36         inverseActionTokens = new int[16];
37         tauActionTokens = new int[16];
38         idxActionTokens = new Hashtable JavaDoc();
39         ActionTokens[0]=new ActionToken( "NULL" );
40         inverseActionTokens[0] = 0; // NULL is inverse to itself
41
tauActionTokens[0] = 0; // NULL is internal to itself;
42
idxActionTokens.put( new ActionToken( "NULL" ).getHashKey(), new Integer JavaDoc (0) );
43         ActionTokens[1]=new ActionToken( "[UPDATE]" );
44         inverseActionTokens[1] = 1; // [UPDATE] is inverse to itself
45
tauActionTokens[1] = 1; // [UPDATE] is internal to itself;
46
idxActionTokens.put( new ActionToken( "[UPDATE]" ).getHashKey(), new Integer JavaDoc (1) );
47         nextindex = 2;
48         size = 16;
49     }
50
51     public static int getActionTokenIdx( ActionToken t ) {
52         Integer JavaDoc a = (Integer JavaDoc)idxActionTokens.get( t.getHashKey() );
53         if( a == null ) {
54         Debug.println(3, "Not found "+t.name);
55         if( nextindex >= size-2 ) { // we need more room
56
Debug.println(3,"We need more room for action tokens");
57
58             // enlarge action tokens
59
ActionToken newat[] = new ActionToken[ nextindex+15 ];
60             for( int i = 0; i<nextindex; i++) newat[i] = ActionTokens[i];
61             ActionTokens = newat;
62
63             // enlarge inverse action tokens
64
int newint[] = new int[ nextindex+15 ];
65             for( int i = 0; i<nextindex; i++) newint[i] = inverseActionTokens[i];
66             inverseActionTokens = newint;
67
68             // enlarge tau action tokens
69
newint = new int[ nextindex+15 ];
70             for( int i = 0; i<nextindex; i++) newint[i] = tauActionTokens[i];
71             tauActionTokens = newint;
72
73             size = nextindex+15;
74             Debug.println(3, "Action token table resized");
75         }
76         int ind = nextindex;
77         Debug.println(2, "Putting new action token "+t.name+" at "+ind);
78         ActionTokens[ind] = t;
79         idxActionTokens.put( t.getHashKey(), new Integer JavaDoc(ind) );
80
81         // fill-in inverse action tokens
82
int inv = findInverseActionToken( t );
83         Debug.println(3, "Inverse is :"+inv);
84         inverseActionTokens[ind] = inv;
85         if( inverseActionTokens[ind] != -1 )
86             inverseActionTokens[ inv ] = ind;
87
88         // fill-in tau action tokens
89
tauActionTokens[ind] = findTauActionToken( t );
90         if( t.name.startsWith( "#" ) ) {
91             t = new ActionToken( "?"+t.name.substring( 1 ) );
92             Integer JavaDoc k = (Integer JavaDoc)idxActionTokens.get( t.getHashKey() );
93             if( k != null ) tauActionTokens[k.intValue()] = ind;
94             k = (Integer JavaDoc)idxActionTokens.get( t.toInverseActionToken().getHashKey() );
95             if( k != null ) tauActionTokens[k.intValue()] = ind;
96         }
97
98         nextindex ++;
99
100         if( Debug.debug>2 ) {
101             Debug.println(3, "Actual action token table:");
102             for( int i = 0 ; i < nextindex ; i++ )
103             Debug.println( 3, " "+i+"\t"+ActionTokens[i].name+"\t inv:"+inverseActionTokens[i]+"\t tau:"+tauActionTokens[i]);
104         }
105
106         return nextindex-1;
107         } else return a.intValue();
108     }
109
110     private static int findInverseActionToken( ActionToken t ) {
111         int i;
112         ActionToken inverse = t.toInverseActionToken();
113         for( i = 0; i < nextindex ; i++ )
114         if( ActionTokens[i].name.compareTo( inverse.name ) == 0 ) break;
115         if( i == nextindex ) return -1;
116         return i;
117     }
118
119     private static int findTauActionToken( ActionToken t ) {
120         int i;
121         for( i = 0; i < nextindex ; i++ )
122         if( ActionTokens[i].name == t.toTauActionToken().name ) break;
123         if( i == nextindex ) return -1;
124         return i;
125     }
126
127     public static void checkTauActionToken( ActionToken t ) {
128         int i;
129         for( i = 0; i < nextindex ; i++ )
130         if( ActionTokens[i].name == t.toTauActionToken().name ) break;
131         if( i == nextindex ) getActionTokenIdx( t.toTauActionToken() );
132     }
133     
134 }
135
Popular Tags