001    package ifs.example.tt;
002    
003    import ifs.model.*;
004    
005    /**
006     * Binary dependence between two activities.
007     * 
008     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
009     * @version 1.0
010     */
011    public class Dependence extends BinaryConstraint {
012        public static final int TYPE_NO_DEPENDENCE = 0;
013        public static final int TYPE_BEFORE = 1;
014        public static final int TYPE_CLOSELY_BEFORE = 2;
015        public static final int TYPE_AFTER = 3;
016        public static final int TYPE_CLOSELY_AFTER = 4;
017        public static final int TYPE_CONCURRENCY = 5;
018        private int iType = TYPE_NO_DEPENDENCE;
019        private String iResourceId = null;
020        
021        public Dependence(String id, int type) {
022            super();
023            iType = type;
024            iResourceId = id;
025        }
026        public int getType() { return iType; }
027        public String getResourceId() { return iResourceId; }
028        
029        public void computeConflicts(Value value, java.util.Set conflicts) {
030            Activity activity = (Activity) value.variable();
031            Location location = (Location) value;
032            Activity another = (Activity)another(activity);
033            Location anotherLocation = (Location)another.getAssignment();
034            if (anotherLocation==null) return;
035            if (isFirst(activity)) {
036                if (!isConsistent(location.getSlot(), activity.getLength(), anotherLocation.getSlot(), another.getLength()))
037                    conflicts.add(anotherLocation);
038            } else {
039                if (!isConsistent(anotherLocation.getSlot(), another.getLength(), location.getSlot(),activity.getLength()))
040                    conflicts.add(anotherLocation);
041            }
042        }
043    
044        public  boolean isConsistent(int s1, int l1, int s2, int l2) {
045            switch (iType) {
046                case TYPE_BEFORE : 
047                    return s1+l1<=s2; 
048                case TYPE_CLOSELY_BEFORE : 
049                    return s1+l1==s2; 
050                case TYPE_AFTER :
051                    return s2+l2<=s1; 
052                case TYPE_CLOSELY_AFTER : 
053                    return s2+l2==s1; 
054                case TYPE_CONCURRENCY : 
055                    return (s1<=s2 && s2+l2<=s1+l1) || (s2<=s1 && s1+l1<=s2+l2);
056                default : 
057                    return true;
058            }
059        }
060        
061        public boolean inConflict(Value value) {
062            Activity activity = (Activity) value.variable();
063            Location location = (Location) value;
064            Activity another = (Activity)another(activity);
065            Location anotherLocation = (Location)another.getAssignment();
066            if (anotherLocation==null) return false;
067            if (isFirst(activity)) {
068                return !isConsistent(location.getSlot(), activity.getLength(), anotherLocation.getSlot(), another.getLength());
069            } else {
070                return !isConsistent(anotherLocation.getSlot(), another.getLength(), location.getSlot(),activity.getLength());
071            }
072        }
073        
074        public boolean isConsistent(Value value1, Value value2) {
075            Activity a1 = (Activity) value1.variable();
076            Activity a2 = (Activity) value2.variable();
077            Location l1 = (Location) value1;
078            Location l2 = (Location) value2;
079            if (isFirst(a1)) {
080                return !isConsistent(l1.getSlot(), a1.getLength(), l2.getSlot(), a2.getLength());
081            } else {
082                return !isConsistent(l2.getSlot(), a2.getLength(), l1.getSlot(),a1.getLength());
083            }
084        }
085        
086        public String getName() {
087            switch (iType) {
088                case TYPE_BEFORE : 
089                    return first().getName()+"<"+second().getName();
090                case TYPE_CLOSELY_BEFORE : 
091                    return first().getName()+"<|"+second().getName();
092                case TYPE_AFTER :
093                    return first().getName()+">"+second().getName();
094                case TYPE_CLOSELY_AFTER : 
095                    return first().getName()+"|>"+second().getName();
096                case TYPE_CONCURRENCY : 
097                    return first().getName()+"||"+second().getName();
098                default : 
099                    return first().getName()+"?"+second().getName();
100            }        
101        }
102        
103    }