KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CDL > CompBindOper


1 /* $Id: CompBindOper.java,v 1.2 2004/05/20 14:23:51 bures Exp $ */
2 package SOFA.SOFAnode.Made.CDL;
3 import java.rmi.RemoteException JavaDoc;
4
5 import SOFA.SOFAnode.Made.TIR.ArchitectureDef;
6 import SOFA.SOFAnode.Made.TIR.ArrayDef;
7 import SOFA.SOFAnode.Made.TIR.BindOperDef;
8 import SOFA.SOFAnode.Made.TIR.BindOperProvReq;
9 import SOFA.SOFAnode.Made.TIR.BindOperSub;
10 import SOFA.SOFAnode.Made.TIR.CDLRepository;
11 import SOFA.SOFAnode.Made.TIR.CDLType;
12 import SOFA.SOFAnode.Made.TIR.Contained;
13 import SOFA.SOFAnode.Made.TIR.Container;
14 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
15 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
16 import SOFA.SOFAnode.Made.TIR.ProvideDef;
17 import SOFA.SOFAnode.Made.TIR.RequireDef;
18 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
20 import SOFA.SOFAnode.Made.TIR.TypedefDef;
21  
22 public class CompBindOper extends CompObjectIm {
23   public static final int b_provreq = 0;
24   public static final int b_sub = 1;
25
26   int kind;
27   List elements;
28   List subs;
29   int provreq; // if it refer prov or req
30

31   CompArchitecture arch;
32
33   public CompBindOper(List els, CompArchitecture a) {
34     super(ObjectsKind.o_BindOper);
35     kind = b_provreq;
36     elements = els;
37     subs = null;
38     arch = a;
39   }
40
41   public CompBindOper(List sub, List els, CompArchitecture a) {
42     super(ObjectsKind.o_BindOper);
43     kind = b_sub;
44     elements = els;
45     subs = sub;
46     arch = a;
47   }
48
49   public String JavaDoc toString() {
50     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
51     CompBindType type;
52     if (kind == b_sub) {
53       subs.toFirst();
54       type = (CompBindType) subs.aktual();
55       sb.append(type.name);
56       if (type.exprs.size() != 0) {
57         sb.append("[");
58         type.exprs.toFirst();
59         for (int i=0;i<type.exprs.size();i++) {
60           if (i!=0)
61             sb.append(",");
62           sb.append(((CompExprOper) type.exprs.aktual()).toString());
63         }
64         sb.append("]");
65       }
66       sb.append(".");
67     }
68     elements.toFirst();
69     type = (CompBindType) elements.aktual();
70     sb.append(type.name);
71     if (type.exprs.size() != 0) {
72       sb.append("[");
73       type.exprs.toFirst();
74       for (int i=0;i<type.exprs.size();i++) {
75         if (i!=0)
76           sb.append(",");
77         sb.append(((CompExprOper) type.exprs.aktual()).toString());
78       }
79       sb.append("]");
80     }
81     return sb.toString();
82   }
83
84   public void checkConsist(EnumList props, CompRepository rep) throws CDLExceptCheck, CDLExceptLock, CDLExceptRemote {
85     int i;
86     elements.toFirst();
87     for (i=0;i<elements.size();i++) {
88       ((CompBindType) elements.aktual()).checkConsist(props,rep);
89       elements.toNext();
90     }
91     if (kind == b_sub) {
92       subs.toFirst();
93       for (i=0;i<subs.size();i++) {
94         ((CompBindType) subs.aktual()).checkConsist(props,rep);
95         subs.toNext();
96       }
97     }
98   }
99
100   public BindOperDef toNormal(ArchitectureDef arch, CDLRepository normRep) throws CDLExceptToNormal, CDLExceptRemote, CDLExceptLock {
101     try {
102       if (kind == b_provreq) {
103         BindOperProvReq ret = arch.create_bindoperprovreq();
104         elements.toFirst();
105         for (int i=0; i<elements.size(); i++) {
106           CompBindType akt = (CompBindType) elements.aktual();
107           if (akt.exprs.size() == 0) {
108             if (ret.add_bindtypenormal(akt.name)==null)
109               throw new CDLExceptToNormal("Can't create bind type normal");
110           } else {
111             ExprOperDef[] ind = new ExprOperDef [akt.exprs.size()];
112             akt.exprs.toFirst();
113             for(int j=0;j<akt.exprs.size();j++) {
114               ind[j] = ((CompExprOper) akt.exprs.aktual()).toNormal(normRep);
115               akt.exprs.toNext();
116             }
117             if (ret.add_bindtypearray(akt.name, ind)==null)
118               throw new CDLExceptToNormal("Can't create bind type array");
119           }
120           elements.toNext();
121         }
122         return ret;
123       } else {
124         BindOperSub ret = arch.create_bindopersub();
125         elements.toFirst();
126         for (int i=0; i<elements.size(); i++) {
127           CompBindType akt = (CompBindType) elements.aktual();
128           if (akt.exprs.size() == 0) {
129             if (ret.add_bindtypenormal(akt.name)==null)
130               throw new CDLExceptToNormal("Can't create bind type normal");
131           } else {
132             ExprOperDef[] ind = new ExprOperDef [akt.exprs.size()];
133             akt.exprs.toFirst();
134             for(int j=0;j<akt.exprs.size();j++) {
135               ind[j] = ((CompExprOper) akt.exprs.aktual()).toNormal(normRep);
136               akt.exprs.toNext();
137             }
138             if (ret.add_bindtypearray(akt.name, ind)==null)
139               throw new CDLExceptToNormal("Can't create bind type array");
140           }
141           elements.toNext();
142         }
143         subs.toFirst();
144         for (int i=0; i<subs.size(); i++) {
145           CompBindType akt = (CompBindType) subs.aktual();
146           if (akt.exprs.size() == 0) {
147             if (ret.add_subnormal(akt.name)==null)
148               throw new CDLExceptToNormal("Can't create bind type normal");
149           } else {
150             ExprOperDef[] ind = new ExprOperDef [akt.exprs.size()];
151             akt.exprs.toFirst();
152             for(int j=0;j<akt.exprs.size();j++) {
153               ind[j] = ((CompExprOper) akt.exprs.aktual()).toNormal(normRep);
154               akt.exprs.toNext();
155             }
156             if (ret.add_subarray(akt.name, ind)==null)
157               throw new CDLExceptToNormal("Can't create bind type array");
158           }
159           subs.toNext();
160         }
161         return ret;
162       }
163     } catch (RemoteException JavaDoc e) {
164       throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
165     } catch (TIRExceptCreate ecr) {
166       throw new CDLExceptToNormal("Can't create bind");
167     }
168   }
169
170   // prov/req
171
public boolean testBOPR() throws CDLExceptRemote, CDLExceptLock {
172     int i,j;
173     elements.toFirst();
174     if (arch.frame.isNew) {
175       CompContainer con = arch.rep;
176       FullID id = arch.frame.ref;
177       id.sn.names.toFirst();
178       for(i=0;i<id.sn.names.size();i++) {
179         CompContained[] a = con.lookup((String JavaDoc) id.sn.names.aktual());
180         if (a==null || a.length==0)
181           return false;
182         if (a.length==1) {
183           if (a[0].isContainer()) con = (CompContainer) a[0];
184           else return false;
185         } else {
186           return false;
187         }
188         id.sn.names.toNext();
189       }
190       CompContained frame = con.lookup(id.name, id.version);
191       if (frame==null)
192         return false;
193       CompContained sec = ((CompContainer) frame).lookup(((CompBindType)elements.aktual()).name, id.version);
194       if (sec == null) // don't exist prov/req with this name
195
return false;
196       if (sec.objectKind()!=ObjectsKind.o_Provides && sec.objectKind()!=ObjectsKind.o_Requires)
197         return false; // it isn't prov/req
198
provreq = sec.objectKind();
199       String JavaDoc tc = null;
200       if (sec.objectKind()==ObjectsKind.o_Provides) {
201         tc = ((CompProvide) sec).typeCode;
202       } else {
203         tc = ((CompRequire) sec).typeCode;
204       }
205       int k=0;
206       for (i=0;i<elements.size();i++) {
207         for (j=0;j<((CompBindType) elements.aktual()).exprs.size();j++) {
208           if (tc.charAt(k)!='a')
209             return false;
210           k++;
211         }
212         elements.toNext();
213       }
214       if (tc.charAt(k)!='i')
215         return false;
216       return true;
217     } else { // search in normal rep
218
try {
219         Container con = arch.rep.cdlRepository;
220         FullID id = arch.frame.ref;
221         id.sn.names.toFirst();
222         for(i=0;i<id.sn.names.size();i++) {
223           Contained[] a = con.lookup_name((String JavaDoc) id.sn.names.aktual());
224           if (a==null || a.length==0)
225             return false;
226           if (a.length==1) {
227             if (a[0] instanceof Container) con = (Container) a[0];
228             else return false;
229           } else {
230             boolean found = false;
231             for(j=0;j<a.length;j++) {
232               if (a[j].get_identification().version().compareTo(id.version)==0) {
233                 if ( !(a[j] instanceof Container)) return false;
234                 found = true;
235                 con = (Container) a[j];
236                 break;
237               }
238             }
239             if (!found) return false;
240           }
241           id.sn.names.toNext();
242         }
243         Contained frame = con.lookup(id.name, id.version);
244         if (frame==null)
245           return false;
246         Contained sec = ((Container) frame).lookup(((CompBindType)elements.aktual()).name, id.version);
247         if (sec == null) // don't exist prov/req with this name
248
return false;
249         if (sec.get_def_kind().value()!=DefinitionKind.dk_Provides && sec.get_def_kind().value()!=DefinitionKind.dk_Requires)
250           return false; // it isn't prov/req
251
provreq = sec.get_def_kind().value();
252         StringBuffer JavaDoc tc = new StringBuffer JavaDoc();
253         boolean ok = false;
254         CDLType tp;
255         if (sec.get_def_kind().value()==DefinitionKind.dk_Provides) {
256           tp = ((ProvideDef) sec).provide();
257         } else {
258           tp = ((RequireDef) sec).require();
259         }
260         while (!ok) {
261           switch (tp.get_def_kind().value()) {
262           case DefinitionKind.dk_Interface:
263             tc.append('i');
264             ok = true;
265             break;
266           case DefinitionKind.dk_Array:
267             tp = ((ArrayDef) tp).element_type();
268             tc.append('a');
269             break;
270           case DefinitionKind.dk_Typedef:
271             tp = ((TypedefDef) tp).original_type();
272             break;
273           }
274         }
275         int k=0;
276         for (i=0;i<elements.size();i++) {
277           for (j=0;j<((CompBindType) elements.aktual()).exprs.size();j++) {
278             if (tc.charAt(k)!='a')
279               return false;
280             k++;
281           }
282           elements.toNext();
283         }
284         if (tc.charAt(k)!='i')
285           return false;
286         return true;
287       } catch (TIRExceptLock e) {
288         throw new CDLExceptLock("Repository is locked.");
289       } catch (RemoteException JavaDoc e) {
290         throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
291       }
292     }
293   }
294
295   // subs
296
public boolean testBOS() {
297     int i,j;
298     subs.toFirst();
299     CompContained inst = arch.lookup(((CompBindType) subs.aktual()).name, arch.getIdent().version);
300     if (inst==null)
301       return false;
302     if (inst.objectKind()!=ObjectsKind.o_Inst)
303       return false;
304     String JavaDoc tc = ((CompInst) inst).typeCode;
305     int k=0;
306     for (i=0;i<subs.size();i++) {
307       for (j=0;j<((CompBindType) subs.aktual()).exprs.size();j++) {
308         if (tc.charAt(k)!='a')
309           return false;
310         k++;
311       }
312       elements.toNext();
313     }
314     if (tc.charAt(k)!='f')
315       return false;
316     return true;
317   }
318 }
319
Popular Tags