001/* Static class for relation node.
002
003 Copyright (c) 2006-2013 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 */
028package ptolemy.domains.modal.kernel;
029
030///////////////////////////////////////////////////////////////////
031////RelationNode
032
033/**
034 An instance of the RelationNode class stores the type and difference
035 information of a relation. For more details of type, see {@link RelationType}.
036 For more details of difference, see
037 {@link ptolemy.domains.modal.kernel.ParseTreeEvaluatorForGuardExpression}.
038 The instance stores the information of two evaluations of the transition,
039 the current evaluation and the previous evaluation.
040
041 @author  Haiyang Zheng
042 @version $Id$
043 @since Ptolemy II 8.0
044 @Pt.ProposedRating Yellow (hyzheng)
045 @Pt.AcceptedRating Red (hyzheng)
046 */
047public final class RelationNode {
048
049    /** Construct a relation node with given type and difference
050     *  information.
051     *  @param type The type, one of {@link ptolemy.domains.modal.kernel.RelationType}.
052     *  @param difference The difference.
053     */
054    public RelationNode(int type, double difference) {
055        // FIXME: Perhaps the type argument should be a type safe enum?
056        _currentType = type;
057        _previousType = type;
058        _difference = difference;
059        _previousDifference = difference;
060    }
061
062    ///////////////////////////////////////////////////////////////////
063    ////                         public methods                    ////
064
065    /** Reset the relation node by setting the former type and difference
066     *  information to RelationType.INVALID and 0.0 respectively.
067     */
068    public void reset() {
069        _previousType = RelationType.INVALID;
070        _previousDifference = 0.0;
071    }
072
073    /** Update the previous type and difference information of this relation
074     *  node with the current evaluation result.
075     */
076    public void commit() {
077        _previousType = _currentType;
078        _previousDifference = _difference;
079    }
080
081    /** Return the difference information from the current evaluation of the
082     *  relation node.
083     *  @return The current difference information.
084     *  @see #setDifference(double)
085     */
086    public double getDifference() {
087        return _difference;
088    }
089
090    /** Return the difference information from the previous evaluation of the
091     *  relation node.
092     *  @return The previous difference information.
093     */
094    public double getPreviousDifference() {
095        return _previousDifference;
096    }
097
098    /** Return true if the relation node has its type changed, and if the
099     *  current type is equal/inequal or the current type changes from
100     *  less_than to bigger_than or bigger_than to less_than. This is used
101     *  to detect whether a continuous variable crosses a level.
102     *  @return True If event has been detected.
103     */
104    public boolean hasEvent() {
105        if (typeChanged()) {
106            return _previousType * _currentType == RelationType.LESS_THAN
107                    * RelationType.GREATER_THAN;
108        }
109        return false;
110    }
111
112    /** Set the type information with the current evaluation result of the
113     *  relation node.
114     *  @param type The current type information.
115     */
116    public void setType(int type) {
117        _currentType = type;
118    }
119
120    /** Set the difference information with the current evaluation result
121     *  of the relation node.
122     *  @param difference The current difference information.
123     *  @see #getDifference()
124     */
125    public void setDifference(double difference) {
126        _difference = difference;
127    }
128
129    /** Return true if the type changed and the previous type
130     *  information is valid.
131     *  @return True If the type changed and the previous type
132     *  information is valid.
133     */
134    public boolean typeChanged() {
135        return _previousType != RelationType.INVALID
136                && _previousType != _currentType;
137    }
138
139    ///////////////////////////////////////////////////////////////////
140    ////                       private fields                  ////
141    private int _currentType;
142
143    private double _difference;
144
145    private double _previousDifference;
146
147    private int _previousType;
148}