From 6e55bee09ee10eedd549434ec2e853069d6157f3 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 2 Apr 2023 21:30:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Sw?= =?UTF-8?q?itchUnit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quarkus/core/action/SwitchUnitAction.java | 39 ++++ .../quarkus/domain/unit/SwitchUnit.java | 201 ++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/SwitchUnitAction.java create mode 100644 bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/SwitchUnit.java diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/SwitchUnitAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/SwitchUnitAction.java new file mode 100644 index 0000000..94d193a --- /dev/null +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/SwitchUnitAction.java @@ -0,0 +1,39 @@ +package dev.struchkov.godfather.quarkus.core.action; + +import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.quarkus.core.Responder; +import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; +import dev.struchkov.godfather.quarkus.domain.unit.SwitchUnit; +import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest; +import io.smallrye.mutiny.Uni; + +import java.util.Set; + +import static dev.struchkov.haiti.utils.Checker.checkNotNull; + +public class SwitchUnitAction implements ActionUnit, Message> { + + @Override + public Uni> action(UnitRequest, Message> unitRequest) { + final Message message = unitRequest.getMessage(); + final Set> nextUnits = unitRequest.getUnit().getNextUnits(); + return Responder.nextUnit(message, nextUnits) + .map(nextUnit -> { + if (checkNotNull(nextUnit)) { + return UnitRequest.of(nextUnit, message); + } + return null; + }); + } + + @Override + public String getUnitType() { + return SwitchUnit.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + +} diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/SwitchUnit.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/SwitchUnit.java new file mode 100644 index 0000000..c257222 --- /dev/null +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/SwitchUnit.java @@ -0,0 +1,201 @@ +package dev.struchkov.godfather.quarkus.domain.unit; + +import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.main.domain.unit.KeyWord; +import dev.struchkov.godfather.main.domain.unit.UnitActiveType; +import dev.struchkov.godfather.quarkus.domain.BoxAnswer; +import dev.struchkov.godfather.quarkus.domain.unit.func.CallBackConsumer; +import dev.struchkov.godfather.quarkus.domain.unit.func.ProcessingData; +import dev.struchkov.godfather.quarkus.domain.unit.func.UniPredicate; +import io.smallrye.mutiny.Uni; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +public class SwitchUnit extends MainUnit { + + public static final String TYPE = "SWITCH_UNIT"; + + private SwitchUnit(Builder builder) { + super( + builder.name, + builder.triggerWords, + builder.triggerPhrases, + builder.triggerPatterns, + builder.triggerCheck, + builder.matchThreshold, + builder.priority, + builder.nextUnits, + builder.description, + TYPE, + builder.activeType, + builder.notSaveHistory + ); + } + + public static SwitchUnit.Builder builder() { + return new SwitchUnit.Builder<>(); + } + + public static final class Builder { + + private String name = UUID.randomUUID().toString(); + private String description; + private Set> nextUnits; + + private Set triggerWords; + private Set triggerPhrases; + private UniPredicate triggerCheck; + private Set triggerPatterns; + + private Integer matchThreshold; + private Integer priority; + + private UnitActiveType activeType; + private boolean notSaveHistory; + + private ProcessingData boxAnswer; + private CallBackConsumer callBack; + + private Builder() { + } + + public SwitchUnit.Builder name(String name) { + this.name = name; + return this; + } + + public SwitchUnit.Builder description(String description) { + this.description = description; + return this; + } + + public SwitchUnit.Builder answer(Consumer answer) { + this.boxAnswer = message -> { + answer.accept(message); + return Uni.createFrom().nullItem(); + }; + return this; + } + + public SwitchUnit.Builder answer(Function> answer) { + this.boxAnswer = answer::apply; + return this; + } + + public SwitchUnit.Builder answer(Supplier answer) { + this.boxAnswer = message -> Uni.createFrom().item(answer.get()); + return this; + } + + public SwitchUnit.Builder callBack(CallBackConsumer callBack) { + this.callBack = callBack; + return this; + } + + public SwitchUnit.Builder triggerWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); + return this; + } + + public SwitchUnit.Builder triggerWord(KeyWord val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); + return this; + } + + public SwitchUnit.Builder triggerStringWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + return this; + } + + public SwitchUnit.Builder triggerWord(String val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); + return this; + } + + public SwitchUnit.Builder triggerPhrase(String... val) { + if (triggerPhrases == null) { + triggerPhrases = new HashSet<>(); + } + if (val.length == 1) { + triggerPhrases.add(val[0]); + } else { + triggerPhrases.addAll(Set.of(val)); + } + triggerPhrases.addAll(List.of(val)); + return this; + } + + public SwitchUnit.Builder triggerPattern(Pattern... val) { + if (triggerPatterns == null) { + triggerPatterns = new HashSet<>(); + } + if (val.length == 1) { + triggerPatterns.add(val[0]); + } else { + triggerPatterns.addAll(Set.of(val)); + } + triggerPatterns.addAll(Set.of(val)); + return this; + } + + public SwitchUnit.Builder triggerCheck(UniPredicate trigger) { + triggerCheck = trigger; + return this; + } + + public SwitchUnit.Builder matchThreshold(Integer val) { + matchThreshold = val; + return this; + } + + public SwitchUnit.Builder priority(Integer val) { + priority = val; + return this; + } + + public SwitchUnit.Builder next(MainUnit val) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } + nextUnits.add(val); + return this; + } + + public SwitchUnit.Builder notSaveHistory() { + notSaveHistory = true; + return this; + } + + public SwitchUnit.Builder activeType(UnitActiveType val) { + activeType = val; + return this; + } + + public SwitchUnit build() { +// isNotNull(boxAnswer, UnitConfigException.unitConfigException("BoxAnswer обязательный параметр юнита")); + return new SwitchUnit<>(this); + } + + } + +}