001    package ifs.example.jobshop;
002    
003    import java.util.*;
004    import ifs.model.*;
005    
006    /**
007     * Job constraint.
008     * <br><br>
009     * Each job contians a given set of operations (variables).
010     * A job constraint is satisfied, if all operations of the job do not overlap in time and are processed in the given order.
011     *
012     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
013     * @version 1.0
014     */
015    public class Job extends Constraint {
016        private int iJobNumber = 0;
017        private int iDueTime = -1;
018        
019        /**
020         * Constructor
021         * @param jobNumber job number
022         */
023        public Job(int jobNumber) {
024            super();
025            iJobNumber = jobNumber;
026        }
027        
028        /**
029         * Set due time
030         */
031        public void setDueTime(int dueTime) { iDueTime = dueTime; }
032        
033        /**
034         * Get due time
035         */
036        public int getDueTime() { return iDueTime; }
037        
038        /**
039         * Get job number
040         */
041        public int getJobNumner() { return iJobNumber; }
042        
043        /**
044         * Count job operations for the job (i.e., the number of variables in this constraint)
045         */
046        public int countOperations() { return variables().size(); }
047        
048        /**
049         * Get operation of the given index (0..countOperations()-1)
050         */
051        public Operation getOperation(int opNumber) { return (Operation)variables().get(opNumber); }
052        
053        /**
054         * Adds conflicting operations into the set of conflicts.
055         */
056        public void computeConflicts(Value value, Set conflicts) {
057            Location location = (Location)value;
058            Operation operation = (Operation)value.variable();
059            for (Enumeration e=assignedVariables().elements();e.hasMoreElements();) {
060                Operation o = (Operation)e.nextElement();
061                if (o.getOperationNumber()==operation.getOperationNumber()) continue;
062                Location l = (Location)o.getAssignment();
063                if (o.getOperationNumber()<operation.getOperationNumber()) {
064                    if (!l.before(location)) conflicts.add(l);
065                } else {
066                    if (!l.after(location)) conflicts.add(l);
067                }
068            }
069        }
070        
071        /**
072         * True if there is an operation from the same job which violates with the given assignment. 
073         */
074        public boolean inConflict(Value value) {
075            Location location = (Location)value;
076            Operation operation = (Operation)value.variable();
077            for (Enumeration e=assignedVariables().elements();e.hasMoreElements();) {
078                Operation o = (Operation)e.nextElement();
079                if (o.getOperationNumber()==operation.getOperationNumber()) continue;
080                Location l = (Location)o.getAssignment();
081                if (o.getOperationNumber()<operation.getOperationNumber()) {
082                    if (!l.before(location)) return true;
083                } else {
084                    if (!l.after(location)) return true;
085                }
086            }
087            return false;
088        }
089        
090        /**
091         * True if the two assignments (placement of opeartions of the same job in time) violates each other.
092         */
093        public boolean isConsistent(Value value1, Value value2) {
094            Location location1 = (Location)value1;
095            Operation operation1 = (Operation)value1.variable();
096            Location location2 = (Location)value2;
097            Operation operation2 = (Operation)value2.variable();
098            if (operation1.getOperationNumber()<operation2.getOperationNumber()) {
099                if (location1.before(location2)) return true;
100            } else {
101                if (location2.before(location1)) return true;
102            }
103            return false;
104        }
105    
106        /**
107         * String representation -- for debuging and printing purposes
108         */
109        public String toString() { return "J"+iJobNumber; }
110        /**
111         * Name of the job (e.g. J10 where 10 is the job number)
112         */
113        public String getName() { return "J"+iJobNumber; }
114    }