001    package net.sf.cpsolver.ifs.example.jobshop;
002    
003    import java.util.Set;
004    
005    import net.sf.cpsolver.ifs.model.Constraint;
006    
007    /**
008     * Job constraint. <br>
009     * <br>
010     * Each job contians a given set of operations (variables). A job constraint is
011     * satisfied, if all operations of the job do not overlap in time and are
012     * processed in the given order.
013     * 
014     * @version IFS 1.2 (Iterative Forward Search)<br>
015     *          Copyright (C) 2006 - 2010 Tomas Muller<br>
016     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
018     * <br>
019     *          This library is free software; you can redistribute it and/or modify
020     *          it under the terms of the GNU Lesser General Public License as
021     *          published by the Free Software Foundation; either version 3 of the
022     *          License, or (at your option) any later version. <br>
023     * <br>
024     *          This library is distributed in the hope that it will be useful, but
025     *          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. <br>
028     * <br>
029     *          You should have received a copy of the GNU Lesser General Public
030     *          License along with this library; if not see
031     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
032     */
033    public class Job extends Constraint<Operation, Location> {
034        private int iJobNumber = 0;
035        private int iDueTime = -1;
036    
037        /**
038         * Constructor
039         * 
040         * @param jobNumber
041         *            job number
042         */
043        public Job(int jobNumber) {
044            super();
045            iJobNumber = jobNumber;
046        }
047    
048        /**
049         * Set due time
050         */
051        public void setDueTime(int dueTime) {
052            iDueTime = dueTime;
053        }
054    
055        /**
056         * Get due time
057         */
058        public int getDueTime() {
059            return iDueTime;
060        }
061    
062        /**
063         * Get job number
064         */
065        public int getJobNumner() {
066            return iJobNumber;
067        }
068    
069        /**
070         * Count job operations for the job (i.e., the number of variables in this
071         * constraint)
072         */
073        public int countOperations() {
074            return variables().size();
075        }
076    
077        /**
078         * Get operation of the given index (0..countOperations()-1)
079         */
080        public Operation getOperation(int opNumber) {
081            return variables().get(opNumber);
082        }
083    
084        /**
085         * Adds conflicting operations into the set of conflicts.
086         */
087        @Override
088        public void computeConflicts(Location location, Set<Location> conflicts) {
089            for (Operation o : assignedVariables()) {
090                if (o.getOperationNumber() == location.variable().getOperationNumber())
091                    continue;
092                Location l = o.getAssignment();
093                if (o.getOperationNumber() < location.variable().getOperationNumber()) {
094                    if (!l.before(location))
095                        conflicts.add(l);
096                } else {
097                    if (!l.after(location))
098                        conflicts.add(l);
099                }
100            }
101        }
102    
103        /**
104         * True if there is an operation from the same job which violates with the
105         * given assignment.
106         */
107        @Override
108        public boolean inConflict(Location location) {
109            for (Operation o : assignedVariables()) {
110                if (o.getOperationNumber() == location.variable().getOperationNumber())
111                    continue;
112                Location l = o.getAssignment();
113                if (o.getOperationNumber() < location.variable().getOperationNumber()) {
114                    if (!l.before(location))
115                        return true;
116                } else {
117                    if (!l.after(location))
118                        return true;
119                }
120            }
121            return false;
122        }
123    
124        /**
125         * True if the two assignments (placement of opeartions of the same job in
126         * time) violates each other.
127         */
128        @Override
129        public boolean isConsistent(Location location1, Location location2) {
130            Operation operation1 = location1.variable();
131            Operation operation2 = location2.variable();
132            if (operation1.getOperationNumber() < operation2.getOperationNumber()) {
133                if (location1.before(location2))
134                    return true;
135            } else {
136                if (location2.before(location1))
137                    return true;
138            }
139            return false;
140        }
141    
142        /**
143         * String representation -- for debuging and printing purposes
144         */
145        @Override
146        public String toString() {
147            return "J" + iJobNumber;
148        }
149    
150        /**
151         * Name of the job (e.g. J10 where 10 is the job number)
152         */
153        @Override
154        public String getName() {
155            return "J" + iJobNumber;
156        }
157    }