Development Guide

Example - First Tagger Subrule

In this example, we are going to create our own tagger subrule type that will allow user to define subrule "StringParam equals ?".

Example Subrule Data

Our rule has to know the stringParam that user defined to the subrule, so we will create our data class:

tagger.ExampleTagSubruleData
public class ExampleTagSubruleData extends TagSubruleData {
  private String stringParam;

  public ExampleTagSubruleData(String stringParam) {
    this.stringParam = stringParam;
  }

  public String getStringParam() {
    return stringParam;
  }

  public void setStringParam(String stringParam) {
    this.stringParam = stringParam;
  }
}

Example Subrule Data Configurator

To let user configure subrule data, we have to create configurator component:

tagger.ExampleTagSubruleConfigurator
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 boolean isValid() {
    return true;
  }

  @Override
  public void setConfig(TagSubruleData data) {
    stringInput.setText(((ExampleTagSubruleData) data).getStringParam());
  }
}

Simple FXML template:

fxml/ExampleTagSubruleConfigurator.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root prefHeight="41.0" prefWidth="249.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/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>

Example Subrule

Now, let's create subrule that will tests if PDU contains configured stringParam:

tagger.ExampleTagSubrule
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;
    }

    return (((ExamplePdu) pdu)
        .getStringParam()
        .equals(((ExampleTagSubruleData) data).getStringParam()));
  }

  @Override
  public String toString() {
    return "StringParam equals '" + ((ExampleTagSubruleData) data).getStringParam() + "'";
  }
}

Example Subrule Factory

In order to add our subrule type to the Tagger extension, we need to create subrule factory:

tagger.ExampleTagSubruleFactory
public class ExampleTagSubruleFactory extends TagSubruleFactory {

  @Override
  public TagSubruleConfigurator createConfigPane() throws IOException {
    return new ExampleTagSubruleConfigurator();
  }

  @Override
  public TagSubrule createSubrule(TagSubruleData data) {
    return new ExampleTagSubrule(this, data);
  }

  @Override
  public String getCode() {
    return "example-string";
  }

  @Override
  public String getName() {
    return "Example - String param equals";
  }

  @Override
  public Type getConfigType() {
    return ExampleTagSubruleData.class;
  }
}

Great, everything is ready, so we can register our subrule factory inside beforeInit method (implement ExtensionInitListener into PetepExtension).

petep.PetepExtension: public void beforeInit(ExtensionHelper helper)
Extension tagger = helper.getExtension("tagger");

if (tagger == null) {
  return;
}

if (((TaggerApi) tagger).registerSubruleFactory(new ExampleTagSubruleFactory())) {
  Logger.getGlobal().info("Example tag subrule registered!");
}
Získejte registraci domén s tld .online, .space, .store, .tech zdarma!
Stačí si k jedné z těchto domén vybrat hosting Plus nebo Mega a registraci domény od nás dostanete za 0 Kč!
Objednat