From defde7f8e26af7f8e11d4da855308c36df4ff428 Mon Sep 17 00:00:00 2001 From: Philipp Katz Date: Mon, 2 Apr 2018 11:02:04 +0100 Subject: [PATCH 1/4] Extract port type information which is not registered via extension point See #10 --- .../jsondocgen/JsonNodeDocuGenerator.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java index cfcaaee..82d838f 100644 --- a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java +++ b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java @@ -52,6 +52,7 @@ import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -148,6 +149,8 @@ private static void printUsage() { private CategoryDocBuilder rootCategoryDoc; + Map, PortType> portTypesFromNodes; + @Override public Object start(final IApplicationContext context) throws Exception { Object o = context.getArguments().get(IApplicationContext.APPLICATION_ARGS); @@ -218,6 +221,11 @@ private void generate() throws Exception { } m_catPath = m_catPath.replaceAll("/", "."); + // get all port types from the nodes, as not all of them are registered via + // extension point, see here: + // https://github.com/qqilihq/knime-json-node-doc-generator/issues/10 + portTypesFromNodes = new HashMap<>(); + // recursively generate the node reference and the node description // pages generate(m_directory, root, null, rootCategoryDoc); @@ -233,12 +241,17 @@ private void generate() throws Exception { if (!m_skipPortDocumentation) { Map, PortTypeDocBuilder> builders = new HashMap<>(); + + Map, PortType> portTypes = new HashMap<>(); + + // ALL port types as defined in the NodeModels' APIs + portTypes.putAll(portTypesFromNodes); // all registered port types indexed by the PortObject class; read this only // once from the registry and cache it, b/c the registry creates new PortTypes // dynamically when requesting an unknown type - Map, PortType> portTypes = PortTypeRegistry.getInstance().availablePortTypes() - .stream().collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity())); + portTypes.putAll(PortTypeRegistry.getInstance().availablePortTypes().stream() + .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); processPorts(portTypes.keySet(), portTypes, builders); @@ -381,9 +394,13 @@ private boolean generate(final File directory, final IRepositoryObject current, NodeModel nodeModel = factory.createNodeModel(); PortType[] outPorts = getPorts(nodeModel, false); builder.setOutPorts(mergePortInfo(builder.build().outPorts, outPorts, current.getID())); + portTypesFromNodes.putAll(Arrays.stream(outPorts) + .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); PortType[] inPorts = getPorts(nodeModel, true); builder.setInPorts(mergePortInfo(builder.build().inPorts, inPorts, current.getID())); + portTypesFromNodes.putAll(Arrays.stream(inPorts) + .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); if (deprecated) { // there are two locations, where nodes can be set to deprecated: From 6328426ea67492073a5a780edd09f8ba4098aaf9 Mon Sep 17 00:00:00 2001 From: Philipp Katz Date: Mon, 2 Apr 2018 11:27:54 +0100 Subject: [PATCH 2/4] Make field `private` --- .../de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java index 82d838f..75a0acf 100644 --- a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java +++ b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java @@ -149,7 +149,7 @@ private static void printUsage() { private CategoryDocBuilder rootCategoryDoc; - Map, PortType> portTypesFromNodes; + private Map, PortType> portTypesFromNodes; @Override public Object start(final IApplicationContext context) throws Exception { From 80d2ca099edab4457ee688f8d906d7564a78034c Mon Sep 17 00:00:00 2001 From: Philipp Katz Date: Mon, 2 Apr 2018 11:28:32 +0100 Subject: [PATCH 3/4] Avoid duplicate key exceptions when creating Maps --- .../jsondocgen/JsonNodeDocuGenerator.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java index 75a0acf..d369d4d 100644 --- a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java +++ b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java @@ -245,14 +245,17 @@ private void generate() throws Exception { Map, PortType> portTypes = new HashMap<>(); // ALL port types as defined in the NodeModels' APIs + System.out.println("# port types from nodes: " + portTypesFromNodes.size()); portTypes.putAll(portTypesFromNodes); // all registered port types indexed by the PortObject class; read this only // once from the registry and cache it, b/c the registry creates new PortTypes // dynamically when requesting an unknown type + System.out.println("# port types from extension point: " + PortTypeRegistry.getInstance().availablePortTypes().size()); portTypes.putAll(PortTypeRegistry.getInstance().availablePortTypes().stream() .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); + System.out.println("# port types total: " + portTypes.size()); processPorts(portTypes.keySet(), portTypes, builders); // get the root element (all PortObjects inherit from this interface). @@ -394,13 +397,11 @@ private boolean generate(final File directory, final IRepositoryObject current, NodeModel nodeModel = factory.createNodeModel(); PortType[] outPorts = getPorts(nodeModel, false); builder.setOutPorts(mergePortInfo(builder.build().outPorts, outPorts, current.getID())); - portTypesFromNodes.putAll(Arrays.stream(outPorts) - .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); + portTypesFromNodes.putAll(portsToMap(outPorts)); PortType[] inPorts = getPorts(nodeModel, true); builder.setInPorts(mergePortInfo(builder.build().inPorts, inPorts, current.getID())); - portTypesFromNodes.putAll(Arrays.stream(inPorts) - .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity()))); + portTypesFromNodes.putAll(portsToMap(inPorts)); if (deprecated) { // there are two locations, where nodes can be set to deprecated: @@ -449,6 +450,19 @@ private boolean generate(final File directory, final IRepositoryObject current, } + /** + * Create a {@link Map} of {@link PortType}s with {@link PortObject} as key. + * Drop duplicates. + * + * @param ports + * Array of ports. + * @return Indexed ports. + */ + private static Map, PortType> portsToMap(PortType[] ports) { + return Arrays.stream(ports) + .collect(Collectors.toMap(PortType::getPortObjectClass, Function.identity(), (p1, p2) -> p1)); + } + /** * Merge port information which is defined (a) in the node's documentation, (b) * via the {@link NodeModel}'s implementation. From 0f4ec4692d36bcf9d1e0d40b5d56d88bbd5b6bd1 Mon Sep 17 00:00:00 2001 From: Philipp Katz Date: Mon, 2 Apr 2018 19:10:49 +0100 Subject: [PATCH 4/4] Catch and log error when creating NodeModel fails --- .../jsondocgen/JsonNodeDocuGenerator.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java index d369d4d..337917f 100644 --- a/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java +++ b/de.philippkatz.knime.jsondocgen.application/src/de/philippkatz/knime/jsondocgen/JsonNodeDocuGenerator.java @@ -394,14 +394,18 @@ private boolean generate(final File directory, final IRepositoryObject current, // port type information -- extract this information separately and do not merge // with the node description's port information, because the documentation and // the actual implementation might be inconsistent. - NodeModel nodeModel = factory.createNodeModel(); - PortType[] outPorts = getPorts(nodeModel, false); - builder.setOutPorts(mergePortInfo(builder.build().outPorts, outPorts, current.getID())); - portTypesFromNodes.putAll(portsToMap(outPorts)); - - PortType[] inPorts = getPorts(nodeModel, true); - builder.setInPorts(mergePortInfo(builder.build().inPorts, inPorts, current.getID())); - portTypesFromNodes.putAll(portsToMap(inPorts)); + try { + NodeModel nodeModel = factory.createNodeModel(); + PortType[] outPorts = getPorts(nodeModel, false); + builder.setOutPorts(mergePortInfo(builder.build().outPorts, outPorts, current.getID())); + portTypesFromNodes.putAll(portsToMap(outPorts)); + + PortType[] inPorts = getPorts(nodeModel, true); + builder.setInPorts(mergePortInfo(builder.build().inPorts, inPorts, current.getID())); + portTypesFromNodes.putAll(portsToMap(inPorts)); + } catch (Throwable t) { + System.out.println(String.format("[warn] Could not create NodeModel for %s: %s", factory, t)); + } if (deprecated) { // there are two locations, where nodes can be set to deprecated: