001    package edu.nrao.sss.model.project.scheduling.priority;
002    
003    import java.util.List;
004    
005    import edu.nrao.sss.measure.TimeDuration;
006    import edu.nrao.sss.measure.TimeUnits;
007    import edu.nrao.sss.model.project.scheduling.Schedulable;
008    
009    import javax.xml.bind.annotation.XmlRootElement;
010    
011    @XmlRootElement
012    public class PriorityMeasureSum extends PriorityMeasure {
013            
014            private boolean weightByDuration = false;
015            private boolean ignoreProjectPriority = true;
016            
017            public PriorityMeasureSum (){
018                    measureType = PriorityMeasureType.WEIGHTED_SUM_OF_PRIORITIES;
019            }
020            
021            public PriorityMeasureSum( PriorityMeasureSum copy ){
022                    super( copy );
023                    weightByDuration = copy.getWeightByDuration();
024            }
025            
026            public String getName(){
027                    String descriptStr = super.getName();
028                    if ( weightByDuration ){
029                            descriptStr = descriptStr + " weighted by duration.";
030                    }
031                    return descriptStr;
032            }
033            
034            public void setWeightByDuration( boolean b ){
035                    weightByDuration = b;
036            }
037            
038            public boolean getWeightByDuration(){
039                    return weightByDuration;
040            }
041            
042            public double getPriority( List<Priority> thePriorities ){
043                    //If there are no priorities then we should return the lowest priority possible.
044                    double sum = Double.MAX_VALUE;
045                    if ( thePriorities != null && thePriorities.size() > 0 ){
046                            sum = 0;
047                            float weightSum = 0;
048                            for ( Priority p : thePriorities ){
049                                    PriorityType pt = p.getPriorityType();
050                                    if ( !ignoreProjectPriority || ! PriorityType.PROJECT_PRIORITY.equals( pt )){
051                                            float weight = getWeight( pt );
052                                            weightSum = weightSum + weight;
053                                            sum = sum + p.getPriorityValue() * weight;
054                                    }
055                            }
056                            if ( weightSum != 0 ){
057                                    sum = sum / weightSum;
058                            }
059                    }
060                    return sum;
061            }
062            
063            public double getPriority( Schedulable theBlock ){
064                    double weight = 0;
065                    List<Priority> priorities = theBlock.getPriorities();
066                    weight = getPriority( priorities );
067                    int duration = 1;
068                    if ( weightByDuration ){
069                            TimeDuration durationTime = theBlock.calculateTimePerExecution();
070                            double durationHours = durationTime.convertTo( TimeUnits.HOUR ).getValue().doubleValue();
071                            duration = (int)Math.ceil( durationHours );
072                    }
073                    weight = weight * duration;
074                    return weight;
075            }
076    }