001    package edu.nrao.sss.model.project.scheduling.constraint;
002    
003    import java.util.List;
004    
005    import javax.xml.bind.annotation.XmlRootElement;
006    import javax.xml.bind.annotation.XmlTransient;
007    
008    import edu.nrao.sss.measure.TimeDuration;
009    import edu.nrao.sss.measure.TimeUnits;
010    
011    /**
012     * Represents the distance in hours from the time the target is at maximum elevation
013     * and the current time as a measure of the probability of successful observation of
014     * the scheduling block.
015     * @author slovelan
016     */
017    
018    //TODO:  If this turns out to be useful, we should make the categories configurable
019    //and the probabilities in each of the categories configurable.
020    @XmlRootElement
021    public class ConstraintPositionMaximum extends Constraint {
022            
023    
024            
025            //Elevation of the target source.
026            private TimeDuration interval = null;
027            
028            //-------------------------------------------------------------------------
029            //                         Construction
030            //-------------------------------------------------------------------------
031            
032            public ConstraintPositionMaximum(){
033                    constraintType = ConstraintType.POSITION_MAXIMUM;
034            }
035            
036            public ConstraintPositionMaximum( ConstraintPositionMaximum cpm ){
037                    super( cpm );
038                    TimeDuration copy = cpm.getInterval();
039                    interval = copy.clone();
040            }
041            
042            
043            //-------------------------------------------------------------------------
044            //                Removing the API Constraint
045            //-------------------------------------------------------------------------
046            
047            public void reset(){
048                    interval = null;
049            }
050            
051            public boolean isSpecified(){
052                    boolean specified = true;
053                    if ( interval == null ){
054                            specified = false;
055                    }
056                    return specified;
057            }
058    
059            //-------------------------------------------------------------------------
060            //                      Elevation
061            //-------------------------------------------------------------------------
062            
063            @XmlTransient
064            public TimeDuration getInterval(){
065                    return interval;
066            }
067            
068            public void setInterval( TimeDuration interval ){
069                    this.interval = interval;
070            }
071            
072            public Object getValue(){
073                    return interval;
074            }
075    
076            //--------------------------------------------------------------------------------
077            //                  Probability
078            //--------------------------------------------------------------------------------
079    
080            
081            /**
082             * Returns the probability of a successful observation based on the number 
083             * of elapsed hours between the current time and the time at which the target 
084             * is at maximum elevation using the following formula:
085             *   0 <= elapsed hours < 30 minutes,  returns 1; 
086             *   30 minutes <= elapsed hours < 1 hour, return 0.9; 
087             *   1 hour <= elapsed hours < 90 minutes, return 0.8; 
088             *   otherwise return 0.
089             *  @return a ConstraintProbability indicating the probability of success
090             *          for the constraint.
091             */
092            
093            public ConstraintProbability getProbabilitySuccess( List<Constraint> constraints ){
094    
095                    ConstraintProbability result = new ConstraintProbability();
096                    result.setConstraintType( constraintType );
097                    result.setProbabilitySuccess( ConstraintProbability.SUCCESS );
098                    
099                    if ( isSpecified() ){
100                            Constraint sbConstraint = getConstraint( constraints );
101                            if ( sbConstraint instanceof ConstraintPositionMaximum ){
102                                    ConstraintPositionMaximum otherConstraint = (ConstraintPositionMaximum) sbConstraint;
103                                    TimeDuration otherInterval = otherConstraint.getInterval();
104                                    double thisHours = interval.convertTo( TimeUnits.HOUR ).getValue().doubleValue();
105                                    double otherHours = otherInterval.convertTo( TimeUnits.HOUR).getValue().doubleValue();
106                                    //The elapsed did not meet the maximum requirement so designate it
107                                    //as a failure.
108                                    if ( otherHours > thisHours ){
109                                            result.setProbabilitySuccess( ConstraintProbability.FAILURE );
110                                            result.setMessage( "The elapsed time of "+otherHours+" is too large for the cut-off of "+thisHours );
111                                    }
112                                    //The elevation met the minimum requirement so designate the sign
113                                    //of the elevation as a measure of success.
114                                    else {
115                                            if ( 0 <= otherHours && otherHours < 0.5 ){
116                                                    result.setProbabilitySuccess( ConstraintProbability.SUCCESS );
117                                            }
118                                            else if ( 0.5 <= otherHours && otherHours < 1 ){
119                                                    result.setProbabilitySuccess( 0.9f );
120                                            }
121                                            else if ( 1 <= otherHours && otherHours < 1.5 ){
122                                                    result.setProbabilitySuccess( 0.8f );
123                                            }
124                                            else {
125                                                    result.setProbabilitySuccess( ConstraintProbability.FAILURE );
126                                            }
127                                    }
128                            }
129                            else {
130                                    result.setMessage( ConstraintProbability.NO_CONSTRAINT_SB );
131                            }
132                    }
133                    else {
134                            result.setMessage( ConstraintProbability.NO_CONSTRAINT_OPERATING );
135                    }
136                    return result;
137            }
138            
139            
140            
141    
142    }