001/** A data structure that provides counterexample information when a graph is
002    tested to see if it is a lattice.
003
004 Copyright (c) 2011-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.graph;
030
031///////////////////////////////////////////////////////////////////
032//// NonLatticeCounterExample
033
034import java.util.ArrayList;
035import java.util.List;
036
037import ptolemy.graph.CPO.BoundType;
038
039/**
040A data structure that provides counterexample information when a graph is
041tested to see if it is a lattice. If a graph is not a lattice, it could be
042because a set of nodes have no least upper or greatest lower bound, or because
043the graph has a cycle.
044
045@author Charles Shelton
046@version $Id$
047@since Ptolemy II 10.0
048@Pt.ProposedRating Red (cshelton)
049@Pt.AcceptedRating Red (cshelton)
050 */
051public class NonLatticeCounterExample {
052
053    /** Construct a NonLatticeCounterExample object with the given example
054     *  type.
055     *  @param exampleType The given example type for this counterexample.
056     */
057    public NonLatticeCounterExample(ExampleType exampleType) {
058        _exampleType = exampleType;
059        _nodeList = new ArrayList();
060    }
061
062    /** Construct a NonLatticeCounterExample object with the given example
063     *  type and list of nodes in the graph.
064     *  @param exampleType The given example type for this counterexample.
065     *  @param nodeList The list of node weights for this counterexample.
066     */
067    public NonLatticeCounterExample(ExampleType exampleType, List nodeList) {
068        _exampleType = exampleType;
069        _nodeList = new ArrayList(nodeList);
070    }
071
072    /** Construct a NonLatticeCounterExample object for a graph with a cycle.
073     *  @param node The weight of one of the nodes in the graph that is on the
074     *   cycle path.
075     */
076    public NonLatticeCounterExample(Object node) {
077        _exampleType = GraphExampleType.GRAPHCYCLE;
078        _nodeList = new ArrayList();
079        _nodeList.add(node);
080    }
081
082    /** Construct a NonLatticeCounterExample object for a pair of nodes that
083     *  have either no least upper or greatest lower bound.
084     *  @param bound The bound type for this counter example.
085     *  @param node1 The first node weight.
086     *  @param node2 The second node weight.
087     */
088    public NonLatticeCounterExample(BoundType bound, Object node1,
089            Object node2) {
090        _exampleType = getExampleTypeFromBoundType(bound);
091        _nodeList = new ArrayList();
092        _nodeList.add(node1);
093        _nodeList.add(node2);
094    }
095
096    ///////////////////////////////////////////////////////////////////
097    ////                         public methods                    ////
098
099    /** Return the example type for this NonLatticeCounterExample.
100     *  @return Either LEASTUPPER, GREATESTLOWER, or GRAPHCYCLE.
101     */
102    public ExampleType getExampleType() {
103        return _exampleType;
104    }
105
106    /** Return the list of node weights in the graph associated with this
107     *  counter example.
108     *  @return The list of node weights.
109     */
110    public List getNodeList() {
111        return _nodeList;
112    }
113
114    ///////////////////////////////////////////////////////////////////
115    ////                         private methods                   ////
116
117    /** Return the correct example type given the CPO bound type (LEASTUPPER or
118     *  GREATESTLOWER).
119     *  @param boundType The given CPO bound type.
120     *  @return The example type for the given bound type.
121     */
122    private ExampleType getExampleTypeFromBoundType(BoundType boundType) {
123        switch (boundType) {
124        case LEASTUPPER:
125            return GraphExampleType.LEASTUPPER;
126        case GREATESTLOWER:
127            return GraphExampleType.GREATESTLOWER;
128        default:
129            return null;
130        }
131    }
132
133    ///////////////////////////////////////////////////////////////////
134    ////                         private variables                 ////
135
136    /** The example type for this NonLatticeCounterExample. */
137    private ExampleType _exampleType;
138
139    /** The list of nodes associated with this NonLatticeCounterExample. */
140    private List _nodeList;
141
142    ///////////////////////////////////////////////////////////////////
143    ////                         public inner classes              ////
144
145    /** Marker interface for the counter example type. This allows
146     *  us to create other enumerations of counter example types in
147     *  subclasses. In particular, this is needed for the ontologies
148     *  package classes ptolemy.data.ontologies.lattice.ProductLatticeOntology
149     *  ProductLatticeOntology and
150     *  ptolemy.data.ontologies.lattice.ProductLatticeCPO ProductLatticeCPO.
151     *  When a product lattice is not a lattice, the reason is because one of
152     *  its component lattices is not a lattice. This is only relevant for
153     *  product lattice ontologies.
154     */
155    public interface ExampleType {
156    }
157
158    /** An enumeration type to represent the types of counterexamples
159     *  that can be found when checking to see if a graph is a lattice.
160     */
161    public static enum GraphExampleType implements ExampleType {
162        /** Represents a counterexample where some nodes have no greatest lower bound. */
163        GREATESTLOWER,
164
165        /** Represents a counterexample where some nodes have no least upper bound. */
166        LEASTUPPER,
167
168        /** Represents a counterexample where the graph has a cycle. */
169        GRAPHCYCLE
170    }
171}