001    package net.sf.cpsolver.ifs.example.jobshop;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import net.sf.cpsolver.ifs.model.Variable;
007    
008    /**
009     * Operation. <br>
010     * <br>
011     * Each operation has its number, job, machine and processing 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 Operation extends Variable<Operation, Location> {
033        private Job iJob = null;
034        private Machine iMachine = null;
035        private int iProcessingTime = 0;
036        private int iOperationNumber = 0;
037    
038        /**
039         * Constructor
040         * 
041         * @param job
042         *            job
043         * @param machine
044         *            machine
045         * @param operationNumber
046         *            operation number
047         * @param processingTime
048         *            processing time
049         */
050        public Operation(Job job, Machine machine, int operationNumber, int processingTime) {
051            super(null);
052            iJob = job;
053            iMachine = machine;
054            iProcessingTime = processingTime;
055            iOperationNumber = operationNumber;
056        }
057    
058        /** Get job */
059        public Job getJob() {
060            return iJob;
061        }
062    
063        /** Get job number */
064        public int getJobNumber() {
065            return iJob.getJobNumner();
066        }
067    
068        /** Get operation number */
069        public int getOperationNumber() {
070            return iOperationNumber;
071        }
072    
073        /** Get machine */
074        public Machine getMachine() {
075            return iMachine;
076        }
077    
078        /** Get machine number */
079        public int getMachineNumber() {
080            return iMachine.getMachineNumber();
081        }
082    
083        /** Get processing time */
084        public int getProcessingTime() {
085            return iProcessingTime;
086        }
087    
088        /** Get the preceeding operation (if any) */
089        public Operation getPrecedingOperation() {
090            return (iOperationNumber == 0 ? null : iJob.getOperation(iOperationNumber - 1));
091        }
092    
093        /** Get the subsequent operation (if any) */
094        public Operation getSubsequentOperation() {
095            return (iOperationNumber + 1 == iJob.countOperations() ? null : iJob.getOperation(iOperationNumber + 1));
096        }
097    
098        /** Get minimal starting time */
099        public int getMinStartTime() {
100            if (iOperationNumber == 0)
101                return 0;
102            else
103                return getPrecedingOperation().getMinStartTime() + iProcessingTime;
104        }
105    
106        /** Get maximal starting time */
107        public int getMaxStartTime() {
108            if (iOperationNumber + 1 == iJob.countOperations())
109                return ((JobShopModel) getModel()).getTotalNumberOfSlots() - iProcessingTime;
110            else
111                return getSubsequentOperation().getMaxStartTime() - iProcessingTime;
112        }
113    
114        /** Compares two operations -- job number and operation number must match */
115        @Override
116        public boolean equals(Object o) {
117            if (o == null || !(o instanceof Operation))
118                return false;
119            Operation op = (Operation) o;
120            return getJobNumber() == op.getJobNumber() && getOperationNumber() == op.getOperationNumber();
121        }
122    
123        /** Initialozation -- fills the variable's domain */
124        public void init() {
125            setValues(computeValues());
126        }
127    
128        private List<Location> computeValues() {
129            List<Location> ret = new ArrayList<Location>();
130            for (int i = getMinStartTime(); i <= getMaxStartTime(); i++)
131                ret.add(new Location(this, i));
132            return ret;
133        }
134    
135        /** string representation -- for debuging and printing purposes */
136        @Override
137        public String toString() {
138            return getName();
139        }
140    
141        /**
142         * Operation's name (e.g., O[2,4] where 2 is the job number and 4 is the
143         * operation number
144         */
145        @Override
146        public String getName() {
147            return "O[" + getJobNumber() + "," + getOperationNumber() + "]";
148        }
149    }