In this example, we are going to create our own tagger subrule type that will allow user to define subrule "StringParam equals ?".
Our rule has to know the stringParam that user defined to the subrule, so we will create our data class:
@Value
@EqualsAndHashCode(callSuper=true)
public class ExampleTagSubruleData extends TagSubruleData {
String stringParam;
}
To let user configure subrule data, we have to create configurator component:
public class ExampleTagSubruleConfigurator extends TagSubruleConfigurator {
@FXML
private TextField stringInput;
public ExampleTagSubruleConfigurator() throws IOException {
super("/fxml/ExampleTagSubruleConfigurator.fxml");
}
@Override
public TagSubruleData getConfig() {
return new ExampleTagSubruleData(stringInput.getText());
}
@Override
public void setConfig(TagSubruleData data) {
stringInput.setText(((ExampleTagSubruleData) data).getStringParam());
}
@Override
public boolean isValid() {
return true;
}
}
Simple FXML template:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" prefHeight="41.0" prefWidth="249.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/11.0.1">
<children>
<Label layoutX="7.0" layoutY="13.0" styleClass="input-label" text="String:" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="13.0"/>
<TextField fx:id="stringInput" layoutX="90.0" layoutY="2.0" text="" AnchorPane.leftAnchor="85.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0"/>
</children>
</fx:root>
Now, let's create subrule that will tests if PDU contains configured stringParam:
public class ExampleTagSubrule extends TagSubrule {
public ExampleTagSubrule(TagSubruleFactory factory, TagSubruleData data) {
super(factory, data);
}
@Override
public boolean test(PDU pdu) {
if (!(pdu instanceof ExamplePdu)) {
return false;
}
// Check if string param equals configured value
return (((ExamplePdu) pdu)
.getStringParam()
.equals(((ExampleTagSubruleData) data).getStringParam()));
}
@Override
public String toString() {
return "StringParam equals '" + ((ExampleTagSubruleData) data).getStringParam() + "'";
}
}
In order to add our subrule type to the Tagger extension, we need to create subrule factory:
public class ExampleTagSubruleFactory extends TagSubruleFactory {
@Override
public String getCode() {
return "example-tagger";
}
@Override
public String getName() {
return "Example - String param equals";
}
@Override
public TagSubrule createSubrule(TagSubruleData data) {
return new ExampleTagSubrule(this, data);
}
@Override
public Optional<Type> getConfigType() {
return Optional.of(ExampleTagSubruleData.class);
}
@Override
public Optional<TagSubruleConfigurator> createConfigPane() throws IOException {
return Optional.of(new ExampleTagSubruleConfigurator());
}
}
Great, everything is ready, so we can register our subrule factory inside beforeInit method (implement ExtensionInitListener into PetepExtension).
var maybeTagger = helper.getExtension("tagger");
if (maybeTagger.isEmpty()) {
return;
}
var tagger = (TaggerApi) maybeTagger.get();
if (tagger.registerSubruleFactory(new ExampleTagSubruleFactory())) {
Logger.getGlobal().info("Example tag subrule registered!");
}