Appendice
Tutto il codice prodotto durante il nostro lavoro è presente in un package Java denominato broker.jar, quindi comprendente sia il codice del Broker server sia quello della brokerLib. Nelle prossime pagine presenteremo solo alcuni sotto-package presenti nel package principale. Il codice verrà illustrato per mezzo di un formalismo simile a quello adottato da tool JavaDoc. Tale formalismo consiste nell’elencare per ogni oggetto la segnatura dei metodi pubblici forniti, eventualmente corredati da un commento sulle funzionalità svolte. Inoltre abbiamo arricchito tale formalismo dettagliando alcuni metodi con il codice sorgente con i quali sono implementati. In particolari sono presentati, in ordine i seguenti oggetti nei relativi package:
Per il package Filter.jar:
• Filter
• Constraint
• ReservationConstraint
• Value
Per il package Reservation.jar
• Reservation
• TimeSlot
• Booking
broker.filter
Class Filter
java.lang.Object
broker.filter.Filter
All Implemented Interfaces:
java.io.Serializable
public class Filter extends java.lang.Object
implements java.io.Serializable
Field Summary
static int MATCH
static int NOFOUND
static int NOMATCH
Constructor Summary
Filter(ResourceType resourceType)
Filter(ResourceType resourceType, Constraint constraint)
Filter(ResourceType resourceType,
ReservationConstraint baseReservationConstraint)
Filter(ResourceType resourceType,
ReservationConstraint baseReservationConstraint, Constraint constraint)
Filter(ResourceType resourceType, java.sql.Timestamp timestamp)
Method Summary
int checkAttributes(java.util.Hashtable attrib utes)
Effettua il matching tra gli attributi del filtro e quelli della Hashtable di attributi.
void consolidateConstraint()
java.util.Iterator consolidateConstraintsIterator()
java.lang.String generateKey()
java.util.TreeMap getAllConstraints()
AttributesCompare getAttributesCompare()
Constraint getConsolidateConstraint(java.lang.String key)
Constraint getConstraint(java.lang.String attribute)
int getConstraintsCount()
java.lang.String getKey()
ReservationConstraint getReservationConstraint(java.lang.String type)
java.util.Hashtable getReservationConstraintHash()
ResourceType getResourceType()
Constraint getVocabularyConstraint(java.lang.String a ttribute)
boolean hasMoreAttributes()
void initCheckAttributes()
boolean mustOrder()
Serve per impostare un filtraggio ordinate.
void mustOrder(boolean flag)
java.lang.String nextAttributes()
boolean NotDiscardIfConstraintNotPresent()
boolean remove()
boolean set(Constraint constraint)
Imposta un nuovo vincolo su un attributo di descrizione delle risorse se e solo se non è già presente un vincolo su tale attributo nella Hashtable del filtro.
boolean set(java.lang.String attribute, java.lang.String metric,
java.lang.String value,
java.lang.String type, byte boolOp)
boolean set(java.lang.String attribute, Value value, byte boolOp)
java.util.Vector set(java.util.Vector constraintVect)
Aggiunge un insieme di vincoli, restituisce un vettore contenente i vincoli non aggiunti perché già esistenti.
void setBeginTimeReservation(java.sql.Timestamp ts)
Imposta l’istante iniziale dello slot temporale della prenotazione valido per ogni parte prenotabile.
void setNotDiscardIfConstraintNotPresent(boolea n flag)
void setRateBaseReservationConstraint(float rat e)
boolean setReservationConstraint(ReservationConstr aint reservationConstraint)
void setResourceType(ResourceType resourceType)
boolean thereAreNewConstraints()
boolean thereAreNewReservationConstraints()
java.lang.String toString()
Constraint update(Constraint constraint)
Constraint update(java.lang.String attribute, Value value, byte boolOp)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Filter
public Filter(ResourceType resourceType, Timestamp timestamp) { reservationConstraint = new BaseReservationConstraint();
reservationConstraint.setTimeSlotBegin( timestamp );
setResourceType(resourceType);
}
Filter
public Filter(ResourceType resourceType) {
reservationConstraint = new BaseReservationConstraint();
reservationConstraint.setTimeSlotBegin( now );
setResourceType(resourceType);
}
Filter
public Filter(ResourceType resourceType, Constraint constraint) { this(resourceType);
set(constraint);
}
Filter
public Filter(ResourceType resourceType, ReservationConstraint baseReservationConstraint) { this.reservationConstraint = reservationConstraint;
reservationConstraint.setTimeSlotBegin( now );
setResourceType(resourceType);
}
Filter
public Filter( ResourceType resourceType, ReservationConstraint baseReservationConstraint,Constraint constraint) { this(resourceType,baseReservationConstraint);
set(constraint); }
Method Detail
set
Parameters:
constraint - Il vincolo da aggiungere alla Hashtable del filtro.
Returns:
true se il vincolo settato non è già presente nella Hashtable del filtro, false altrimenti.
Throws:
public boolean set( Constraint constraint ) { String attribute = constraint.getAttribute();
Value value;
if ( (value = resourceType.getVocabularyValue(attribute)) == null ) {
if ( constraints.containsKey( attribute ) || addedConstraints.containsKey( attribute ) ) return false;
else {
addedConstraints.put( attribute, constraint );
if ( (constraint.mustOrder()) ) attributesCompare.add( attribute );
return true;
} } else {
if ( !constraint.isTyped() ) constraint.assignTypeAndMetric(value.getType(),"");
vocabularyConstraints.put(attribute,constraint);
if ( (constraint.mustOrder()) ) attributesCompare.add( attribute );
return true;
} }
set
public boolean set( String attribute, Value value, byte boolOp ) { Constraint tmp = new Constraint(attribute, value, boolOp);
return set(tmp);
}
set
public boolean set( String attribute, String metric, String value, String type, byte boolOp ) { Constraint tmp = new Constraint(attribute, type, value, metric, boolOp);
return set(tmp);
}
set
public Vector set( Vector constraintVect ) { Vector notAdded = new Vector();
Constraint tmp;
Iterator iter = constraintVect.iterator();
while ( iter.hasNext() ) { tmp = (Constraint) iter.next();
if ( !set(tmp) ) notAdded.add(tmp);
}
return notAdded;
}
setReservationConstraint
public boolean setReservationConstraint( ReservationConstraint reservationConstrain t) { newReservationConstraints = true;
reservationConstraintHash.put( reservationConstraint.getType(), reservationConstraint );
reservationConstraint.setTimeSlotBegin( now );
return true;
}
getReservationConstraint
public ReservationConstraint getReservationConstraint( String type ) { ReservationConstraint rc;
if ( (rc = (ReservationConstraint)reservationConstraintHash.get(type)) == null ) { return reservationConstraint;
}
else return rc;
}
getKey
public java.lang.String getKey() { String key = "";
String[] tmp = new String[constraints.size()];
Iterator iter = constraints.entrySet().iterator();
Entry entry;
while ( iter.hasNext() ) { entry = (Entry)iter.next();
key += (String)entry.getKey() + ((Constraint)entry.getValue()).getRelOp();
}
iter = vocabularyConstraints.entrySet().iterator();
while ( iter.hasNext() ) { entry = (Entry)iter.next();
key += (String)entry.getKey() + ((Constraint)entry.getValue()).getRelOp();
}
return key;
}
checkAttributes Parameters:
filter - il filtro con cui confrontare gli attributi.
Returns:
Ritorna il numero di attributi trovati e verificati per mezzo dell'operatore booleano contenuto nel Constraint. -1 nel caso che almeno un attrubuto non rispetti il Constraint.
Throws:
public int checkAttributes( java.util.Hashtable attributes ) { Value value;
String attribute = currentConstraint.getAttribute();
if ( (value = (Value) attributes.get( attribute )) == null ) return NOFOUND;
if ( currentConstraint.isTyped() ) {
if ( value.match(currentConstraint.getValue(), currentConstraint.getRelOp()) ) return MATCH;
else return NOMATCH;
} else {
currentConstraint.assignTypeAndMetric(value.getType(), value.getMetric());
if ( value.inferringMatch( currentConstraint.getVaueString(), currentConstraint.getRelOp()) )
return MATCH;
else return NOMATCH;
} }
mustOrder
public void mustOrder( boolean flag ) { if ((flag) && (!order)) {
order = true;
} else {
order = false;
} }
broker.filter
Class Constraint
java.lang.Object
broker.filter.Constraint
All Implemented Interfaces:
java.io.Serializable
public class Constraint extends java.lang.Object
implements java.io.Serializable
Constructor Summary
Constraint(java.lang.String attribute, java.lang.String value, byte boolOp)
Costruisce un vincolo. Per default tutti i vincoli sono ordinanti.
Constraint(java.lang.String attribute, java.lang.String value, byte boolOp, boolean mustOrder)
Constraint(java.lang.String attribute, java.lang.String type, java.lang.String value, java.lang.String metric, byte boolOp)
Constraint(java.lang.String attribute, Value value, byte boolOp)
Constraint(java.lang.String attribute, Value value, byte boolOp, boolean mustOrder)
Method Summary
void assignRelOp(byte relOp)
boolean assignTypeAndMetric(java.lang.String type, java.lang.String metric)
Java.lang.String getAttribute()
byte getRelOp()
Value getValue()
java.lang.String getVaueString()
boolean isTyped()
boolean mustOrder()
Serve per segnalare che il vincolo una volta inserito in un filtro sarà usato come un o dei criteri per ordinare le risorse filtrate.
java.lang.String toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Constraint
public Constraint(String attribute, Value value, byte boolOp) { this.attribute = attribute;
this.value = value;
this.boolOp = boolOp;
this.valueString = this.value.getStringValue();
}
Constraint.
Parameters:
attribute – E’ il nome dell'attributo su cui si vuole imporre un vincolo.
value è una stringa rappresentante il valore con il quale l'attributo dovrà essere verificato. In questo caso il tipo del valore dell'attributo
sarà inferito automaticamente. boolOp E’ l'operatore booleano da
verificare sul valore value dell'attributo.
public Constraint( String attribute, String value, byte boolOp ) { this.attribute = attribute;
this.value = null;
this.boolOp = boolOp;
this.valueString = value;
}
Constraint
public Constraint( String attribute, String value, byte boolOp, boolean mustOrde r) { this.attribute = attribute;
this.value = null;
this.boolOp = boolOp;
this.valueString = value;
this.order = mustOrder;
}
Constraint
public Constraint (String attribute, Value value, byte boolOp, boolean mustOrder ) { this.attribute = attribute;
this.value = value;
this.boolOp = boolOp;
this.valueString = this.value.getStringValue();
this.order = mustOrder;
}
Method Detail
assignTypeAndMetric
public boolean assignTypeAndMetric( String type, String metric ) { this.value = new Value(metric,this.valueString,type);
return true;
}
getValue
public Value getValue() { if ( isTyped() ) return value;
else return null;
}
broker.filter
Class ReservationConstraint
java.lang.Object
broker.filter.ReservationConstraint
All Implemented Interfaces:
java.io.Serializable Direct Known Subclasses:
BaseReservationConstraint, DiskResrvConstr, MemoryResrvConstr, ProcessorResrvConstr
public abstract class ReservationConstraint extends java.lang.Object
implements java.io.Serializable
Field Summary
protected Booking booking
static byte MAX_PRIORITY
static byte MIN_PRIORITY
protected byte priority
protected float rate
protected TimeSlot timeSlot
protected java.lang.String type
Constructor Summary
ReservationConstraint(java.lang.String type)
ReservationConstraint(java.lang.String type, float rate)
ReservationConstraint(java.lang.String type, TimeSlot timeSlot, byte priority)
Method Summary
boolean checkReservation(Reservable resource)
protected abstract Booking
checkRule(Reservable resource)
Metodo astratto deve essere implementato da tutte le classi per la prenotazioni delle parti reservebili delle Varie risorse.
Booking getBooking()
byte getPriority()
float getRate()
TimeSlot getTimeSlot()
java.lang.String getType()
void setBooking(Booking booking)
void setRate(float rate)
void setTimeSlotBegin(java.sql.Timestamp ts)
java.lang.String toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
ReservationConstraint
public ReservationConstraint( String type ) {
timeSlot = new TimeSlot(TimeSlot.NOW,TimeSlot.UNDEFINED);
priority = MIN_PRIORITY;
rate = 100.0f;
this.type = type;
}
ReservationConstraint
public ReservationConstraint( String type,float rate ) {
timeSlot = new TimeSlot(TimeSlot.NOW,TimeSlot.UNDEFINED);
priority = MIN_PRIORITY;
this.rate = rate;
this.type = type;
}
ReservationConstraint
public ReservationConstraint( String type,TimeSlot timeSlot, byte priority ) { this.timeSlot = timeSlot;
this.priority = priority;
rate = 100.0f;
this.type = type;
}
Method Detail
getBooking
public Booking getBooking() {
TimeSlot ts = new TimeSlot(booking.getBegin().getTime(), booking.getEnd().getTime());
return new Booking(ts, booking.getRate(), booking.getPriority() );
}
checkReservation
public boolean checkReservation( Reservable resource ) { Booking booking;
booking = checkRule( resource );
if ( booking != null ) {
resource.setBooking( booking );
return true;
}
else return false;
}
broker.filter
Class Value
java.lang.Object
broker.filter.Value
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
public class Value
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable
Field Summary
static java.lang.String BOOL
static byte EQUAL
static java.lang.String INT
static byte LESS
static byte LESSEQUAL
static byte MORE
static byte MOREEQUAL
static byte NOTEQUAL
static java.lang.String REAL
static java.lang.String STRING
Constructor Summary
Value(java.lang.String m, java.lang.String v, java.lang.String t)
Method Summary
java.lang.Object clone()
java.lang.String getMetric()
java.lang.String getStringValue()
java.lang.String getType()
java.lang.Object getValue()
Restituisce il valore contenuto dall'oggetto tipandolo, bisogna effettuare un cast esplicito sull Object di ritorno..
boolean inferringMatch(java.lang.String op, byte logicOp)
boolean isComparable()
boolean match(java.lang.Boolean op, byte logicOp)
boolean match(java.lang.Float op, byte logicOp)
boolean match(java.lang.Integer op, byte logicOp)
boolean match(java.lang.String op, byte logicOp)
boolean match(Value v, byte logicOp)
questo metodo invoca il match adeguato deipendentemente dal tipo del Value passato come parametro.
void print()
void setType(java.lang.String type)
java.lang.String toString()
void writeXML(java.io.PrintStream ps)
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Value
public Value( String m, String v, String t ) { metric = m;
type = t;
value = v;
setType(type);
}
Method Detail
setType
public void setType(String type) { if (type.equals(INT)) {
valueInt = new Integer(value);
} else if (type.equals(REAL)) { valueFloat = new Float(value);
} else if (type.equals(STRING)) { valueString = new String(value);
} else if (type.equals(BOOL)) { valueBoolean = new Boolean(value);
} }
getValue
Parameters:
Returns:
Un Object contenente il valore tipato, su cui effettuare il cast appropriato (Integer, Float, String, Boolean), null se la non ha tipo settato.
Throws:
public Object getValue() { if (type.equals(INT)) { return valueInt;
} else
if (type.equals(REAL)) { return valueFloat;
} else
if (type.equals(STRING)) { return valueString;
} else
if (type.equals(BOOL)) { return valueBoolean;
} return null;
}
match
public boolean match ( Value v, byte logicOp ) { if (v.getType().equals(INT)) {
return match( (Integer)v.getValue(), logicOp );
} else if (v.getType().equals(REAL)) {
return match( (Float)v.getValue(), logicOp );
} else if (v.getType().equals(STRING)) { return match( (String)v.getValue(), logicOp );
} else if (v.getType().equals(BOOL)) {
return match( (Boolean)v.getValue(), logicOp );
} else return inferringMatch(v.getStringValue(), logicOp);
}
match
public boolean match( Integer op,byte logicOp ){
switch(logicOp) { case EQUAL:
if ( op.intValue() == valueInt.intValue() ) return true;
break;
case MORE:
if ( op.intValue() < valueInt.intValue() ) return true;
break;
case LESS:
if ( op.intValue() > valueInt.intValue() ) return true;
case MOREEQUAL:
if ( op.intValue() <= valueInt.intValue() ) return true;
break;
case LESSEQUAL:
if ( op.intValue() >= valueInt.intValue() ) return true;
break;
case NOTEQUAL:
if ( op.intValue() != valueInt.intValue() ) return true;
break;
}
return false;
}
match
public boolean match( Float op,byte logicOp ){
switch(logicOp) { case EQUAL:
if (op.floatValue() == valueFloat.floatValue() ) return true;
break;
case MORE:
if (op.floatValue() < valueFloat.floatValue() ) return true;
break;
case LESS:
if (op.floatValue() > valueFloat.floatValue() ) return true;
break;
case MOREEQUAL:
if (op.floatValue() <= valueFloat.floatValue() ) return true;
break;
case LESSEQUAL:
if (op.floatValue() >= valueFloat.floatValue() ) return true;
break;
case NOTEQUAL:
if (op.floatValue() != valueFloat.floatValue() ) return true;
break;
}
return false;
}
match
public boolean match( String op,byte logicOp ) { switch(logicOp) {
case EQUAL:
if (op.equals(valueString) ) return true;
break;
case NOTEQUAL:
if ( !op.equals(valueString) ) return true;
break;
case MORE:
if ( valueString.compareTo(op) > 0) return true;
break;
case LESS:
if ( valueString.compareTo(op) < 0) return true;
break;
case MOREEQUAL:
if ( valueString.compareTo(op) >= 0) return true;
break;
case LESSEQUAL:
if ( valueString.compareTo(op) <= 0) return true;
break;
}
return false;
}
match
public boolean match( Boolean op,byte logicOp ) { switch(logicOp) {
case EQUAL:
if (op == valueBoolean ) return true;
break;
case NOTEQUAL:
if (op != valueBoolean ) return true;
break;
case MORE:
case LESS:
case MOREEQUAL:
case LESSEQUAL:
return false;
}
return false;
}
inferringMatch
public boolean inferringMatch( String op,byte logicOp ) { if (type.equals(INT)) {
return match( new Integer(op), logicOp );
} else if (type.equals(REAL)) {
return match( new Float(op), logicOp );
} else if (type.equals(STRING)) {
return match( new String(op), logicOp );
} else if (type.equals(BOOL)) {
return match( new Boolean(op), logicOp );
} return false;
}
broker.reservation
Class Reservation
java.lang.Object
broker.reservation.Reservation
All Implemented Interfaces:
java.lang.Cloneable
public class Reservation extends java.lang.Object
implements java.lang.Cloneable
Field Summary
static boolean DEBUG
static BookingId NOTRESERVED
static int ONESECOND
Constructor Summary
Reservation()
Method Summary
BookingId book(Booking booking)
BookingId book(java.lang.String begin,
java.lang.String end, float rate, byte priority)
java.lang.Object clone()
boolean isFree(Booking booking)
boolean isFree(java.lang.String begin,
java.lang.String end, float rate, byte priority)
boolean isFree(TimeSlot timeSlot, float rate, byte priority)
void print()
boolean unBook(BookingId bookingId)
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail
Reservation
public Reservation() {
timeSlotSchedules = new LinkedList();
}
Method Detail
isFree
public boolean isFree(TimeSlot timeSlot, float rate,
byte priority)
isFree
public boolean isFree(java.lang.String begin, java.lang.String end, float rate,
byte priority)
isFree
public boolean isFree(Booking booking)
isFreeAux
private boolean isFreeAux( Booking booking ) { int state = INIT;
boolean done = false;
ListIterator listIter = timeSlotSchedules.listIterator(0);
TimeSlotSchedule tss;
candidates = new Vector();
while ( (listIter.hasNext()) && (!done) ) { tss = (TimeSlotSchedule)listIter.next();
byte ret = booking.compare(tss);
switch ( ret ) { case TimeSlot.PRE:
/* se sono al primo elemento della lista o ho gia incontrato un POST, creo un nuovo tss
* e lo inseriesco nella lista */
if ( ( state == INIT ) || ( state == TimeSlot.POST ) ) {
if ( state == INIT ) candidates.add(new SplitOperation( tss,TimeSlot.PRE,true ) );
else candidates.add(new SplitOperation( tss,TimeSlot.PRE,false ) );
done = true;
break;
}
if ( state == TimeSlot.BEGIN) {
//candidates.add(new SplitOperation( (TimeSlotSchedule)listIter.previous() ,STOP, false) );
done = true;
break;
}
if ( state == TimeSlot.OVERLAP ){
done = true;
break;
}
else if (DEBUG) System.out.println("--->caso non trattato! state = " + state);
break;
case TimeSlot.POST:
/* se non ci sono piu' elementi nella lista dei timelotSchedule creo e appendo un nuovo tss */
if ( !listIter.hasNext() ) { if ( state == INIT )
candidates.add(new SplitOperation( tss ,TimeSlot.POST,true) );
else candidates.add(new SplitOperation( tss ,TimeSlot.POST,false) );
done = true;
break;
}
else state = TimeSlot.POST;
break;
/* case TimeSlot.EXTERNAL:
if ( ((SplitOperation)candidates.lastElement()).state == TimeSlot.BEGIN ) done = true;
break; */
case TimeSlot.END:
case TimeSlot.INTERNAL:
case TimeSlot.MATCH:
case TimeSlot.MATCHEND:
case TimeSlot.BEGINMATCH:
if ( tss.canBook(booking) ){
if ( state == INIT )
candidates.add(new SplitOperation( tss ,ret,true) );
else candidates.add(new SplitOperation( tss ,ret,false) );
done = true;
} else {
candidates = null;
done = true;
} break;
case TimeSlot.BEGIN:
if ( tss.canBook(booking) ) {
candidates.add(new SplitOperation( tss ,ret,true) );
} else {
candidates = null;
done = true;
}
state = TimeSlot.BEGIN;
break;
case TimeSlot.OVERLAP:
if ( tss.canBook(booking) ) {
if ( ( state == INIT ) || ( state == TimeSlot.POST )
|| ( state == TimeSlot.BEGIN) || ( state == TimeSlot.OVERLAP )) { candidates.add(new SplitOperation( tss ,ret,true) );
state = TimeSlot.OVERLAP;
}
else if (DEBUG) System.out.println("---> Erore!");
} else {
candidates = null;
done = true;
} break;
case TimeSlot.ERROR:
if (DEBUG) System.out.println("--->Error: problemi nella compare");
done = true;
break;
} }
if (candidates == null) return false;
else return true;
}
book
public BookingId book(java.lang.String begin, java.lang.String end, float rate,
byte priority)
book
public synchronized BookingId book( Booking booking ) { Vector removedBookings = new Vector();
TimeSlotSchedule tss,tssNew,tssNew2,tss3;
int tssIndex,beginIndex,endIndex;
SplitOperation splitOp;
/* set id della prenotazione */
//booking.setBookingId(generateBookingId());
booking.setBookingId(new BookingId());
/* se non ci sono prenotazioni creo il primo elemento della lista */
if ( timeSlotSchedules.isEmpty() ) {
timeSlotSchedules.add( new TimeSlotSchedule(booking) );
return booking.getBookingId();
}
/* eseguo le operazioni sugli slot necessarie per la nuova prenotazione */
if ( isFreeAux(booking) ) {
Iterator iter = candidates.iterator();
while ( iter.hasNext() ) {
splitOp = (SplitOperation) iter.next();
tssIndex = timeSlotSchedules.indexOf(splitOp.tss);
switch ( splitOp.state ) { case TimeSlot.PRE:
/* recupero il timeslotschedule precedente ( a meno che non esista )*/
if (timeSlotSchedules.get(0).equals(splitOp.tss)) { /* inserisco un nuovo tss */
tss = new TimeSlotSchedule(booking);
timeSlotSchedules.add(tssIndex,tss);
} else {
tss3 = (TimeSlotSchedule)timeSlotSchedules.get(
timeSlotSchedules.indexOf(splitOp.tss) - 1 );
/* controllo che il delta temporale fra i due tss sia > di un secondo */
if ( splitOp.tss.getBegin().getTime() - tss3.getEnd().getTime() >
ONESECOND ){
/* inserisco un nuovo tss */
tss = new TimeSlotSchedule(booking);
timeSlotSchedules.add(tssIndex,tss);
} } break;
case TimeSlot.POST:
/* appendo in coda un nuovo tss */
tss = new TimeSlotSchedule(booking);
timeSlotSchedules.add(tss);
break;
case TimeSlot.BEGINMATCH:
tssNew = splitOp.tss.split( booking.getBegin(), TimeSlot.BEGIN );
removedBookings.addAll( tssNew.addBooking(booking) );
timeSlotSchedules.add(tssIndex + 1,tssNew);
break;
case TimeSlot.BEGIN:
Timestamp ts = booking.getBegin();
// ts.setTime(booking.getBegin().getTime() - ONESECOND );
tssNew = splitOp.tss.split( ts, TimeSlot.BEGIN );
removedBookings.addAll( tssNew.addBooking(booking) );
timeSlotSchedules.add(tssIndex + 1,tssNew);
/* se non ci sono piu' operazioni in coda creo un nuovo slot e lo metto dopo tssNew
*/
if ( !iter.hasNext() ) {
tss = new TimeSlotSchedule(tssNew.getEnd().getTime() + ONESECOND,booking.getEnd().getTime(),booking);
timeSlotSchedules.add(tssIndex + 2,tss);
} break;
case TimeSlot.END:
tssNew = splitOp.tss.split( booking.getEnd(), TimeSlot.END );
removedBookings.addAll( splitOp.tss.addBooking(booking) );
timeSlotSchedules.add(tssIndex + 1,tssNew);
/* se questa era l'unica operazione in coda creo un nuovo slot e lo metto prima di tss
*/
if ( candidates.size() == 1 /*&& timeSlotSchedules.get(0).equals(splitOp.tss) */) { tss = new
TimeSlotSchedule(booking.getBegin().getTime(),splitOp.tss.getBegin().getTime() - ONESECOND,booking);
timeSlotSchedules.add(tssIndex,tss);
} else {
/* recupero il timeslotschedule precedente */
tss = (TimeSlotSchedule)timeSlotSchedules.get(
timeSlotSchedules.indexOf(splitOp.tss) - 1 );
/* controllo che il delta temporale fra i due tss sia > di un secondo */
if ( splitOp.tss.getBegin().getTime() - tss.getEnd().getTime() > ONESECOND ){
tssNew = new TimeSlotSchedule(tss.getEnd().getTime() + ONESECOND, splitOp.tss.getBegin().getTime() - ONESECOND, booking);
timeSlotSchedules.add(tssIndex,tssNew);
} } break;
case TimeSlot.MATCHEND:
tssNew = splitOp.tss.split( booking.getEnd(), TimeSlot.END );
removedBookings.addAll( splitOp.tss.addBooking(booking) );
timeSlotSchedules.add(tssIndex + 1,tssNew);
break;
case TimeSlot.INTERNAL:
tssNew = splitOp.tss.split( booking.getBegin(), TimeSlot.BEGIN );
timeSlotSchedules.add(tssIndex + 1,tssNew);
tssNew2 = tssNew.split( booking.getEnd(), TimeSlot.END );
removedBookings.addAll( tssNew.addBooking(booking) );
timeSlotSchedules.add(tssIndex + 2,tssNew2);
break;
case TimeSlot.MATCH:
removedBookings.addAll( splitOp.tss.addBooking(booking) );
break;
case TimeSlot.OVERLAP:
int offset = 1;
removedBookings.addAll( splitOp.tss.addBooking(booking) );
int splitOpIndex = candidates.indexOf(splitOp);
if ( (booking.getBegin().getTime() < splitOp.tss.getBegin().getTime()) ){
if ( splitOpIndex == 0 ) { tss = new
TimeSlotSchedule(booking.getBegin().getTime(),splitOp.tss.getBegin().getTime() - ONESECOND,booking);
timeSlotSchedules.add(tssIndex,tss);
offset++;
} else {
/* recupero il timeslotschedule precedente */
tss = (TimeSlotSchedule)timeSlotSchedules.get(
timeSlotSchedules.indexOf(splitOp.tss) - 1 );
/* controllo che il delta temporale fra i due tss sia > di un secondo */
if ( splitOp.tss.getBegin().getTime() - tss.getEnd().getTime() > ONESECOND ){
tssNew = new TimeSlotSchedule(tss.getEnd().getTime() + ONESECOND, splitOp.tss.getBegin().getTime() - ONESECOND, booking);
timeSlotSchedules.add(tssIndex,tssNew);
offset++;
} } }
/* se e' l'ultima del vettore creo il timeslotschedule finale */
if ( (candidates.size() == (splitOpIndex + 1)) && (booking.getEnd().getTime() >
splitOp.tss.getEnd().getTime()) ){
tss = new TimeSlotSchedule(splitOp.tss.getEnd().getTime() +
ONESECOND,booking.getEnd().getTime(),booking);
timeSlotSchedules.add(tssIndex + offset,tss);
} break;
case STOP:
tss = (TimeSlotSchedule) timeSlotSchedules.get(tssIndex - 1);
tssNew = new TimeSlotSchedule(tss.getEnd().getTime() + ONESECOND,booking.getEnd().getTime(),booking);
timeSlotSchedules.add(tssIndex + 1,tssNew);
break;
case TimeSlot.ERROR:
break;
} } }
else return NOTRESERVED;
/* rimuovo i timeslot eventualmente vuoti */
Iterator pIter = null;
Booking b_tmp = null;
TimeSlotSchedule tss_tmp = null;
Vector tss_to_remove = new Vector();
for ( int i = 0; i < removedBookings.size(); i++ ) { b_tmp = (Booking) removedBookings.elementAt(i);
pIter = b_tmp.getParentsIterator();
while ( pIter.hasNext() ) {
tss_tmp = (TimeSlotSchedule) pIter.next();
if ( tss_tmp.isEmpty() )
tss_to_remove.add( tss_tmp );
} }
for ( int i = 0; i < tss_to_remove.size(); i++ )
timeSlotSchedules.remove( (TimeSlotSchedule) tss_to_remove.elementAt(i) );
aggregate(booking.getBookingId());
return booking.getBookingId();
}
unBook
public synchronized boolean unBook( BookingId bookingId ) { if ( bookingId == null ) return false;
ListIterator listIter = timeSlotSchedules.listIterator(0);
TimeSlotSchedule first_tss,current_tss,previous_tss,next_tss,target_tss,end_tss;
first_tss = current_tss = previous_tss = target_tss = end_tss = next_tss = null;
int first_index,current_index,previous_index;
first_index = current_index = previous_index = 0;
Booking book = null;
boolean found = false;
Vector tss_to_remove = new Vector();
Iterator parentsIterator = null;
/* cerco nella lista dei timeslot */
while ( listIter.hasNext() ) {
current_tss = (TimeSlotSchedule) listIter.next();
current_index = current_tss.contains(bookingId);
if ( current_index != Booking.NOT_FOUND && !found) { book = current_tss.getBooking(current_index);
first_tss = current_tss;
first_index = listIter.nextIndex() - 1;
found = true;
}
if ( found && (current_index == Booking.NOT_FOUND) ) { next_tss = current_tss;
break;
}
if ( !found ) {
previous_tss = current_tss;
previous_index = current_index;
} }
if ( book != null ) {
parentsIterator = book.getParentsIterator();
book.removeFromAllTimeSlotSchedule();
if ( first_tss.isEmpty() ) { tss_to_remove.add( first_tss );
if ( book.getParentsCount() > 1 ) {
// if ( timeSlotSchedules.size() < first_index + 1 )
target_tss = (TimeSlotSchedule) timeSlotSchedules.get(first_index + 1);
} else target_tss = first_tss;
} else {
if ( (previous_tss != null) && (previous_tss.hasSameBookingsOf( first_tss )) ) target_tss = previous_tss;
else target_tss = first_tss;
}
while ( parentsIterator.hasNext() ) {
current_tss = (TimeSlotSchedule) parentsIterator.next();
// current_tss.print();
if ( current_tss.hasSameBookingsOf(target_tss) && ( current_tss != target_tss ) ){
tss_to_remove.add(current_tss);
target_tss.setEnd(current_tss.getEnd());
/* elimino i riferimenti a current_tss da tutti i booking di target_tss */
int limit = target_tss.getBookingsCount();
Booking tmp_book = null;
for ( int i = 0; i < limit ; i++ ) {
tmp_book = target_tss.getBooking(i);
tmp_book.removeTimeSlotScheduleParent(current_tss);
} }
else target_tss = current_tss;
if ( current_tss.isEmpty() )
tss_to_remove.add( current_tss );
}
if ( (next_tss != null) && target_tss.hasSameBookingsOf(next_tss) ) { target_tss.setEnd(next_tss.getEnd());
/* elimino i riferimenti a next_tss da tutti i booking di target_tss */
int limit = target_tss.getBookingsCount();
Booking tmp_book = null;
for ( int i = 0; i < limit ; i++ ) {
tmp_book = target_tss.getBooking(i);
tmp_book.removeTimeSlotScheduleParent(next_tss);
}
tss_to_remove.add( next_tss );
}
for ( int i = 0; i < tss_to_remove.size(); i++ ) {
timeSlotSchedules.remove( (TimeSlotSchedule) tss_to_remove.elementAt(i) );
}
return true;
}
return false;
}
broker.reservation
Class TimeSlot
java.lang.Object
broker.reservation.TimeSlot
All Implemented Interfaces:
java.io.Serializable Direct Known Subclasses:
Booking, TimeSlotSchedule
public class TimeSlot extends java.lang.Object
implements java.io.Serializable
Field Summary
static byte BEGIN
static byte BEGINMATCH
static byte END
static byte ERROR
static byte EXTERNAL
static byte INTERNAL
static byte MATCH
static byte MATCHEND
static java.lang.String NOW
static byte OVERLAP
static byte POST
static byte PRE
static long RUN
static java.lang.String UNDEFINED
static long UNLIMITED
Constructor Summary
TimeSlot()
TimeSlot(long begin, long end)
TimeSlot(java.lang.String begin, java.lang.String end)
TimeSlot(TimeSlot timeSlot)
Method Summary
byte compare(TimeSlot timeSlot)
java.sql.Timestamp getBegin()
java.sql.Timestamp getEnd()
void print()
void setBegin(long begin)
void setBegin(java.lang.String begin)
void setBegin(java.sql.Timestamp begin)
void setEnd(long end)
void setEnd(java.lang.String end)
void setEnd(java.sql.Timestamp end)
java.lang.String toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
TimeSlot
public TimeSlot() { this.begin = null;
this.end = null;
}
TimeSlot
public TimeSlot( TimeSlot timeSlot ) { this.begin = timeSlot.getBegin();
this.end = timeSlot.getEnd();
}
TimeSlot
public TimeSlot( String begin, String end ) { setBegin(begin);
setEnd(end);
}
TimeSlot
public TimeSlot( long begin, long end ) {
this.begin = new Timestamp(begin);
this.end = new Timestamp(end);
}
Method Detail
setBegin
public void setBegin( String begin ) { try {
//DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRENCH);
if (begin == NOW) this.begin = new Timestamp(System.currentTimeMillis());
else this.begin = new Timestamp( ((Date)myFormat.parse(begin)).getTime() );
} catch (ParseException pe) { System.out.println("setBegin" + pe); } }
setEnd
public void setEnd( String end ) { try {
// DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.ITALIAN);
if (end == UNDEFINED) this.end = new Timestamp(UNLIMITED);
else this.end = new Timestamp( ((Date)myFormat.parse(end)).getTime());
} catch (ParseException pe) { System.out.println("setEnd:" + pe); } }
compare
public byte compare( TimeSlot timeSlot ) {
TimeSlot timeslot = new TimeSlot( timeSlot.getBegin().getTime(), timeSlot.getEnd().getTime() );
Timestamp tmpEnd = new Timestamp( end.getTime() );
/* verifico se lo slot temporale da controllare è infinito */
if ( (timeSlot.getEnd().getTime() == UNLIMITED) && (this.end.getTime() ==
UNLIMITED) )
if ( this.begin.after(timeslot.getBegin()) ) {
timeslot = new TimeSlot( timeSlot.getBegin().getTime(),(this.begin.getTime() + 1) );
tmpEnd = new Timestamp( this.begin.getTime() + 1 );
} else {
timeslot = new TimeSlot( timeSlot.getBegin().getTime(),(timeSlot.getBegin().getTime() + 1) );
tmpEnd = new Timestamp( timeSlot.getBegin().getTime() + 1 );
} else {
if (timeSlot.getEnd().getTime() == UNLIMITED)
timeslot = new TimeSlot( timeSlot.getBegin().getTime(),(this.end.getTime() + 1) );
if (this.end.getTime() == UNLIMITED)
tmpEnd = new Timestamp( timeSlot.getEnd().getTime() + 1 );
}
/* comparazione */
// if ( (this.begin.after(timeslot.getEnd())) || (tmpEnd.before(timeslot.getBegin())) ) return EXTERNAL;
/* accorpare al caso sottostante */
// if ( this.begin.equals(timeslot.getBegin()) && tmpEnd.equals(timeslot.getEnd())) return OVERLAP;
if (( this.begin.before(timeslot.getBegin()) || this.begin.equals(timeslot.getBegin()) ) &&
( tmpEnd.after(timeslot.getEnd()) || tmpEnd.equals(timeslot.getEnd()) )) return OVERLAP;
if ( tmpEnd.before(timeslot.getBegin()) ) return PRE;
if ( this.begin.after(timeslot.getEnd())) return POST;
if ( tmpEnd.before(timeslot.getEnd())) {
if ( this.begin.before(timeslot.getBegin()) ) return END;
else
if ( this.begin.equals(timeslot.getBegin()) ) return MATCHEND;
else return INTERNAL;
}
if ( this.begin.after(timeslot.getBegin()) ) {
if ( tmpEnd.after(timeslot.getEnd()) ) return BEGIN;
else
if ( tmpEnd.equals(timeslot.getEnd()) ) return BEGINMATCH;
else return INTERNAL;
}
/* per debug */
return ERROR;
}
public void print() {
System.out.print( toString() );
}
public String toString() {
if ( end.getTime() == UNLIMITED ) return new String( "B:" + begin.toString() + " E:
unlimited" );
else return new String( "B:" + begin.toString() + " E:" + end.toString() );
} }
broker.reservation
Class Booking
java.lang.Object
broker.reservation.TimeSlot broker.reservation.Booking
All Implemented Interfaces:
java.io.Serializable
public class Booking extends TimeSlot
implements java.io.Serializable
Field Summary
static int NOT_FOUND
Fields inherited from class broker.reservation.TimeSlot
BEGIN, BEGINMATCH, END, ERROR, EXTERNAL, INTERNAL, MATCH, MATCHEND, NOW, OVERLAP, POST, PRE, RUN, UNDEFINED, UNLIMITED
Constructor Summary
Booking()
Booking(java.lang.String begin)
Booking(java.lang.String begin, java.lang.String end)
Booking(java.lang.String begin, java.lang.String end, float rate, byte priority)
Booking(TimeSlot timeSlot, float rate, byte priority)
Method Summary
void addTimeSlotScheduleParent(TimeSlotSchedule tss)
BookingId getBookingId()
int getParentsCount()
java.util.Iterator getParentsIterator()
byte getPriority()
float getRate()
void print()
void removeFromAllTimeSlotSchedule()
void removeTimeSlotScheduleParent(TimeSlotSchedule tss)
protected void setBookingId(BookingId id)
void setRate(float rate)
java.lang.String toString()
Methods inherited from class broker.reservation.TimeSlot
compare, getBegin, getEnd, setBegin, setBegin, setBegin, setEnd, setEnd, setEnd
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Booking
public Booking()
Booking
public Booking(String begin) { setBegin(begin);
setEnd(TimeSlot.UNDEFINED);
}
Booking
public Booking(String begin, String end, float rate, byte priority){
setBegin(begin);
setEnd(end);
this.bookingId = null;
this.usingRate = rate;
this.priority = priority;
}
Booking
public Booking(TimeSlot timeSlot, float rate, byte priority) { super(timeSlot);
this.bookingId = null;
this.usingRate = rate;
this.priority = priority;
}
Method Detail
removeFromAllTimeSlotSchedule
public void removeFromAllTimeSlotSchedule(){
Iterator iter = timeSlotScheduleParents.iterator();
TimeSlotSchedule tmp;
while ( iter.hasNext() ) {
tmp = (TimeSlotSchedule) iter.next();
tmp.removeBooking(this);
} }