001/** An inequality over a CPO.
002
003 Copyright (c) 1997-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027
028 */
029package ptolemy.graph;
030
031import ptolemy.kernel.util.IllegalActionException;
032
033///////////////////////////////////////////////////////////////////
034//// Inequality
035
036/**
037 An inequality over a CPO.
038 Each inequality consists of two <code>InequalityTerms</code>, the lesser
039 term and the greater term. The relation between them is <i>less than or
040 equal to</i>.  In addition, an inequality keeps a list of variables in it.
041 The variables are <code>InequalityTerms</code> that consist of a single
042 variable.
043
044 @author Yuhong Xiong
045 @version $Id$
046 @since Ptolemy II 0.2
047 @Pt.ProposedRating Green (yuhong)
048 @Pt.AcceptedRating Green (kienhuis)
049 @see InequalityTerm
050 */
051public class Inequality {
052    /** Construct an inequality.
053     *  @param lesserTerm An <code>InequalityTerm</code> that is less than or
054     *   equal to the second argument.
055     *  @param greaterTerm An <code>InequalityTerm</code> that is greater than
056     *   or equal to the first argument.
057     *  @exception IllegalArgumentException If the <code>lesserTerm</code> or
058     *   the <code>greaterTerm</code> is <code>null</code>.
059     */
060    public Inequality(InequalityTerm lesserTerm, InequalityTerm greaterTerm) {
061        if (lesserTerm == null) {
062            throw new IllegalArgumentException(
063                    "Inequality.Inequality: " + "lesserTerm is null.");
064        }
065
066        if (greaterTerm == null) {
067            throw new IllegalArgumentException(
068                    "Inequality.Inequality: " + "greaterTerm is null.");
069        }
070
071        _lesserTerm = lesserTerm;
072        _greaterTerm = greaterTerm;
073    }
074
075    ///////////////////////////////////////////////////////////////////
076    ////                         public methods                    ////
077
078    /** Return true if object named by the argument is equal to
079     *  this Inequality object.
080     *  <p>Override to return true if the greater and lesser terms of
081     *  this object are the same as the greater and lesser terms of
082     *  the specified object.
083     *  @param object Object to compare against.
084     *  @return true If the object is an Inequality and both the
085     *  greater term and lesser term are equal to the corresponding
086     *  terms of this object.
087     */
088    @Override
089    public boolean equals(Object object) {
090        if (object instanceof Inequality) {
091            return ((Inequality) object)._greaterTerm.equals(_greaterTerm)
092                    && ((Inequality) object)._lesserTerm.equals(_lesserTerm);
093        }
094        return false;
095    }
096
097    /** Return the greater term of this inequality.
098     *  @return An <code>InequalityTerm</code>
099     */
100    public InequalityTerm getGreaterTerm() {
101        return _greaterTerm;
102    }
103
104    /** Return the lesser term of this inequality.
105     *  @return An <code>InequalityTerm</code>
106     */
107    public InequalityTerm getLesserTerm() {
108        return _lesserTerm;
109    }
110
111    /** Return the hashCode of this object.
112     *  <p>Override to return the exclusive OR of the hashcodes
113     *  of the greater and lesser terms. This ensures that two
114     *  objects that return true to equals() have the same
115     *  hashcode.
116     *  @return The XOR of the greater and lesser terms.
117     */
118    @Override
119    public int hashCode() {
120        return _lesserTerm.hashCode() ^ _greaterTerm.hashCode();
121    }
122
123    /** Test if this inequality is satisfied with the current value
124     *  of variables.
125     *  @param cpo A CPO over which this inequality is defined.
126     *  @return True if this inequality is satisfied;
127     *  false otherwise.
128     *  @exception IllegalActionException If thrown while getting
129     *  the value of the terms.
130     */
131    public boolean isSatisfied(CPO cpo) throws IllegalActionException {
132        int result = cpo.compare(_lesserTerm.getValue(),
133                _greaterTerm.getValue());
134        return result == CPO.LOWER || result == CPO.SAME;
135    }
136
137    /** Override the base class to describe the inequality.
138     *  @return A string describing the inequality.
139     */
140    @Override
141    public String toString() {
142        return _lesserTerm.toString() + " <= " + _greaterTerm.toString();
143    }
144
145    ///////////////////////////////////////////////////////////////////
146    ////                         private variables                 ////
147    private InequalityTerm _lesserTerm = null;
148
149    private InequalityTerm _greaterTerm = null;
150}