public static String getUsageTextForGraph(String path, CommandLineContext commandLineContext) { final Graph graph; try { graph = commandLineContext.readGraph(path, new HashMap<String, String>()); } catch (GraphException e) { return e.getMessage(); } catch (IOException e) { return e.getMessage(); } final StringBuilder usageText = new StringBuilder(1024); final Header header = graph.getHeader(); if (header != null) { usageText.append("Usage:\n"); usageText.append(MessageFormat.format(" {0} {1} [options] ", CommandLineTool.TOOL_NAME, path)); ArrayList<DocElement> sourceDocElementList = createSourceDocElementList(header.getSources()); ArrayList<DocElement> paramDocElementList = createParamDocElementList(header.getParameters()); if (!sourceDocElementList.isEmpty()) { usageText.append("\nSource Options:\n"); appendDocElementList(usageText, sourceDocElementList); } if (!paramDocElementList.isEmpty()) { usageText.append("\nParameter Options:\n"); appendDocElementList(usageText, paramDocElementList); } } return usageText.toString(); }
public void initTargetProduct() throws GraphException { try { targetProduct = operator.getTargetProduct(); } catch (OperatorException e) { throw new GraphException(e.getMessage(), e); } }
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()); } }
/** * Deserializes a {@link Graph} from a XML Reader using a mapping * for the substitution of template variables inside the XML-based * {@link Graph} definition. * * @param reader the XML reader * @param variables a mapping from template variable names to their string values. * @return the deserialized <code>graph</code> * @throws GraphException if an error occurs during reading */ public static Graph read(Reader reader, Map<String, String> variables) throws GraphException { XStream xStream = initXstream(); Reader inputReader = reader; if (variables != null) { addSourceProductsVariable(variables); inputReader = new TemplateReader(reader, variables); } // todo - throw exception if a given variable does not exist in the graph definition or if variables are not set. Graph graph = (Graph) xStream.fromXML(inputReader); if (graph.getVersion() == null) { throw new GraphException("No version is specified in the graph XML."); } else if (!Graph.CURRENT_VERSION.equals(graph.getVersion())) { throw new GraphException("Wrong version given in the graph XML. " + "Given version: " + graph.getVersion() + " Current version: " + Graph.CURRENT_VERSION); } return graph; }
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()); } }
/** * Creates a GraphContext for the given {@code graph} and a {@code logger}. * * @param graph the {@link org.esa.beam.framework.gpf.graph.Graph} to create the context for * @throws GraphException if the graph context could not be created */ public GraphContext(Graph graph, Operator graphOp) throws GraphException { if (graph.getNodeCount() == 0) { throw new GraphException("Empty graph."); } this.graph = graph; this.logger = BeamLogManager.getSystemLogger(); outputNodeContextList = new ArrayList<>(graph.getNodeCount() / 2); nodeContextMap = new HashMap<>(graph.getNodeCount() * 2); for (Node node : graph.getNodes()) { nodeContextMap.put(node, new NodeContext(this, node)); } initNodeContextDeque = new ArrayDeque<>(graph.getNodeCount()); initNodeDependencies(); initOutput(graphOp); }
graphContext = new GraphContext(graph); } catch (GraphException e) { fail(e.getMessage());
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); } }
graphContext = new GraphContext(graph); } catch (GraphException e) { fail(e.getMessage());
final OperatorSpi operatorSpi = operatorSpiRegistry.getOperatorSpi(operatorName); if (operatorSpi == null) { throw new GraphException(String.format("Unknown operator name '%s'.", operatorName));
new GraphContext(graph); } catch (GraphException ge) { fail("GraphException not expected, exactly 2 sources given. Error: " + ge.getMessage());
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); }
graphContext = new GraphContext(graph); } catch (GraphException e) { fail(e.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); } } } }