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     * Machine constraint. <br>
009     * <br>
010     * Each machine contians a given set of operations (variables). A machine
011     * constraint is satisfied, if all operations on it do not overlap in time.
012     * 
013     * @version IFS 1.2 (Iterative Forward Search)<br>
014     *          Copyright (C) 2006 - 2010 Tomas Muller<br>
015     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
016     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
017     * <br>
018     *          This library is free software; you can redistribute it and/or modify
019     *          it under the terms of the GNU Lesser General Public License as
020     *          published by the Free Software Foundation; either version 3 of the
021     *          License, or (at your option) any later version. <br>
022     * <br>
023     *          This library is distributed in the hope that it will be useful, but
024     *          WITHOUT ANY WARRANTY; without even the implied warranty of
025     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
026     *          Lesser General Public License for more details. <br>
027     * <br>
028     *          You should have received a copy of the GNU Lesser General Public
029     *          License along with this library; if not see
030     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
031     */
032    public class Machine extends Constraint<Operation, Location> {
033        private int iMachineNumber = -1;
034    
035        /**
036         * Constructor
037         * 
038         * @param machineNumber
039         *            machine number
040         */
041        public Machine(int machineNumber) {
042            super();
043            iMachineNumber = machineNumber;
044        }
045    
046        /** Get machine number */
047        public int getMachineNumber() {
048            return iMachineNumber;
049        }
050    
051        /**
052         * Adds conflicting operations into the set of conflicts.
053         */
054        @Override
055        public void computeConflicts(Location location, Set<Location> conflicts) {
056            for (Operation o : assignedVariables()) {
057                if (o.getOperationNumber() == location.variable().getOperationNumber()
058                        && o.getJobNumber() == location.variable().getJobNumber())
059                    continue;
060                if (o.getAssignment().overlap(location))
061                    conflicts.add(o.getAssignment());
062            }
063        }
064    
065        /**
066         * True if there is an operation from the machine which violates with the
067         * given assignment.
068         */
069        @Override
070        public boolean inConflict(Location location) {
071            for (Operation o : assignedVariables()) {
072                if (o.getOperationNumber() == location.variable().getOperationNumber()
073                        && o.getJobNumber() == location.variable().getJobNumber())
074                    continue;
075                if (o.getAssignment().overlap(location))
076                    return true;
077            }
078            return false;
079        }
080    
081        /**
082         * True if the two assignments (placement of opeartions of the machine in
083         * time) violates each other.
084         */
085        @Override
086        public boolean isConsistent(Location value1, Location value2) {
087            return !value1.overlap(value2);
088        }
089    
090        /** string representation -- for debuging and printing purposes */
091        @Override
092        public String toString() {
093            return getName();
094        }
095    
096        /**
097         * Name of the machine (e.g. M10 where 10 is the machine number)
098         */
099        @Override
100        public String getName() {
101            return "M" + iMachineNumber;
102        }
103    }