/** * Returns the {@link Node} with the given {@code id} or * {@code null} if the graph contains no respective {@link Node}. * * @param id the id of the Node to be removed * @return {@code true} if the graph contains a {@link Node} with the given {@code id}. Else {@code false}. */ public Node getNode(String id) { for (Node node : nodeList) { if (node.getId().equalsIgnoreCase(id)) { return node; } } return null; }
private void initOperator() throws GraphException { final OperatorSpiRegistry spiRegistry = GPF.getDefaultInstance().getOperatorSpiRegistry(); OperatorSpi operatorSpi = spiRegistry.getOperatorSpi(node.getOperatorName()); if (operatorSpi == null) { throw new GraphException("SPI not found for operator '" + node.getOperatorName() + "'"); } try { this.operator = operatorSpi.createOperator(); this.operator.setLogger(graphContext.getLogger()); // this.operator.setConfiguration(node.getConfiguration()); } catch (OperatorException e) { throw new GraphException("Failed to create instance of operator '" + node.getOperatorName() + "'", e); } }
@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()); }
configuration.createChild("file").setValue(sourceFilePath); Node sourceNode = new Node(sourceNodeId, readOperatorAlias); sourceNode.setConfiguration(configuration); final String operatorName = lastNode.getOperatorName(); final OperatorSpi operatorSpi = operatorSpiRegistry.getOperatorSpi(operatorName); if (operatorSpi == null) { Boolean.toString(commandLineArgs.isClearCacheAfterRowWrite())); Node targetNode = new Node(WRITE_OP_ID_PREFIX + lastNode.getId(), writeOperatorAlias); targetNode.addSource(new NodeSource("source", lastNode.getId())); targetNode.setConfiguration(configuration);
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 Graph createTestGraph(String opName) { Graph graph = new Graph("graph"); Node input1Node = new Node("input1", "InputOp"); Node input2Node = new Node("input2", "InputOp"); Node input3Node = new Node("input3", "InputOp"); Node outputNode = new Node("output", opName); graph.addNode(input1Node); graph.addNode(input2Node); graph.addNode(input3Node); graph.addNode(outputNode); return graph; }
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 testSpiCreation() throws GraphException { Graph graph = new Graph("chain1"); Node node1 = new Node("node1", "Op1"); graph.addNode(node1); GraphContext graphContext = new GraphContext(graph); NodeContext nodeContext = graphContext.getNodeContext(node1); assertEquals("Op1", graph.getNode("node1").getOperatorName()); assertEquals("org.esa.beam.framework.gpf.TestOps$Op1", nodeContext.getOperator().getClass().getName()); }
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); }
assertSame(nodes[2], node3); assertEquals("Op1", node1.getOperatorName()); assertEquals("Op2", node2.getOperatorName()); assertEquals("Op3", node3.getOperatorName()); NodeSource[] sources1 = node1.getSources(); assertNotNull(sources1); assertEquals(0, sources1.length); NodeSource[] sources2 = node2.getSources(); assertNotNull(sources2); assertEquals(1, sources2.length); assertEquals("grunt", sources2[0].getSourceNodeId()); NodeSource[] sources3 = node3.getSources(); assertNotNull(sources3); assertEquals(2, sources3.length);
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()); }
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()); } }
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); } } } }
DomElement configurationDomElement = bandMathsNode.getConfiguration(); ParameterDescriptorFactory parameterDescriptorFactory = new ParameterDescriptorFactory();
/** * Indirectly used by {@link GraphIO}. DO NOT REMOVE! * * @return this */ @SuppressWarnings({"UnusedDeclaration"}) private Object readResolve() { init(); return this; }
public void testAddExistingNode() { Graph graph = new Graph("chain1"); Node node1 = new Node("node1", "org.esa.beam.framework.gpf.TestOps$Op1Spi"); Node node2 = new Node("node1", "org.esa.beam.framework.gpf.TestOps$Op2Spi"); try { graph.addNode(node1); graph.addNode(node2); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { // expected } }
public void testOneNodeChain() throws Exception { Graph graph1 = new Graph("myOneNodeGraph"); Node node1 = new Node("node1", "Op1"); graph1.addNode(node1); Graph chain2 = doIO(graph1); Node[] nodes = chain2.getNodes(); assertNotNull(nodes); assertEquals(1, nodes.length); assertNotNull(nodes[0]); assertEquals(graph1.getId(), chain2.getId()); Node node1Copy = chain2.getNode("node1"); assertSame(nodes[0], node1Copy); assertEquals("Op1", node1Copy.getOperatorName()); }
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(expectedSourceFilepath1, generatedReaderNode1.getConfiguration().getChild("file").getValue()); Node generatedReaderNode2 = executedGraph.getNode(expectedSourceNodeId2); assertNotNull(generatedReaderNode2); assertEquals(expectedSourceFilepath2, generatedReaderNode2.getConfiguration().getChild("file").getValue()); assertEquals("node2", generatedWriterNode.getSource(0).getSourceNodeId()); DomElement parameters = generatedWriterNode.getConfiguration(); assertNotNull(parameters); assertNotNull(expectedTargetFilepath, parameters.getChild("file").getValue());
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()); } }