# 创建新的KNIME扩展程序:快速入门指南


# 介绍

本快速入门指南介绍了如何创建新的KNIME扩展,即编写要在KNIME Analytics Platform中使用的新节点实现。您将学习如何设置KNIME SDK,如何创建新的KNIME Extension项目,如何实现简单的操作节点,如何测试该节点以及如何轻松部署该节点以使其对其他用户可用。 。

为此,我们创建了可以用作方向的参考扩展。该KNIME Extension项目可org.knime.examples.numberformatterknime-examplesGitHub存储库的文件夹中找到。它包含所有必需的项目和配置文件,以及一个简单的Number Formatter示例节点的实现,该节点执行输入表的数值的数字格式化。我们将使用该示例实现来指导您完成创建新的KNIME扩展所涉及的所有必要步骤。

# 设置KNIME SDK

为了开始开发KNIME源代码,您需要设置KNIME SDK。KNIME SDK是已配置的Eclipse安装,其中包含KNIME Analytics Platform依赖项。这是必需的,因为Eclipse是KNIME Analytics Platform的基础,即KNIME Analytics Platform是一组置于Eclipse和Eclipse基础结构之上的插件。此外,Eclipse是一个IDE (opens new window),您将使用它来编写新节点实现的实际源代码。

要设置KNIME SDK,我们首先安装“用于RCP和RAP开发人员的Eclipse IDE”(此版本的Eclipse提供用于插件开发的工具),然后添加所有KNIME Analytics Platform依赖项。为此,请按照SDK安装 (opens new window)说明进行操作。除了提供有关如何设置KNIME SDK的说明之外,SDK安装程序还将提供有关Eclipse基础结构,其插件机制以及其他有用主题(如如何探索KNIME源代码)的背景知识。

# 创建一个新的KNIME扩展项目

设置并配置Eclipse之后,创建一个新的KNIME Extension项目。KNIME Extension项目是Eclipse插件项目,包含一个或多个节点的实现以及某些KNIME Analytics Platform特定的配置。创建KNIME扩展项目的最简单方法是使用KNIME节点向导,该向导将自动生成项目结构,插件清单和所有必需的Java类。此外,该向导将负责将生成的文件嵌入KNIME框架。

# KNIME节点向导

  1. 安装KNIME节点向导

    在打开Eclipse安装向导Help → Install New Software…,输入以下更新站点位置:http://update.knime.com/analytics-platform/4.1/在标记为的位置框中Work with:

    将更新站点位置中的版本号替换为KNIME Analytics Platform的最新版本(错误修正版本除外)。最新版本号可以在这里 (opens new window)找到。

    按下Enter键,然后放入KNIME Node Wizard搜索框。KNIME Node Wizard在类别下打勾KNIME Node Development Tools,单击下一步按钮,然后按照说明进行操作。最后,重新启动Eclipse。

    安装节点向导

    图1. KNIME节点向导安装对话框。

  2. 启动KNIME节点向导

    Eclipse重新启动后,从以下位置启动KNIME Node Wizard File → New → Other…,选择Create a new KNIME Node-Extension(可以在类别中找到Other),然后单击Next按钮。

    启动新节点向导

    图2. KNIME节点向导启动对话框。

  3. 创建一个KNIME扩展项目

    Create new KNIME Node-Extension对话框窗口中,输入以下值:

    • 新项目名称: org.knime.examples.numberformatter
    • 节点类名称: NumberFormatter
    • 包裹名字: org.knime.examples.numberformatter
    • 节点供应商: ``
    • 节点类型:Manipulator在下拉菜单中选择。

    用``您希望成为所创建扩展名作者的名称替换。保留所有其他选项,然后单击完成。

    节点向导对话框

    图3. KNIME节点向导对话框。

    经过一些处理后,新项目将显示在Eclipse的Package Explorer视图中,并带有您在向导对话框中为其指定的项目名称。

    确保Include sample code in generated classes已选中该复选框。这将在生成的文件中包括上述Number Formatter节点的代码。

    蚀

    图4.运行KNIME节点向导后的Eclipse视图。

    Package ExplorerEclipse的视图(左侧)中,您现在应该看到三个项目。这两个项目org.apache.xmlbeans以及org.knime.sdk.setup您在SDK安装程序中 (opens new window)导入的,以及org.knime.examples.numberformatter您刚刚使用KNIME节点向导创建的项目。

# 测试示例扩展

此时,新的KNIME Extension所需的所有部分都包含在Eclipse工作区中并可以运行。要测试您的节点,请按照SDK设置 (opens new window)的“启动KNIME分析平台”部分中提供的说明进行操作。从Eclipse启动KNIME Analytics Platform之后,Number Formatter节点将在节点存储库的根级别可用。使用该节点创建一个新的工作流(请参见下图),检查输入和输出表,并在该节点上进行操作。

刀

图5.从Eclipse开始的KNIME Analytics Platform开发版本显示了示例工作流程。Eclipse工作区中包含的Number Formatter节点显示在节点存储库的底部。

The node will perform simple rounding of numbers from the input table. To change the number of decimal places the node should round to, change the digit contained in the format String that can be entered in the node configuration (e.g. %.2f will round to two decimal places,the default value is %.3f). After you are done, close KNIME Analytics Platform.

# Project Structure

Next, let’s review the important parts of the extension project you’ve just created. First, we’ll have a look at the files located in org.knime.examples.numberformatter.

The files contained in this folder correspond to the actual node implementation. There are four Java classes implementing what the node should do, how the dialog and the view looks like, one XML file that contains the node description, and an image which is used as the node icon (in this case a default icon) displayed in the workflow view of KNIME Analytics Platform. Generally, a node implementation comprises of the following classes: NodeFactory, NodeModel, NodeDialog, NodeView. In our case, these classes are prefixed with the name you gave the node in the KNIME Node Wizard, i.e. NumberFormatter.

  • NumberFormatterNodeFactory.java

    The NodeFactory bundles all parts that make up a node. Thus, the factory provides creation methods for the NodeModel, NodeDialog, and NodeView. Furthermore, the factory will be registered via a KNIME extension point (opens new window) such that the node is discoverable by the framework and will be displayed in the node repository view of KNIME Analytics Platform. The registration of this file happens in the plugin.xml (see description of the plugin.xml file below).

  • NumberFormatterNodeModel.java

    The NodeModel contains the actual implementation of what the node is supposed to do. Furthermore, it specifies the number of inputs and outputs of a node. In this case the node model implements the actual number formatting.

  • NumberFormatterNodeDialog.java (optional)

    The NodeDialog provides the dialog window that opens when you configure (double click) a node in KNIME Analytics Platform. It provides the user with a GUI to adjust node specific configuration settings. In the case of the Number Formatter node this is just a simple text box where the user can enter a format String. Another example would be the file path for a file reader node.

  • NumberFormatterNodeView.java (optional)

    The NodeView provides a view of the output of the node. In the case of the Number Formatter node there will be no view as the output is a simple table. Generally, an example for a view could be a tree view of a node creating a decision tree model.

  • NumberFormatterNodeFactory.xml

    该XML文件包含节点描述和节点的一些元数据。根元素必须是标签。此标签的属性还指定节点图标(icon=”…”)的位置和节点的类型(type=”…”)。请注意,这是您先前在“节点向导”对话框中选择的类型。最常见的类型有SourceManipulatorPredictorLearnerSinkViewer,和Loop。在根标记的子代中指定节点的描述。看一些示例的文件内容。将.xml必须位于同一个包中NodeFactory,它必须具有相同的名称(仅仅结束的区别在于,文件)。

  • default.png

    这是工作流编辑器中显示的节点的图标。节点图标的路径在NumberFormatterNodeFactory.xml(标签的icon属性knimeNode)中指定。在这种情况下,图标只是显示问号的占位符。对于您自己的节点,将其替换为代表该节点功能的适当映像。它的分辨率应为16x16像素。

除了.xml定义节点实现的Java类和factory外,还有两个文件指定项目配置:

  • plugin.xmlMETA-INF/MANIFEST.MF

    这些文件包含有关扩展项目的重要配置数据,例如对其他插件的依赖关系以及上述扩展点。您可以双击plugin.xml打开Eclipse概述并查看某些配置选项(例如,我们在KNIME Node Wizard中输入的值显示在General Information左侧的概述页面中)。但是,您现在不必更改任何值。

插件xml限定符

图6.的Eclipse概述plugin.xml and MANIFEST.MF

# 数字格式化程序节点实现

审查完项目结构后,我们将介绍一些实施细节。我们将介绍最重要的部分,因为您先前创建的项目中的示例代码已经在实现的方法的代码中包含了详细的注释(也可以查看存储库org.knime.examples.numberformatter文件夹中的参考实现knime-examples)。

通常,“*数字格式器”*节点将数据表作为输入,并将用户指定的格式字符串应用于Double输入表的每一列。为简单起见,输出表仅包含格式化的数字列作为字符串列。这基本上是将String.format(…)应用于Double值列表的Java函数的功能包装到KNIME Analytics Platform中可用的节点中。

Let’s work through the most important methods that each node has to implement. The functionality of the node is implemented in the NumberFormatterNodeModel.java class:

protected NumberFormatterNodeModel() {
    super(1, 1);
}

The super(1, 1) call in the constructor of the node model specifies the number of output and input tables the node should have. In this case it is one input and one output table.

BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec)

The actual algorithm of the node is implemented in the execute method. The method is invoked only after all preceding nodes have been successfully executed and all data is therefore available at the input ports. The input table will be available in the given array inData which contains as many data tables as specified in the constructor. Hence, the index of the array corresponds to the port index of the node. The type of the input is BufferedDataTable, which is the standard type of all tabular data in KNIME Analytics Platform. The persistence of the table (e.g. when the workflow is saved) is automatically handled by the framework. Furthermore, a BufferedDataTable is able to handle data larger than the size of the main memory as the data will be automatically flushed to disk if necessary. A table contains DataRow objects, which in turn contain DataCell objects. DataCells provide the actual access to the data. There are a lot of DataCell implementation for all types of data, e.g. a DoubleCell containing a floating point number in double precision (for a list of implementations have a look at the type hierarchy of the DataCell class). Additionally, each DataCell implements one or multiple DataValue interfaces. These define which access methods the cell has i.e. which types it can be represented as. For example, a BooleanCell implements IntValue as a Boolean can be easily represented as 0 and 1. Hence, for each DataValue there could be several compatible DataCell classes. The second argument exec of the method is the ExecutionContext which provides means to create/modify BufferedDataTable objects and report the execution status to the user. The most straightforward way to create a new DataTable is via the createDataContainer(final DataTableSpec spec) method of the ExecutionContext. This will create an empty container where you can add rows to. The added rows must comply with the DataTableSpec the data container was created with. E.g. if the container was created with a table specification containing two Double columns, each row that is added to the container must contain two DoubleCells. After you are finished adding rows to the container close it via the close() method and retrieve the BufferedDataTable with getTable(). This way of creating tables is also used in the example code (see NumberFormatterNodeModel.java). Apart from creating a new data container, there are more powerful ways to modify already existing input tables. However, these are not in the scope of this quickstart guide, but you can have a look at the methods of the ExecutionContext. The execute method should return an array of output BufferedDataTable objects with the length of the number of tables as specified in the constructor. These tables contain the output of the node.

DataTableSpec[] configure(final DataTableSpec[] inSpecs)

configure方法有两个责任。首先,它必须检查传入的数据表规范是否适合节点根据用户提供的设置执行。例如,用户可能不允许节点对话框中使用某种列类型,然后我们需要根据此设置检查输入表中是否仍然有适用的列。其次,基于输入来计算节点输出的表规范。例如:假设Number Formatter节点获取一个包含两Double列和一String列的表作为输入。然后此方法应该返回一个DataTableSpec(不要忘记将其包装在一个阵列)包含两个DataColumnSpec类型的String(的Double列将被格式化为String,所有其他列都将被忽略)。与该execute方法类似,该configure方法使用输入DataTableSpec对象数组调用,并输出DataTableSpec包含计算出的表规范的输出对象数组。如果传入表规范不适合该节点执行或不适合用户提供的配置,请向用户抛出InvalidSettingsException带有提示信息的。

saveSettingsTo(final NodeSettingsWO settings)

loadValidatedSettingsFrom(final NodeSettingsRO settings)

这些方法负责控制节点行为的设置的加载和保存,即用户在节点对话框中输入的设置。这用于在节点模型和节点对话框之间进行通信,并在保存工作流程时保留用户设置。两种方法都由一个NodeSettings对象(只读(RO)和只写(WO)版本)调用,该对象存储设置并管理将其写入文件或从文件中读取。该NodeSettings对象是键值存储,因此很容易在设置对象中写入或从中读取。只需NodeSettings在Eclipse编辑器中查看对象提供的方法即可。在我们的示例中,我们没有直接将设置写入NodeSettings对象,而是使用SettingsModel对象存储用户定义的格式String。SettingsModel对象已经知道如何NodeSettings通过(通过accept方法NodeSettings)写入和读取设置,并帮助保持模型和对话框之间的设置同步简单。此外,它们可用于创建简单的对话框,其中已完成设置的保存和保存。

您可以在类的方法中找到Number Formatter节点的实际算法。我们鼓励您通读上述类的代码,以更深入地了解节点的所有部分。有关节点行为的更为详尽的解释,请参阅“ KNIME Noding Guidelines” (opens new window)execute``NumberFormatterNodeModel.java

# 部署您的扩展

本节以数字格式化扩展为例,介绍完成实施后如何手动部署扩展。有两种选择:

# 选项1:本地更新站点(推荐)

第一个选项是创建本地[更新站点](https://help.eclipse.org/2018-12/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fconcepts%2Fupdate_site.htm&resultof="update" "updat" "site" )版本,可以使用标准的KNIME Analytics Platform更新机制进行安装。

若要创建本地更新站点版本,您需要创建一个Feature (opens new window)包含扩展的项目。功能用于将一组插件打包到一个可安装和可更新的单元中。为此,请转到File → New → Other…,打开Plug-in Development类别,选择Feature Project并单击下一步按钮。

启动功能向导

图7. Feature Project Wizard启动对话框。

Feature Properties对话框窗口中输入以下值:

  • 项目编号: org.knime.examples.numberformatter.feature
  • 功能名称: Number Formatter
  • 功能部件版本:保持不变
  • 特色供应商: ``
  • 安装处理程序库:留空

用``您希望成为所创建扩展名作者的名称替换。另外,为新功能项目选择一个位置(例如,在Number Formatter Extension旁边),然后单击Next(下一步)按钮。在下一个对话框中,选择Initialize from the plug-ins list:并选择org.knime.examples.numberformatter插件(您可以使用搜索栏轻松找到该插件)。此处选择的插件是功能部件将捆绑到可安装单元中的插件。当然,您可以稍后再编辑该列表。最后,点击完成按钮。

运行功能向导

图8. Feature Project Wizard对话框。

向导完成后,您将在Eclipse Package Explorer中看到一个新项目,其中包含Project ID您之前提供的项目,Eclipse将自动打开该项目的概述feature.xml(您也可以通过双击feature.xmlFeature Project中的文件来打开此视图。)。功能概述与概述类似plugin.xml,请注意不要混淆它们。您可以通过选择Included Plug-ins概述对话框底部的选项卡来查看/修改所包含插件的列表。

除了在“功能项目向导”中输入的信息之外,还应在“功能”元数据中提供详细的功能说明,许可和版权信息。这可以通过选择Information概述对话框底部的选项卡来完成。在功能安装期间,此信息将显示给用户。

功能概述标记

图9. Eclipse概述feature.xml。创建更新站点项目的链接标记为红色。

接下来,您需要在本地更新站点上发布功能。为此,首先[Update Site Project](https://help.eclipse.org/2018-12/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fproject_wizards%2Fnew_update_site_project.htm&resultof="update" "updat" "site" )通过单击Update Site Project的Eclipse概述对话框右下角的链接创建一个feature.xml(请参见上 (opens new window))。这将启动“更新站点项目向导”。

运行更新向导

图10.“更新站点项目向导”对话框。

在显示的对话框中,输入以下内容:

  • 项目名称: org.knime.examples.numberformatter.update

再次,为新的更新站点项目选择一个位置,然后单击完成按钮。与Feature Project Wizard相似,您将在Eclipse Package Explorer中看到一个新项目,Project name并在向导对话框中输入了该项目,Eclipse将自动打开被site.xml调用的概述Update Site Map。再次类似于功能,更新站点捆绑了一个或多个功能,这些功能可以由Eclipse更新机制安装。

更新网站概述

图11.的Eclipse概述site.xml。更新站点此图像中已经包含称为一类number_formatting,其中org.knime.examples.numberformatter.feature加入。这样,在安装过程中将在此类别下列出Number Formatter Extension。

在的Eclipse概述上site.xml,首先通过单击新建类别按钮创建一个新类别。这将创建一个新的默认类别,该类别显示在左侧的树视图中。在右侧,输入一个IDnumber_formatting和一个NameNumber Formatting。此名称将显示为类别,并在安装功能部件时用于搜索。另外,请提供该类别的简短说明。

其次,从树中选择查看新创建的类别,然后点击添加功能...按钮。在显示的对话框中,搜索org.knime.examples.numberformatter.feature并单击添加按钮。

更新网站功能选择

图12. Feature Selection对话框。

最后,单击全部构建按钮。这将构建添加到更新站点的所有功能,并创建可用于将Number Formatter Extension安装到KNIME Analytics Platform实例的可安装单元。

建立更新站点可能需要一些时间。您可以在Eclipse的右下角查看进度。

构建完成后,您现在可以将KNIME Analytics Platform指向该文件夹(该文件夹现在包含本地更新站点)以安装扩展程序。要做到这一点,在KNIME分析平台打开Install New Software…对话框,单击添加按钮旁边的更新站点位置,对在打开的对话框点击地方......,并选择包含更新站点的文件夹。最后,给本地更新站点命名,然后单击确定。现在,您可以像安装其他任何插件一样安装Number Formatter Extension。

上面的描述显示了如何使用Eclipse更新站点机制手动部署新的扩展。但是,在实际情况下,此过程应自动完成。如果您认为您的新节点或扩展对其他人可能有价值,则KNIME可以成为社区贡献者并提供社区扩展,从而提供基础架构来托管和自动部署您的扩展。这样,您的扩展程序将可以通过社区扩展程序更新站点进行安装。有关社区扩展以及如何成为贡献者的更多信息,请参见我们网站上的“社区” (opens new window)部分或与我们联系

# 选项2:直接插入

第二个选项是在Eclipse中dropin使用Deployable plug-ins and fragments向导创建一个。Adropin只是.jar包含您的Extension的文件,只需将其放入Eclipsedropins文件夹中 (opens new window)即可进行安装。

要创建dropin包含您的扩展程序的文件,请转到File → Export → Plug-in Development → Deployable plug-ins and fragments并单击下一步。打开的对话框将显示您工作空间中可部署插件的列表。选中旁边的复选框org.knime.examples.numberformatter。在对话框的底部,您可以选择导出方法。选择Directory并提供指向要将插件导出到的文件夹的路径。最后单击完成。

出口

图13.部署向导对话框。

导出完成后,所选文件夹将包含一个.jar包含插件的文件。要将其安装到任何Eclipse或KNIME Analytics Platform安装中,请将.jar文件放在dropinsKNIME / Eclipse安装文件夹的文件夹中。请注意,必须重新启动KNIME / Eclipse才能发现新插件。在此示例中,该节点随后显示在KNIME Analytics Platform中节点存储库的顶层。

# 进一步阅读