001    package net.sf.cpsolver.ifs.example.tt;
002    
003    import net.sf.cpsolver.ifs.model.*;
004    
005    /**
006     * Binary dependence between two activities.
007     * 
008     * @version
009     * IFS 1.1 (Iterative Forward Search)<br>
010     * Copyright (C) 2006 Tomáš Müller<br>
011     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
012     * Lazenska 391, 76314 Zlin, Czech Republic<br>
013     * <br>
014     * This library is free software; you can redistribute it and/or
015     * modify it under the terms of the GNU Lesser General Public
016     * License as published by the Free Software Foundation; either
017     * version 2.1 of the License, or (at your option) any later version.
018     * <br><br>
019     * This library is distributed in the hope that it will be useful,
020     * but WITHOUT ANY WARRANTY; without even the implied warranty of
021     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
022     * Lesser General Public License for more details.
023     * <br><br>
024     * You should have received a copy of the GNU Lesser General Public
025     * License along with this library; if not, write to the Free Software
026     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
027     */
028    public class Dependence extends BinaryConstraint {
029        public static final int TYPE_NO_DEPENDENCE = 0;
030        public static final int TYPE_BEFORE = 1;
031        public static final int TYPE_CLOSELY_BEFORE = 2;
032        public static final int TYPE_AFTER = 3;
033        public static final int TYPE_CLOSELY_AFTER = 4;
034        public static final int TYPE_CONCURRENCY = 5;
035        private int iType = TYPE_NO_DEPENDENCE;
036        private String iResourceId = null;
037        
038        public Dependence(String id, int type) {
039            super();
040            iType = type;
041            iResourceId = id;
042        }
043        public int getType() { return iType; }
044        public String getResourceId() { return iResourceId; }
045        
046        public void computeConflicts(Value value, java.util.Set conflicts) {
047            Activity activity = (Activity) value.variable();
048            Location location = (Location) value;
049            Activity another = (Activity)another(activity);
050            Location anotherLocation = (Location)another.getAssignment();
051            if (anotherLocation==null) return;
052            if (isFirst(activity)) {
053                if (!isConsistent(location.getSlot(), activity.getLength(), anotherLocation.getSlot(), another.getLength()))
054                    conflicts.add(anotherLocation);
055            } else {
056                if (!isConsistent(anotherLocation.getSlot(), another.getLength(), location.getSlot(),activity.getLength()))
057                    conflicts.add(anotherLocation);
058            }
059        }
060    
061        public  boolean isConsistent(int s1, int l1, int s2, int l2) {
062            switch (iType) {
063                case TYPE_BEFORE : 
064                    return s1+l1<=s2; 
065                case TYPE_CLOSELY_BEFORE : 
066                    return s1+l1==s2; 
067                case TYPE_AFTER :
068                    return s2+l2<=s1; 
069                case TYPE_CLOSELY_AFTER : 
070                    return s2+l2==s1; 
071                case TYPE_CONCURRENCY : 
072                    return (s1<=s2 && s2+l2<=s1+l1) || (s2<=s1 && s1+l1<=s2+l2);
073                default : 
074                    return true;
075            }
076        }
077        
078        public boolean inConflict(Value value) {
079            Activity activity = (Activity) value.variable();
080            Location location = (Location) value;
081            Activity another = (Activity)another(activity);
082            Location anotherLocation = (Location)another.getAssignment();
083            if (anotherLocation==null) return false;
084            if (isFirst(activity)) {
085                return !isConsistent(location.getSlot(), activity.getLength(), anotherLocation.getSlot(), another.getLength());
086            } else {
087                return !isConsistent(anotherLocation.getSlot(), another.getLength(), location.getSlot(),activity.getLength());
088            }
089        }
090        
091        public boolean isConsistent(Value value1, Value value2) {
092            Activity a1 = (Activity) value1.variable();
093            Activity a2 = (Activity) value2.variable();
094            Location l1 = (Location) value1;
095            Location l2 = (Location) value2;
096            if (isFirst(a1)) {
097                return !isConsistent(l1.getSlot(), a1.getLength(), l2.getSlot(), a2.getLength());
098            } else {
099                return !isConsistent(l2.getSlot(), a2.getLength(), l1.getSlot(),a1.getLength());
100            }
101        }
102        
103        public String getName() {
104            switch (iType) {
105                case TYPE_BEFORE : 
106                    return first().getName()+"<"+second().getName();
107                case TYPE_CLOSELY_BEFORE : 
108                    return first().getName()+"<|"+second().getName();
109                case TYPE_AFTER :
110                    return first().getName()+">"+second().getName();
111                case TYPE_CLOSELY_AFTER : 
112                    return first().getName()+"|>"+second().getName();
113                case TYPE_CONCURRENCY : 
114                    return first().getName()+"||"+second().getName();
115                default : 
116                    return first().getName()+"?"+second().getName();
117            }        
118        }
119        
120    }