001/*
002 * Copyright (c) 2007-2016 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.util.IdentityHashMap;
024
025import cascading.flow.FlowElement;
026import cascading.flow.planner.Scope;
027import org.jgrapht.DirectedGraph;
028import org.jgrapht.graph.DirectedMultigraph;
029
030import static cascading.flow.planner.graph.ElementGraphs.directed;
031
032/**
033 *
034 */
035public class ElementMultiGraph extends BaseAnnotatedElementGraph implements ElementGraph, AnnotatedGraph
036  {
037  public ElementMultiGraph()
038    {
039    graph = new DirectedMultiGraph();
040    }
041
042  public ElementMultiGraph( ElementGraph parent )
043    {
044    graph = new DirectedMultiGraph( directed( parent ) );
045
046    addParentAnnotations( parent );
047    }
048
049  @Override
050  public ElementGraph copyElementGraph()
051    {
052    return new ElementMultiGraph( this );
053    }
054
055  protected class DirectedMultiGraph extends DirectedMultigraph<FlowElement, Scope>
056    {
057    public DirectedMultiGraph()
058      {
059      super( Scope.class );
060      }
061
062    public DirectedMultiGraph( DirectedGraph<FlowElement, Scope> parent )
063      {
064      this();
065
066      // safe to assume there are no unconnected vertices
067      for( Scope edge : parent.edgeSet() )
068        {
069        FlowElement s = parent.getEdgeSource( edge );
070        FlowElement t = parent.getEdgeTarget( edge );
071        addVertex( s );
072        addVertex( t );
073        addEdge( s, t, edge );
074        }
075      }
076
077    @Override
078    protected DirectedSpecifics createDirectedSpecifics()
079      {
080      return new DirectedSpecifics( new IdentityHashMap<FlowElement, DirectedEdgeContainer<FlowElement, Scope>>() );
081      }
082    }
083  }