001/*
002 * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.flow.planner.graph;
022
023import java.io.Serializable;
024import java.util.Collection;
025import java.util.List;
026import java.util.Set;
027
028import cascading.flow.FlowElement;
029import cascading.flow.planner.Scope;
030import cascading.util.Util;
031import org.jgrapht.DirectedGraph;
032import org.jgrapht.Graphs;
033import org.jgrapht.graph.SimpleDirectedGraph;
034
035/**
036 *
037 */
038public abstract class BaseElementGraph implements ElementGraph, Serializable
039  {
040  public static final ElementGraph NULL = new BaseElementGraph( new SimpleDirectedGraph<FlowElement, Scope>( Scope.class ) )
041  {
042  @Override
043  public ElementGraph copyElementGraph()
044    {
045    return null;
046    }
047  };
048
049  protected DirectedGraph<FlowElement, Scope> graph;
050
051  public BaseElementGraph()
052    {
053    }
054
055  public BaseElementGraph( DirectedGraph<FlowElement, Scope> graph )
056    {
057    this.graph = graph;
058    }
059
060  public boolean containsEdge( FlowElement sourceVertex, FlowElement targetVertex )
061    {
062    return graph.containsEdge( sourceVertex, targetVertex );
063    }
064
065  public boolean removeAllEdges( Collection<? extends Scope> edges )
066    {
067    return graph.removeAllEdges( edges );
068    }
069
070  public Set<Scope> removeAllEdges( FlowElement sourceVertex, FlowElement targetVertex )
071    {
072    return graph.removeAllEdges( sourceVertex, targetVertex );
073    }
074
075  public boolean removeAllVertices( Collection<? extends FlowElement> vertices )
076    {
077    return graph.removeAllVertices( vertices );
078    }
079
080  public Set<Scope> getAllEdges( FlowElement sourceVertex, FlowElement targetVertex )
081    {
082    return graph.getAllEdges( sourceVertex, targetVertex );
083    }
084
085  public Scope getEdge( FlowElement sourceVertex, FlowElement targetVertex )
086    {
087    return graph.getEdge( sourceVertex, targetVertex );
088    }
089
090  public Scope addEdge( FlowElement sourceVertex, FlowElement targetVertex )
091    {
092    return graph.addEdge( sourceVertex, targetVertex );
093    }
094
095  public boolean addEdge( FlowElement sourceVertex, FlowElement targetVertex, Scope scope )
096    {
097    return graph.addEdge( sourceVertex, targetVertex, scope );
098    }
099
100  public boolean addVertex( FlowElement flowElement )
101    {
102    return graph.addVertex( flowElement );
103    }
104
105  public FlowElement getEdgeSource( Scope scope )
106    {
107    return graph.getEdgeSource( scope );
108    }
109
110  public FlowElement getEdgeTarget( Scope scope )
111    {
112    return graph.getEdgeTarget( scope );
113    }
114
115  public boolean containsEdge( Scope scope )
116    {
117    return graph.containsEdge( scope );
118    }
119
120  public boolean containsVertex( FlowElement flowElement )
121    {
122    return graph.containsVertex( flowElement );
123    }
124
125  public Set<Scope> edgeSet()
126    {
127    return graph.edgeSet();
128    }
129
130  public Set<Scope> edgesOf( FlowElement vertex )
131    {
132    return graph.edgesOf( vertex );
133    }
134
135  public int inDegreeOf( FlowElement vertex )
136    {
137    return graph.inDegreeOf( vertex );
138    }
139
140  public Set<Scope> incomingEdgesOf( FlowElement vertex )
141    {
142    return graph.incomingEdgesOf( vertex );
143    }
144
145  public int outDegreeOf( FlowElement vertex )
146    {
147    return graph.outDegreeOf( vertex );
148    }
149
150  public Set<Scope> outgoingEdgesOf( FlowElement vertex )
151    {
152    return graph.outgoingEdgesOf( vertex );
153    }
154
155  public Scope removeEdge( FlowElement sourceVertex, FlowElement targetVertex )
156    {
157    return graph.removeEdge( sourceVertex, targetVertex );
158    }
159
160  public boolean removeEdge( Scope scope )
161    {
162    return graph.removeEdge( scope );
163    }
164
165  public boolean removeVertex( FlowElement flowElement )
166    {
167    return graph.removeVertex( flowElement );
168    }
169
170  public Set<FlowElement> vertexSet()
171    {
172    return graph.vertexSet();
173    }
174
175  @Override
176  public List<FlowElement> predecessorListOf( FlowElement flowElement )
177    {
178    return Graphs.predecessorListOf( graph, flowElement );
179    }
180
181  @Override
182  public List<FlowElement> successorListOf( FlowElement flowElement )
183    {
184    return Graphs.successorListOf( graph, flowElement );
185    }
186
187  @Override
188  public void writeDOT( String filename )
189    {
190    boolean success = ElementGraphs.printElementGraph( filename, this, null );
191
192    if( success )
193      Util.writePDF( filename );
194    }
195
196  @Override
197  public boolean equals( Object object )
198    {
199    return ElementGraphs.equals( this, (ElementGraph) object );
200    }
201
202  @Override
203  public int hashCode()
204    {
205    int result = graph.hashCode();
206    result = 31 * result; // parity with AnnotatedGraph types
207    return result;
208    }
209  }