/** * Connect up a new named input port node to the first child of the terminal * node. If the terminal node doesn't have any children then create a new * cross product node, connect it to the terminal and connect the new input * port node to the cross product (saneish default behaviour) * * @param nipn */ public synchronized void connectDefault(NamedInputPortNode nipn) { if (terminal.getChildCount() == 0) { CrossProduct cp = new CrossProduct(); cp.setParent(terminal); nipn.setParent(cp); } else nipn.setParent((AbstractIterationStrategyNode) terminal .getChildAt(0)); }
if (getChildCount() == 1) { pushJob(newJob); return; for (int i = 0; i < getChildCount(); i++) perInputCache.add(new HashSet<Job>()); ownerToCache.put(newJob.getOwningProcess(), perInputCache); workingSet.add(newJob); for (int i = 1; i < getChildCount(); i++) { Set<Job> thisSet = perInputCache.get(i); if (i == inputIndex) { thisSet.add(newJob); workingSet = merge(workingSet, thisSet); pushJob(outputJob); if (canClearCache(inputIndex, newJob.getOwningProcess()))
@Override public int getIterationDepth(Map<String, Integer> inputDepths) throws IterationTypeMismatchException { if (isLeaf()) // No children! throw new IterationTypeMismatchException( "Cross product with no children"); int temp = 0; for (IterationStrategyNode child : getChildren()) temp += child.getIterationDepth(inputDepths); return temp; } }
@Test public void addCrossProduct() throws Exception { assertNull(crossProduct1.getParent()); crossProduct1.setParent(root); assertEquals(root, crossProduct1.getParent()); assertEquals(1, root.getChildCount()); assertEquals(crossProduct1, root.getChildAt(0)); assertEquals(Arrays.asList(crossProduct1), root.getChildren()); assertEquals(0, crossProduct1.getChildCount()); crossProduct1.insert(input1); assertEquals(input1, crossProduct1.getChildAt(0)); crossProduct1.insert(input2, 0); assertEquals(input2, crossProduct1.getChildAt(0)); assertEquals(input1, crossProduct1.getChildAt(1)); assertEquals(2, crossProduct1.getChildCount()); assertEquals(Arrays.asList(input2, input1), crossProduct1.getChildren()); // A re-insert should move it crossProduct1.insert(input2, 2); assertEquals(2, crossProduct1.getChildCount()); assertEquals(Arrays.asList(input1, input2), crossProduct1.getChildren()); crossProduct1.insert(input2, 0); assertEquals(Arrays.asList(input2, input1), crossProduct1.getChildren()); crossProduct1.insert(input1, 1); assertEquals(Arrays.asList(input2, input1), crossProduct1.getChildren()); }
@Test public void moveNodeToDifferentParent() { crossProduct1.setParent(root); crossProduct1.insert(input1); crossProduct1.insert(dotProduct1); dotProduct1.insert(input2); dotProduct1.insert(crossProduct2); // Check tree assertEquals(crossProduct2, root.getChildAt(0).getChildAt(1) .getChildAt(1)); assertEquals(Arrays.asList(input2, crossProduct2), dotProduct1 .getChildren()); crossProduct1.insert(crossProduct2, 1); assertEquals(Arrays.asList(input1, crossProduct2, dotProduct1), crossProduct1.getChildren()); assertEquals(crossProduct1, crossProduct2.getParent()); // Should no longer be in dotProduct1 assertEquals(Arrays.asList(input2), dotProduct1.getChildren()); }
@Test public void addCrossProductMany() { crossProduct1.insert(dotProduct1); crossProduct1.insert(dotProduct2); crossProduct1.insert(input1); crossProduct1.insert(input2); crossProduct1.insert(crossProduct2); assertEquals(5, crossProduct1.getChildCount()); assertEquals(Arrays.asList(dotProduct1, dotProduct2, input1, input2, crossProduct2), crossProduct1.getChildren()); Enumeration<IterationStrategyNode> enumeration = crossProduct1 .children(); assertTrue(enumeration.hasMoreElements()); assertEquals(dotProduct1, enumeration.nextElement()); assertEquals(dotProduct2, enumeration.nextElement()); assertEquals(input1, enumeration.nextElement()); assertEquals(input2, enumeration.nextElement()); assertEquals(crossProduct2, enumeration.nextElement()); assertFalse(enumeration.hasMoreElements()); }
private AbstractIterationStrategyNode nodeForElement(Element e) { AbstractIterationStrategyNode node = null; String eName = e.getName(); if (eName.equals("dot")) node = new DotProduct(); else if (eName.equals("cross")) node = new CrossProduct(); else if (eName.equals("prefix")) node = new PrefixDotProduct(); else if (eName.equals("port")) { NamedInputPortNode nipn = new NamedInputPortNode( e.getAttributeValue("name"), Integer.parseInt(e .getAttributeValue("depth"))); node = nipn; addInput(nipn); } for (Object child : e.getChildren()) nodeForElement((Element) child).setParent(node); return node; }
@Test public void cantSetSelfParent() throws Exception { crossProduct1.insert(dotProduct1); try { dotProduct1.setParent(dotProduct1); fail("Didn't throw IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Make sure we didn't loose our old parent and // ended up in a funny state assertEquals(crossProduct1, dotProduct1.getParent()); assertEquals(dotProduct1, crossProduct1.getChildAt(0)); } }
/** * Returns true iff completions have been received for all other inputs. * * @param inputIndex * @param owningProcess * @return true iff completions have been received for all other inputs */ private boolean canClearCache(int inputIndex, String owningProcess) { boolean[] completionState = getCompletionState(owningProcess).inputComplete; for (int i = 0; i < completionState.length; i++) if (i != inputIndex && !completionState[i]) return false; return true; }
@Test public void cantAddSelf() throws Exception { dotProduct1.setParent(crossProduct1); try { dotProduct1.insert(dotProduct1); fail("Didn't throw IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Make sure we didn't loose our old parent and // ended up in a funny state assertEquals(crossProduct1, dotProduct1.getParent()); assertEquals(dotProduct1, crossProduct1.getChildAt(0)); } }
@Before public void makeNodes() throws Exception { root = new DummyTerminalNode(); input1 = new NamedInputPortNode("input1", 1); input2 = new NamedInputPortNode("input2", 2); crossProduct1 = new CrossProduct(); crossProduct2 = new CrossProduct(); dotProduct1 = new DotProduct(); dotProduct2 = new DotProduct(); }
private IterationStrategyImpl getISCross(int depthA, int depthB) { IterationStrategyImpl is1 = new IterationStrategyImpl(); NamedInputPortNode nipn1 = new NamedInputPortNode("a", depthA); NamedInputPortNode nipn2 = new NamedInputPortNode("b", depthB); is1.addInput(nipn1); is1.addInput(nipn2); CrossProduct cp = new CrossProduct(); nipn1.setParent(cp); nipn2.setParent(cp); cp.setParent(is1.getTerminalNode()); return is1; }