Добавил SwitchUnit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-04-02 21:30:50 +03:00
parent 5379471aea
commit 6e55bee09e
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
2 changed files with 240 additions and 0 deletions

View File

@ -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<SwitchUnit<Message>, Message> {
@Override
public Uni<UnitRequest<MainUnit, Message>> action(UnitRequest<SwitchUnit<Message>, Message> unitRequest) {
final Message message = unitRequest.getMessage();
final Set<MainUnit<Message>> 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<Message> getMessageType() {
return Message.class;
}
}

View File

@ -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<M extends Message> extends MainUnit<M> {
public static final String TYPE = "SWITCH_UNIT";
private SwitchUnit(Builder<M> 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 <M extends Message> SwitchUnit.Builder<M> builder() {
return new SwitchUnit.Builder<>();
}
public static final class Builder<M extends Message> {
private String name = UUID.randomUUID().toString();
private String description;
private Set<MainUnit<M>> nextUnits;
private Set<KeyWord> triggerWords;
private Set<String> triggerPhrases;
private UniPredicate<M> triggerCheck;
private Set<Pattern> triggerPatterns;
private Integer matchThreshold;
private Integer priority;
private UnitActiveType activeType;
private boolean notSaveHistory;
private ProcessingData<M> boxAnswer;
private CallBackConsumer callBack;
private Builder() {
}
public SwitchUnit.Builder<M> name(String name) {
this.name = name;
return this;
}
public SwitchUnit.Builder<M> description(String description) {
this.description = description;
return this;
}
public SwitchUnit.Builder<M> answer(Consumer<M> answer) {
this.boxAnswer = message -> {
answer.accept(message);
return Uni.createFrom().nullItem();
};
return this;
}
public SwitchUnit.Builder<M> answer(Function<M, Uni<BoxAnswer>> answer) {
this.boxAnswer = answer::apply;
return this;
}
public SwitchUnit.Builder<M> answer(Supplier<BoxAnswer> answer) {
this.boxAnswer = message -> Uni.createFrom().item(answer.get());
return this;
}
public SwitchUnit.Builder<M> callBack(CallBackConsumer callBack) {
this.callBack = callBack;
return this;
}
public SwitchUnit.Builder<M> triggerWords(Set<KeyWord> val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();
}
triggerWords.addAll(val);
return this;
}
public SwitchUnit.Builder<M> triggerWord(KeyWord val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();
}
triggerWords.add(val);
return this;
}
public SwitchUnit.Builder<M> triggerStringWords(Set<String> val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();
}
triggerWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet()));
return this;
}
public SwitchUnit.Builder<M> triggerWord(String val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();
}
triggerWords.add(KeyWord.of(val));
return this;
}
public SwitchUnit.Builder<M> 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<M> 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<M> triggerCheck(UniPredicate<M> trigger) {
triggerCheck = trigger;
return this;
}
public SwitchUnit.Builder<M> matchThreshold(Integer val) {
matchThreshold = val;
return this;
}
public SwitchUnit.Builder<M> priority(Integer val) {
priority = val;
return this;
}
public SwitchUnit.Builder<M> next(MainUnit<M> val) {
if (nextUnits == null) {
nextUnits = new HashSet<>();
}
nextUnits.add(val);
return this;
}
public SwitchUnit.Builder<M> notSaveHistory() {
notSaveHistory = true;
return this;
}
public SwitchUnit.Builder<M> activeType(UnitActiveType val) {
activeType = val;
return this;
}
public SwitchUnit<M> build() {
// isNotNull(boxAnswer, UnitConfigException.unitConfigException("BoxAnswer обязательный параметр юнита"));
return new SwitchUnit<>(this);
}
}
}