public static Integer findOrdinal( Pipe pipe, Pipe previous ) { Pipe[] previousPipes = pipe.getPrevious(); TreeMap<Integer, Integer> sorted = new TreeMap<>(); for( int i = 0; i < previousPipes.length; i++ ) { int result = isPrevious( previousPipes[ i ], (Pipe) previous ); if( result == -1 ) continue; sorted.put( result, i ); } return sorted.firstEntry().getValue(); } }
static Pipe resolvePrevious( Pipe pipe ) { if( pipe instanceof Splice || pipe instanceof Operator ) return pipe; Pipe[] pipes = pipe.getPrevious(); if( pipes.length > 1 ) throw new IllegalStateException( "cannot resolve SubAssemblies with multiple tails at this time" ); for( Pipe previous : pipes ) { if( previous instanceof Splice || previous instanceof Operator ) return previous; return resolvePrevious( previous ); } return pipe; }
/** * Is responsible for unwinding nested SubAssembly instances. * * @param tails of type Pipe[] * @return a Pipe[] */ public static Pipe[] unwind( Pipe... tails ) { Set<Pipe> previous = new HashSet<Pipe>(); for( Pipe pipe : tails ) { if( pipe instanceof SubAssembly ) Collections.addAll( previous, unwind( pipe.getPrevious() ) ); else previous.add( pipe ); } return previous.toArray( new Pipe[ previous.size() ] ); }
private static List<String> getIncomingFieldNames( Pipe pipe ) { if( pipe.getPrevious().length != 1 ) return null; final Pipe previous = pipe.getPrevious()[ 0 ]; if( !( previous instanceof Splice ) ) return null; final Splice splice = (Splice) previous; if( splice.getDeclaredFields() == null ) return null; return fieldNames( splice.getDeclaredFields() ); }
/** * 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() ] ); }
private static void collectPipes( String name, Pipe[] tails, Set<Pipe> pipes ) { for( Pipe tail : tails ) { if( !( tail instanceof SubAssembly ) && tail.getName().equals( name ) ) pipes.add( tail ); collectPipes( name, SubAssembly.unwind( tail.getPrevious() ), pipes ); } }
private void setParent( Set<Pipe> stopSet, Pipe[] tails ) { if( tails == null ) return; for( Pipe tail : tails ) { if( stopSet.contains( tail ) ) continue; tail.setParent( this ); Pipe[] current; if( tail instanceof SubAssembly ) current = ( (SubAssembly) tail ).previous; else current = tail.getPrevious(); if( current == null && tail instanceof SubAssembly ) LOG.warn( "previous pipes not set via setPrevious or constructor on: {}", tail ); setParent( stopSet, current ); } }
private static void collectNames( Pipe[] pipes, Set<String> names ) { for( Pipe pipe : pipes ) { if( pipe instanceof SubAssembly ) names.addAll( asList( ( (SubAssembly) pipe ).getTailNames() ) ); else names.add( pipe.getName() ); collectNames( SubAssembly.unwind( pipe.getPrevious() ), names ); } }
private static int collectPipes( Pipe pipe, int depth, Pipe... allPrevious ) { depth++; for( Pipe previous : allPrevious ) { if( pipe == previous ) return depth; int result; if( previous instanceof SubAssembly ) result = collectPipes( pipe, depth, SubAssembly.unwind( previous ) ); else result = collectPipes( pipe, depth, previous.getPrevious() ); if( result != -1 ) return result; } return -1; }
for( Pipe pipe : SubAssembly.unwind( current.getPrevious() ) ) makeGraph( pipe, sources, sinks ); if( SubAssembly.unwind( current.getPrevious() ).length == 0 ) for( Pipe previous : SubAssembly.unwind( current.getPrevious() ) )
@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 ); }