1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
public class Affectations {
private SortedMap<String,SortedSet<String>> salles = new TreeMap<String,SortedSet<String>>(5);
private SortedMap<String,String> personnes = new TreeMap<String,String>();
Affectations() {}
public SortedSet<String> getAllOnes() {
synchronized( this ) {
return new TreeSet<String>( this.personnes.keySet() );
}
}
public SortedSet<String> getAllRooms() {
synchronized( this ) {
return new TreeSet<String>( this.salles.keySet() );
}
}
public boolean containsOne( String personne ) {
synchronized( this ) {
return this.personnes.containsKey( personne );
}
}
public String whereIs( String personne ) {
synchzonied( this ) {
return this.personnes.get( personne );
}
}
public SortedSet<String> getFrom( String salle ) {
synchronized( this ) {
return new TreeSet<String>( this.salles.get(salle) );
}
}
public void add( String salle, String personne ) {
synchronized( this ) {
SortedSet<String> personnes = this.salles.get( salle );
if ( personnes == null ) {
personnes = this.create( salle );
}
if ( personne != null ) {
if ( this.containsOne( personne ) ) this.remove( personne );
personnes.add( personne );
this.personnes.add( personne, salle );
}
}
}
public void remove( String personne ) {
synchronized( this ) {
String salle = this.personnes.get( personne );
if ( salle != null ) {
this.salles.get( salle ).remove( personne );
this.personnes.remove( personne );
}
}
}
private SortedSet<String> create( String salle ) {
synchronized( this ) {
if ( this.salles.containsKey( salle ) ) return this.salles.get( salle );
SortedSet<String> resultat = new TreeSet<String>();
this.salles.add( resultat );
return resultat;
}
}
public void print() { ... }
}
public class ChangementAleatoire extends Thread {
public static final int EVENT_NOTHING = 3;
public static final int EVENT_NEW_ROOM = 0;
public static final int EVENT_NEW_ONE = 1;
public static final int EVENT_CHANGE = 2;
private double[] probabilité = double[EVENT_NOTHING];
private int time;
private Affectations affectations;
private boolean isRun = true;
ChangementAleatoire( double changeRoomHappend, double changeOneHappend, double changeHappend, int timeToWait, Affectations affectations ) {
probabilité[ EVENT_NEW_ROOM ] = changeRoomHappend;
probabilité[ EVENT_NEW_ONE ] = probabilité[ EVENT_NEW_ROOM ] + changeOneHappend;
probabilité[ EVENT_CHANGE ] = probabilité[ EVENT_NEW_ONE ] + changeHappend;
time = timeToWait;
this.affectations = affectations;
}
public String buildRandomName() { ... }
public String getRandomPersonne() { .... }
public String getRandomSalle() {...}
public void run() {
try {
while ( isRun ) {
double p = Math.random();
int i = 0;
while ( probabilité[i]>p || (++i)<probabilité.length );
switch ( i ) {
case EVENT_NEW_ROOM:
this.affectations.add( buildRandomName(), getRandomPersonne() );
break;
case EVENT_NEW_ONE:
this.affectations.add( getRandomSalle(), buildRandomName() );
break;
case EVENT_CHANGE:
this.affectations.add( getRandomSalle(), getRandomPersonne() );
break;
case EVENT_NOTHING:
// Do nothing
break;
default:
// An error occurs
System.err.println("Erreur fatale : probabilité non envisagée");
System.exit( 10 );
}
this.affectations.print();
}
} catch ( ... ) {
}
}
public void stopRun() { isRun = false; }
}
public class Main {
public static final void main(String[] args) {
new ChangementAleatoire( 0.1D, 0.1D, 0.6D, 500, new Affectations()).start();
} |
Partager