/** * Method getHeads returns the first Pipe instances in this pipe assembly. * * @return the first (type Pipe[]) of this Pipe object. */ public Pipe[] getHeads() { Pipe[] pipes = getPrevious(); if( pipes.length == 0 ) return new Pipe[]{this}; if( pipes.length == 1 ) return pipes[ 0 ].getHeads(); Set<Pipe> heads = new HashSet<Pipe>(); for( Pipe pipe : pipes ) Collections.addAll( heads, pipe.getHeads() ); return heads.toArray( new Pipe[ heads.size() ] ); }
/** * Method connect links the given source, sink, and trap Taps to the given pipe assembly. The given trap will * be linked to the assembly head along with the source. * * @param name name to give the resulting Flow * @param source source Tap to bind to the head of the given tail Pipe * @param sink sink Tap to bind to the given tail Pipe * @param trap trap Tap to sink all failed Tuples into * @param tail tail end of a pipe assembly * @return Flow */ public Flow connect( String name, Tap source, Tap sink, Tap trap, Pipe tail ) { Map<String, Tap> sources = new HashMap<String, Tap>(); sources.put( tail.getHeads()[ 0 ].getName(), source ); Map<String, Tap> traps = new HashMap<String, Tap>(); traps.put( tail.getHeads()[ 0 ].getName(), trap ); return connect( name, sources, sink, traps, tail ); }
protected Map<String, Tap> getSourceTapsMap( Pipe... sinkPipes ) { Set<Pipe> sourcePipesSet = new HashSet<Pipe>(); for( Pipe pipe : sinkPipes ) Collections.addAll( sourcePipesSet, pipe.getHeads() ); Pipe[] sourcePipes = sourcePipesSet.toArray( new Pipe[ sourcePipesSet.size() ] ); String[] names = makeNames( sourcePipes ); return Cascades.tapsMap( sourcePipes, getSourceTapsFor( names ) ); }
/** * Method connect links the given source and sink Taps to the given pipe assembly. * * @param name name to give the resulting Flow * @param source source Tap to bind to the head of the given tail Pipe * @param sink sink Tap to bind to the given tail Pipe * @param tail tail end of a pipe assembly * @return Flow */ public Flow connect( String name, Tap source, Tap sink, Pipe tail ) { Map<String, Tap> sources = new HashMap<String, Tap>(); sources.put( tail.getHeads()[ 0 ].getName(), source ); return connect( name, sources, sink, tail ); }
/** * Method connect links the named source Taps and sink Tap to the given pipe assembly. * <p> * Since only once source Tap is given, it is assumed to be associated with the 'head' pipe. * So the head pipe does not need to be included as an argument. * * @param name name to give the resulting Flow * @param source source Tap to bind to the head of the given tail Pipes * @param sinks all tail names and sink Taps to bind to the given tail Pipes * @param tails all tail ends of a pipe assembly * @return Flow */ public Flow connect( String name, Tap source, Map<String, Tap> sinks, Pipe... tails ) { Set<Pipe> heads = new HashSet<Pipe>(); for( Pipe pipe : tails ) Collections.addAll( heads, pipe.getHeads() ); if( heads.isEmpty() ) throw new IllegalArgumentException( "no pipe instance found" ); if( heads.size() != 1 ) throw new IllegalArgumentException( "there may be only 1 head pipe instance, found " + heads.size() ); Map<String, Tap> sources = new HashMap<String, Tap>(); for( Pipe pipe : heads ) sources.put( pipe.getName(), source ); return connect( name, sources, sinks, tails ); }
/** * Method connect links the named trap Taps, source and sink Tap to the given pipe assembly. * * @param name name to give the resulting Flow * @param source source Tap to bind to the head of the given tail Pipe * @param sink sink Tap to bind to the given tail Pipe * @param traps all pipe names and trap Taps to sink all failed Tuples into * @param tail tail end of a pipe assembly * @return Flow */ public Flow connect( String name, Tap source, Tap sink, Map<String, Tap> traps, Pipe tail ) { Map<String, Tap> sources = new HashMap<String, Tap>(); sources.put( tail.getHeads()[ 0 ].getName(), source ); Map<String, Tap> sinks = new HashMap<String, Tap>(); sinks.put( tail.getName(), sink ); return connect( name, sources, sinks, traps, tail ); }
/** * Method addSource adds a new source {@link Tap} named after the given {@link Pipe} for use in the resulting {@link Flow}. * <p> * If the given pipe is not a head pipe, it will be resolved. If more than one is found, an * {@link IllegalArgumentException} will be thrown. * * @param pipe of Pipe * @param source of Tap * @return FlowDef */ public FlowDef addSource( Pipe pipe, Tap source ) { if( pipe == null ) throw new IllegalArgumentException( "pipe may not be null" ); Pipe[] heads = pipe.getHeads(); if( heads.length != 1 ) throw new IllegalArgumentException( "pipe has too many heads, found: " + Arrays.toString( Pipe.names( heads ) ) ); addSource( heads[ 0 ].getName(), source ); return this; }
for( Pipe head : pipe.getHeads() )
@Test public void testGetFirstJoin() { Pipe pipeFirst = new Pipe( "first" ); Pipe pipeSecond = new Pipe( "second" ); Pipe pipe = new CoGroup( pipeFirst, pipeSecond ); pipe = new Pipe( pipe ); pipe = new Pipe( pipe ); pipe = new Pipe( pipe ); assertTrue( pipe.getHeads()[ 0 ] == pipeFirst || pipe.getHeads()[ 0 ] == pipeSecond ); }
Collections.addAll(heads, pipe.getHeads());
@Test public void testGetFirstSplit() { Pipe pipeFirst = new Pipe( "first" ); Pipe pipe = new Pipe( pipeFirst ); Pipe pipeA = new Pipe( pipe ); Pipe pipeB = new Pipe( pipe ); pipeA = new Pipe( pipeA ); pipeB = new Pipe( pipeB ); assertEquals( pipeFirst, pipeA.getHeads()[ 0 ] ); assertEquals( pipeFirst, pipeB.getHeads()[ 0 ] ); }
@Test public void testGetFirst() { Pipe pipeFirst = new Pipe( "first" ); Pipe pipe = new Pipe( pipeFirst ); pipe = new Pipe( pipe ); pipe = new Pipe( pipe ); pipe = new Pipe( pipe ); assertEquals( pipeFirst, pipe.getHeads()[ 0 ] ); }
@Test public void testNestedAssembliesAccessors() throws IOException { Pipe pipe = new Pipe( "test" ); pipe = new SecondAssembly( pipe ); Pipe[] allPrevious = pipe.getPrevious(); assertEquals( "wrong number of previous", 1, allPrevious.length ); // for( Pipe previous : allPrevious ) // assertFalse( previous instanceof PipeAssembly ); Pipe[] heads = pipe.getHeads(); assertEquals( "wrong number of heads", 1, heads.length ); for( Pipe head : heads ) assertFalse( head instanceof SubAssembly ); }
@Test public void testNestedAssembliesAccessors() throws IOException { Pipe pipe = new Pipe( "test" ); pipe = new SecondAssembly( pipe ); Pipe[] allPrevious = pipe.getPrevious(); assertEquals( "wrong number of previous", 1, allPrevious.length ); // for( Pipe previous : allPrevious ) // assertFalse( previous instanceof PipeAssembly ); Pipe[] heads = pipe.getHeads(); assertEquals( "wrong number of heads", 1, heads.length ); for( Pipe head : heads ) assertFalse( head instanceof SubAssembly ); }