Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

/* ---------------------------------------------------------------------------------- ESPACES DE STRANDS ---------------------------------------------------------------------------------- BIBLIOTHEQUE DE PROGRAMMATION AUTEUR : MOHAMED ANIS MEKKI SPECIALITE : MASTERE EN SYSTEMES DE COMMUNICTAIONS UNIVERSITE : ENIT/ UNIVERSITE TUNIS EL MANAR I ---------------------------------------------------------------------------------- */ public class strandspace // SUPER CLASSE strandspace { static final private int nospss = 20; // nombre max de strands dans un strandspace static final private int noaps = 100; // nombre max d'actions par strand static final private int nonpa = 20; // nombre max de successeurs (au sens I/O) d'une action static final private int noppa = 2000; // nombre max de prédécesseurs (au sens I/O) d'une action // ------------------------------ Partie déclarative ----------------------------- // Classe Interne coord ---------------------------------------------------------- public static class coord // C'est un couple d'entier, ou coordonnées entières. { int s; int p; public coord() // Construction d'un coord nul. { s = -1; p = -1; } public coord(int ss, int pp) // Construction à partir de valeurs entières. { s = ss; p = pp; } public coord(coord c) // Construction à partir d'un coord : clonage effectif et non référencé. { if ( c != null ) { coord tmp = new coord(c.s,c.p); s = tmp.s; p = tmp.p; } else { s=-1; p=-1; } } }// fin classe coord //-------------------------------------------------------------------------------- // Classe Interne action --------------------------------------------------------- public static class action { String event; // libellé de l'événement : "out" ou "in" coord next[]; // next au sens I/O, la précédance au sens causal, // sera sous-entendue par la position de l'action dans le strand. public action() // Construction d'une action nulle ... { event = new String(); next = new coord[nonpa]; } public action(String e,coord n[]) // Construction d'une action à partir de valeurs. { if (e != null) { event = e; } else { event = new String(); } next = new coord[nonpa]; if ( n != null ) { int i = 0; while ( i < nonpa) { if (n[i] != null) { next[i] = new coord(n[i]); } i++; } } } public action(action at) // Construction d'une action à partir d'une autre action : // clonage effectif et non référencé. { if (at != null) { action tmp = new action(at.event,at.next); event = tmp.event; next = tmp.next; } else { event = new String(); next = new coord[nonpa]; } } }// fin class action //-------------------------------------------------------------------------------- // Classe Interne strand --------------------------------------------------------- public static class strand { action actions[]; public strand() // Construction d'un strand nul. { actions = new action[noaps]; } public strand(action ac[]) // Construction d'un strand à partir de valeurs. { actions = new action[noaps]; if (ac != null) { int k = 0; while ( k < noaps ) { if (ac[k] != null) { actions[k] = new action(ac[k].event,ac[k].next); } k++; } } } public strand(strand strd) // construction d'un Strand à partir d'un autre strand : // clonage effectif et non référencé. { if (strd != null) { strand tmp = new strand (strd.actions); actions = tmp.actions; } else { actions = new action[noaps]; } } }// fin classe strand //-------------------------------------------------------------------------------- //-------------------------------------------------------------------------------- // Déclaration d'un strandspace : Espace de Strands //------------------------------------------------- // strand strands[]; // tableau de strand int conflict[][]; // Relation de conflit représentée par une matrice. // CONVENTIONS : //---------- // - L'indice d'un strand est son indice dans le tableau strands[]. // - Les strands d'indice p et q ne sont pas en conflit : conflict[p][q] == 0. // - Les strands d'indice p et q sont en conflit : conflict[p][q] == 1. // ---------------- Méthodes de la super classe strandspace --------------------- public strandspace() // Construction d'un strandspace nul. { conflict = new int[nospss][nospss]; strands = new strand[nospss]; } public strandspace(strand strs[],int conf[][]) // Construction d'un strandspace à partir de ces propriétés. { conflict = new int[nospss][nospss]; if (conf != null) { int m=0,n=0; while (m= 1) { j--; strands[i].actions[j+1] = strands[i].actions[j]; if (!(((strands[i].actions[j])== null))) { while ( k < (nonpa-1) ) { k++; if (!((((strands[i]).actions[j]).next[k])== null)) { ((((strands[i]).actions[j]).next[k]).p) ++; } // fin if strand[i].actions[j].next[k] non nul } // fin while k } // fin if action non nulle k=-1; } // fin while j strands[i].actions[0] = a; } // fin if strand non nul j=noaps-1; } //fin while i } // fin si assez de conflit return(result); }// fin méthode prefix() //-------------------------------------------------------------------------------- // Surcharge de toString() : // Renvoie une chaîne de caractère contenant une vue du graphe du strand ... // ( intitulés des actions ) //-------------------------------------------------------------------------------- public String toString() { String res=""; int ii=-1; int jj=-1; while ( ii < (nospss-1)) //while i { ii++; if (!(strands[ii]== null)) //if 1 { while ( jj < (noaps-1)) //while j { jj++; if (!(strands[ii].actions[jj]== null)) //if2 { res=res+"("+ii+","+jj+")"+strands[ii].actions[jj].event+"\n"; }//fin if2 }// fin while j }// fin if1 jj=-1; }//while i return(res); } // Show() Renvoie une chaîne de caractère contenant une vue du graphe du strand // ( intitulés des actions + leurs next) //-------------------------------------------------------------------------------- public String show() { String res=""; int ii=-1; int jj=-1; int kk=-1; coord x = new coord(); while ( ii < (nospss-1))//while i { ii++; if (!(strands[ii]== null))//if 1 { while ( jj < (noaps-1))//while j { jj++; if (!(strands[ii].actions[jj]== null))//if2 { res=res+"("+ii+","+jj+")"+strands[ii].actions[jj].event+"\n"; while (kk < (nonpa-1)) { kk++; if (!(strands[ii].actions[jj].next[kk]== null))//if3 { x = strands[ii].actions[jj].next[kk]; res = res+"\t\t\t\t\t\t\t\t===>"; res = res+"("+x.s+","+x.p+")\n"; } // fin if3 } //fin while k } //fin if2 kk=-1; } // fin while j } // fin if1 jj=-1; } //while i return(res); } // fin méthode show() // Renvoie une chaîne de caractère contenant la relation de conflit du strandspace //-------------------------------------------------------------------------------- public String show_conflict() { boolean done = false; int i = -1; int j = -1; String cols ="\t\t"; String res = ""; while (i < (nospss-1)) //while i { i++; if (!(strands[i]== null))// i non nul { res = res+"["+format(i)+"] "; while (j < (nospss-1)) //while j { j++; if (!(strands[j]== null))// j non nul { if (!done) { cols = cols+"["+format(j)+"]"+"\t"; } res=res+" "+conflict[i][j]+" "+"\t"; }// fin j non nul } //fin while j done = !cols.equals(""); res = res+"\n"; }// fin i non nul j=-1; }// fin while i cols = cols+"\n"; return(cols+res); }// fin show_conflict() //-------------------------------------------------------------------------------- // ---------------------------------------------- // ----------------------- Méthode public void newindex(coord c[],int l) ------- // ---------------------------------------------- // Arguments : // -------- // - Une bijection de réindexation coord[20] : // coord[i].s = nouvel indice, coord[i].p = ancien indice // - Cardinal de l'ensemble de définition de la bijection : int l. // // Description : // -------- // Effectue la réindexation du strandspace qui l'invoque, // en fonction de la bijection coord c[]. // ------------------------------------------------------------------------------- public void newindex(coord c[],int l) { int n=-1; int j=-1; int k=-1; int m = -1; int x=0; int y=0; int a=-1; int b=-1; strand s=new strand(); strandspace ess = new strandspace(this); // Copie de suavegarde de l'ancien strandspace. while (m < l-1) // while m // boucle de parcours de la bijection c[] { m++; x = c[m].s; y = c[m].p; // ------------- // ----------------------- Réindexation du Strand -------------------------------- // ------------- // Réindexe le strand d'indice y, en x. s = new strand(ess.strands[y]); // Clonage effectif du strand d'indice y et non référencé strands[x] = new strand(s); // ---------------------- // ----------------- Mise à jour de la précédence de contrôle ------------------- // ---------------------- // Dans le strand en cours (strands[x] = s), // met à jour tous les appels s.actions[j].next[k] // en réindexant leur strands d'arrivée ( s.actions[j].next[k].s ) // à l'aide de la bijection c[] : c[n].s -->c[n].p // // Remarque : // ess reste intacte et toujours égale à la valeur initiale de l'objet appelant. j=-1; k=-1; n=-1; if (!(s== null)) // si le strand s n'est pas nul { while ( j < (noaps-1)) // parcours des actions du strand s : j { j++; if (!((s.actions[j])== null)) // si l'action s.actions[j] n'est pas nulle { while ( k < (nonpa-1)) // parcours des next de l'action s.actions[j]: k { k++; if (!(((s.actions[j]).next[k])== null)) // si le next (s.actions[j])[k] n'est pas nul { while ( n < l-1 )// while n : parcours { n++; if ( ((s.actions[j]).next[k]).s == c[n].p ) // Si next pointe vers un ancien indice { (((strands[x]).actions[j]).next[k]).s = c[n].s; // Alors mettre à jour l'indice. } } //fin while n n=-1; } // fin if strand[i].actions[j].next[k] non nul } // fin while k } // fin if strand[i].actions[j] non nul k=-1; } // fin while j } // fin if strands[i] non nul } // fin while m : Fin de la boucle de parcours de la bijection c[]. // -------------------- // ----------------------- Réindexation de la relation de conflit ---------------- // -------------------- a=-1; b=-1; m=-1; int aa = 0; int bb = 0; while ( a < (nospss-1) ) // parcours des indices de lignes de la nouvelle relation de conflit { a++; while ( b < (nospss-1) ) // parcours des indices de colonnes de la nouvelle relation de conflit { b++; while (m < l-1) // parcours de la bijection de réindexation { m++; if ((c[m].s == a)) // L'ancien indice de la ligne a, trouvé : aa { aa = c[m].p; } if ((c[m].s == b)) // L'ancien indice de la colonne b, trouvé : bb { bb = c[m].p; } } // while m conflict[a][b] = ess.conflict[aa][bb]; // Deux strands d'indices a et b sont en conflit dans l'ES réindexé // ssi // les strands portant leurs anciens indices aa et bb // sont en conflit dans l'ancien (ess). m=-1; } //while b b=-1; } //while a //-------------------------- Fin Réindexation de la relation de conflit ---------- } // -------------------------- FIN METHODE newindex() ----------------------------- // ------------------------------------------------------------------ // - Méthode public static strandspace compose(strandspace s01,strandspace s02) - // ------------------------------------------------------------------ // Arguments : // -------- // - strandspace s01,s02 : deux strandpace à composer // // Description : // -------- // Renvoie le strandspace composé de deux strandspace // // Remarque : // ------- // La composition se fait en insérant d'abord s01, ensuite s02 // Autrement dit si s01 contient n strands // Le premier strand de s02 dans le strand composé renvoyé aura l'indice n+1. // // Deux hypothèses sont à vérifier avant d'appeler compose() : // --------------------------------- // 1 - s01 et s02 sont bien indexés dans le sens que : // entre deux indices de strands non nuls, // il n'existe pas un indice, dont le strand est nul. // 2 - si on appelle card(s) = // card( {i | i indice de strands de s \ s.strands[i] <> null }) alors : // card(s01)+card(s02) <= static final private nospss // ( nospss = nombre maximum de strands dans un strandspace ) // En effet il faut bien que compose() tienne // dans la structure de strandspace définie plus haut !!! // ------------------------------------------------------------------------------- public static strandspace compose(strandspace s01,strandspace s02) { strandspace s = new strandspace(); strandspace s1 = new strandspace(s01); strandspace s2 = new strandspace(s02); int is1=nospss; // variable servant à stocker card(s1)+1 int i =-1; int j; // Insertion des strands de s1 dans le strand composé .... while ( i < (nospss-1) ) { i++; if (!(s1.strands[i]== null)) // si on n'a pas atteint la fin de s1 ( premier strand nul trouvé) { s.strands[i] = s1.strands[i]; // insérer les strands de s1 dans s. } else // Premier strand nul : // on stocke son indice dans is1 et on force la fin de la boucle while { is1 = i; i=nospss; } // fin else } // fin while // Prépéparation de la bijection de réindexation de s2 // ( il faut décaler ses indices de is1 ) i=-1; int is2 = nospss;// contiendra card(s02)+1 coord reindexs2[] = new coord[nospss]; while (i < (nospss-1)) { i++; if (s2.strands[i] == null) { if (is2 == nospss) { is2 = i; } } // if 1 if ( (i <= (nospss-1-is1)) && (!(s2.strands[i]== null)) ) // On ne réindexera que le nombre de strands non nuls de s2 // et dont le nombre ne dépasse pas nosposs - card(s1) { reindexs2[i] = new coord(i+is1,i); reindexs2[i+is1] = new coord(i,i+is1); // le strand i sera réindexé en i+is1 // et réciproquement ... (*) // les strands nuls ne seront pas réindexés ... } // fin if 1 else { if (reindexs2[i] == null) // if2 : pour ne pas écraser les réindexations réciproques voir (*) { reindexs2[i] = new coord(i,i); } // fin if 2 } // fin else if 1 } // fin while // Application de la réindexation à s2 s2.newindex(reindexs2,nospss); // Insertion des strands de s2, bien réindexés à la suite de ceux de s1 i=-1; while ( i < (is2-1) ) { i++; s.strands[i+is1] = s2.strands[i+is1]; } // Mise à jour de la relation de conflit de compose() i=-1; j=-1; while ( i < (nospss-1) ) // parcours des lignes i { i++; while ( j < (nospss-1) ) // parcours des colonnes j { j++; if ( (i < is1) && (j < is1) ) // La relation de conflit de compose(), restreinte à s1 est exactement celle de s1 { s.conflict[i][j] = s1.conflict[i][j]; }// fin if else { //if 2 if ( (i >= is1) && (j >= is1) ) // La relation de conflit de compose(), restreinte à s2 est exactement celle de s2 { s.conflict[i][j] = s2.conflict[i][j]; } // fin if2 //else2 else //Pas de conflit entre des strands non issues de la même composnate initiale { s.conflict[i][j] = 0; } // fin else2 } // fin else } // fin parcours des colonnes j j=-1; } // fin parcours des lignes i return(s); } // -------------------------- Fin Compose() -------------------------------------- // // -- Méthode public static strandspace somme(strandspace s01,strandspace s02) -- // // Arguments : // -------- // strandspace s01,s02 : deux strandpace à sommer // // Description : // -------- // Renvoie le strandspace somme de deux strandspace // // Remarque : // ------- // La composition se fait en insérant d'abord s01, ensuite s02 // Autrement dit si s01 contient n strands // Le premier strand de s02 dans le strand somme renvoyé aura l'indice n+1. // // Deux hypothèses sont à vérifier avant d'appeler compose() : // ----------------------------------------------------------- // 1 - s01 et s02 sont bien indexés dans le sens que : // entre deux indices de strands non nuls, // il n'existe pas un indice, dont le strand est nul. // 2 - si on appelle : // card(s) = card( {i | i indice de strands de s \ s.strands[i] <> null }) // alors : // card(s01)+card(s02) <= static final private nospss // ( nospss = nombre maximum de strands dans un strandspace ) // En effet il faut bien que somme() // tienne dans la structure de strandspace définie plus haut !!! // ------------------------------------------------------------------------------- public static strandspace somme(strandspace s01,strandspace s02) { strandspace s = new strandspace(); strandspace s1 = new strandspace(s01); strandspace s2 = new strandspace(s02); int is1=-1; // variable servant à stocker card(s1)+1 int i =-1; int j; // Insertion des strands de s1 dans le strand somme .... while ( i < (nospss-1) ) { i++; if (!(s1.strands[i]== null)) // si on n'a pas atteint la fin de s1 ( premier strand nul trouvé) { s.strands[i] = s1.strands[i]; // insérer les strands de s1 dans s. } else // Premier strand nul : //on stocke son indice dans is1 et on force la fin de la boucle while { is1 = i; i=nospss; } // fin else } // fin while // Prépéparation de la bijection de réindexation de s2 //( il faut décaler ses indices de is1 ) i=-1; int is2 = nospss; // contiendra card(s02)+1 coord reindexs2[] = new coord[nospss]; while (i < (nospss-1)) { i++; if (s2.strands[i] == null) { if (is2 == nospss) { is2 = i; } } // if 1 if ( (i <= (nospss-1-is1)) && (!(s2.strands[i]== null)) ) // On ne réindexera que le nombre de strands non nuls de s2 // et dont le nombre ne dépasse pas nosposs - card(s1) // les strands nuls ne seront pas réindexés ... { reindexs2[i] = new coord(i+is1,i); // le strand i sera réindexé en i+is1 reindexs2[i+is1] = new coord(i,i+is1); // et réciproquement ... (*) } // fin if 1 else { if (reindexs2[i] == null) // if2 : pour ne pas écraser les réindexations réciproques voir (*) { reindexs2[i] = new coord(i,i); } // fin if 2 } // fin else if 1 } // fin while // Application de la réindexation à s2 s2.newindex(reindexs2,nospss); // Insertion des strands de s2, bien réindexés à la suite de ceux de s1 i=-1; while ( i < (is2-1) ) { i++; s.strands[i+is1] = s2.strands[i+is1]; } // Mise à jour de la relation de conflit de somme() i=-1; j=-1; while ( i < (nospss-1) ) // parcours des lignes i { i++; while ( j < (nospss-1) ) // parcours des colonnes j { j++; if ( (i < is1) && (j < is1) ) // La relation de conflit de somme(), restreinte à s1 est exactement celle de s1 { s.conflict[i][j] = s1.conflict[i][j]; }// fin if else { //if 2 if ( (i >= is1) && (j >= is1) ) //La relation de conflit de somme(), restreinte à s2 est exactement celle de s2 { s.conflict[i][j] = s2.conflict[i][j]; } // fin if2 //else2 else // Conflit entre des strands non issues de la même composnate initiale { s.conflict[i][j] = 1; } // fin else2 } // fin else } // fin parcours des colonnes j j=-1; } // fin parcours des lignes i return(s); } // ------------------------------------- Fin somme() ----------------------------- public static String format(int n) { if (n < 10) { return("0"+n); } else { return(n+""); } } public static void main(String args[]) { strandspace es = new strandspace(); strandspace aes = new strandspace(); strandspace aaes = new strandspace(); strandspace aaaes = new strandspace(); // Définition de 4 strandspace vides es.conflict[0][1] = 1; es.conflict[0][2] = 1; es.conflict[0][3] = 1; es.conflict[1][0] = 1; es.conflict[1][2] = 0; es.conflict[1][3] = 1; es.conflict[2][0] = 1; es.conflict[2][1] = 0; es.conflict[2][3] = 1; es.conflict[3][0] = 1; es.conflict[3][1] = 1; es.conflict[3][2] = 1; // Définition de la realtion de conflit pour le strandspace es // Remarque : les strands 1 et 2 ne sont pas en conflit. strand s1 = new strand(); strand s2 = new strand(); strand s3 = new strand(); strand s4 = new strand(); // Définition de quelques strands pour alimenter les strandspaces action a1 = new action(); action a2 = new action(); action a3 = new action(); action a4 = new action(); action a41 = new action(); action a42 = new action(); action a5 = new action(); action a6 = new action(); action a7 = new action(); action a51 = new action(); action a61 = new action(); action a71 = new action(); action for_prefix = new action(); // Définition de quelques actions pour alimenter les strands for_prefix.event="in/out[i,i]"; for_prefix.next[0] = new coord(3,2); a1.event = "in/out[0,0]"; a1.next[0] = new coord(3,0); a2.event = "in/out[0,1]"; a2.next[0] = new coord(3,1); a3.event = "in/out[0,2]"; a3.next[0] = new coord(3,2); a4.event = "in/out[1,0]"; a4.next[0] = new coord(0,0); a41.event = "in/out[1,1]"; a41.next[0] = new coord(0,1); a42.event = "in/out[1,2]"; a42.next[0] = new coord(0,2); a5.event = "in/out[2,0]"; a5.next[0] = new coord(0,0); a6.event = "in/out[2,1]"; a6.next[0] = new coord(0,1); a7.event = "in/out[2,2]"; a7.next[0] = new coord(0,2); a51.event = "in/out[3,0]"; a51.next[0] = new coord(0,0); a61.event = "in/out[3,1]"; a61.next[0] = new coord(0,1); a71.event = "in/out[3,2]"; a71.next[0] = new coord(0,2); // Pour chaque action, définition d'un next et de son intitulé // L'intitulé de chaque action a été choisi de manière à repérer // son emplacement futur dans le strandspace : // [indice du strand,indice de l'action] // Ceci nous permettra d'observer l'impact d'un futur préfixage // ou d'une future réindexation ... s1.actions[0] = a1; s1.actions[1] = a2; s1.actions[2] = a3; s2.actions[0] = a4; s2.actions[1] = a41; s2.actions[2] = a42; s3.actions[0] = a5; s3.actions[1] = a6; s3.actions[2] = a7; s4.actions[0] = a51; s4.actions[1] = a61; s4.actions[2] = a71; // Pour chaque strand, définition des actions qui le composent es.strands[0] = s1; es.strands[1] = s2; es.strands[2] = s3; es.strands[3] = s4; // Définition des strands de es System.out.println("Graphe du strand es :\n"); System.out.println(es.show()); System.out.println("Relation de conflit du strand es :\n"); System.out.println(es.show_conflict()+"\n"); // Affichage du strand es : Graphe + Relation de conflit System.out.println("1ère tentative de préfixage de es ...\n"); System.out.print("Résultat du préfixage de es : "); System.out.println(es.prefix(for_prefix)+"\n"); // Echec lors du préfixage de es : pas assez de conflit (strands 1 et 2) es.conflict[1][2] = 1; es.conflict[2][1] = 1; System.out.println("\nMise à jour de la relation de conflit du strandsapce es ...\n\n"); // Ajout du conflit entre les strands 1 et 2 System.out.println("2ème tentative de préfixage de es ...\n"); System.out.print("Résultat du préfixage de es : "); System.out.println(es.prefix(for_prefix)+"\n"); // Préfixage du strandspace es par l'action for_prefix System.out.println("Graphe du strand es préfixé :\n"); System.out.println(es.show()); System.out.println("Relation de conflit du strand es préfixé :\n"); System.out.println(es.show_conflict()+"\n"); // Affichage du strand es préfixé : Graphe + Relation de conflit coord crd[] = new coord[4]; crd[0] = new coord(0,3); crd[1] = new coord(1,1); crd[2] = new coord(2,2); crd[3] = new coord(3,0); // Définition d'une réindexation de strands es.newindex(crd,4); // Réindexation des strands de es, à l'aide de la réindexation précédente System.out.println("Graphe du strand es réindexé :\n"); System.out.println(es.show()); System.out.println("Relation de conflit du strand es réindexé:\n"); System.out.println(es.show_conflict()+"\n"); // Affichage du strand es réindexé : Graphe + Relation de conflit aes = new strandspace(compose(es,es)); // Composition de 2 copies du strandspace es dans le strandspace aes System.out.println("Graphe du strand es || es :\n"); System.out.println(aes.show()); System.out.println("Relation de conflit du strand es || es :\n"); System.out.println(aes.show_conflict()+"\n"); // Affichage du strand aes = es || es : Graphe + Relation de conflit aaes = new strandspace(somme(es,es)); // Sommation de 2 copies du strandspace es dans le strandspace aes System.out.println("Graphe du strand es + es :\n"); System.out.println(aaes.show()); System.out.println("Relation de conflit du strand es + es :\n"); System.out.println(aaes.show_conflict()); // Affichage du strand aes = es + es : Graphe + Relation de conflit }// fin main() }