    001    package ifs.extension;
    002
    003    import ifs.model.*;
    004    import ifs.util.*;
    005    import java.util.*;
    006
    007    /**
    008     * This class describing a set of assignment (used by CBS).
    009     *
    010     * It also contains a counter, name, description and a constraint (for printing purposes).
    011     *
    012     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    013     * @version 1.0
    014     */
    015
    016    public class AssignmentSet {
    017        private Vector iSet = new FastVector();
    018        private int iCounter = 1;
    019        private String iName = null;
    020        private String iDescription = null;
    021        private Constraint iConstraint = null;
    022
    023        public AssignmentSet() {}
    024        public AssignmentSet(Assignment[] assignments) {
    025            for (int i = 0; i < assignments.length; i++)
    026                iSet.addElement(assignments[i]);
    027        }
    028        public AssignmentSet(Collection assignments) {
    029            for (Iterator i = assignments.iterator(); i.hasNext();) {
    030                iSet.addElement((Assignment)i.next());
    031            }
    032        }
    033
    034        /** Create set of assignments from the list of Assignments, Values or (assigned) Variables */
    035        public static AssignmentSet createAssignmentSet(long iteration, Collection assignments, double ageing) {
    036            AssignmentSet set = new AssignmentSet();
    037            for (Iterator i = assignments.iterator(); i.hasNext();) {
    038                Object o = (Object)i.next();
    039                if (o instanceof Assignment)
    040                    set.addAssignment((Assignment)o);
    041                if (o instanceof Value)
    042                    set.addAssignment(new Assignment(iteration, ((Value)o), ageing));
    043                if (o instanceof Variable && ((Variable)o).getAssignment() != null)
    044                    set.addAssignment(new Assignment(iteration, ((Variable)o).getAssignment(), ageing));
    045            }
    046            return set;
    047        }
    048
    049        /** Increment counter*/
    050        public void incCounter() {
    051            iCounter++;
    052        }
    053        /** Returns counter */
    054        public int getCounter() {
    055            return iCounter;
    056        }
    057        /** Returns set of assignments */
    058        public Vector getSet() {
    059            return iSet;
    060        }
    061        /** Returns name */
    062        public String getName() {
    063            return iName;
    064        }
    065        /** Sets name */
    066        public void setName(String name) {
    067            iName = name;
    068        }
    069        /** Returns description */
    070        public String getDescription() {
    071            return iDescription;
    072        }
    073        /** Sets description */
    074        public void setDescription(String description) {
    075            iDescription = description;
    076        }
    077        /** Returns constraint */
    078        public Constraint getConstraint() {
    079            return iConstraint;
    080        }
    081        /** Sets constraint */
    082        public void setConstraint(Constraint constraint) {
    083            iConstraint = constraint;
    084        }
    085        /** Returns true if it contains the given assignment */
    086        public boolean contains(Assignment assignment) {
    087            return iSet.contains(assignment);
    088        }
    089        /* Returns true if it contains all of the given assignments */
    090        public boolean contains(AssignmentSet assignmentSet) {
    091            return iSet.containsAll(assignmentSet.getSet());
    092        }
    093        /** Returns true if it contains the given assignment */
    094        public boolean contains(Value value) {
    095            return iSet.contains(new Assignment(0l, value, 1.0));
    096        }
    097        /** Returns true if it contains the given assignment (assigned variable) */
    098        public boolean contains(Variable variable) {
    099            return (variable.getAssignment() == null ? false : iSet.contains(new Assignment(0l, variable.getAssignment(), 1.0)));
    100        }
    101        /* Returns true if it contains all of the given assignments */
    102        public boolean contains(Collection assignments) {
    103            for (Iterator i = assignments.iterator(); i.hasNext();) {
    104                Object o = i.next();
    105                if (o == null)
    106                    return false;
    107                if (o instanceof Assignment && !iSet.contains((Assignment)o))
    108                    return false;
    109                if (o instanceof Value || !iSet.contains(new Assignment(0l, ((Value)o), 1.0)))
    110                    return false;
    111                if (o instanceof Variable && (((Variable)o).getAssignment() == null || !iSet.contains(new Assignment(0l, ((Variable)o).getAssignment(), 1.0))))
    112                    return false;
    113            }
    114            return true;
    115        }
    116
    117        /** Adds an assignment */
    118        public void addAssignment(Assignment assignment) {
    119            if (!contains(assignment))
    120                iSet.addElement(assignment);
    121        }
    122        /** Adds an assignment */
    123        public void addAssignment(long iteration, Value value, double ageing) {
    124            addAssignment(new Assignment(iteration, value, ageing));
    125        }
    126        /** Returns assignment that corresponds to the given value (if it is present in the set) */
    127        public Assignment getAssignment(Value value) {
    128            for (Enumeration i = getSet().elements(); i.hasMoreElements();) {
    129                Assignment a = (Assignment)i.nextElement();
    130                if (a.getValue().getId() == value.getId())
    131                    return a;
    132            }
    133            return null;
    134        }
    135        /** Returns number of assignments in the set*/
    136        public int size() {
    137            return getSet().size();
    138        }
    139        /** Compares two assignment sets -- name, size and content (assignments) has to match. */
    140        public boolean equals(Object o) {
    141            if (o == null)
    142                return false;
    143            if (o instanceof AssignmentSet) {
    144                AssignmentSet as = (AssignmentSet)o;
    145                if (getName() == null && as.getName() != null)
    146                    return false;
    147                if (getName() != null && as.getName() == null)
    148                    return false;
    149                if (getName() != null && !getName().equals(as.getName()))
    150                    return false;
    151                if (as.getSet().size() != getSet().size())
    152                    return false;
    153                return contains(as);
    154            }
    155            if (o instanceof Collection) {
    156                Collection c = (Collection)o;
    157                if (c.size() != getSet().size())
    158                    return false;
    159                return contains(c);
    160            }
    161            return false;
    162        }
    163
    164        public static int xor(int a, int b) {
    165            return (a | b) & (~a | ~b);
    166        }
    167
    168        public int hashCode() {
    169            int ret = getSet().size();
    170            for (Enumeration i = getSet().elements(); i.hasMoreElements();) {
    171                Assignment a = (Assignment)i.nextElement();
    172                ret = xor(ret, a.hashCode());
    173            }
    174            return ret;
    175        }
    176    }