From 0bbdcaae073b7965cbfe3a082c04bb0f00082ec8 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Tue, 26 Jul 2022 09:27:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B8=D0=BF=20=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D0=B3=D0=B3=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 2 +- .../godfather/context/domain/UnitRequest.java | 2 +- .../context/domain/content/Message.java | 3 +- .../context/domain/unit/AnswerCheck.java | 120 ++++++---- .../context/domain/unit/AnswerSave.java | 139 +++++++----- .../context/domain/unit/AnswerText.java | 142 +++++++----- .../context/domain/unit/AnswerTimer.java | 93 +++++--- .../context/domain/unit/AnswerValidity.java | 212 ------------------ .../context/domain/unit/MainUnit.java | 11 +- .../context/domain/unit/cmd/ReplaceCmd.java | 102 +++++---- .../context/domain/unit/cmd/RollBackCmd.java | 126 +++++++---- .../context/domain/unit/cmd/TeleportCmd.java | 103 +++++---- .../context/service/StorylineService.java | 12 +- .../context/service/save/CheckSave.java | 4 +- .../context/service/save/PreservableData.java | 4 +- .../service/usercode/ProcessingData.java | 4 +- bot-core/pom.xml | 2 +- .../godfather/core/GeneralAutoResponder.java | 56 +++-- .../struchkov/godfather/core/Storyline.java | 23 +- .../godfather/core/StorylineMaker.java | 23 +- .../core/domain/question/Question.java | 32 --- .../core/domain/question/QuestionAnswer.java | 35 --- .../core/domain/question/QuestionResult.java | 32 --- .../core/service/StorylineMailService.java | 16 +- .../core/service/action/ActionUnit.java | 4 +- .../service/action/AnswerCheckAction.java | 11 +- .../core/service/action/AnswerSaveAction.java | 14 +- .../core/service/action/AnswerTextAction.java | 18 +- .../service/action/AnswerTimerAction.java | 14 +- .../service/action/AnswerValidityAction.java | 57 ----- .../service/action/cmd/RollBackCmdAction.java | 17 +- .../service/action/cmd/TeleportCmdAction.java | 22 +- .../godfather/core/utils/QuestionUtils.java | 95 -------- pom.xml | 8 +- 34 files changed, 651 insertions(+), 907 deletions(-) delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/Question.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionAnswer.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionResult.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index 0cc1745..8d7da4f 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.17 + 0.0.18 bot-context diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java index 2773e4a..35ec569 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java @@ -19,7 +19,7 @@ public class UnitRequest { this.message = message; } - public static UnitRequest of(U mainUnit, M message) { + public static , M extends Message> UnitRequest of(U mainUnit, M message) { return new UnitRequest<>(mainUnit, message); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Message.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Message.java index 011efe6..3e9d336 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Message.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Message.java @@ -1,5 +1,6 @@ package dev.struchkov.godfather.context.domain.content; +import dev.struchkov.autoresponder.entity.DeliverableText; import dev.struchkov.godfather.context.domain.BasicEntity; import javax.persistence.Column; @@ -16,7 +17,7 @@ import java.util.Objects; * @author upagge [08/07/2019] */ @MappedSuperclass -public abstract class Message extends BasicEntity { +public abstract class Message extends BasicEntity implements DeliverableText { /** * Тип сообщения. diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java index 7ea377a..6f3ac3a 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java @@ -6,7 +6,6 @@ import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.usercode.CheckData; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; @@ -22,7 +21,7 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; * * @author upagge [08/07/2019] */ -public class AnswerCheck extends MainUnit { +public class AnswerCheck extends MainUnit { /** * Unit для true. @@ -37,15 +36,15 @@ public class AnswerCheck extends MainUnit { /** * Условие проверки. */ - private final CheckData check; + private final CheckData check; - private AnswerCheck(Builder builder) { + private AnswerCheck(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, new HashSet<>(), @@ -59,8 +58,8 @@ public class AnswerCheck extends MainUnit { check = builder.check; } - public static Builder builder() { - return new Builder(); + public static Builder builder() { + return new Builder<>(); } public MainUnit getUnitTrue() { @@ -71,117 +70,144 @@ public class AnswerCheck extends MainUnit { return unitFalse; } - public CheckData getCheck() { + public CheckData getCheck() { return check; } - public static final class Builder { + public static final class Builder { private String name; - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private Pattern pattern; + + private Set triggerWords; + private Set triggerPhrases; + private Predicate triggerCheck; + private Set triggerPatterns; private Integer matchThreshold; + private Integer priority; - private MainUnit unitTrue; - private MainUnit unitFalse; - private CheckData check; private UnitActiveType activeType; + private Accessibility accessibility; private boolean notSaveHistory; + private MainUnit unitTrue; + private MainUnit unitFalse; + private CheckData check; + private Builder() { } - public Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public 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(Set.of(val)); return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - private Builder triggerCheck(Predicate trigger) { + private Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public Builder unitTrue(MainUnit unitTrue) { + public Builder unitTrue(MainUnit unitTrue) { this.unitTrue = unitTrue; return this; } - public Builder unitFalse(MainUnit unitFalse) { + public Builder unitFalse(MainUnit unitFalse) { this.unitFalse = unitFalse; return this; } - public Builder check(CheckData check) { + public Builder check(CheckData check) { this.check = check; return this; } - public Builder accessibility(Accessibility val) { + public Builder accessibility(Accessibility val) { accessibility = val; return this; } - public Builder activeType(UnitActiveType val) { + public Builder activeType(UnitActiveType val) { activeType = val; return this; } - public Builder notSaveHistory() { + public Builder notSaveHistory() { notSaveHistory = true; return this; } - public AnswerCheck build() { + public AnswerCheck build() { isNotNull(check, unitConfigException("Необходимо установить параметр проверки.")); isAnyNotNull(unitConfigException("Необходимо задать хотя бы один unit результата проверки.")); - return new AnswerCheck(this); + return new AnswerCheck<>(this); } } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java index 83b776b..f4a2851 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java @@ -24,7 +24,7 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; * * @author upagge [08/07/2019] */ -public class AnswerSave extends MainUnit { +public class AnswerSave extends MainUnit { /** * Объект отвечающий за сохранение - репозиторий. @@ -44,22 +44,22 @@ public class AnswerSave extends MainUnit { /** * Данные для скрытого сохранения. */ - private final PreservableData preservableData; + private final PreservableData preservableData; /** * Скрытое сохранение. */ private final boolean hidden; - private final CheckSave checkSave; + private final CheckSave checkSave; - private AnswerSave(Builder builder) { + private AnswerSave(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, builder.nextUnits, @@ -77,11 +77,11 @@ public class AnswerSave extends MainUnit { checkSave = builder.checkSave; } - public static Builder builder() { + public static Builder builder() { return new Builder<>(); } - private void maintenanceNextUnit(Collection units) { + private void maintenanceNextUnit(Collection> units) { if (units != null) { units.forEach(mainUnit -> mainUnit.setActiveType(UnitActiveType.AFTER)); } @@ -99,7 +99,7 @@ public class AnswerSave extends MainUnit { return pusher; } - public PreservableData getPreservableData() { + public PreservableData getPreservableData() { return preservableData; } @@ -107,137 +107,162 @@ public class AnswerSave extends MainUnit { return hidden; } - public CheckSave getCheckSave() { + public CheckSave getCheckSave() { return checkSave; } - public static final class Builder { + public static final class Builder { private String name; - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private Pattern pattern; + private Set> nextUnits; + + private Set triggerWords; + private Set triggerPhrases; + private Set triggerPatterns; + private Predicate triggerCheck; + private Integer matchThreshold; private Integer priority; - private Set nextUnits = new HashSet<>(); + + private Accessibility accessibility; + private boolean notSaveHistory; + private AnswerSavePreservable preservable; private String key; private Pusher pusher; - private PreservableData preservableData; + private PreservableData preservableData; private boolean hidden; - private CheckSave checkSave; - private Accessibility accessibility; - private boolean notSaveHistory; + private CheckSave checkSave; private Builder() { } - public Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public Builder triggerPhrase(String... val) { + if (checkNull(triggerPhrases)) { + triggerPhrases = new HashSet<>(); + } + if (val.length == 1) { + triggerPhrases.add(val[0]); + } else { + triggerPhrases.addAll(Set.of(val)); + } + triggerPhrases.addAll(Set.of(val)); return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public Builder triggerPattern(Pattern... val) { + if (checkNull(triggerPatterns)) { + 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public Builder nextUnits(Set val) { - nextUnits = val; - return this; - } - - public Builder nextUnit(MainUnit val) { + public Builder next(MainUnit val) { + if (checkNull(nextUnits)) { + nextUnits = new HashSet<>(); + } nextUnits.add(val); return this; } - public Builder preservable(AnswerSavePreservable val) { + public Builder preservable(AnswerSavePreservable val) { this.preservable = val; return this; } - public Builder key(String val) { + public Builder key(String val) { this.key = val; return this; } - public Builder pusher(Pusher val) { + public Builder pusher(Pusher val) { this.pusher = val; return this; } - public Builder preservableData(PreservableData val) { + public Builder preservableData(PreservableData val) { this.preservableData = val; return this; } - public Builder hidden(boolean val) { + public Builder hidden(boolean val) { this.hidden = val; return this; } - public Builder checkSave(CheckSave val) { + public Builder checkSave(CheckSave val) { this.checkSave = val; return this; } - public Builder accessibility(Accessibility val) { + public Builder accessibility(Accessibility val) { accessibility = val; return this; } - public Builder notSaveHistory() { + public Builder notSaveHistory() { notSaveHistory = true; return this; } - public AnswerSave build() { + public AnswerSave build() { isNotNull(preservable, "Не указан репозиторий для сохранения формы пользователя"); if (checkNull(pusher)) { isNotNull(preservableData, "Не указаны данные для сохранения"); diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java index 2501d27..e9eb7c4 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java @@ -10,10 +10,14 @@ import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.service.usercode.Insert; import dev.struchkov.godfather.context.service.usercode.ProcessingData; -import java.util.Arrays; import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -24,12 +28,12 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; * * @author upagge [08/07/2019] */ -public class AnswerText extends MainUnit { +public class AnswerText extends MainUnit { /** * Объект, который необходимо отправить пользователю. */ - private final ProcessingData boxAnswer; + private final ProcessingData answer; /** * Информация, которую необходимо вставить вместо маркеров в строку ответа. @@ -39,15 +43,15 @@ public class AnswerText extends MainUnit { /** * Объект нестандартной отправки ответа. */ - private final Sending sending; + private final Sending sender; private AnswerText(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, builder.nextUnits, @@ -56,25 +60,25 @@ public class AnswerText extends MainUnit { builder.accessibility, TypeUnit.TEXT ); - boxAnswer = builder.boxAnswer; + answer = builder.boxAnswer; insert = builder.insert; - sending = builder.sending; + sender = builder.sending; } public static AnswerText of(String message) { - return AnswerText.builder().boxAnswer(BoxAnswer.boxAnswer(message)).build(); + return AnswerText.builder().answer(BoxAnswer.boxAnswer(message)).build(); } public static AnswerText of(BoxAnswer boxAnswer) { - return AnswerText.builder().boxAnswer(boxAnswer).build(); + return AnswerText.builder().answer(boxAnswer).build(); } public static Builder builder() { return new Builder<>(); } - public ProcessingData getBoxAnswer() { - return boxAnswer; + public ProcessingData getAnswer() { + return answer; } public Insert getInsert() { @@ -82,25 +86,29 @@ public class AnswerText extends MainUnit { } public Sending getSending() { - return sending; + return sender; } public static final class Builder { - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; private String name; - private ProcessingData boxAnswer; - private Insert insert; - private Sending sending; - private Pattern pattern; + private Set> nextUnits; + + private Set triggerWords; + private Set triggerPhrases; + private Predicate triggerCheck; + private Set triggerPatterns; + private Integer matchThreshold; private Integer priority; - private Set nextUnits = new HashSet<>(); + private UnitActiveType activeType; private Accessibility accessibility; private boolean notSaveHistory; + private ProcessingData boxAnswer; + private Insert insert; + private Sending sending; + private Builder() { } @@ -109,13 +117,26 @@ public class AnswerText extends MainUnit { return this; } - public Builder message(ProcessingData message) { - this.boxAnswer = message; + public Builder processing(Consumer answer) { + this.boxAnswer = message -> { + answer.accept(message); + return Optional.empty(); + }; return this; } - public Builder boxAnswer(BoxAnswer boxAnswer) { - this.boxAnswer = message -> boxAnswer; + public Builder answer(Function answer) { + this.boxAnswer = message -> Optional.of(answer.apply(message)); + return this; + } + + public Builder answer(BoxAnswer answer) { + this.boxAnswer = message -> Optional.of(answer); + return this; + } + + public Builder answer(Supplier answer) { + this.boxAnswer = message -> Optional.of(answer.get()); return this; } @@ -129,46 +150,69 @@ public class AnswerText extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public 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 Builder phrases(String... val) { - phrases.addAll(Arrays.asList(val)); + public 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 Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder pattern(Pattern val) { - pattern = val; - return this; - } - public Builder matchThreshold(Integer val) { matchThreshold = val; return this; @@ -179,12 +223,10 @@ public class AnswerText extends MainUnit { return this; } - public Builder nextUnits(Set val) { - nextUnits = val; - return this; - } - - public Builder nextUnit(MainUnit val) { + public Builder next(MainUnit val) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } nextUnits.add(val); return this; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java index ae9ed31..6016aa4 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java @@ -6,7 +6,6 @@ import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.usercode.CheckData; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; @@ -21,12 +20,12 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; * * @author upagge [08/07/2019] */ -public class AnswerTimer extends MainUnit { +public class AnswerTimer extends MainUnit { /** * Unit обработку которого необходимо отложить. */ - private final MainUnit unitAnswer; + private final MainUnit unitAnswer; /** * Задержка обработки в секундах. @@ -46,10 +45,10 @@ public class AnswerTimer extends MainUnit { private AnswerTimer(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, null, @@ -68,7 +67,7 @@ public class AnswerTimer extends MainUnit { return new Builder<>(); } - public MainUnit getUnitAnswer() { + public MainUnit getUnitAnswer() { return unitAnswer; } @@ -86,20 +85,23 @@ public class AnswerTimer extends MainUnit { public static final class Builder { private String name; - private MainUnit unitAnswer; - private Integer timeDelaySec; - private Integer timeDeathSec; - private CheckData checkLoop; - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private Pattern pattern; + + private Set triggerWords; + private Set triggerPhrases; + private Predicate triggerCheck; + private Set triggerPatterns; private Integer matchThreshold; + private Integer priority; private UnitActiveType activeType = UnitActiveType.AFTER; private Accessibility accessibility; private boolean notSaveHistory; + private MainUnit unitAnswer; + private Integer timeDelaySec; + private Integer timeDeathSec; + private CheckData checkLoop; + private Builder() { } @@ -108,7 +110,7 @@ public class AnswerTimer extends MainUnit { return this; } - public Builder unitAnswer(MainUnit val) { + public Builder unitAnswer(MainUnit val) { unitAnswer = val; return this; } @@ -128,42 +130,65 @@ public class AnswerTimer extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (triggerWords == null) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public 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(Set.of(val)); return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java deleted file mode 100644 index dfd3d27..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java +++ /dev/null @@ -1,212 +0,0 @@ -package dev.struchkov.godfather.context.domain.unit; - -import dev.struchkov.autoresponder.entity.KeyWord; -import dev.struchkov.godfather.context.domain.TypeUnit; -import dev.struchkov.godfather.context.repository.preser.AnswerSaveMapPreservable; -import dev.struchkov.godfather.context.repository.preser.Preservable; -import dev.struchkov.godfather.context.service.Accessibility; -import dev.struchkov.godfather.context.service.ClarificationQuestion; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; -import java.util.function.Predicate; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -/** - * Обработка данных со страницы пользователя. - * - * @author upagge [11/07/2019] - */ -public class AnswerValidity extends MainUnit { - - /** - * Unit обрабатывается, если пользователь подтверждает данные. - */ - private final MainUnit unitYes; - - /** - * Unit обрабатывается, если пользователь отклоняет данные. - */ - private final MainUnit unitNo; - - /** - * Unit обрабатывается, если данные не найдены. - */ - private final MainUnit unitNull; - - private final Preservable tempSave = new AnswerSaveMapPreservable<>(); - - private final ClarificationQuestion clarificationQuestion; - - private AnswerValidity(Builder builder) { - super( - builder.name, - builder.keyWords, - builder.phrases, - builder.triggerCheck, - builder.pattern, - builder.matchThreshold, - builder.priority, - builder.nextUnits, - UnitActiveType.DEFAULT, - builder.notSaveHistory, - builder.accessibility, - TypeUnit.VALIDITY - ); - unitYes = builder.unitYes; - unitNo = builder.unitNo; - unitNull = builder.unitNull; - clarificationQuestion = builder.clarificationQuestion; - } - - public static Builder builder() { - return new Builder(); - } - - public MainUnit getUnitYes() { - return unitYes; - } - - public MainUnit getUnitNo() { - return unitNo; - } - - public MainUnit getUnitNull() { - return unitNull; - } - - public Preservable getTempSave() { - return tempSave; - } - - public ClarificationQuestion getClarificationQuestion() { - return clarificationQuestion; - } - - public static final class Builder { - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private String name; - private MainUnit unitYes; - private MainUnit unitNo; - private MainUnit unitNull; - private ClarificationQuestion clarificationQuestion; - private Pattern pattern; - private Integer matchThreshold; - private Integer priority; - private Set nextUnits = new HashSet<>(); - private Accessibility accessibility; - private boolean notSaveHistory; - - private Builder() { - } - - public Builder name(String name) { - this.name = name; - return this; - } - - public Builder unitYes(MainUnit val) { - unitYes = val; - return this; - } - - public Builder unitNo(MainUnit val) { - unitNo = val; - return this; - } - - public Builder unitNull(MainUnit val) { - unitNull = val; - return this; - } - - public Builder clarificationQuestion(ClarificationQuestion val) { - clarificationQuestion = val; - return this; - } - - public Builder keyWords(Set val) { - keyWords.addAll(val); - return this; - } - - public Builder keyWord(KeyWord val) { - keyWords.add(val); - return this; - } - - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); - return this; - } - - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); - return this; - } - - public Builder phrase(String val) { - phrases.add(val); - return this; - } - - public Builder phrases(Collection val) { - phrases.addAll(val); - return this; - } - - public Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder triggerCheck(Predicate trigger) { - triggerCheck = trigger; - return this; - } - - public Builder matchThreshold(Integer val) { - matchThreshold = val; - return this; - } - - public Builder priority(Integer val) { - priority = val; - return this; - } - - public Builder nextUnits(Set val) { - nextUnits = val; - return this; - } - - public Builder nextUnit(MainUnit val) { - nextUnits.add(val); - return this; - } - - public Builder clearKeyWords() { - keyWords.clear(); - return this; - } - - public Builder accessibility(Accessibility val) { - accessibility = val; - return this; - } - - public Builder notSaveHistory() { - notSaveHistory = true; - return this; - } - - public AnswerValidity build() { - return new AnswerValidity(this); - } - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java index 91e7cbc..9bf3ec2 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.context.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.autoresponder.entity.Unit; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.service.Accessibility; import java.util.Objects; @@ -16,7 +17,7 @@ import java.util.regex.Pattern; * * @author upagge [08/07/2019] */ -public abstract class MainUnit extends Unit { +public abstract class MainUnit extends Unit, M> { /** * Тип Unit-а. @@ -49,17 +50,17 @@ public abstract class MainUnit extends Unit { String name, Set keyWords, Set phrases, - Predicate triggerCheck, - Pattern pattern, + Predicate triggerCheck, + Set patterns, Integer matchThreshold, Integer priority, - Set nextUnits, + Set> nextUnits, UnitActiveType activeType, boolean notSaveHistory, Accessibility accessibility, String type ) { - super(keyWords, phrases, triggerCheck, pattern, matchThreshold, priority, nextUnits); + super(keyWords, phrases, triggerCheck, patterns, matchThreshold, priority, nextUnits); this.name = name; this.activeType = Optional.ofNullable(activeType).orElse(UnitActiveType.DEFAULT); this.accessibility = accessibility; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java index 1259290..8369bf6 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java @@ -2,27 +2,28 @@ package dev.struchkov.godfather.context.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.TypeUnit; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.domain.unit.UnitActiveType; +import dev.struchkov.haiti.utils.Checker; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; -public class ReplaceCmd extends MainUnit { +public class ReplaceCmd extends MainUnit { private final MainUnit thisUnit; - private ReplaceCmd(Builder builder) { + private ReplaceCmd(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, new HashSet<>(), @@ -34,21 +35,23 @@ public class ReplaceCmd extends MainUnit { this.thisUnit = builder.thisUnit; } - public static Builder builder() { - return new Builder(); + public static Builder builder() { + return new Builder<>(); } public MainUnit getThisUnit() { return thisUnit; } - public static final class Builder { - private final Set keyWords = new HashSet<>(); + public static final class Builder { private String name; - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private Pattern pattern; + + private Set triggerPhrases; + private Predicate triggerCheck; + private Set triggerPatterns; + private Set triggerWords; private Integer matchThreshold; + private Integer priority; private UnitActiveType activeType = UnitActiveType.AFTER; @@ -57,74 +60,95 @@ public class ReplaceCmd extends MainUnit { private Builder() { } - public Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (Checker.checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (Checker.checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (Checker.checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (Checker.checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public Builder triggerPhrase(String... val) { + if (Checker.checkNull(triggerPhrases)) { + triggerPhrases = new HashSet<>(); + } + if (val.length == 1) { + triggerPhrases.add(val[0]); + } else { + triggerPhrases.addAll(Set.of(val)); + } return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public Builder triggerPattern(Pattern... val) { + if (Checker.checkNull(triggerPatterns)) { + 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public Builder activeType(UnitActiveType val) { + public Builder activeType(UnitActiveType val) { activeType = val; return this; } - public Builder thisUnit(MainUnit val) { + public Builder thisUnit(MainUnit val) { thisUnit = val; return this; } - - public ReplaceCmd build() { - return new ReplaceCmd(this); + public ReplaceCmd build() { + return new ReplaceCmd<>(this); } } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java index 6d740b6..1886ed9 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java @@ -2,21 +2,23 @@ package dev.struchkov.godfather.context.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.TypeUnit; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.domain.unit.UnitActiveType; import dev.struchkov.godfather.context.exception.UnitConfigException; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static dev.struchkov.haiti.utils.Checker.checkNull; + /** * Юнит, который позволяет откатить пользователя на предыдущие юниты в сценарии с сохранением ранее введенной информации в сообщениях. */ -public class RollBackCmd extends MainUnit { +public class RollBackCmd extends MainUnit { /** * Количество юнитов, на которые можно откатиться назад. @@ -28,13 +30,13 @@ public class RollBackCmd extends MainUnit { */ private final String rollbackUnitName; - private RollBackCmd(Builder builder) { + private RollBackCmd(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, new HashSet<>(), @@ -47,28 +49,28 @@ public class RollBackCmd extends MainUnit { this.rollbackUnitName = builder.rollbackUnitName; } - public static RollBackCmd.Builder builder() { - return new RollBackCmd.Builder(); + public static RollBackCmd.Builder builder() { + return new RollBackCmd.Builder<>(); } - public static RollBackCmd rollBack(int countToBack) { - return RollBackCmd.builder().countBack(countToBack).build(); + public static RollBackCmd rollBack(int countToBack) { + return RollBackCmd.builder().countBack(countToBack).build(); } - public static RollBackCmd singleRollBack() { - return RollBackCmd.builder().countBack(1).build(); + public static RollBackCmd singleRollBack() { + return RollBackCmd.builder().countBack(1).build(); } - public static RollBackCmd doubleRollBack() { - return RollBackCmd.builder().countBack(2).build(); + public static RollBackCmd doubleRollBack() { + return RollBackCmd.builder().countBack(2).build(); } - public static RollBackCmd rollBack(String unitName) { - return RollBackCmd.builder().rollbackUnitName(unitName).build(); + public static RollBackCmd rollBack(String unitName) { + return RollBackCmd.builder().rollbackUnitName(unitName).build(); } - public static RollBackCmd rollBack(String phrase, String unitName) { - return RollBackCmd.builder().phrase(phrase).rollbackUnitName(unitName).build(); + public static RollBackCmd rollBack(String phrase, String unitName) { + return RollBackCmd.builder().triggerPhrase(phrase).rollbackUnitName(unitName).build(); } public int getCountBack() { @@ -79,96 +81,122 @@ public class RollBackCmd extends MainUnit { return rollbackUnitName; } - public static final class Builder { - private final Set keyWords = new HashSet<>(); + public static final class Builder { private String name; - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; - private Pattern pattern; + + private Set triggerPhrases; + private Predicate triggerCheck; + private Set triggerPatterns; + private Set triggerWords; private Integer matchThreshold; + private Integer priority; private UnitActiveType activeType = UnitActiveType.DEFAULT; + private int countBack; private String rollbackUnitName; private Builder() { } - public Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public Builder triggerPhrase(String... val) { + if (checkNull(triggerPhrases)) { + triggerPhrases = new HashSet<>(); + } + if (val.length == 1) { + triggerPhrases.add(val[0]); + } else { + triggerPhrases.addAll(Set.of(val)); + } + triggerPhrases.addAll(Set.of(val)); return this; } - public Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public Builder triggerPattern(Pattern... val) { + if (checkNull(triggerPatterns)) { + 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public Builder activeType(UnitActiveType val) { + public Builder activeType(UnitActiveType val) { activeType = val; return this; } - public Builder countBack(int val) { + public Builder countBack(int val) { countBack = val + 1; return this; } - public Builder rollbackUnitName(String val) { + public Builder rollbackUnitName(String val) { rollbackUnitName = val; return this; } - public RollBackCmd build() { + public RollBackCmd build() { if (rollbackUnitName == null && countBack < 2) { throw new UnitConfigException("Ошибка конфигурирования юнита {0}: Количество юнитов для отката не должно быть меньше 1.", name); } - return new RollBackCmd(this); + return new RollBackCmd<>(this); } } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java index fd0fe3a..3904a05 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java @@ -2,33 +2,35 @@ package dev.struchkov.godfather.context.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.TypeUnit; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.domain.unit.UnitActiveType; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static dev.struchkov.haiti.utils.Checker.checkNull; + /** * Позволяет перенести пользователя в произвольное место в сценарии. */ -public class TeleportCmd extends MainUnit { +public class TeleportCmd extends MainUnit { /** * Название юнита, в которое необходимо осуществить перенос. */ private final String unitNameToTeleport; - private TeleportCmd(Builder builder) { + private TeleportCmd(Builder builder) { super( builder.name, - builder.keyWords, - builder.phrases, + builder.triggerWords, + builder.triggerPhrases, builder.triggerCheck, - builder.pattern, + builder.triggerPatterns, builder.matchThreshold, builder.priority, new HashSet<>(), @@ -40,21 +42,23 @@ public class TeleportCmd extends MainUnit { this.unitNameToTeleport = builder.unitNameToTeleport; } - public static Builder builder() { - return new Builder(); + public static Builder builder() { + return new Builder<>(); } public String getUnitNameToTeleport() { return unitNameToTeleport; } - public static final class Builder { - private final Set keyWords = new HashSet<>(); - private final Set phrases = new HashSet<>(); - private Predicate triggerCheck; + public static final class Builder { private String name; - private Pattern pattern; + + private Set triggerWords; + private Set triggerPhrases; + private Set triggerPatterns; + private Predicate triggerCheck; private Integer matchThreshold; + private Integer priority; private UnitActiveType activeType = UnitActiveType.DEFAULT; private String unitNameToTeleport; @@ -62,73 +66,96 @@ public class TeleportCmd extends MainUnit { private Builder() { } - public Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder keyWords(Set val) { - keyWords.addAll(val); + public Builder triggerWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val); return this; } - public Builder keyWord(KeyWord val) { - keyWords.add(val); + public Builder triggerWord(KeyWord val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(val); return this; } - public Builder stringKeyWords(Set val) { - keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + public Builder triggerStringWords(Set val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public Builder keyWord(String val) { - keyWords.add(KeyWord.of(val)); + public Builder triggerWord(String val) { + if (checkNull(triggerWords)) { + triggerWords = new HashSet<>(); + } + triggerWords.add(KeyWord.of(val)); return this; } - public Builder phrase(String val) { - phrases.add(val); + public Builder triggerPhrase(String... val) { + if (checkNull(triggerPhrases)) { + triggerPhrases = new HashSet<>(); + } + if (val.length == 1) { + triggerPhrases.add(val[0]); + } else { + triggerPhrases.addAll(Set.of(val)); + } + triggerPhrases.addAll(Set.of(val)); return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public Builder triggerPattern(Pattern... val) { + if (checkNull(triggerPatterns)) { + 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 Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } - public Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public Builder activeType(UnitActiveType val) { + public Builder activeType(UnitActiveType val) { activeType = val; return this; } - public Builder unitNameToTeleport(String val) { + public Builder unitNameToTeleport(String val) { unitNameToTeleport = val; return this; } - public TeleportCmd build() { - return new TeleportCmd(this); + public TeleportCmd build() { + return new TeleportCmd<>(this); } } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java index a5f7275..bd001e5 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java @@ -8,28 +8,28 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; import java.util.Set; -public interface StorylineService { +public interface StorylineService { void save(@NotNull StorylineHistory storylineHistory); - Optional getUnitNameByPersonId(@NotNull Long personId); + Optional> getUnitNameByPersonId(@NotNull Long personId); - Set getNextUnitByPersonId(@NotNull Long personId); + Set> getNextUnitByPersonId(@NotNull Long personId); - void save(Long personId, String name, T message); + void save(Long personId, String name, M message); Optional replaceUserToBack(long personId, int countUnitsToBack); Optional replaceUserToBack(long personId, String unitName); - Optional getDefaultUnit(); + Optional> getDefaultUnit(); /** * Ленивая (поздняя) связка юнитов между собой. Осуществляется уже после создания сценария. С помощью данного подхода можно реализовать циклические зависимости юнитов. Либо можно использовать {@link dev.struchkov.godfather.context.domain.unit.cmd.TeleportCmd} */ void lazyLink(String firstName, String secondName); - Optional getUnitByName(String unitName); + Optional> getUnitByName(String unitName); void setDefaultUnit(String unitName); diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java index 57a4b60..a16560c 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java @@ -9,8 +9,8 @@ import dev.struchkov.godfather.context.domain.unit.MainUnit; * @author upagge [04/08/2019] */ @FunctionalInterface -public interface CheckSave { +public interface CheckSave { - MainUnit check(D content); + MainUnit check(M content); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java index 6c1ed99..ec210d7 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java @@ -3,8 +3,8 @@ package dev.struchkov.godfather.context.service.save; import dev.struchkov.godfather.context.domain.content.Message; @FunctionalInterface -public interface PreservableData { +public interface PreservableData { - E getData(C content); + D getData(M content); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java index ca46acf..622b2c0 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java @@ -2,9 +2,11 @@ package dev.struchkov.godfather.context.service.usercode; import dev.struchkov.godfather.context.domain.BoxAnswer; +import java.util.Optional; + @FunctionalInterface public interface ProcessingData { - BoxAnswer processing(C content); + Optional processing(C content); } diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 60ff90a..ff80c97 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.17 + 0.0.18 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java index 09ccf9d..92de264 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java @@ -18,7 +18,6 @@ import dev.struchkov.godfather.core.service.action.AnswerCheckAction; import dev.struchkov.godfather.core.service.action.AnswerSaveAction; import dev.struchkov.godfather.core.service.action.AnswerTextAction; import dev.struchkov.godfather.core.service.action.AnswerTimerAction; -import dev.struchkov.godfather.core.service.action.AnswerValidityAction; import dev.struchkov.godfather.core.service.action.cmd.ReplaceCmdAction; import dev.struchkov.godfather.core.service.timer.TimerService; import dev.struchkov.haiti.context.exception.NotFoundException; @@ -30,18 +29,18 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -public class GeneralAutoResponder { +public class GeneralAutoResponder { private final PersonSettingService personSettingService; - private final StorylineService storyLineService; - protected Map> actionUnitMap = new HashMap<>(); - protected List> modifiable; + private final StorylineService storyLineService; + protected Map actionUnitMap = new HashMap<>(); + protected List> modifiable; private ErrorHandler errorHandler; protected GeneralAutoResponder( Sending sending, PersonSettingService personSettingService, - StorylineService storyLineService + StorylineService storyLineService ) { this.personSettingService = personSettingService; this.storyLineService = storyLineService; @@ -49,17 +48,16 @@ public class GeneralAutoResponder { } private void init(Sending sending) { - actionUnitMap.put(TypeUnit.CHECK, new AnswerCheckAction()); + actionUnitMap.put(TypeUnit.CHECK, new AnswerCheckAction<>()); actionUnitMap.put(TypeUnit.TEXT, new AnswerTextAction(sending)); - actionUnitMap.put(TypeUnit.VALIDITY, new AnswerValidityAction()); actionUnitMap.put(TypeUnit.REPLACE_CMD, new ReplaceCmdAction()); } - public void initModifiable(List> modifiable) { + public void initModifiable(List> modifiable) { this.modifiable = modifiable; } - public void initActionUnit(String typeUnit, ActionUnit actionUnit) { + public void initActionUnit(String typeUnit, ActionUnit, M> actionUnit) { if (!actionUnitMap.containsKey(typeUnit)) { actionUnitMap.put(typeUnit, actionUnit); } else { @@ -67,7 +65,7 @@ public class GeneralAutoResponder { } } - public void initSaveAction(AnswerSaveAction answerSaveAction) { + public void initSaveAction(AnswerSaveAction answerSaveAction) { actionUnitMap.put(TypeUnit.SAVE, answerSaveAction); } @@ -86,7 +84,7 @@ public class GeneralAutoResponder { storyLineService.setDefaultUnit(unitName); } - public void processingNewMessage(T newMessage) { + public void processingNewMessage(M newMessage) { if (newMessage != null) { final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true); if (state) { @@ -95,39 +93,39 @@ public class GeneralAutoResponder { } } - public void processingNewMessages(List newMessages) { + public void processingNewMessages(List newMessages) { if (newMessages != null && !newMessages.isEmpty()) { final Set personIds = newMessages.stream() .map(Message::getPersonId) .collect(Collectors.toSet()); final Set disableIds = personSettingService.getAllPersonIdDisableMessages(personIds); - final List allowedMessages = newMessages.stream() + final List allowedMessages = newMessages.stream() .filter(message -> !disableIds.contains(message.getPersonId())) .toList(); allowedMessages.parallelStream().forEach(this::processing); } } - private void processing(T message) { + private void processing(M message) { if (modifiable != null) { modifiable.forEach(m -> m.change(message)); } - final Set units = storyLineService.getNextUnitByPersonId(message.getPersonId()); - final Optional optAnswer = Responder.nextUnit(message.getText(), units) + final Set> units = storyLineService.getNextUnitByPersonId(message.getPersonId()); + final Optional> optAnswer = Responder.nextUnit(message, units) .or(storyLineService::getDefaultUnit); if (optAnswer.isPresent()) { - final MainUnit answer = optAnswer.get(); + final MainUnit answer = optAnswer.get(); if (checkPermission(answer.getAccessibility(), message)) { answer(UnitRequest.of(answer, message)); } } } - private boolean checkPermission(Optional accessibility, T message) { + private boolean checkPermission(Optional accessibility, M message) { return accessibility.isEmpty() || accessibility.get().check(message); } - public void answer(UnitRequest unitRequest) { + public void answer(UnitRequest, M> unitRequest) { try { unitRequest = getAction(unitRequest); activeUnitAfter(unitRequest); @@ -140,10 +138,10 @@ public class GeneralAutoResponder { } } - private UnitRequest activeUnitAfter(UnitRequest unitRequest) { - final Set nextUnits = unitRequest.getUnit().getNextUnits(); + private UnitRequest, M> activeUnitAfter(UnitRequest, M> unitRequest) { + final Set> nextUnits = unitRequest.getUnit().getNextUnits(); if (nextUnits != null) { - Optional first = nextUnits.stream() + Optional> first = nextUnits.stream() .filter(unit -> UnitActiveType.AFTER.equals(unit.getActiveType())) .findFirst(); if (first.isPresent()) { @@ -154,18 +152,18 @@ public class GeneralAutoResponder { return unitRequest; } - private UnitRequest getAction(UnitRequest unitRequest) { - final MainUnit unit = unitRequest.getUnit(); - final T message = unitRequest.getMessage(); + private UnitRequest, M> getAction(UnitRequest, M> unitRequest) { + final MainUnit unit = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); final String typeUnit = unit.getType(); if (actionUnitMap.containsKey(typeUnit)) { ActionUnit actionUnit = actionUnitMap.get(typeUnit); - UnitRequest newUnitRequest = actionUnit.action(unitRequest); - final Optional optDefaultUnit = storyLineService.getDefaultUnit(); + UnitRequest, M> newUnitRequest = actionUnit.action(unitRequest); + final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { storyLineService.save(message.getPersonId(), unit.getName(), message); } - final MainUnit newUnit = newUnitRequest.getUnit(); + final MainUnit newUnit = newUnitRequest.getUnit(); return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest; } else { throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java b/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java index 8b32ebf..8a75c3d 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java @@ -1,5 +1,6 @@ package dev.struchkov.godfather.core; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.haiti.utils.Inspector; import org.jetbrains.annotations.NotNull; @@ -10,22 +11,22 @@ import java.util.Map; import java.util.Optional; import java.util.Set; -public class Storyline { +public class Storyline { - private final Set startingUnits = new HashSet<>(); - private final Set globalUnits = new HashSet<>(); - private final Map units = new HashMap<>(); + private final Set> startingUnits = new HashSet<>(); + private final Set> globalUnits = new HashSet<>(); + private final Map> units = new HashMap<>(); - public Storyline(Set startingUnits, Map units) { + public Storyline(Set> startingUnits, Map> units) { this.startingUnits.addAll(startingUnits); this.units.putAll(units); } - public void addGlobalUnits(Set globalUnits) { + public void addGlobalUnits(Set> globalUnits) { this.globalUnits.addAll(globalUnits); } - public Set getGlobalUnits() { + public Set> getGlobalUnits() { return globalUnits; } @@ -34,20 +35,20 @@ public class Storyline { * * @param unitName Название юнита. */ - public Optional getUnit(String unitName) { + public Optional> getUnit(String unitName) { Inspector.isNotNull(unitName); return Optional.ofNullable(units.get(unitName)); } - public Set getStartingUnits() { + public Set> getStartingUnits() { return startingUnits; } //TODO [22.06.2022]: Временное решение ленивой связки юнитов, пока не будет реализован нормальный механизм. public void link(@NotNull String firstName, @NotNull String secondName) { Inspector.isNotNull(firstName, secondName); - final MainUnit firstUnit = units.get(firstName); - final MainUnit secondUnit = units.get(secondName); + final MainUnit firstUnit = units.get(firstName); + final MainUnit secondUnit = units.get(secondName); Inspector.isNotNull(firstUnit, secondUnit); firstUnit.getNextUnits().add(secondUnit); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java b/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java index 4c3fbfb..fcd704e 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.core; import dev.struchkov.godfather.context.domain.UnitDefinition; import dev.struchkov.godfather.context.domain.annotation.Unit; +import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.exception.UnitConfigException; import dev.struchkov.haiti.utils.Inspector; @@ -22,14 +23,14 @@ import java.util.stream.Collectors; import static dev.struchkov.godfather.context.exception.UnitConfigException.unitConfigException; -public class StorylineMaker { +public class StorylineMaker { private static final Logger log = LoggerFactory.getLogger(StorylineMaker.class); private final List configurations = new ArrayList<>(); private final Map unitDefinitions = new HashMap<>(); - private final Map unitMap = new HashMap<>(); + private final Map> unitMap = new HashMap<>(); private final Set mainUnits = new HashSet<>(); private final Set globalUnits = new HashSet<>(); @@ -42,17 +43,17 @@ public class StorylineMaker { return unitDefinitions; } - public Map getUnitMap() { + public Map> getUnitMap() { return unitMap; } - public Storyline createStoryLine() { + public Storyline createStoryLine() { generateUnitDefinitions(); try { createUnitMap(); - final Set mainUnit = getMainUnit(); - final Set globalUnit = getGlobalUnit(); - final Storyline storyline = new Storyline(mainUnit, unitMap); + final Set> mainUnit = getMainUnit(); + final Set> globalUnit = getGlobalUnit(); + final Storyline storyline = new Storyline<>(mainUnit, unitMap); storyline.addGlobalUnits(globalUnit); return storyline; } catch (IllegalAccessException | InvocationTargetException e) { @@ -61,14 +62,14 @@ public class StorylineMaker { throw new UnitConfigException("Ошибка построения StoryLine"); } - private Set getMainUnit() { + private Set> getMainUnit() { Inspector.isNotEmpty(mainUnits, unitConfigException("Не задан ни один mainUnit. Установите хотя бы для одного Unit флаг mainUnit")); return mainUnits.stream() .map(unitMap::get) .collect(Collectors.toSet()); } - private Set getGlobalUnit() { + private Set> getGlobalUnit() { return globalUnits.stream() .map(unitMap::get) .collect(Collectors.toSet()); @@ -85,7 +86,7 @@ public class StorylineMaker { } } - private MainUnit createUnit(UnitDefinition unitDefinition) throws IllegalAccessException, InvocationTargetException { + private MainUnit createUnit(UnitDefinition unitDefinition) throws IllegalAccessException, InvocationTargetException { final Object objectConfig = unitDefinition.getObjectConfig(); final String currentUnitName = unitDefinition.getName(); final Method method = unitDefinition.getMethod(); @@ -95,7 +96,7 @@ public class StorylineMaker { .map(Unit::value) .map(unitMap::get) .toArray(); - MainUnit newUnit = (MainUnit) method.invoke(objectConfig, nextUnits); + MainUnit newUnit = (MainUnit) method.invoke(objectConfig, nextUnits); newUnit.setName(currentUnitName); unitMap.put(currentUnitName, newUnit); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/Question.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/Question.java deleted file mode 100644 index 66a83a2..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/Question.java +++ /dev/null @@ -1,32 +0,0 @@ -//package dev.struchkov.godfather.core.domain.question; -// -//import dev.struchkov.godfather.context.domain.BoxAnswer; -//import lombok.Builder; -//import lombok.Getter; -//import lombok.Setter; -//import lombok.Singular; -// -//import java.util.List; -// -///** -// * Используется для конфигурации генерации цепочки Unit-ов в виде тестов для прохождения пользователем. -// * -// * @author upagge [14/07/2019] -// */ -//@Getter -//@Setter -//@Builder -//public class Question { -// -// /** -// * Вопрос. -// */ -// private BoxAnswer boxAnswer; -// -// /** -// * Список предполагаемых ответов. -// */ -// @Singular -// private List questionAnswers; -// -//} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionAnswer.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionAnswer.java deleted file mode 100644 index 1f07e80..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionAnswer.java +++ /dev/null @@ -1,35 +0,0 @@ -//package dev.struchkov.godfather.core.domain.question; -// -//import lombok.Getter; -//import lombok.Setter; -// -///** -// * Используется для конфигурации генерации цепочки Unit-ов в виде тестов для прохождения пользователем. -// * Отвечает за варианты ответов. -// * -// * @author upagge [14/07/2019] -// */ -//@Getter -//@Setter -//public class QuestionAnswer { -// -// /** -// * Текстовый ответ. -// */ -// private String text; -// -// /** -// * Количество балов за ответ. -// */ -// private int points; -// -// public QuestionAnswer(String text, Integer points) { -// this.text = text; -// this.points = points; -// } -// -// public QuestionAnswer(String text) { -// this.text = text; -// } -// -//} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionResult.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionResult.java deleted file mode 100644 index 683ccb2..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/question/QuestionResult.java +++ /dev/null @@ -1,32 +0,0 @@ -//package dev.struchkov.godfather.core.domain.question; -// -//import lombok.AllArgsConstructor; -//import lombok.Getter; -//import lombok.Setter; -// -///** -// * Используется для сохранения результатов ответов на вопросы. -// * -// * @author upagge [14/07/2019] -// */ -//@Getter -//@Setter -//@AllArgsConstructor -//public class QuestionResult { -// -// /** -// * Вопрос. -// */ -// private String question; -// -// /** -// * Ответ. -// */ -// private String answer; -// -// /** -// * Количество баллов за ответ. -// */ -// private Integer points; -// -//} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java index 9bdd3bd..b3540a7 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java @@ -26,7 +26,7 @@ public class StorylineMailService implements StorylineService { private final UnitPointerService unitPointerService; private final StorylineRepository storylineRepository; - private final Storyline storyLine; + private final Storyline storyLine; private String defaultUnitName; public StorylineMailService( @@ -34,7 +34,7 @@ public class StorylineMailService implements StorylineService { StorylineRepository storylineRepository, List unitConfigurations ) { - this.storyLine = new StorylineMaker(unitConfigurations).createStoryLine(); + this.storyLine = new StorylineMaker(unitConfigurations).createStoryLine(); this.unitPointerService = unitPointerService; this.storylineRepository = storylineRepository; } @@ -47,21 +47,21 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional getUnitNameByPersonId(@NotNull Long personId) { + public Optional> getUnitNameByPersonId(@NotNull Long personId) { Inspector.isNotNull(personId); return unitPointerService.getUnitNameByPersonId(personId) .flatMap(storyLine::getUnit); } @Override - public Set getNextUnitByPersonId(@NotNull Long personId) { - final Optional> optMainUnits = getUnitNameByPersonId(personId) + public Set> getNextUnitByPersonId(@NotNull Long personId) { + final Optional>> optMainUnits = getUnitNameByPersonId(personId) .map(Unit::getNextUnits) .filter(mainUnits -> !mainUnits.isEmpty()); if (optMainUnits.isEmpty()) { storylineRepository.cleanHistoryByPersonId(personId); } - final Set nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); + final Set> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); nextUnits.addAll(storyLine.getGlobalUnits()); return nextUnits; } @@ -89,7 +89,7 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional getDefaultUnit() { + public Optional> getDefaultUnit() { if (defaultUnitName == null) return Optional.empty(); return storyLine.getUnit(defaultUnitName); } @@ -106,7 +106,7 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional getUnitByName(String unitName) { + public Optional> getUnitByName(String unitName) { return storyLine.getUnit(unitName); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java index da726de..6281ab4 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java @@ -10,13 +10,13 @@ import dev.struchkov.godfather.context.domain.unit.MainUnit; * @author upagge [11/07/2019] */ @FunctionalInterface -public interface ActionUnit { +public interface ActionUnit { /** * Метод обработки Unit-а. * * @return Новый Unit, который может нуждаться в обработке */ - UnitRequest action(UnitRequest unitRequest); + UnitRequest action(UnitRequest unitRequest); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java index 8e65ae1..4f2e43d 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java @@ -8,23 +8,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Objects; -import java.util.Optional; /** * Обработчик Unit-а {@link AnswerCheck}. * * @author upagge [11/07/2019] */ -public class AnswerCheckAction implements ActionUnit { +public class AnswerCheckAction implements ActionUnit, M> { private static final Logger log = LoggerFactory.getLogger(AnswerCheckAction.class); @Override - public UnitRequest action(UnitRequest unitRequest) { - final AnswerCheck unit = unitRequest.getUnit(); - final Message message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, M> unitRequest) { + final AnswerCheck unit = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); - MainUnit unitAnswer; + MainUnit unitAnswer; if (unit.getCheck().checked(message)) { log.info("Проверка пройдена"); unitAnswer = unit.getUnitTrue(); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java index 21d5aed..19efb76 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java @@ -16,25 +16,25 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerSaveAction implements ActionUnit, Message> { +public class AnswerSaveAction implements ActionUnit, M> { @Override - public UnitRequest action(UnitRequest, Message> unitRequest) { - final AnswerSave answerSave = unitRequest.getUnit(); - final Message message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, M> unitRequest) { + final AnswerSave answerSave = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); final AnswerSavePreservable preservable = answerSave.getPreservable(); final Long personId = message.getPersonId(); - final CheckSave checkSave = answerSave.getCheckSave(); + final CheckSave checkSave = answerSave.getCheckSave(); if (checkNotNull(checkSave)) { - MainUnit unit = checkSave.check(message); + MainUnit unit = checkSave.check(message); if (checkNotNull(unit)) { return UnitRequest.of(unit, message); } } - final PreservableData preservableData = answerSave.getPreservableData(); + final PreservableData preservableData = answerSave.getPreservableData(); if (checkNotNull(preservableData)) { D data = preservableData.getData(message); if (checkNotNull(data)) { diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java index 30eac31..11aa50e 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java @@ -10,6 +10,7 @@ import dev.struchkov.godfather.context.utils.InsertWords; import dev.struchkov.godfather.context.utils.Sender; import java.util.List; +import java.util.Optional; /** * Обработчик Unit-а {@link AnswerText}. @@ -29,14 +30,17 @@ public class AnswerTextAction implements ActionUnit, Message final AnswerText unit = unitRequest.getUnit(); final Message message = unitRequest.getMessage(); - final BoxAnswer boxAnswer = unit.getBoxAnswer().processing(message); - replaceMarkers(unit, message, boxAnswer); + final Optional optAnswer = unit.getAnswer().processing(message); + if (optAnswer.isPresent()) { + final BoxAnswer answer = optAnswer.get(); + replaceMarkers(unit, message, answer); - final Sending answerTextSending = unit.getSending(); - if (answerTextSending != null) { - Sender.sends(message, boxAnswer, answerTextSending); - } else { - Sender.sends(message, boxAnswer, this.sending); + final Sending answerTextSending = unit.getSending(); + if (answerTextSending != null) { + Sender.sends(message, answer, answerTextSending); + } else { + Sender.sends(message, answer, this.sending); + } } return UnitRequest.of(unit, message); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java index a80967a..2f4659d 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java @@ -2,11 +2,11 @@ package dev.struchkov.godfather.core.service.action; import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.AnswerTimer; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.exception.TimerSettingException; import dev.struchkov.godfather.core.GeneralAutoResponder; import dev.struchkov.godfather.core.domain.Timer; -import dev.struchkov.godfather.context.domain.unit.AnswerTimer; -import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.core.service.timer.TimerActionTask; import dev.struchkov.godfather.core.service.timer.TimerService; @@ -20,9 +20,9 @@ import java.util.Optional; * * @author upagge [11/07/2019] */ -public class AnswerTimerAction implements ActionUnit { +public class AnswerTimerAction implements ActionUnit, M> { - private TimerService timerService; + private final TimerService timerService; private Long verificationPeriodSec = 15L; public AnswerTimerAction(TimerService timerService, GeneralAutoResponder generalAutoresponder) { @@ -41,9 +41,9 @@ public class AnswerTimerAction implements ActionUnit { } @Override - public UnitRequest action(UnitRequest unitRequest) { - final AnswerTimer unit = unitRequest.getUnit(); - final Message message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, M> unitRequest) { + final AnswerTimer unit = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); final LocalDateTime timeActive = LocalDateTime .now(Clock.tickSeconds(ZoneId.systemDefault())) diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java deleted file mode 100644 index 3f4f5c4..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.struchkov.godfather.core.service.action; - -import dev.struchkov.godfather.context.domain.Clarification; -import dev.struchkov.godfather.context.domain.UnitRequest; -import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.context.domain.unit.AnswerText; -import dev.struchkov.godfather.context.domain.unit.AnswerValidity; -import dev.struchkov.godfather.context.domain.unit.MainUnit; - -import java.util.Set; - -/** - * Обработчик Unit-а {@link AnswerValidity}. - * - * @author upagge [11/07/2019] - */ -public class AnswerValidityAction implements ActionUnit { - - public static final Set WORDS_YES = Set.of("да", "ага"); - public static final Set WORDS_NO = Set.of("нет", "неа"); - public static final Set WORDS_YES_NO = Set.of("да", "ага", "нет", "неа"); - - @Override - public UnitRequest action(UnitRequest unitRequest) { - final AnswerValidity unit = unitRequest.getUnit(); - final Message content = unitRequest.getMessage(); - - String message = content.getText(); - Long personId = content.getPersonId(); - if (WORDS_YES.contains(message.toLowerCase())) { - unit.getTempSave().getByKey(personId, "temp").ifPresent(content::setText); - return UnitRequest.of(unit.getUnitYes(), content); - } else if (WORDS_NO.contains(message.toLowerCase())) { - unit.getTempSave().getByKey(personId, "temp").ifPresent(content::setText); - return UnitRequest.of(unit.getUnitNo(), content); - } else { - Clarification clarification = unit.getClarificationQuestion().getClarification(content); - final String value = clarification.getValue(); - if (value == null) { - return UnitRequest.of(unit.getUnitNull(), content); - } else { - unit.getTempSave().save(personId, "temp", value); - AnswerValidity newValidity = unit.builder() - .clearKeyWords() - .stringKeyWords(WORDS_YES_NO) - .build(); - return UnitRequest.of( - AnswerText.builder() - .message(mes -> clarification.getQuestion()) - .nextUnit(newValidity) - .build(), - content - ); - } - } - } -} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java index 5d0b8c8..0c79f0d 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java @@ -10,18 +10,18 @@ import dev.struchkov.godfather.core.service.action.ActionUnit; import static dev.struchkov.godfather.context.exception.RollBackException.rollBackException; -public class RollBackCmdAction implements ActionUnit { +public class RollBackCmdAction implements ActionUnit, M> { - private final StorylineService storyLineService; + private final StorylineService storyLineService; - public RollBackCmdAction(StorylineService storyLineService) { + public RollBackCmdAction(StorylineService storyLineService) { this.storyLineService = storyLineService; } @Override - public UnitRequest action(UnitRequest unitRequest) { - final RollBackCmd unit = unitRequest.getUnit(); - final T message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, M> unitRequest) { + final RollBackCmd unit = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); final int countToBack = unit.getCountBack(); final String rollbackUnitName = unit.getRollbackUnitName(); @@ -30,8 +30,9 @@ public class RollBackCmdAction implements ActionUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); + final M oldMessage = (M) history.getMessage(); return UnitRequest.of(nextUnit, oldMessage); } + } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java index 16b1745..a2b613b 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java @@ -9,22 +9,24 @@ import dev.struchkov.godfather.core.service.action.ActionUnit; import java.util.Optional; -public class TeleportCmdAction implements ActionUnit { +public class TeleportCmdAction implements ActionUnit, M> { - private final StorylineService storyLineService; + private final StorylineService storyLineService; - public TeleportCmdAction(StorylineService storyLineService) { + public TeleportCmdAction(StorylineService storyLineService) { this.storyLineService = storyLineService; } @Override - public UnitRequest action(UnitRequest unitRequest) { - final TeleportCmd unit = unitRequest.getUnit(); - final T message = unitRequest.getMessage(); - final Optional optNextUnit = storyLineService.getUnitByName(unit.getUnitNameToTeleport()); - return optNextUnit - .map(mainUnit -> UnitRequest.of(mainUnit, message)) - .orElseGet(() -> UnitRequest.of(unit, message)); + public UnitRequest action(UnitRequest, M> unitRequest) { + final TeleportCmd unit = unitRequest.getUnit(); + final M message = unitRequest.getMessage(); + final Optional> optNextUnit = storyLineService.getUnitByName(unit.getUnitNameToTeleport()); + if (optNextUnit.isPresent()) { + return UnitRequest.of(optNextUnit.get(), message); + } else { + return UnitRequest.of(unit, message); + } } } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java b/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java deleted file mode 100644 index a1e2558..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java +++ /dev/null @@ -1,95 +0,0 @@ -//package dev.struchkov.godfather.core.utils; -// -//import dev.struchkov.godfather.context.domain.BoxAnswer; -//import dev.struchkov.godfather.context.utils.KeyBoards; -//import dev.struchkov.godfather.core.domain.question.Question; -//import dev.struchkov.godfather.core.domain.question.QuestionAnswer; -//import dev.struchkov.godfather.core.domain.question.QuestionResult; -//import dev.struchkov.godfather.core.domain.unit.AnswerSave; -//import dev.struchkov.godfather.core.domain.unit.AnswerText; -//import dev.struchkov.godfather.core.domain.unit.MainUnit; -//import dev.struchkov.godfather.core.domain.unit.UnitActiveType; -//import dev.struchkov.godfather.context.repository.preser.Preservable; -//import dev.struchkov.godfather.context.service.save.Pusher; -// -//import java.util.List; -//import java.util.Optional; -//import java.util.stream.Collectors; -// -///** -// * Утилита для быстрой генерации цепочки Unit-ов, образующих сценарий "Тестирование". -// * -// * @author upagge [14/07/2019] -// */ -//public class QuestionUtils { -// -// private final Preservable preservable; -// private final List questions; -// private Pusher pusher; -// -// private QuestionUtils(List questions, Preservable preservable) { -// this.questions = questions; -// this.preservable = preservable; -// } -// -// private QuestionUtils(List questions, Preservable preservable, Pusher pusher) { -// this.questions = questions; -// this.preservable = preservable; -// this.pusher = pusher; -// } -// -// public static QuestionUtils builder(Preservable preservable, List questions) { -// return new QuestionUtils(questions, preservable); -// } -// -// public static QuestionUtils builder(Preservable preservable, Pusher pusher, List list) { -// return new QuestionUtils(list, preservable, pusher); -// } -// -// public MainUnit build(MainUnit finishUnit) { -// return generateTest(finishUnit); -// } -// -// public MainUnit build() { -// return generateTest(null); -// } -// -// private MainUnit generateTest(MainUnit finishUnit) { -// AnswerText previousUnit = null; -// for (int i = questions.size() - 1; i >= 0; i--) { -// Question question = this.questions.get(i); -// List collectAnswer = question.getQuestionAnswers().stream() -// .map(QuestionAnswer::getText) -// .collect(Collectors.toList()); -// BoxAnswer boxAnswer = question.getBoxAnswer().toBuilder() -// .keyBoard(KeyBoards.verticalDuoMenuString(collectAnswer)).build(); -// -// AnswerText.Builder answerTextBuilder = AnswerText.builder() -// .boxAnswer(message -> boxAnswer); -// -// for (QuestionAnswer questionAnswer : question.getQuestionAnswers()) { -// AnswerSave.AnswerSaveBuilder answerSaveBuilder = AnswerSave.builder() -// .preservable(preservable) -// .preservableData( -// message -> new QuestionResult( -// question.getBoxAnswer().getMessage(), -// questionAnswer.getText(), -// questionAnswer.getPoints() -// ) -// ) -// .phrase(questionAnswer.getText()); -// if (i != this.questions.size() - 1) { -// answerSaveBuilder.nextUnit(previousUnit).build(); -// } else { -// answerSaveBuilder.pusher(pusher); -// Optional.of(finishUnit).ifPresent(answerSaveBuilder::nextUnit); -// } -// answerTextBuilder.nextUnit(answerSaveBuilder.build()); -// } -// if (i == 0) answerTextBuilder.activeType(UnitActiveType.AFTER); -// previousUnit = answerTextBuilder.build(); -// } -// return previousUnit; -// } -// -//} diff --git a/pom.xml b/pom.xml index d56854e..c3cce25 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.17 + 0.0.18 pom @@ -32,13 +32,13 @@ UTF-8 UTF-8 - 0.0.17 + 0.0.18 ${godfather.ver} ${godfather.ver} - 3.3.0 - 1.0.3 + 3.4.0 + 1.2.0 2.2 2.0.1.Final