@Test public void testMerge() { DagNode<String> dagNode1 = new DagNode<>("val1"); DagNode<String> dagNode2 = new DagNode<>("val2"); DagNode<String> dagNode3 = new DagNode<>("val3"); DagNode<String> dagNode4 = new DagNode<>("val4"); DagNode<String> dagNode5 = new DagNode<>("val5"); dagNode2.addParentNode(dagNode1); dagNode3.addParentNode(dagNode1); dagNode4.addParentNode(dagNode2); dagNode4.addParentNode(dagNode3); dagNode5.addParentNode(dagNode3); List<DagNode<String>> dagNodeList = Lists.newArrayList(dagNode1, dagNode2, dagNode3, dagNode4, dagNode5); Dag<String> dag1 = new Dag<>(dagNodeList); DagNode<String> dagNode6 = new DagNode<>("val6"); DagNode<String> dagNode7 = new DagNode<>("val7"); DagNode<String> dagNode8 = new DagNode<>("val8"); dagNode8.addParentNode(dagNode6); dagNode8.addParentNode(dagNode7); Dag<String> dag2 = new Dag<>(Lists.newArrayList(dagNode6, dagNode7, dagNode8)); Dag<String> dagNew = dag1.merge(dag2); Assert.assertEquals(dagNew.getStartNodes().size(), 3); for (DagNode<String> dagNode: Lists.newArrayList(dagNode1, dagNode6, dagNode7)) { Assert.assertTrue(dagNew.getStartNodes().contains(dagNode)); Assert.assertEquals(dagNew.getParents(dagNode).size(), 0); if (dagNode == dagNode1) { List<DagNode<String>> nextNodes = dagNew.getChildren(dagNode); Assert.assertEquals(nextNodes.size(), 2); Assert.assertTrue(nextNodes.contains(dagNode2)); Assert.assertTrue(nextNodes.contains(dagNode3)); } else { Assert.assertEquals(dagNew.getChildren(dagNode).size(), 1); Assert.assertTrue(dagNew.getChildren(dagNode).contains(dagNode8)); } } Assert.assertEquals(dagNew.getEndNodes().size(), 3); for (DagNode<String> dagNode: Lists.newArrayList(dagNode4, dagNode5, dagNode8)) { Assert.assertTrue(dagNew.getEndNodes().contains(dagNode)); Assert.assertEquals(dagNew.getChildren(dagNode).size(), 0); if (dagNode == dagNode8) { Assert.assertEquals(dagNew.getParents(dagNode).size(), 2); Assert.assertTrue(dagNew.getParents(dagNode).contains(dagNode6)); Assert.assertTrue(dagNew.getParents(dagNode).contains(dagNode7)); } else { Assert.assertTrue(dagNew.getParents(dagNode).contains(dagNode3)); if (dagNode == dagNode4) { Assert.assertEquals(dagNew.getParents(dagNode).size(), 2); Assert.assertTrue(dagNew.getParents(dagNode).contains(dagNode2)); } else { Assert.assertEquals(dagNew.getParents(dagNode).size(), 1); } } } Assert.assertEquals(dagNew.getChildren(dagNode2).size(), 1); Assert.assertTrue(dagNew.getChildren(dagNode2).contains(dagNode4)); Assert.assertEquals(dagNew.getChildren(dagNode3).size(), 2); Assert.assertTrue(dagNew.getChildren(dagNode3).contains(dagNode4)); Assert.assertTrue(dagNew.getChildren(dagNode3).contains(dagNode5)); }
public Dag<T> merge(Dag<T> other) { if (other == null || other.isEmpty()) { return this; } if (this.isEmpty()) { return other; } for (Map.Entry<DagNode, List<DagNode<T>>> entry : other.parentChildMap.entrySet()) { this.parentChildMap.put(entry.getKey(), entry.getValue()); } this.startNodes.addAll(other.startNodes); this.endNodes.addAll(other.endNodes); this.nodes.addAll(other.nodes); return this; }
Dag { public Dag<T> merge(Dag<T> other) { if (other == null || other.isEmpty()) { return this; } if (this.isEmpty()) { return other; } for (Map.Entry<DagNode, List<DagNode<T>>> entry : other.parentChildMap.entrySet()) { this.parentChildMap.put(entry.getKey(), entry.getValue()); } this.startNodes.addAll(other.startNodes); this.endNodes.addAll(other.endNodes); this.nodes.addAll(other.nodes); return this; } }
Dag { public Dag<T> merge(Dag<T> other) { if (other == null || other.isEmpty()) { return this; } if (this.isEmpty()) { return other; } for (Map.Entry<DagNode, List<DagNode<T>>> entry : other.parentChildMap.entrySet()) { this.parentChildMap.put(entry.getKey(), entry.getValue()); } this.startNodes.addAll(other.startNodes); this.endNodes.addAll(other.endNodes); this.nodes.addAll(other.nodes); return this; } Dag(List<DagNode<T>> dagNodes); }
Dag { public Dag<T> merge(Dag<T> other) { if (other == null || other.isEmpty()) { return this; } if (this.isEmpty()) { return other; } for (Map.Entry<DagNode, List<DagNode<T>>> entry : other.parentChildMap.entrySet()) { this.parentChildMap.put(entry.getKey(), entry.getValue()); } this.startNodes.addAll(other.startNodes); this.endNodes.addAll(other.endNodes); this.nodes.addAll(other.nodes); return this; } Dag(List<DagNode<T>> dagNodes); List<DagNode<T>> getChildren(DagNode node); List<DagNode<T>> getParents(DagNode node); Set<DagNode<T>> getDependencyNodes(Set<DagNode<T>> forkNodes); boolean isEmpty(); Dag<T> concatenate(Dag<T> other); Dag<T> concatenate(Dag<T> other, Set<DagNode<T>> forkNodes); Dag<T> merge(Dag<T> other); @Override String toString(); }
Dag { public Dag<T> merge(Dag<T> other) { if (other == null || other.isEmpty()) { return this; } if (this.isEmpty()) { return other; } for (Map.Entry<DagNode, List<DagNode<T>>> entry : other.parentChildMap.entrySet()) { this.parentChildMap.put(entry.getKey(), entry.getValue()); } this.startNodes.addAll(other.startNodes); this.endNodes.addAll(other.endNodes); this.nodes.addAll(other.nodes); return this; } Dag(List<DagNode<T>> dagNodes); List<DagNode<T>> getChildren(DagNode node); List<DagNode<T>> getParents(DagNode node); Set<DagNode<T>> getDependencyNodes(Set<DagNode<T>> forkNodes); boolean isEmpty(); Dag<T> concatenate(Dag<T> other); Dag<T> concatenate(Dag<T> other, Set<DagNode<T>> forkNodes); Dag<T> merge(Dag<T> other); @Override String toString();  }