1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
28  
29  package demo.base;
30  
31  import y.base.EdgeMap;
32  import y.base.Graph;
33  import y.base.Edge;
34  import y.base.NodeMap;
35  import y.base.Node;
36  import y.util.YRandom;
37  import y.base.EdgeCursor;
38  import y.base.NodeCursor;
39  import y.util.D;
40  
41  /**
42   * This class represents an extended Graph object whose nodes
43   * and edges carry additional data. The yFiles way of adding additional 
44   * features to nodes and edges is not to extend the node and edge
45   * objects themselves but to extend the graph that contains the
46   * nodes and edges. The graph stores the extra data in internal Node-
47   * and/or EdgeMaps. Access to the additional node and edge data is provided
48   * by setter and getter methods of the extended graph.
49   * <br>
50   * There is a main method in this class that serves as a test driver for 
51   * the implementation.
52   *
53   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/custom_data#custom_data" target="_blank">Section Binding Data to Graph Elements</a> in the yFiles for Java Developer's Guide
54   */
55  public class ExtendedGraph extends Graph
56  {
57    /**
58     * internal NodeMap that stores additional node data
59     */
60    private NodeMap extraNodeData;
61    
62    /**
63     * internal EdgeMap that stores additional edge data
64     */
65    private EdgeMap extraEdgeData;
66   
67    /////////////////////////////////////////////////////////////////////////////
68    // CONSTRUCTION /////////////////////////////////////////////////////////////
69    /////////////////////////////////////////////////////////////////////////////
70    
71    /** Creates a new instance of ExtendedGraph */
72    public ExtendedGraph()
73    {
74      extraNodeData = createNodeMap();
75      extraEdgeData = createEdgeMap();
76    }
77    
78    /** Creates a graph of type ExtendedGraph */
79    public Graph createGraph()
80    {
81      return new ExtendedGraph();
82    }
83    
84    /////////////////////////////////////////////////////////////////////////////
85    // SETTER AND GETTER ////////////////////////////////////////////////////////
86    /////////////////////////////////////////////////////////////////////////////
87    
88    /**
89     * Returns the edge weight associated with the given edge.
90     * By default 0 will be returned.
91     */
92    public double getEdgeWeight(Edge e)
93    {
94      return extraEdgeData.getDouble(e);
95    }
96    
97    /**
98     * Sets the edge weight associated with the given edge.
99     */
100   public void setEdgeWeight(Edge e, double weight)
101   { 
102     extraEdgeData.setDouble(e, weight);
103   }
104   
105   /**
106    * Returns the node info associated with the given node.
107    *
108    * By default 0 will be returned.
109    */
110   public NodeInfo getNodeInfo(Node v)
111   {
112     NodeInfo info = (NodeInfo)extraNodeData.get(v);
113     
114 //lazy default initialisation could be performed here 
115 //    if(info == null)
116 //    {
117 //      info = new NodeInfo();
118 //      setNodeInfo(v, info);
119 //    }
120 
121     return info;
122 
123   }
124   
125   public void setNodeInfo(Node v, NodeInfo info)
126   {
127     extraNodeData.set(v, info);
128   }
129 
130   public static class NodeInfo
131   {
132     public String name;
133     public String type;
134     public int    version;
135   
136     public NodeInfo()
137     {
138       this("unknown","unknown",0);
139     }
140     
141     public NodeInfo(String name, String type, int version)
142     {
143       this.name = name;
144       this.type = type;
145       this.version = version;
146     }
147     
148     public String toString()
149     {
150       return "Name=" + name + "  Type=" + type + "  Version=" + version;
151     }
152   }
153   
154   
155   public static void main(String[] args)
156   {
157      ExtendedGraph graph = new ExtendedGraph();
158      RandomGraphGenerator rg = new RandomGraphGenerator(0);
159      rg.setNodeCount(10);
160      rg.setEdgeCount(10);
161      rg.generate(graph);
162      YRandom random = new YRandom(0);
163      for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
164      {
165        graph.setEdgeWeight(ec.edge(), random.nextDouble(0.0,10.0));
166      }
167      for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
168      {
169        Node v = nc.node();
170        NodeInfo info = new NodeInfo("Node #" + v.index(),"Extra Node",1);
171        graph.setNodeInfo(v, info);
172      }
173      
174      for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
175      {
176        D.bug(graph.getNodeInfo(nc.node()));
177      }
178      for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
179      {
180        D.bug("Edge " + ec.edge().index() + " weight= " + graph.getEdgeWeight(ec.edge()));
181      }
182      
183   }
184 }
185