001/*
002 * Copyright (c) 2016 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
004 *
005 * Project and contact information: http://www.cascading.org/
006 *
007 * This file is part of the Cascading project.
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package cascading.flow.planner.graph;
023
024import java.util.IdentityHashMap;
025
026import cascading.flow.FlowElement;
027import cascading.flow.planner.Scope;
028import cascading.util.EnumMultiMap;
029import org.jgrapht.DirectedGraph;
030import org.jgrapht.graph.DirectedMultigraph;
031
032import static cascading.flow.planner.graph.ElementGraphs.directed;
033
034/**
035 *
036 */
037public class ElementMultiGraph extends BaseAnnotatedElementGraph implements ElementGraph, AnnotatedGraph
038  {
039  public ElementMultiGraph()
040    {
041    graph = new DirectedMultiGraph();
042    }
043
044  public ElementMultiGraph( ElementGraph parent )
045    {
046    graph = new DirectedMultiGraph( directed( parent ) );
047
048    addParentAnnotations( parent );
049    }
050
051  public ElementMultiGraph( ElementGraph parent, EnumMultiMap annotations )
052    {
053    this( parent );
054
055    getAnnotations().addAll( annotations );
056    }
057
058  @Override
059  public ElementGraph copyElementGraph()
060    {
061    return new ElementMultiGraph( this );
062    }
063
064  protected class DirectedMultiGraph extends DirectedMultigraph<FlowElement, Scope>
065    {
066    public DirectedMultiGraph()
067      {
068      super( Scope.class );
069      }
070
071    public DirectedMultiGraph( DirectedGraph<FlowElement, Scope> parent )
072      {
073      this();
074
075      // safe to assume there are no unconnected vertices
076      for( Scope edge : parent.edgeSet() )
077        {
078        FlowElement s = parent.getEdgeSource( edge );
079        FlowElement t = parent.getEdgeTarget( edge );
080        addVertex( s );
081        addVertex( t );
082        addEdge( s, t, edge );
083        }
084      }
085
086    @Override
087    protected DirectedSpecifics createDirectedSpecifics()
088      {
089      return new DirectedSpecifics( new IdentityHashMap<FlowElement, DirectedEdgeContainer<FlowElement, Scope>>() );
090      }
091    }
092  }