Добавил новый юнит ReplaceCmd, который позволяет заменять тригеры для юнитов. Также добавил новый признак юнитов, возможность отключать сохранение в историю сценария.

This commit is contained in:
Struchkov Mark 2022-07-15 10:27:48 +03:00
parent 6d9454fd03
commit fc3f9563e2
15 changed files with 277 additions and 85 deletions

View File

@ -14,9 +14,10 @@ public class TypeUnit {
public static final String TIMER = "TIMER";
public static final String CHECK = "CHECK";
public static final String VALIDITY = "VALIDITY";
public static final String BACK_CMD = "BACK_CMD";
public static final String BACK_CMD = "BACK_CMD";
public static final String TELEPORT_CMD = "TELEPORT_CMD";
public static final String REPLACE_CMD = "REPLACE_CMD";
private TypeUnit() {
utilityClass();

View File

@ -6,6 +6,7 @@ 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.regex.Pattern;
@ -41,12 +42,13 @@ public class AnswerCheck extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
new HashSet<>(),
builder.activeType,
builder.notSaveHistory,
builder.accessibility,
TypeUnit.CHECK
);
@ -73,8 +75,8 @@ public class AnswerCheck extends MainUnit {
public static final class Builder {
private String name;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>();
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
@ -83,6 +85,7 @@ public class AnswerCheck extends MainUnit {
private CheckData<Message> check;
private UnitActiveType activeType;
private Accessibility accessibility;
private boolean notSaveHistory;
private Builder() {
}
@ -113,7 +116,12 @@ public class AnswerCheck extends MainUnit {
}
public Builder phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}
@ -157,6 +165,11 @@ public class AnswerCheck extends MainUnit {
return this;
}
public Builder notSaveHistory() {
notSaveHistory = true;
return this;
}
public AnswerCheck build() {
isNotNull(check, unitConfigException("Необходимо установить параметр проверки."));
isAnyNotNull(unitConfigException("Необходимо задать хотя бы один unit результата проверки."));

View File

@ -55,12 +55,13 @@ public class AnswerSave<D> extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
builder.nextUnits,
(builder.hidden) ? UnitActiveType.AFTER : UnitActiveType.DEFAULT,
builder.notSaveHistory,
builder.accessibility,
TypeUnit.SAVE
);
@ -110,7 +111,7 @@ public class AnswerSave<D> extends MainUnit {
public static final class Builder<D> {
private String name;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private Set<String> phrases = new HashSet<>();
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
@ -122,6 +123,7 @@ public class AnswerSave<D> extends MainUnit {
private boolean hidden;
private CheckSave<? super Message> checkSave;
private Accessibility accessibility;
private boolean notSaveHistory;
private Builder() {
}
@ -152,7 +154,12 @@ public class AnswerSave<D> extends MainUnit {
}
public Builder<D> phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder<D> phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}
@ -211,11 +218,16 @@ public class AnswerSave<D> extends MainUnit {
return this;
}
public Builder accessibility(Accessibility val) {
public Builder<D> accessibility(Accessibility val) {
accessibility = val;
return this;
}
public Builder<D> notSaveHistory() {
notSaveHistory = true;
return this;
}
public AnswerSave<D> build() {
isNotNull(preservable, "Не указан репозиторий для сохранения формы пользователя");
isNotNull(preservableData, "Не указаны данные для сохранения");

View File

@ -11,6 +11,7 @@ import dev.struchkov.godfather.context.service.usercode.Insert;
import dev.struchkov.godfather.context.service.usercode.MessageFunction;
import dev.struchkov.godfather.context.service.usercode.ProcessingData;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
@ -45,12 +46,13 @@ public class AnswerText<M extends Message> extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
builder.nextUnits,
builder.activeType,
builder.notSaveHistory,
builder.accessibility,
TypeUnit.TEXT
);
@ -80,18 +82,19 @@ public class AnswerText<M extends Message> extends MainUnit {
}
public static final class Builder<M extends Message> {
private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>();
private String name;
private ProcessingData<M> boxAnswer;
private Insert insert;
private Sending sending;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
private Set<MainUnit> nextUnits = new HashSet<>();
private UnitActiveType activeType;
private Accessibility accessibility;
private boolean notSaveHistory;
private Builder() {
}
@ -158,7 +161,12 @@ public class AnswerText<M extends Message> extends MainUnit {
}
public Builder<M> phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder<M> phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}
@ -187,11 +195,16 @@ public class AnswerText<M extends Message> extends MainUnit {
return this;
}
public Builder accessibility(Accessibility val) {
public Builder<M> accessibility(Accessibility val) {
accessibility = val;
return this;
}
public Builder<M> notSaveHistory() {
notSaveHistory = true;
return this;
}
public Builder<M> activeType(UnitActiveType val) {
activeType = val;
return this;

View File

@ -3,10 +3,10 @@ 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.domain.content.Message;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
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.regex.Pattern;
@ -46,12 +46,13 @@ public class AnswerTimer<M extends Message> extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
null,
builder.activeType,
builder.notSaveHistory,
builder.accessibility,
TypeUnit.TIMER
);
@ -87,13 +88,14 @@ public class AnswerTimer<M extends Message> extends MainUnit {
private Integer timeDelaySec;
private Integer timeDeathSec;
private CheckData<M> checkLoop;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>();
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
private UnitActiveType activeType = UnitActiveType.AFTER;
private Accessibility accessibility;
private boolean notSaveHistory;
private Builder() {
}
@ -144,7 +146,12 @@ public class AnswerTimer<M extends Message> extends MainUnit {
}
public Builder<M> phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder<M> phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}
@ -163,11 +170,16 @@ public class AnswerTimer<M extends Message> extends MainUnit {
return this;
}
public Builder accessibility(Accessibility val) {
public Builder<M> accessibility(Accessibility val) {
accessibility = val;
return this;
}
public Builder<M> notSaveHistory() {
notSaveHistory = true;
return this;
}
public Builder<M> activeType(UnitActiveType val) {
activeType = val;
return this;

View File

@ -7,6 +7,7 @@ import dev.struchkov.godfather.context.service.ClarificationQuestion;
import dev.struchkov.godfather.context.service.save.LocalPreservable;
import dev.struchkov.godfather.context.service.save.Preservable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
@ -42,12 +43,13 @@ public class AnswerValidity extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
builder.nextUnits,
UnitActiveType.DEFAULT,
builder.notSaveHistory,
builder.accessibility,
TypeUnit.VALIDITY
);
@ -82,18 +84,19 @@ public class AnswerValidity extends MainUnit {
}
public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>();
private String name;
private MainUnit unitYes;
private MainUnit unitNo;
private MainUnit unitNull;
private ClarificationQuestion clarificationQuestion;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
private Set<MainUnit> nextUnits = new HashSet<>();
private Accessibility accessibility;
private boolean notSaveHistory;
private Builder() {
}
@ -144,7 +147,12 @@ public class AnswerValidity extends MainUnit {
}
public Builder phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}
@ -183,6 +191,11 @@ public class AnswerValidity extends MainUnit {
return this;
}
public Builder notSaveHistory() {
notSaveHistory = true;
return this;
}
public AnswerValidity build() {
return new AnswerValidity(this);
}

View File

@ -1,36 +0,0 @@
package dev.struchkov.godfather.context.domain.unit;
import dev.struchkov.autoresponder.entity.KeyWord;
import dev.struchkov.godfather.context.domain.UnitDefinition;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.context.service.Accessibility;
import java.util.Set;
import java.util.regex.Pattern;
public class LazyUnit extends MainUnit {
private final UnitDefinition unitDefinition;
private LazyUnit(
String name,
Set<KeyWord> keyWords,
String phrase,
Pattern pattern,
Integer matchThreshold,
Integer priority,
Set<MainUnit> nextUnits,
UnitActiveType activeType,
String type,
UnitDefinition unitDefinition,
Accessibility accessibility
) {
super(name, keyWords, phrase, pattern, matchThreshold, priority, nextUnits, activeType, accessibility, type);
this.unitDefinition = unitDefinition;
}
public static LazyUnit create(String name, UnitDefinition unitDefinition) {
return new LazyUnit(name, null, null, null, null, null, null, null, null, unitDefinition, null);
}
}

View File

@ -40,25 +40,29 @@ public abstract class MainUnit extends Unit<MainUnit> {
/**
* Проверка доступа пользователя к юниту.
*/
private Accessibility accessibility;
private final Accessibility accessibility;
private final boolean notSaveHistory;
protected MainUnit(
String name,
Set<KeyWord> keyWords,
String phrase,
Set<String> phrases,
Pattern pattern,
Integer matchThreshold,
Integer priority,
Set<MainUnit> nextUnits,
UnitActiveType activeType,
boolean notSaveHistory,
Accessibility accessibility,
String type
) {
super(keyWords, phrase, pattern, matchThreshold, priority, nextUnits);
super(keyWords, phrases, pattern, matchThreshold, priority, nextUnits);
this.name = name;
this.activeType = Optional.ofNullable(activeType).orElse(UnitActiveType.DEFAULT);
this.accessibility = accessibility;
this.type = type;
this.notSaveHistory = notSaveHistory;
}
public String getType() {
@ -85,6 +89,10 @@ public abstract class MainUnit extends Unit<MainUnit> {
return name;
}
public boolean isNotSaveHistory() {
return notSaveHistory;
}
public Optional<Accessibility> getAccessibility() {
return Optional.ofNullable(accessibility);
}

View File

@ -0,0 +1,124 @@
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.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.regex.Pattern;
import java.util.stream.Collectors;
public class ReplaceCmd extends MainUnit {
private final MainUnit thisUnit;
private ReplaceCmd(Builder builder) {
super(
builder.name,
builder.keyWords,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
new HashSet<>(),
builder.activeType,
true,
null,
TypeUnit.REPLACE_CMD
);
this.thisUnit = builder.thisUnit;
}
public static Builder builder() {
return new Builder();
}
public MainUnit getThisUnit() {
return thisUnit;
}
public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>();
private String name;
private Set<String> phrases = new HashSet<>();
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
private UnitActiveType activeType = UnitActiveType.AFTER;
private MainUnit thisUnit;
private Builder() {
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder keyWords(Set<KeyWord> val) {
keyWords.addAll(val);
return this;
}
public Builder keyWord(KeyWord val) {
keyWords.add(val);
return this;
}
public Builder stringKeyWords(Set<String> 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<String> val) {
phrases.addAll(val);
return this;
}
public Builder pattern(Pattern val) {
pattern = val;
return this;
}
public Builder matchThreshold(Integer val) {
matchThreshold = val;
return this;
}
public Builder priority(Integer val) {
priority = val;
return this;
}
public Builder activeType(UnitActiveType val) {
activeType = val;
return this;
}
public Builder thisUnit(MainUnit val) {
thisUnit = val;
return this;
}
public ReplaceCmd build() {
return new ReplaceCmd(this);
}
}
}

View File

@ -6,6 +6,7 @@ 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.regex.Pattern;
@ -30,12 +31,13 @@ public class RollBackCmd extends MainUnit {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
new HashSet<>(),
builder.activeType,
true,
null,
TypeUnit.BACK_CMD
);
@ -66,7 +68,7 @@ public class RollBackCmd extends MainUnit {
public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>();
private String name;
private String phrase;
private Set<String> phrases = new HashSet<>();
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
@ -103,7 +105,12 @@ public class RollBackCmd extends MainUnit {
}
public Builder phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.context.domain.TypeUnit;
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.regex.Pattern;
@ -18,36 +19,37 @@ public class TeleportCmd extends MainUnit {
/**
* Название юнита, в которое необходимо осуществить перенос.
*/
private String unitNameToTeleport;
private final String unitNameToTeleport;
private TeleportCmd(Builder builder) {
super(
builder.name,
builder.keyWords,
builder.phrase,
builder.phrases,
builder.pattern,
builder.matchThreshold,
builder.priority,
new HashSet<>(),
builder.activeType,
true,
null,
TypeUnit.TELEPORT_CMD
);
this.unitNameToTeleport = builder.unitNameToTeleport;
}
public String getUnitNameToTeleport() {
return unitNameToTeleport;
}
public static Builder builder() {
return new Builder();
}
public String getUnitNameToTeleport() {
return unitNameToTeleport;
}
public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>();
private String name;
private Set<KeyWord> keyWords = new HashSet<>();
private String phrase;
private Pattern pattern;
private Integer matchThreshold;
private Integer priority;
@ -83,7 +85,12 @@ public class TeleportCmd extends MainUnit {
}
public Builder phrase(String val) {
phrase = val;
phrases.add(val);
return this;
}
public Builder phrases(Collection<String> val) {
phrases.addAll(val);
return this;
}

View File

@ -39,7 +39,7 @@ public class StorylineMapRepository implements StorylineRepository {
public Optional<StorylineHistory> findByCountLast(long personId, String unitName) {
if (map.containsKey(personId)) {
final Stack<StorylineHistory> stack = map.get(personId);
StorylineHistory storylineHistory = null;
StorylineHistory storylineHistory;
while (!stack.isEmpty()) {
storylineHistory = stack.pop();
if (unitName.equals(storylineHistory.getUnitName())) {

View File

@ -19,6 +19,7 @@ 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;
@ -51,6 +52,7 @@ public class GeneralAutoResponder<T extends Message> {
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<T>> modifiable) {
@ -128,14 +130,7 @@ public class GeneralAutoResponder<T extends Message> {
public void answer(UnitRequest<MainUnit, T> unitRequest) {
try {
unitRequest = getAction(unitRequest);
unitRequest = activeUnitAfter(unitRequest);
final Optional<MainUnit> optDefaultUnit = storyLineService.getDefaultUnit();
final MainUnit unit = unitRequest.getUnit();
final T message = unitRequest.getMessage();
if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit)) {
storyLineService.save(message.getPersonId(), unit.getName(), message);
}
activeUnitAfter(unitRequest);
} catch (Exception e) {
if (errorHandler != null) {
errorHandler.handle(unitRequest.getMessage(), e);
@ -161,10 +156,15 @@ public class GeneralAutoResponder<T extends Message> {
private UnitRequest<MainUnit, T> getAction(UnitRequest<MainUnit, T> unitRequest) {
final MainUnit unit = unitRequest.getUnit();
final T message = unitRequest.getMessage();
final String typeUnit = unit.getType();
if (actionUnitMap.containsKey(typeUnit)) {
ActionUnit actionUnit = actionUnitMap.get(typeUnit);
UnitRequest<MainUnit, T> newUnitRequest = actionUnit.action(unitRequest);
final Optional<MainUnit> optDefaultUnit = storyLineService.getDefaultUnit();
if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) {
storyLineService.save(message.getPersonId(), unit.getName(), message);
}
final MainUnit newUnit = newUnitRequest.getUnit();
return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest;
} else {

View File

@ -0,0 +1,18 @@
package dev.struchkov.godfather.core.service.action.cmd;
import dev.struchkov.godfather.context.domain.UnitRequest;
import dev.struchkov.godfather.context.domain.content.Message;
import dev.struchkov.godfather.context.domain.unit.MainUnit;
import dev.struchkov.godfather.context.domain.unit.cmd.ReplaceCmd;
import dev.struchkov.godfather.core.service.action.ActionUnit;
public class ReplaceCmdAction implements ActionUnit<ReplaceCmd, Message> {
@Override
public UnitRequest<MainUnit, Message> action(UnitRequest<ReplaceCmd, Message> unitRequest) {
final ReplaceCmd unit = unitRequest.getUnit();
final Message message = unitRequest.getMessage();
return UnitRequest.of(unit.getThisUnit(), message);
}
}

View File

@ -37,7 +37,7 @@
<godfather.context.ver>${godfather.ver}</godfather.context.ver>
<godfather.core.ver>${godfather.ver}</godfather.core.ver>
<autoresponder.ver>3.1.0</autoresponder.ver>
<autoresponder.ver>3.2.0</autoresponder.ver>
<haiti.utils>1.0.2</haiti.utils>
<javax.persistence.api.ver>2.2</javax.persistence.api.ver>