/** * Auxiliary method to collect all vertices which are reachable from the input vertices. * * @param jv the currently considered job vertex * @param collector a temporary list to store the vertices that have already been visisted */ private void collectVertices( final AbstractJobVertex jv, final HashSet<JobVertexID> visited, final List<AbstractJobVertex> collector) { visited.add(jv.getID()); collector.add(jv); for (int i = 0; i < jv.getNumberOfForwardConnections(); i++) { AbstractJobVertex vertex = jv.getForwardConnection(i).getConnectedVertex(); if (!visited.contains(vertex.getID())) { collectVertices(vertex, visited, collector); } } }
/** * Auxiliary method for cycle detection. Performs a depth-first traversal with vertex markings to * detect a cycle. If a node with a temporary marking is found, then there is a cycle. Once all * children of a vertex have been traversed the parent node cannot be part of another cycle and is * thus permanently marked. * * @param jv current job vertex to check * @param temporarilyMarked set of temporarily marked nodes * @param permanentlyMarked set of permanently marked nodes * @return <code>true</code> if there is a cycle, <code>false</code> otherwise */ private boolean detectCycle( final AbstractJobVertex jv, final HashSet<JobVertexID> temporarilyMarked, final HashSet<JobVertexID> permanentlyMarked) { JobVertexID vertexID = jv.getID(); if (permanentlyMarked.contains(vertexID)) { return false; } else if (temporarilyMarked.contains(vertexID)) { return true; } else { temporarilyMarked.add(vertexID); for (int i = 0; i < jv.getNumberOfForwardConnections(); i++) { if (detectCycle( jv.getForwardConnection(i).getConnectedVertex(), temporarilyMarked, permanentlyMarked)) { return true; } } permanentlyMarked.add(vertexID); return false; } }
@Test public void testSerialization() { try { JobGraph jg = new JobGraph("The graph"); // add some configuration values { jg.getJobConfiguration().setString("some key", "some value"); jg.getJobConfiguration().setDouble("Life of ", Math.PI); } // add some vertices { AbstractJobVertex source1 = new AbstractJobVertex("source1"); AbstractJobVertex source2 = new AbstractJobVertex("source2"); AbstractJobVertex target = new AbstractJobVertex("target"); target.connectNewDataSetAsInput(source1, DistributionPattern.POINTWISE); target.connectNewDataSetAsInput(source2, DistributionPattern.ALL_TO_ALL); jg.addVertex(source1); jg.addVertex(source2); jg.addVertex(target); } // de-/serialize and compare JobGraph copy = CommonTestUtils.createCopySerializable(jg); assertEquals(jg.getName(), copy.getName()); assertEquals(jg.getJobID(), copy.getJobID()); assertEquals(jg.getJobConfiguration(), copy.getJobConfiguration()); assertEquals(jg.getNumberOfVertices(), copy.getNumberOfVertices()); for (AbstractJobVertex vertex : copy.getVertices()) { AbstractJobVertex original = jg.findVertexByID(vertex.getID()); assertNotNull(original); assertEquals(original.getName(), vertex.getName()); assertEquals(original.getNumberOfInputs(), vertex.getNumberOfInputs()); assertEquals( original.getNumberOfProducedIntermediateDataSets(), vertex.getNumberOfProducedIntermediateDataSets()); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Returns an array of all job vertices than can be reached when traversing the job graph from the * input vertices. Each job vertex is contained only one time. * * @return an array of all job vertices than can be reached when traversing the job graph from the * input vertices */ public AbstractJobVertex[] getAllReachableJobVertices() { if (bufferedAllReachableJobVertices == null) { final List<AbstractJobVertex> collector = new ArrayList<AbstractJobVertex>(); final HashSet<JobVertexID> visited = new HashSet<JobVertexID>(); final Iterator<AbstractJobInputVertex> inputs = getInputVertices(); while (inputs.hasNext()) { AbstractJobVertex vertex = inputs.next(); if (!visited.contains(vertex.getID())) { collectVertices(vertex, visited, collector); } } bufferedAllReachableJobVertices = collector.toArray(new AbstractJobVertex[0]); } return bufferedAllReachableJobVertices; }