@Test(expected = IllegalArgumentException.class) public void testDuplicatedSourceNodeNameNotAllowed() throws Exception { final Node node = new Node("myId", "opName"); node.addSource(new NodeSource("duplicated", "sourceNode1")); node.addSource(new NodeSource("duplicated", "sourceNode2")); } }
private void initNodeContext(final NodeContext nodeContext, Operator graphOp) throws GraphException { if (nodeContext.isInitialized()) { return; } for (NodeSource source : nodeContext.getNode().getSources()) { NodeContext sourceNodeContext = getNodeContext(source.getSourceNode()); Product sourceProduct = null; if (sourceNodeContext != null) { initNodeContext(sourceNodeContext, graphOp); sourceProduct = sourceNodeContext.getTargetProduct(); } else { if (graphOp != null) { sourceProduct = graphOp.getSourceProduct(source.getSourceNodeId()); } } if (sourceProduct == null) { throw new GraphException(getMissingSourceMessage(nodeContext.getNode(), source)); } nodeContext.addSourceProduct(source.getName(), sourceProduct); } Node node = nodeContext.getNode(); DomElement configuration = node.getConfiguration(); OperatorConfiguration opConfiguration = this.createOperatorConfiguration(configuration, new HashMap<String, Object>()); nodeContext.setOperatorConfiguration(opConfiguration); nodeContext.initTargetProduct(); getInitNodeContextDeque().addFirst(nodeContext); }
private void initNodeDependencies() throws GraphException { Graph graph = getGraph(); for (Node node : graph.getNodes()) { for (NodeSource source : node.getSources()) { String sourceNodeId = source.getSourceNodeId(); Node sourceNode = graph.getNode(sourceNodeId); if (sourceNode == null) { Header header = graph.getHeader(); boolean sourceDefinedInHeader = header != null && isSourceNodeIdInHeader(sourceNodeId, header.getSources()); if (!sourceDefinedInHeader) { throw new GraphException(getMissingSourceMessage(node, source)); } } if (sourceNode != null) { getNodeContext(sourceNode).incrementReferenceCount(); source.setSourceNode(sourceNode); } } } }
@Override public void marshal(Object object, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) { SourceList sourceList = (SourceList) object; NodeSource[] sources = sourceList.getSources(); for (NodeSource source : sources) { hierarchicalStreamWriter.startNode(source.getName()); hierarchicalStreamWriter.addAttribute("refid", source.getSourceNodeId()); hierarchicalStreamWriter.endNode(); } }
@Test public void testAddSource() throws Exception { final Node node = new Node("myId", "opName"); node.addSource(new NodeSource("nodeName", "source1")); node.addSource(new NodeSource("anotherName", "source2")); node.addSource(new NodeSource("thirdName", "source3")); assertEquals(3, node.getSources().length); assertEquals("nodeName", node.getSources()[0].getName()); assertEquals("anotherName", node.getSources()[1].getName()); assertEquals("thirdName", node.getSources()[2].getName()); }
public void testSourceToNodeResolving() throws GraphException { Graph graph = new Graph("chain1"); Node node1 = new Node("node1", "org.esa.beam.framework.gpf.TestOps$Op1$Spi"); Node node2 = new Node("node2", "org.esa.beam.framework.gpf.TestOps$Op2$Spi"); node2.addSource(new NodeSource("input", "node1")); Node node3 = new Node("node3", "org.esa.beam.framework.gpf.TestOps$Op3$Spi"); node3.addSource(new NodeSource("input1", "node1")); node3.addSource(new NodeSource("input2", "node2")); graph.addNode(node1); graph.addNode(node2); graph.addNode(node3); GraphContext graphContext = new GraphContext(graph); assertEquals(node2.getSource(0).getSourceNode(), node1); assertEquals(node3.getSource(0).getSourceNode(), node1); assertEquals(node3.getSource(1).getSourceNode(), node2); assertFalse(graphContext.getNodeContext(node1).isOutput()); assertFalse(graphContext.getNodeContext(node2).isOutput()); assertTrue(graphContext.getNodeContext(node3).isOutput()); }
public void testTwoNodeChain() throws Exception { Graph graph1 = new Graph("myTwoNodeGraph"); Node node1 = new Node("node1", "Op1"); Node node2 = new Node("node2", "Op2"); node2.addSource(new NodeSource("key1", "node1")); graph1.addNode(node1); graph1.addNode(node2); Graph graph2 = doIO(graph1); assertEquals(graph1.getId(), graph2.getId()); Node[] nodes = graph2.getNodes(); assertNotNull(nodes); assertEquals(2, nodes.length); assertNotNull(nodes[0]); assertNotNull(nodes[1]); Node node1Copy = graph2.getNode("node1"); Node node2Copy = graph2.getNode("node2"); assertSame(nodes[0], node1Copy); assertSame(nodes[1], node2Copy); NodeSource[] sources1 = node1Copy.getSources(); assertNotNull(sources1); assertEquals(0, sources1.length); NodeSource[] sources2 = node2Copy.getSources(); assertNotNull(sources2); assertEquals(1, sources2.length); NodeSource source2 = node2Copy.getSource(0); assertSame(sources2[0], source2); assertEquals("Op1", node1Copy.getOperatorName()); assertEquals("Op2", node2Copy.getOperatorName()); assertEquals("node1", source2.getSourceNodeId()); }
private static String getMissingSourceMessage(Node node, NodeSource source) { return MessageFormat.format("Missing source ''{0}'' in node ''{1}''", source.getSourceNodeId(), node.getId()); } }
/** * Adds a {@link NodeSource} to this list. * * @param source the {@link NodeSource} */ public void addSource(NodeSource source) { final String sourceName = source.getName(); for (NodeSource nodeSource : sourceList) { if (nodeSource.getName().equals(sourceName)) { throw new IllegalArgumentException("duplicated source node name"); } } sourceList.add(source); }
private void testGraph(String[] args, int expectedNodeCount, String expectedLog, Map<String, String> expectedSourceNodeIdFilePathMap, String expectedTargetNodeId, String expectedTargetFilepath, String expectedTargetFormat) throws Exception { clTool.run(args); assertEquals(expectedLog, context.logString); Graph executedGraph = context.executedGraph; assertNotNull(executedGraph); assertEquals(expectedNodeCount, executedGraph.getNodeCount()); for (Map.Entry<String, String> entry : expectedSourceNodeIdFilePathMap.entrySet()) { String expectedSourceFilePath = entry.getValue(); if (expectedSourceFilePath != null) { final String key = entry.getKey(); Node generatedReaderNode1 = executedGraph.getNode(key); assertNotNull("Source ID not found: " + key, generatedReaderNode1); assertEquals(expectedSourceFilePath, generatedReaderNode1.getConfiguration().getChild("file").getValue()); } } Node generatedWriterNode = executedGraph.getNode(expectedTargetNodeId); assertNotNull(generatedWriterNode); assertEquals("node1", generatedWriterNode.getSource(0).getSourceNodeId()); DomElement parameters = generatedWriterNode.getConfiguration(); assertNotNull(parameters); assertNotNull(expectedTargetFilepath, parameters.getChild("file").getValue()); assertNotNull(expectedTargetFormat, parameters.getChild("formatName").getValue()); }
@Override public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) { SourceList sourceList = new SourceList(); while (hierarchicalStreamReader.hasMoreChildren()) { hierarchicalStreamReader.moveDown(); String name = hierarchicalStreamReader.getNodeName(); if ("sourceProducts".equals(name)) { final String sourceNodeIdsString = hierarchicalStreamReader.getValue().trim(); if (StringUtils.isNotNullAndNotEmpty(sourceNodeIdsString)) { final String[] sourceNodeIds = StringUtils.csvToArray(sourceNodeIdsString); for (int i = 0; i < sourceNodeIds.length; i++) { String sourceNodeId = sourceNodeIds[i]; sourceList.addSource(new NodeSource(String.format("%s%d", name, i), sourceNodeId)); } } } else { String refid = hierarchicalStreamReader.getAttribute("refid"); if (refid == null) { refid = hierarchicalStreamReader.getValue().trim(); } sourceList.addSource(new NodeSource(name, refid)); } hierarchicalStreamReader.moveUp(); } return sourceList; } }
assertEquals(expectedSourceNodeId1, node1.getSource(0).getSourceNodeId()); assertEquals(expectedThreshold, node1.getConfiguration().getChild("threshold").getValue()); assertEquals("node1", node2.getSource(0).getSourceNodeId()); assertEquals(expectedSourceNodeId2, node2.getSource(1).getSourceNodeId()); assertEquals(expectedExpression, node2.getConfiguration().getChild("expression").getValue()); assertEquals("node2", generatedWriterNode.getSource(0).getSourceNodeId());
public void testMissingSpi() { Graph graph = new Graph("graph"); Node node1 = new Node("node1", "Opa"); Node node2 = new Node("node2", "Op2"); Node node3 = new Node("node3", "Op3"); node2.addSource(new NodeSource("input", "node1")); node3.addSource(new NodeSource("input1", "node1")); node3.addSource(new NodeSource("input2", "node2")); try { graph.addNode(node1); graph.addNode(node3); new GraphContext(graph); fail("GraphException expected."); } catch (GraphException e) { // expected } }
assertNotNull(sources2); assertEquals(1, sources2.length); assertEquals("grunt", sources2[0].getSourceNodeId()); assertEquals("grunt", sources3[0].getSourceNodeId()); assertEquals("baz", sources3[1].getSourceNodeId());
public void testMissingNode() { Graph graph = new Graph("graph"); Node node1 = new Node("node1", "Op1"); Node node2 = new Node("node2", "Op2"); Node node3 = new Node("node3", "Op3"); node2.addSource(new NodeSource("input", "node1")); node3.addSource(new NodeSource("input1", "node1")); node3.addSource(new NodeSource("input2", "node2")); try { graph.addNode(node1); graph.addNode(node3); new GraphContext(graph); fail("GraphException expected."); } catch (GraphException e) { // expected } }
public void testAnySourcesOp() { final String opName = "AnySourcesOp"; Graph graph; Node outputNode; graph = createTestGraph(opName); try { new GraphContext(graph); fail("GraphException expected, at least one source expected"); } catch (GraphException ge) { } graph = createTestGraph(opName); outputNode = graph.getNode("output"); outputNode.addSource(new NodeSource("dummy1", "input1")); outputNode.addSource(new NodeSource("dummy2", "input2")); outputNode.addSource(new NodeSource("dummy3", "input3")); try { new GraphContext(graph); } catch (GraphException ge) { fail("GraphException not expected, any number of sources allowed. Error: " + ge.getMessage()); } }
public void testOptSourcesOp() { final String opName = "OptSourcesOp"; Graph graph; Node outputNode; graph = createTestGraph(opName); try { new GraphContext(graph); } catch (GraphException ge) { fail("GraphException not expected, sources not checked. Error: " + ge.getMessage()); } graph = createTestGraph(opName); outputNode = graph.getNode("output"); outputNode.addSource(new NodeSource("dummy1", "input1")); outputNode.addSource(new NodeSource("dummy2", "input2")); outputNode.addSource(new NodeSource("dummy3", "input3")); try { new GraphContext(graph); } catch (GraphException ge) { fail("GraphException not expected, sources not checked. Error: " + ge.getMessage()); } }
outputNode.addSource(new NodeSource("dummy", "input1")); try { new GraphContext(graph); outputNode.addSource(new NodeSource("dummy1", "input1")); outputNode.addSource(new NodeSource("dummy2", "input2")); try { new GraphContext(graph); outputNode.addSource(new NodeSource("dummy1", "input1")); outputNode.addSource(new NodeSource("dummy2", "input2")); outputNode.addSource(new NodeSource("dummy3", "input3")); try { new GraphContext(graph);
targetNode.addSource(new NodeSource("source", lastNode.getId())); targetNode.setConfiguration(configuration);
public void testOptionalAndWrongProductIsGiven() { Graph graph = new Graph("graph"); Node wrongBandsNode = new Node("WrongBands", wrongBandsOpSpi.getOperatorAlias()); Node consumerNode = new Node("OptionalConsumer", optionalConsumerOpSpi.getOperatorAlias()); consumerNode.addSource(new NodeSource("input1", "WrongBands")); graph.addNode(wrongBandsNode); graph.addNode(consumerNode); try { new GraphContext(graph); fail("GraphException expected, caused by missing bands, even if optional"); } catch (GraphException ge) { } }