    001    package ifs.example.rpp;
    002
    003    import ifs.model.*;
    004    import ifs.util.*;
    005    import java.util.*;
    006
    007    /**
    008     * Rectangle (variable).
    009     * It encodes the name, width and height of the rectangle, minimal and maximal position of the rectangle.
    010     * It also contains an information about prohibited X and Y coordinate (for MPP).
    011     *
    012     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    013     * @version 1.0
    014     */
    015    public class Rectangle extends Variable {
    016        private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(Rectangle.class);
    017        private String iName;
    018        private int iMinX, iMaxX, iMinY, iMaxY;
    019        private int iHeight, iWidth;
    020        private int iProhibitedX = -1, iProhibitedY = -1;
    021
    022        /**
    023         * Constructor.
    024         * @param name variable's name
    025         * @param width width of the rectangle
    026         * @param height height of the rectangle
    027         * @param minX minimal X-coordinate
    028         * @param maxX maximal X-coordinate
    029         * @param minY minimal Y-coordinate
    030         * @param maxY maximal Y-coordinate
    031         * @param initialLocation initial location (null if none)
    032         */
    033        public Rectangle(String name, int width, int height, int minX, int maxX, int minY, int maxY,Location initialLocation) {
    034            super(initialLocation);
    035            iName = name;
    036            iWidth = width;
    037            iHeight = height;
    038            iMinX = minX;
    039            iMaxX = maxX;
    040            iMinY = minY;
    041            iMaxY = maxY;
    042            setValues(computeValues());
    043        }
    044
    045        /**
    046         * Prohibits given X and Y coordinates (for MPP).
    047         */
    048        public void setProhibited(int x, int y) {
    049            iProhibitedX = x;
    050            iProhibitedY = y;
    051            setValues(computeValues());
    052            if (getInitialAssignment()!=null && !values().contains(getInitialAssignment()))
    053                setInitialAssignment(null);
    054        }
    055
    056        /**
    057         * Prohibits given initial location (for MPP).
    058         */
    059        public void setProhibited() {
    060            if (getInitialAssignment()==null) return;
    061            setProhibited(((Location)getInitialAssignment()).getX(),((Location)getInitialAssignment()).getY());
    062        }
    063
    064        /**
    065         * Returns true if the given location is prohibited. This means that either X or Y equals to the prohibited X or Y coordinate respectively.
    066         */
    067        public boolean isProhibited(int x, int y) {
    068            return (iProhibitedX == x || iProhibitedY == y);
    069        }
    070
    071        public int getProhibitedX() { return iProhibitedX; }
    072        public int getProhibitedY() { return iProhibitedY; }
    073        public int getMinX() { return iMinX; }
    074        public int getMaxX() { return iMaxX; }
    075        public int getMinY() { return iMinY; }
    076        public int getMaxY() { return iMaxY; }
    077
    078        /** Returns width of the rectangle */
    079        public int getWidth() {
    080            return iWidth;
    081        }
    082
    083        /** Returns height of the rectangle */
    084        public int getHeight() {
    085            return iHeight;
    086        }
    087
    088        /** Returns name of the rectangle */
    089        public String getName() {
    090            return iName;
    091        }
    092
    093        /** Set the bounds (minimal and maximal values of X and Y coordinates). */
    094        public void setBounds(int minX, int maxX, int minY, int maxY) {
    095            iMinX = minX;
    096            iMaxX = maxX;
    097            iMinY = minY;
    098            iMaxY = maxY;
    099            if (getInitialAssignment() != null && !values().contains(getInitialAssignment()))
    100                setInitialAssignment(null);
    101        }
    102
    103        private Vector computeValues() {
    104            Vector locations = new FastVector((iMaxX - iMinX) * (iMaxY - iMinY));
    105            for (int x = iMinX; x <= iMaxX; x++) {
    106                for (int y = iMinY; y <= iMaxY; y++) {
    107                    if (!isProhibited(x, y)) {
    108                        Value val = new Location(this, x, y);
    109                        locations.addElement(val);
    110                        if (getInitialAssignment() != null && getInitialAssignment().equals(val))
    111                            setInitialAssignment(val);
    112                        if (getBestAssignment() != null && getBestAssignment().equals(val))
    113                            setBestAssignment(val);
    114                    }
    115                }
    116            }
    117            return locations;
    118        }
    119
    120        /** String representation (for printing and debugging purposes) */
    121        public String toString() {
    122            return "Rectangle{name='" + getName() + "', size=[" + getWidth() + "," + getHeight() + "], bounds=[" + iMinX + ".." + iMaxX + "," + iMinY + ".." + iMaxY + "], super=" + super.toString() + "}";
    123        }
    124
    125        /** Compares two rectangles (based on rectangle names)*/
    126        public boolean equals(Object o) {
    127            return ((Rectangle)o).getName().equals(getName());
    128        }
    129    }