001    package net.sf.cpsolver.ifs.example.jobshop;
002    
003    import java.util.*;
004    
005    import net.sf.cpsolver.ifs.model.*;
006    
007    /**
008     * Job constraint.
009     * <br><br>
010     * Each job contians a given set of operations (variables).
011     * A job constraint is satisfied, if all operations of the job do not overlap in time and are processed in the given order.
012     *
013     * @version
014     * IFS 1.1 (Iterative Forward Search)<br>
015     * Copyright (C) 2006 Tomáš Müller<br>
016     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017     * Lazenska 391, 76314 Zlin, Czech Republic<br>
018     * <br>
019     * This library is free software; you can redistribute it and/or
020     * modify it under the terms of the GNU Lesser General Public
021     * License as published by the Free Software Foundation; either
022     * version 2.1 of the License, or (at your option) any later version.
023     * <br><br>
024     * This library is distributed in the hope that it will be useful,
025     * but WITHOUT ANY WARRANTY; without even the implied warranty of
026     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027     * Lesser General Public License for more details.
028     * <br><br>
029     * You should have received a copy of the GNU Lesser General Public
030     * License along with this library; if not, write to the Free Software
031     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
032     */
033    public class Job extends Constraint {
034        private int iJobNumber = 0;
035        private int iDueTime = -1;
036        
037        /**
038         * Constructor
039         * @param jobNumber job number
040         */
041        public Job(int jobNumber) {
042            super();
043            iJobNumber = jobNumber;
044        }
045        
046        /**
047         * Set due time
048         */
049        public void setDueTime(int dueTime) { iDueTime = dueTime; }
050        
051        /**
052         * Get due time
053         */
054        public int getDueTime() { return iDueTime; }
055        
056        /**
057         * Get job number
058         */
059        public int getJobNumner() { return iJobNumber; }
060        
061        /**
062         * Count job operations for the job (i.e., the number of variables in this constraint)
063         */
064        public int countOperations() { return variables().size(); }
065        
066        /**
067         * Get operation of the given index (0..countOperations()-1)
068         */
069        public Operation getOperation(int opNumber) { return (Operation)variables().get(opNumber); }
070        
071        /**
072         * Adds conflicting operations into the set of conflicts.
073         */
074        public void computeConflicts(Value value, Set conflicts) {
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)) conflicts.add(l);
083                } else {
084                    if (!l.after(location)) conflicts.add(l);
085                }
086            }
087        }
088        
089        /**
090         * True if there is an operation from the same job which violates with the given assignment. 
091         */
092        public boolean inConflict(Value value) {
093            Location location = (Location)value;
094            Operation operation = (Operation)value.variable();
095            for (Enumeration e=assignedVariables().elements();e.hasMoreElements();) {
096                Operation o = (Operation)e.nextElement();
097                if (o.getOperationNumber()==operation.getOperationNumber()) continue;
098                Location l = (Location)o.getAssignment();
099                if (o.getOperationNumber()<operation.getOperationNumber()) {
100                    if (!l.before(location)) return true;
101                } else {
102                    if (!l.after(location)) return true;
103                }
104            }
105            return false;
106        }
107        
108        /**
109         * True if the two assignments (placement of opeartions of the same job in time) violates each other.
110         */
111        public boolean isConsistent(Value value1, Value value2) {
112            Location location1 = (Location)value1;
113            Operation operation1 = (Operation)value1.variable();
114            Location location2 = (Location)value2;
115            Operation operation2 = (Operation)value2.variable();
116            if (operation1.getOperationNumber()<operation2.getOperationNumber()) {
117                if (location1.before(location2)) return true;
118            } else {
119                if (location2.before(location1)) return true;
120            }
121            return false;
122        }
123    
124        /**
125         * String representation -- for debuging and printing purposes
126         */
127        public String toString() { return "J"+iJobNumber; }
128        /**
129         * Name of the job (e.g. J10 where 10 is the job number)
130         */
131        public String getName() { return "J"+iJobNumber; }
132    }