KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: CompArchitecture.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.BindDef;
7 import SOFA.SOFAnode.Made.TIR.BindOperDef;
8 import SOFA.SOFAnode.Made.TIR.CDLRepository;
9 import SOFA.SOFAnode.Made.TIR.Contained;
10 import SOFA.SOFAnode.Made.TIR.Container;
11 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
12 import SOFA.SOFAnode.Made.TIR.FrameDef;
13 import SOFA.SOFAnode.Made.TIR.InstDef;
14 import SOFA.SOFAnode.Made.TIR.PropertyDef;
15 import SOFA.SOFAnode.Made.TIR.ProtocolBinOperationDef;
16 import SOFA.SOFAnode.Made.TIR.ProtocolDef;
17 import SOFA.SOFAnode.Made.TIR.ProtocolMetaOperandDef;
18 import SOFA.SOFAnode.Made.TIR.ProtocolMode;
19 import SOFA.SOFAnode.Made.TIR.ProtocolNestedOperandDef;
20 import SOFA.SOFAnode.Made.TIR.ProtocolOperDef;
21 import SOFA.SOFAnode.Made.TIR.ProtocolOperKind;
22 import SOFA.SOFAnode.Made.TIR.ProtocolOperandDef;
23 import SOFA.SOFAnode.Made.TIR.ProtocolUnOperationDef;
24 import SOFA.SOFAnode.Made.TIR.ProvideDef;
25 import SOFA.SOFAnode.Made.TIR.RequireDef;
26 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
27 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
28 import SOFA.SOFAnode.Util.ConnectorProtocolGen.CannotGenerateException;
29 import SOFA.SOFAnode.Util.ConnectorProtocolGen.GeneratorNotFoundException;
30 import SOFA.SOFAnode.Util.ConnectorProtocolGen.RoleDescription;
31
32 class CompArchitecture extends CompContainedContainerIm {
33
34   boolean isSystem;
35
36   EnumList propNames;
37   CompReffer frame;
38
39   int numOfBind;
40
41   java.util.ArrayList JavaDoc provReq;
42   String JavaDoc[] provOpArray; // filled by createOpArrays (depends on provReq)
43
String JavaDoc[] allOpArray; // filled by createOpArrays (depends on provReq)
44

45   public CompArchitecture(ID id, CompContainer in, CompRepository inRep, CompReffer frame, EnumList propNames, boolean sys) {
46     super(ObjectsKind.o_Architecture, id, in, inRep);
47     this.frame = frame;
48     this.propNames = propNames;
49     numOfBind = 0;
50     isSystem = sys;
51 // Debug.message("Vytvarim architecturu: "+id);
52
}
53
54   public void checkConsist(EnumList props, CompRepository rep) throws CDLExceptCheck, CDLExceptLock, CDLExceptRemote {
55 // Debug.message("testuji konzistenci architectury: "+getIdent());
56
CompContained[] cont = contents();
57     for (int i=0;i<cont.length;i++) {
58       try {
59         cont[i].checkConsist(propNames, rep);
60         //
61
//if (cont[i].objectKind() == ObjectsKind.o_Inst)
62
// Debug.message(((CompInst)cont[i]).provOps.toString());
63
} catch (CDLExceptCheck e) {
64         throw new CDLExceptCheck(e.getMessage()+" in architecture "+fullName());
65       }
66     }
67     DetectedConnectors dc;
68     try {
69       dc = detectConnectors();
70     } catch (BadBindException e) {
71       Output.out.println(e.getMessage());
72       Output.out.println(e.getCause().getMessage());
73       return;
74     }
75     if (rep.testProtocols && (this.cont.size()!=0)) {
76       Output.out.println("Checking protocol of architecture: "+getIdent());
77
78       String JavaDoc archP = generateArchProtocol(dc);
79       if (archP==null || archP.compareTo("")==0) {
80         Output.out.println("Warning: frame of architecture "+getIdent()+" is empty, protocols can't be checked.");
81         return;
82       }
83       String JavaDoc instP = "";
84       try {
85         instP = generateInstProtocol(dc);
86       } catch (BadBindException e) {
87         Output.out.println("Warning: in architecture "+getIdent()+", there is bad binds. Protocols can't be checked.\n * "+e.getMessage());
88         return;
89       } catch (CannotGenerateException e) {
90         Output.out.println("Warning: protocols in architecture "+getIdent()+" can't be checked.\n * "+e.getMessage());
91         return;
92       }
93       createOpArrays();
94       String JavaDoc res;
95       try {
96         if (System.getProperty("cdl.debug","no").compareTo("yes")==0) {
97           res = SOFA.SOFAnode.Util.ProtocolChecker.frame2arch(archP, provOpArray, instP, allOpArray);
98         }
99         long tmb = java.util.Calendar.getInstance().getTime().getTime();
100         res = repository().protocolChecker.frame2arch(archP, provOpArray, instP, allOpArray);
101         long tme = java.util.Calendar.getInstance().getTime().getTime();
102         Output.out.println("Time of protocol checking of architecture: "+getIdent().name+": "+Long.toString(tme-tmb)+" ms");
103         if (res != null)
104           Output.out.println("Warning: "+res+" ("+fullName()+")");
105       } catch (SOFA.SOFAnode.Util.SyntaxErrorException e) {
106         Output.out.println("Protocol syntax error\n"+e.getMessage());
107       }
108       System.gc();
109     }
110   }
111
112   public void addToNormal(Container obj, CDLRepository normRep, java.util.ArrayList JavaDoc delayed) throws CDLExceptToNormal, CDLExceptRemote, CDLExceptLock {
113     if (id.isin == IDKind.versiontag) {
114       try {
115         ((Contained)obj).tag(id.tag);
116       } catch (RemoteException JavaDoc e) {
117         throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
118       } catch (TIRExceptLock e) {
119         throw new CDLExceptLock("Repository is locked.");
120       } catch (TIRExceptCreate ecr) {
121         throw new CDLExceptToNormal("Can't set tag for "+ fullName());
122       }
123     }
124     cont.toFirst();
125     try {
126       for (int i=0;i<cont.size(); i++) {
127         CompContained akt = (CompContained) cont.aktual();
128         switch (akt.objectKind()) {
129         // *****************
130
case ObjectsKind.o_Inst:
131           try {
132             InstDef inst = ((ArchitectureDef) obj).create_inst(akt.getIdent().name, ((CompInst)akt).subcom.toNormal(normRep), ((CompInst)akt).mode);
133             if (inst == null)
134               throw new CDLExceptToNormal("Can't create instance "+((CompContained)akt).fullName());
135           } catch (TIRExceptCreate ecr) {
136             throw new CDLExceptToNormal("Can't create instance "+((CompContained)akt).fullName());
137           }
138           break;
139         // *****************
140
case ObjectsKind.o_Bind:
141           try {
142             BindOperDef lhs = ((CompBind) akt).lhs.toNormal((ArchitectureDef) obj, normRep);
143             BindOperDef rhs = ((CompBind) akt).rhs.toNormal((ArchitectureDef) obj, normRep);
144             BindDef bnd = ((ArchitectureDef) obj).create_bind(lhs,rhs,((CompBind) akt).mode, ((CompBind) akt).using);
145             if (bnd==null)
146               throw new CDLExceptToNormal("Can't create bind "+((CompContained)akt).fullName());
147           } catch (TIRExceptCreate ecr) {
148             throw new CDLExceptToNormal("Can't create bind "+((CompContained)akt).fullName());
149           }
150           break;
151         // *****************
152
case ObjectsKind.o_Property:
153           try {
154             PropertyDef prp = ((ArchitectureDef) obj).create_property(akt.getIdent().name,((CompProperty)akt).tp.toNormal(normRep) );
155             if (prp == null)
156               throw new CDLExceptToNormal("Can't create property "+((CompContained)akt).fullName());
157           } catch (TIRExceptCreate ecr) {
158             throw new CDLExceptToNormal("Can't create property "+((CompContained)akt).fullName()+"\n"+ ecr.getMessage() );
159           }
160           break;
161         default:
162           throw new CDLExceptToNormal("Unexcepted kind of object");
163         }
164         cont.toNext();
165       }
166     } catch (RemoteException JavaDoc e) {
167       throw new CDLExceptRemote("Remote exception occured: "+e.getMessage());
168     } catch (TIRExceptLock e) {
169       throw new CDLExceptLock("Repository is locked.");
170     }
171   }
172
173   /* from CompContainedContainerIm */
174   public boolean addObject(CompContained o) {
175     if (!super.addObject(o))
176       return false;
177     if (o instanceof CompProperty) {
178       propNames.addName(((CompProperty) o).getIdent().name);
179     }
180     return true;
181   }
182   
183   static class reProvReq {
184     String JavaDoc orig;
185     String JavaDoc rew;
186     int type;
187     public reProvReq(String JavaDoc o, int t) {
188       orig = o;
189       type = t;
190     }
191   }
192   
193   public String JavaDoc generateArchProtocol(DetectedConnectors dc) throws CDLExceptLock, CDLExceptRemote {
194     int i,j;
195     if (frame.isNew) { // frame is currently compiled
196
CompFrame fr = (CompFrame) frame.getRefferedObject(repository());
197       provReq = new java.util.ArrayList JavaDoc();
198       //java.util.ArrayList ln = new java.util.ArrayList();
199
CompContained[] cont = fr.contents();
200       for (i=0;i<cont.length;i++) {
201         if (cont[i].objectKind() == ObjectsKind.o_Provides) {
202           /*if (((CompProvide)cont[i]).typeCode.indexOf('a')!= -1) {
203             return null;
204           }*/

205           provReq.add(new reProvReq(new String JavaDoc(cont[i].getIdent().name), ObjectsKind.o_Provides));
206         }
207         if (cont[i].objectKind() == ObjectsKind.o_Requires) {
208           /*if (((CompRequire)cont[i]).typeCode.indexOf('a')!= -1) {
209             return null;
210           }*/

211           provReq.add(new reProvReq(new String JavaDoc(cont[i].getIdent().name), ObjectsKind.o_Requires));
212         }
213       }
214       rewriteProt(provReq, dc);
215       return compFrameProtToArch(fr.protocol,provReq);
216     } else { // frame is in repository
217
try {
218         FrameDef fr = (FrameDef) frame.getRefferedObject(repository());
219         ProtocolDef protocol = fr.protocol();
220         if (protocol == null)
221           return null;
222         provReq = new java.util.ArrayList JavaDoc();
223         Contained[] contRep = fr.contents(null);
224         for (i=0;i<contRep.length;i++) {
225           if (contRep[i].get_def_kind().value() == DefinitionKind.dk_Provides) {
226             provReq.add(new reProvReq(new String JavaDoc(contRep[i].get_identification().name()), DefinitionKind.dk_Provides));
227           }
228           if (contRep[i].get_def_kind().value() == DefinitionKind.dk_Requires) {
229             provReq.add(new reProvReq(new String JavaDoc(contRep[i].get_identification().name()), DefinitionKind.dk_Requires));
230           }
231         }
232         rewriteProt(provReq, dc);
233         return normFrameProtToArch(protocol.protocol(), provReq);
234       } catch (RemoteException JavaDoc e) {
235         throw new CDLExceptRemote(e.getMessage());
236       } catch (TIRExceptLock e) {
237         throw new CDLExceptLock(e.getMessage());
238       }
239     }
240   }
241
242   /** called from generateArchProtocol. Method assign "new" names for interfaces in
243     * architecture protocol. */

244   void rewriteProt(java.util.ArrayList JavaDoc provReq, DetectedConnectors dc) {
245     int i,j;
246     CompContained[] cont = contents(); // get content of this architecture
247
for(i=0;i<cont.length;i++) {
248       if (cont[i].objectKind() == ObjectsKind.o_Bind) { //this is bind
249
CompBind bind = (CompBind) cont[i];
250         String JavaDoc t,r1,r2,l1,l2;
251         CompBindOper rhs, lhs;
252         int index;
253         DetectedConnector cn;
254         switch (bind.mode) {
255           case CompBind.m_delegate:
256             lhs = bind.lhs;
257             rhs = bind.rhs;
258             cn = dc.containsConnWithSRole(rhs);
259             index = cn.indexOfCRole(lhs);
260             lhs.elements.toFirst();
261             t = ((CompBindType) lhs.elements.aktual()).name;
262             for(j=0;j<provReq.size();j++) {
263               if ( t.compareTo(((reProvReq)(provReq.get(j))).orig) == 0 ) {
264                 rhs.elements.toFirst();
265                 rhs.subs.toFirst();
266                 r1 = ((CompBindType) rhs.subs.aktual()).name;
267                 r2 = ((CompBindType) rhs.elements.aktual()).name;
268                 ((reProvReq)(provReq.get(j))).rew = "<"+t+"-"+r1+":"+r2+">";
269               /* ((reProvReq)(provReq.get(j))).rew = "<"+t+"-cRole"+Integer.toString(index)+">"; */
270                 break;
271               }
272             }
273             break;
274           case CompBind.m_subsume:
275             rhs = bind.rhs;
276             cn = dc.containsConnWithSRole(rhs);
277             rhs.elements.toFirst();
278             t = ((CompBindType) rhs.elements.aktual()).name;
279             for(j=0;j<provReq.size();j++) {
280               if ( t.compareTo(((reProvReq)(provReq.get(j))).orig) == 0 ) {
281                 lhs = bind.lhs;
282                 lhs.elements.toFirst();
283                 lhs.subs.toFirst();
284                 l1 = ((CompBindType) lhs.subs.aktual()).name;
285                 l2 = ((CompBindType) lhs.elements.aktual()).name;
286                 ((reProvReq)(provReq.get(j))).rew = "<"+l1+":"+l2+"-"+t+">";
287                 /*((reProvReq)(provReq.get(j))).rew = "<sRole-"+t+">"; */
288                 break;
289               }
290             }
291             break;
292           case CompBind.m_bind:
293             if (bind.rhs.kind==CompBindOper.b_provreq && bind.lhs.kind==CompBindOper.b_provreq) {
294               cn = dc.containsConnWithSRole(bind.rhs);
295               index = cn.indexOfCRole(bind.lhs);
296               bind.rhs.elements.toFirst();
297               String JavaDoc rname = ((CompBindType)bind.rhs.elements.aktual()).name;
298               bind.lhs.elements.toFirst();
299               String JavaDoc lname = ((CompBindType)bind.lhs.elements.aktual()).name;
300               for(j=0;j<provReq.size();j++) {
301                 /*if ( rname.compareTo(((reProvReq)(provReq.get(j))).orig) == 0 || lname.compareTo(((reProvReq)(provReq.get(j))).orig) == 0) {
302                   ((reProvReq)(provReq.get(j))).rew = "<"+((reProvReq)(provReq.get(j))).orig+">";
303                 }*/

304                 if ( rname.compareTo(((reProvReq)(provReq.get(j))).orig) == 0 ) {
305                   ((reProvReq)(provReq.get(j))).rew = "<sRole0-"+rname+">";
306                 }
307                 if (lname.compareTo(((reProvReq)(provReq.get(j))).orig) == 0) {
308                    ((reProvReq)(provReq.get(j))).rew = "<"+lname+"-cRole"+Integer.toString(index)+">";
309                 }
310               }
311             }
312             break;
313         } //-- end of switch
314
}
315     }
316   }
317
318   public static String JavaDoc compFrameProtToArch(CompProtOper p, java.util.ArrayList JavaDoc names) {
319     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
320     int i;
321     switch (p.type) {
322     case CompProtKind.p_operand:
323       CompProtOperand op = (CompProtOperand) p;
324       if (op.mode == CompProtMode.pm_in)
325         sb.append("?");
326       else
327         sb.append("!");
328       for (i=0;i<names.size();i++) {
329         if ( op.ifacename.compareTo(((reProvReq)(names.get(i))).orig) == 0 ) {
330           sb.append(((reProvReq)(names.get(i))).rew);
331         }
332       }
333       sb.append(".");
334       sb.append(((CompProtOperand) p).operand);
335       break;
336     case CompProtKind.p_nulloperand:
337       sb.append("NULL");
338       break;
339     case CompProtKind.p_metaoperand:
340       sb.append("[");
341       sb.append(((CompProtMetaOperand) p).name);
342       sb.append("]");
343       break;
344     case CompProtKind.p_nestedoperand:
345       CompProtNestedOperand nop = (CompProtNestedOperand) p;
346       if (nop.mode == CompProtMode.pm_in)
347         sb.append("?");
348       else
349         sb.append("!");
350       for (i=0;i<names.size();i++) {
351         if ( nop.ifacename.compareTo(((reProvReq)(names.get(i))).orig) == 0 ) {
352           sb.append(((reProvReq)(names.get(i))).rew);
353           break;
354         }
355       }
356       sb.append(".");
357       sb.append(nop.operand);
358       sb.append("{");
359       sb.append(compFrameProtToArch(nop.nestOperand, names));
360       sb.append("}");
361       break;
362     case CompProtKind.p_repetition:
363       sb.append("(");
364       sb.append(compFrameProtToArch(((CompProtUnOper) p).operand, names));
365       sb.append(")*");
366       break;
367     case CompProtKind.p_andparallel:
368       sb.append("(");
369       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand1, names));
370       sb.append(")|(");
371       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand2, names));
372       sb.append(")");
373       break;
374     case CompProtKind.p_orparallel:
375       sb.append("(");
376       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand1, names));
377       sb.append(")||(");
378       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand2, names));
379       sb.append(")");
380       break;
381     case CompProtKind.p_alternative:
382       sb.append("(");
383       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand1, names));
384       sb.append(")+(");
385       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand2, names));
386       sb.append(")");
387       break;
388     case CompProtKind.p_sequence:
389       sb.append("(");
390       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand1, names));
391       sb.append(");(");
392       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand2, names));
393       sb.append(")");
394       break;
395     case CompProtKind.p_restriction:
396       sb.append("(");
397       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand1, names));
398       sb.append(")/(");
399       sb.append(compFrameProtToArch(((CompProtBinOper) p).operand2, names));
400       sb.append(")");
401       break;
402     default: break;
403     }
404     return sb.toString();
405   }
406
407   public static String JavaDoc normFrameProtToArch(ProtocolOperDef p, java.util.ArrayList JavaDoc names) throws RemoteException JavaDoc {
408     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
409     int i;
410     String JavaDoc ifacename;
411     switch (p.get_po_kind().value()) {
412     case ProtocolOperKind.pok_operand:
413       ProtocolOperandDef op = (ProtocolOperandDef) p;
414       if (op.operand_mode().value() == ProtocolMode.pm_in)
415         sb.append("?");
416       else
417         sb.append("!");
418       ifacename = op.operand()[0];
419       for (i=0;i<names.size();i++) {
420         if ( ifacename.compareTo(((reProvReq)(names.get(i))).orig) == 0 ) {
421           sb.append(((reProvReq)(names.get(i))).rew);
422         }
423       }
424       sb.append(".");
425       sb.append(op.operand()[1]);
426       break;
427     case ProtocolOperKind.pok_nulloperand:
428       sb.append("NULL");
429       break;
430     case ProtocolOperKind.pok_metaoperand:
431       sb.append("[");
432       sb.append(((ProtocolMetaOperandDef)p).name());
433       sb.append("]");
434       break;
435     case ProtocolOperKind.pok_nestedoperand:
436       ProtocolNestedOperandDef nop = (ProtocolNestedOperandDef) p;
437       if (nop.operand_mode().value() == ProtocolMode.pm_in)
438         sb.append("?");
439       else
440         sb.append("!");
441       ifacename = nop.operand()[0];
442       for (i=0;i<names.size();i++) {
443         if ( ifacename.compareTo(((reProvReq)(names.get(i))).orig) == 0 ) {
444           sb.append(((reProvReq)(names.get(i))).rew);
445         }
446       }
447       sb.append(".");
448       sb.append(nop.operand()[1]);
449       sb.append("{");
450       sb.append(normFrameProtToArch(nop.nested_operand(), names));
451       sb.append("}");
452       break;
453     case ProtocolOperKind.pok_repetition:
454       sb.append("(");
455       sb.append(normFrameProtToArch(((ProtocolUnOperationDef) p).operand(), names));
456       sb.append(")*");
457       break;
458     case ProtocolOperKind.pok_andparallel:
459       sb.append("(");
460       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand1(), names));
461       sb.append(")|(");
462       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand2(), names));
463       sb.append(")");
464       break;
465     case ProtocolOperKind.pok_orparallel:
466       sb.append("(");
467       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand1(), names));
468       sb.append(")||(");
469       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand2(), names));
470       sb.append(")");
471       break;
472     case ProtocolOperKind.pok_alternative:
473       sb.append("(");
474       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand1(), names));
475       sb.append(")+(");
476       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand2(), names));
477       sb.append(")");
478       break;
479     case ProtocolOperKind.pok_sequence:
480       sb.append("(");
481       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand1(), names));
482       sb.append(");(");
483       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand2(), names));
484       sb.append(")");
485       break;
486     case ProtocolOperKind.pok_restriction:
487       sb.append("(");
488       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand1(), names));
489       sb.append(")/(");
490       sb.append(normFrameProtToArch(((ProtocolBinOperationDef) p).operand2(), names));
491       sb.append(")");
492       break;
493     default: break;
494     }
495     return sb.toString();
496   }
497
498   private static class arrayOfArraysOfInsts extends java.util.ArrayList JavaDoc {
499     public arrayOfInsts myGet(int i) {
500       return (arrayOfInsts) get(i);
501     }
502     public int containsName(String JavaDoc name) {
503       for (int i=0;i<size();i++) {
504         if (myGet(i).containsName(name) != -1)
505           return i;
506       }
507       return -1;
508     }
509     public String JavaDoc protocol() {
510       StringBuffer JavaDoc ret = new StringBuffer JavaDoc(myGet(0).protocol());
511       for (int i=1;i<size();i++) {
512         ret.append("&");
513         ret.append(myGet(i).protocol());
514       }
515       return ret.toString();
516     }
517   }
518
519   private static class arrayOfInsts extends java.util.ArrayList JavaDoc {
520     public protInst myGet(int i) {
521       return (protInst) get(i);
522     }
523     public int containsName(String JavaDoc name) {
524       for (int i=0;i<size();i++) {
525         if (myGet(i).inst.getIdent().name.compareTo(name)==0)
526           return i;
527       }
528       return -1;
529     }
530     public String JavaDoc protocol() {
531       StringBuffer JavaDoc ret = new StringBuffer JavaDoc(myGet(0).protocol());
532       for (int i=1;i<size();i++) {
533         ret.append("&");
534         ret.append(myGet(i).setWithProtocol());
535       }
536       return ret.toString();
537     }
538     public void appendAI(arrayOfInsts a) {
539       for(int i=0;i<a.size();i++) {
540         add(a.myGet(i));
541       }
542     }
543   }
544
545   private static class protInst {
546     CompInst inst;
547     java.util.ArrayList JavaDoc set; //list of Strings
548
public protInst(CompInst i) {
549       inst = i;
550       set = new java.util.ArrayList JavaDoc();
551     }
552     public String JavaDoc protocol() {
553       return inst.protocol;
554     }
555     public String JavaDoc setWithProtocol() {
556       StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
557       if (set.size()==0)
558         return inst.protocol;
559       for (int i=0;i<set.size();i++) {
560         ret.append((String JavaDoc)set.get(i));
561         if (i!=set.size()-1)
562           ret.append(",");
563       }
564       ret.append("&");
565       ret.append(inst.protocol);
566       return ret.toString();
567     }
568     public void addToSet(String JavaDoc[] a) {
569       for(int i=0;i<a.length;i++) {
570         set.add(a[i]);
571       }
572     }
573   }
574
575   public String JavaDoc generateInstProtocol(DetectedConnectors dc) throws BadBindException, CDLExceptLock, CDLExceptRemote, CannotGenerateException {
576     StringBuffer JavaDoc protocol = new StringBuffer JavaDoc();
577     boolean first = true;
578     CompContained[] cont = contents();
579     for(int i=0;i<cont.length;i++) {
580       if (cont[i].objectKind() == ObjectsKind.o_Inst) {
581         if (first) {
582           first = false;
583         } else {
584           protocol.append("|");
585         }
586         protocol.append("("+ ((CompInst) cont[i]).generateArchProtocol(dc) +")");
587       }
588     }
589     if (dc.numOfBind() > 0) {
590       for(int i=0;i<dc.size();i++) {
591         DetectedConnector cn = dc.getConn(i);
592         if (cn.bindMode == CompBind.m_bind) {
593           protocol.insert(0,"((");
594           protocol.append(")&");
595           RoleDescription[] cRoles = new RoleDescription[cn.cRoles.size()];
596           RoleDescription[] sRoles = new RoleDescription[cn.sRoles.size()];
597           String JavaDoc[] operations = new String JavaDoc [cn.operations.length];
598           String JavaDoc prep;
599           // genrate sRole
600
for (int k=0;k<cn.sRoles.size();k++) {
601             operations = new String JavaDoc [cn.operations.length];
602             CompBindOper sRole = (CompBindOper) cn.sRoles.get(k);
603             if (sRole.kind == CompBindOper.b_provreq) {
604               sRole.elements.toFirst();
605               prep = "<sRole"+Integer.toString(k)+"-"+((CompBindType)sRole.elements.aktual()).name+">";
606             } else {
607               sRole.subs.toFirst();
608               sRole.elements.toFirst();
609               prep = "<sRole"+Integer.toString(k)+"-"+((CompBindType)sRole.subs.aktual()).name+":"+((CompBindType)sRole.elements.aktual()).name+">";
610             }
611             for (int j=0; j<operations.length;j++) {
612               operations[j] = prep +"."+ cn.operations[j];
613             }
614             sRoles[k] = new RoleDescription(sRole.toString(), operations);
615           }
616           //generate cRoles
617
for (int k=0;k<cn.cRoles.size();k++) {
618             operations = new String JavaDoc [cn.operations.length];
619             CompBindOper cRole = (CompBindOper) cn.cRoles.get(k);
620             if (cRole.kind == CompBindOper.b_provreq) {
621               cRole.elements.toFirst();
622               prep = "<"+((CompBindType)cRole.elements.aktual()).name+"-cRole"+Integer.toString(k)+">";
623             } else {
624               cRole.subs.toFirst();
625               cRole.elements.toFirst();
626               prep = "<"+((CompBindType)cRole.subs.aktual()).name+":"+((CompBindType)cRole.elements.aktual()).name+"-cRole"+Integer.toString(k)+">";
627             }
628             for (int j=0; j<operations.length;j++) {
629               operations[j] = prep +"."+ cn.operations[j];
630             }
631             cRoles[k] = new RoleDescription(cRole.toString(), operations);
632           }
633           // roles generated
634

635           for (int j=0; j<cRoles.length; j++) {
636             for (int k=0; k<operations.length; k++) {
637               protocol.append(cRoles[j].methods[k]);
638               protocol.append(",");
639             }
640           }
641           for (int k=0; k<operations.length; k++) {
642             if (k!=0)
643               protocol.append(",");
644             protocol.append(sRoles[0].methods[k]);
645           }
646           protocol.append("&");
647
648           SOFA.SOFAnode.Util.ConnectorProtocolGen.ConnectorProtocolGen generator;
649           try {
650             generator=SOFA.SOFAnode.Util.ConnectorProtocolGen.GenFactory.getConnectorProtocolGen(cn.using);
651           } catch (Exception JavaDoc e) {
652             throw new BadBindException("Unknown connector type '"+cn.using+"'",e);
653           }
654           protocol.append("("+generator.generate(cRoles, sRoles)+")");
655           protocol.append(")");
656         }
657       }
658     }
659     
660     return protocol.toString();
661    /* arrayOfArraysOfInsts aai = new arrayOfArraysOfInsts();
662     CompContained[] cont = contents();
663     for (int i=0;i<cont.length;i++) {
664       if (cont[i].objectKind() == ObjectsKind.o_Inst) {
665         arrayOfInsts ai = new arrayOfInsts();
666         ai.add(new protInst((CompInst)cont[i]));
667         aai.add(ai);
668       }
669       if (cont[i].objectKind() == ObjectsKind.o_Bind) {
670         CompBind bind = (CompBind) cont[i];
671         if (bind.mode == CompBind.m_bind && bind.lhs.kind==CompBindOper.b_sub) {
672           int ri, li; // index of rhs, index of lhs
673           bind.rhs.subs.toFirst(); bind.lhs.subs.toFirst();
674           bind.rhs.elements.toFirst(); bind.lhs.elements.toFirst();
675           ri = aai.containsName(((CompBindType)bind.rhs.subs.aktual()).name);
676           li = aai.containsName(((CompBindType)bind.lhs.subs.aktual()).name);
677           if (ri == li) { // this subcomps are already connected
678             arrayOfInsts rarr = aai.myGet(ri); // get current list
679             ri = rarr.containsName(((CompBindType)bind.rhs.subs.aktual()).name);
680             li = rarr.containsName(((CompBindType)bind.lhs.subs.aktual()).name);
681             int max = Math.max(ri, li);
682             protInst cur = rarr.myGet(ri); // provide inst
683             String provName = ((CompBindType) bind.rhs.elements.aktual()).name;
684             int j;
685             for (j=0;j<cur.inst.provOps.size();j++) { // searching provision in Inst
686               if ( ((CompInst.provIfaceOperations)cur.inst.provOps.get(j)).provide.compareTo(provName)==0 )
687                 break;
688             }
689             protInst toAdd = rarr.myGet(max);
690             toAdd.addToSet(((CompInst.provIfaceOperations)cur.inst.provOps.get(j)).toRewOperations());
691           } else { // connect these subcomps to one
692             arrayOfInsts rarr = (arrayOfInsts)aai.remove(ri);
693             protInst cur = rarr.myGet(0); // first inst
694             String provName = ((CompBindType) bind.rhs.elements.aktual()).name;
695             int j;
696             boolean found = false;
697             for (j=0;j<cur.inst.provOps.size();j++) { // searching provision in Inst
698               if ( ((CompInst.provIfaceOperations)cur.inst.provOps.get(j)).provide.compareTo(provName)==0 ) {
699                 found = true;
700                 break;
701               }
702             }
703             if (!found) { // some inst is used in several binds
704               throw new BadBindException();
705             }
706             cur.addToSet(((CompInst.provIfaceOperations)cur.inst.provOps.get(j)).toRewOperations());
707             // removed "ri" --> get new index of "li"
708             li = aai.containsName(((CompBindType)bind.lhs.subs.aktual()).name);
709             aai.myGet(li).appendAI(rarr);
710           }
711         }
712       }
713     }
714     return aai.protocol();
715
716     */

717   }
718
719   public void createOpArrays() throws CDLExceptLock, CDLExceptRemote{
720     int i,j;
721     java.util.ArrayList JavaDoc prov = new java.util.ArrayList JavaDoc();
722     java.util.ArrayList JavaDoc all = new java.util.ArrayList JavaDoc();
723     if (frame.isNew) { // frame is currently compiled
724
CompFrame fr = (CompFrame) frame.getRefferedObject(repository());
725       CompContained[] cont = fr.contents();
726       for (i=0;i<cont.length;i++) {
727         if (cont[i].objectKind() == ObjectsKind.o_Provides) {
728           String JavaDoc[] ops = ((CompProvide)cont[i]).operations.toArray();
729           String JavaDoc prep="";
730           for(j=0;j<provReq.size();j++) {
731             if ( ((reProvReq)provReq.get(j)).orig.compareTo(((CompProvide)cont[i]).getIdent().name)==0)
732               prep = ((reProvReq)provReq.get(j)).rew;
733           }
734           CompFrame.prependStringArray("?"+prep+".",ops);
735           for(j=0;j<ops.length;j++) {
736             all.add(ops[j]);
737             prov.add(ops[j]);
738           }
739         }
740         if (cont[i].objectKind() == ObjectsKind.o_Requires) {
741           String JavaDoc[] ops = ((CompRequire)cont[i]).operations.toArray();
742           String JavaDoc prep="";
743           for(j=0;j<provReq.size();j++) {
744             if ( ((reProvReq)provReq.get(j)).orig.compareTo(((CompRequire)cont[i]).getIdent().name)==0)
745               prep = ((reProvReq)provReq.get(j)).rew;
746           }
747           CompFrame.prependStringArray("!"+prep+".",ops);
748           for(j=0;j<ops.length;j++) {
749             all.add(ops[j]);
750           }
751         }
752       }
753     } else { // frame is in repository
754
try {
755         FrameDef fr = (FrameDef) frame.getRefferedObject(repository());
756         Contained[] cont = fr.contents(null);
757         for (i=0;i<cont.length;i++) {
758           if (cont[i].get_def_kind().value() == DefinitionKind.dk_Provides) {
759             String JavaDoc[] ops = CompInst.getNormFrameProvideOperations((ProvideDef)cont[i], repository());
760             String JavaDoc prep="";
761             for(j=0;j<provReq.size();j++) {
762               if ( ((reProvReq)provReq.get(j)).orig.compareTo(((ProvideDef)cont[i]).get_identification().name())==0)
763                 prep = ((reProvReq)provReq.get(j)).rew;
764             }
765             CompFrame.prependStringArray("?"+prep+".",ops);
766             for(j=0;j<ops.length;j++) {
767               prov.add(ops[j]);
768               all.add(ops[j]);
769             }
770           }
771           if (cont[i].get_def_kind().value() == DefinitionKind.dk_Requires) {
772             String JavaDoc[] ops = CompInst.getNormFrameRequireOperations((RequireDef)cont[i], repository());
773             String JavaDoc prep="";
774             for(j=0;j<provReq.size();j++) {
775               if ( ((reProvReq)provReq.get(j)).orig.compareTo(((RequireDef)cont[i]).get_identification().name())==0)
776                 prep = ((reProvReq)provReq.get(j)).rew;
777             }
778             CompFrame.prependStringArray("!"+prep+".",ops);
779             for(j=0;j<ops.length;j++) {
780               all.add(ops[j]);
781             }
782           }
783         }
784       } catch (RemoteException JavaDoc e) {
785         throw new CDLExceptRemote(e.getMessage());
786       } catch (TIRExceptLock e) {
787         throw new CDLExceptLock(e.getMessage());
788       }
789     }
790     provOpArray = new String JavaDoc[prov.size()];
791     allOpArray = new String JavaDoc[all.size()];
792     for(i=0;i<prov.size();i++) {
793       provOpArray[i] = (String JavaDoc) prov.get(i);
794     }
795     for(i=0;i<all.size();i++) {
796       allOpArray[i] = (String JavaDoc) all.get(i);
797     }
798   }
799
800   // *************** new protocols (with connectors) *************
801

802
803   /** Perform several tests, if bindings are correct and detects, which binds create connector.
804     * * Currently tests, if one interface is binded by connectors of one kind.
805     * @exception BadBindException bad bind occured
806     * @return detected connectors
807     */

808   private DetectedConnectors detectConnectors() throws BadBindException, CDLExceptRemote, CDLExceptLock {
809     DetectedConnectors dc = new DetectedConnectors();
810     CompContained[] cont = contents();
811     CompBindOper rhs, lhs;
812     for (int i=0;i<cont.length;i++) {
813       if (cont[i].objectKind() == ObjectsKind.o_Bind) {
814         try {
815           SOFA.SOFAnode.Util.ConnectorProtocolGen.GenFactory.addDetectedConnector(getConnectorOperations((CompBind)cont[i]),dc,(CompBind)cont[i]);
816         } catch (GeneratorNotFoundException e) {
817           throw new BadBindException("Unknown connector type used for binding.",e);
818         }
819       }
820     }
821     return dc;
822   } // *** end of detectConnectors()
823

824
825   private String JavaDoc[] getConnectorOperations(CompBind bind) throws BadBindException, CDLExceptLock, CDLExceptRemote {
826     int mode = bind.mode;
827     if (mode == CompBind.m_subsume || mode == CompBind.m_delegate || (mode == CompBind.m_bind &&bind.rhs.kind==CompBindOper.b_provreq && bind.lhs.kind==CompBindOper.b_provreq ) ) { // this frame
828
String JavaDoc sname;
829       if (mode == CompBind.m_delegate) {
830         bind.lhs.elements.toFirst();
831         sname = ((CompBindType) bind.lhs.elements.aktual()).name;
832       } else {
833         bind.rhs.elements.toFirst();
834         sname = ((CompBindType) bind.rhs.elements.aktual()).name;
835       }
836       if (frame.isNew) { // frame is currently compiled
837
CompFrame fr = (CompFrame) frame.getRefferedObject(repository());
838         CompContained[] prq = fr.lookup(sname);
839         if (prq==null || prq.length == 0)
840           throw new BadBindException("Unknown part \""+sname+"\" of bind in "+id.toString());
841         if (prq[0].objectKind() == ObjectsKind.o_Provides)
842           return ((CompProvide)prq[0]).operations.toArray();
843         if (prq[0].objectKind() == ObjectsKind.o_Requires)
844           return ((CompRequire)prq[0]).operations.toArray();
845       } else { // frame is in repository
846
try {
847           FrameDef fr = (FrameDef) frame.getRefferedObject(repository());
848           Contained[] prq = fr.lookup_name(sname);
849           if (prq==null || prq.length == 0)
850             throw new BadBindException("Unknown part \""+sname+"\" of bind in "+id.toString());
851           if (prq[0].get_def_kind().value() == DefinitionKind.dk_Provides)
852             return CompInst.getNormFrameProvideOperations((ProvideDef)prq[0], repository());
853           if (prq[0].get_def_kind().value() == DefinitionKind.dk_Requires)
854             return CompInst.getNormFrameRequireOperations((RequireDef)prq[0], repository());
855         } catch (RemoteException JavaDoc e) {
856           throw new CDLExceptRemote(e.getMessage());
857         } catch (TIRExceptLock e) {
858           throw new CDLExceptLock(e.getMessage());
859         }
860       }
861     } else { // it is bind between instances
862
String JavaDoc sname;
863       String JavaDoc iname;
864       bind.rhs.elements.toFirst();
865       bind.rhs.subs.toFirst();
866       sname = ((CompBindType) bind.rhs.elements.aktual()).name;
867       iname = ((CompBindType) bind.rhs.subs.aktual()).name;
868       CompContained[] inst = lookup(iname);
869       if (inst==null || inst.length == 0)
870         throw new BadBindException("Unknown part \""+iname+"\" of bind in "+id.toString());
871       if (((CompInst)inst[0]).subcom.isNew) {
872         CompFrame fr = (CompFrame) CompInst.getInstFrame(((CompInst)inst[0]).subcom, repository()).getRefferedObject(repository());
873         CompContained[] prq = fr.lookup(sname);
874         if (prq==null || prq.length == 0)
875           throw new BadBindException("Unknown part \""+sname+"\" of bind in "+id.toString()+"\n ### "+ fr.id.toString());
876         if (prq[0].objectKind() == ObjectsKind.o_Provides)
877           return ((CompProvide)prq[0]).operations.toArray();
878         if (prq[0].objectKind() == ObjectsKind.o_Requires)
879           return ((CompRequire)prq[0]).operations.toArray();
880       } else {
881         try {
882           FrameDef fr = (FrameDef) CompInst.getInstFrame(((CompInst)inst[0]).subcom, repository()).getRefferedObject(repository());
883           Contained[] prq = fr.lookup_name(sname);
884           if (prq==null || prq.length == 0)
885             throw new BadBindException("Unknown part \""+sname+"\"of bind in "+id.toString());
886           if (prq[0].get_def_kind().value() == DefinitionKind.dk_Provides)
887             return CompInst.getNormFrameProvideOperations((ProvideDef)prq[0], repository());
888           if (prq[0].get_def_kind().value() == DefinitionKind.dk_Requires)
889             return CompInst.getNormFrameRequireOperations((RequireDef)prq[0], repository());
890         } catch (RemoteException JavaDoc e) {
891           throw new CDLExceptRemote(e.getMessage());
892         } catch (TIRExceptLock e) {
893           throw new CDLExceptLock(e.getMessage());
894         }
895       }
896     }
897   
898
899     return null;
900   }
901
902 }
903
Popular Tags