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