diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index f95d38d..87c07cd 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -14,43 +14,46 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +/* + Отвечает за функционирование автоответчика + */ public class Autoresponder { private static final Logger log = LoggerFactory.getLogger(Autoresponder.class); + private static final UnitPriorityComparator UNIT_PRIORITY_COMPARATOR = new UnitPriorityComparator(); - private Set menuUnits; - private UnitPointerService unitPointerService; + /* + Начальные unit, по которым происходит поиск, если пользователь только обратился или закончил сценарий. + */ + private final Set menuUnits; + private final UnitPointerService unitPointerService; - public Autoresponder(UnitPointerService unitPointerService) { - this.unitPointerService = unitPointerService; - } - - public void setMenuUnits(Set menuUnits) { + public Autoresponder(UnitPointerService unitPointerService, Set menuUnits) { this.menuUnits = menuUnits; + this.unitPointerService = unitPointerService; } public UnitPointerService getUnitPointerService() { return unitPointerService; } - public void setUnitPointerService(UnitPointerService unitPointerService) { - this.unitPointerService = unitPointerService; - } - - public Unit answer(Integer idPerson, String message) { - UnitPointer unitPointer = checkAndAddPerson(idPerson); + /* + Возвращает unit на основании сообщения пользователя + */ + public Unit answer(Integer personId, String message) { + UnitPointer unitPointer = checkAndAddPerson(personId); Unit unit; if (unitPointer.getUnit() == null) { - unit = nextUnit(menuUnits, message); + unit = nextUnit(menuUnits, message); // выбирает unit из menuUnits, если пользователь обращается впервые } else { if (unitPointer.getUnit().getNextUnits() == null) { - unit = nextUnit(menuUnits, message); + unit = nextUnit(menuUnits, message); // если пользователь закончил сценарий, то выбирает следующий юнит из menuUnits } else { unit = nextUnit(unitPointer.getUnit().getNextUnits(), message); } } if (unit != null) { - unitPointer.setUnit(unit); + unitPointerService.edit(personId, unit); } return unit; } @@ -68,19 +71,21 @@ public class Autoresponder { private Unit nextUnit(Set nextUnits, String message) { if (nextUnits.size() > 0) { - UnitPriorityComparator unitPriorityComparator = new UnitPriorityComparator(); - Optional patternUnits = nextUnits.stream().filter(nextUnit -> nextUnit.getPattern() != null).filter(nextUnit -> patternReg(nextUnit, message)).max(unitPriorityComparator); + Optional patternUnits = nextUnits.stream() + .filter(nextUnit -> nextUnit.getPattern() != null) + .filter(nextUnit -> patternReg(nextUnit, message)) + .max(UNIT_PRIORITY_COMPARATOR); if (!patternUnits.isPresent()) { patternUnits = nextUnits.stream() .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold()) - .max(unitPriorityComparator); + .max(UNIT_PRIORITY_COMPARATOR); } if (!patternUnits.isPresent()) { patternUnits = nextUnits.stream() .filter(nextUnit -> (nextUnit.getPattern() == null && nextUnit.getKeyWords() == null)) - .max(unitPriorityComparator); + .max(UNIT_PRIORITY_COMPARATOR); } return patternUnits.orElse(null); @@ -99,7 +104,6 @@ public class Autoresponder { if (unit.getKeyWords() != null) { Set temp = new HashSet<>(unit.getKeyWords()); temp.retainAll(words); - log.info("Юнит: " + unit.getClass().getSimpleName()); log.info("Ключевые слова юнита: " + unit.getKeyWords() + " (" + unit.getKeyWords().size() + ")"); log.info("Ключевые слова от пользователя: " + words); log.info("Пересечение: " + temp + " (" + temp.size() + ")"); diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 1a160e6..4efc004 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,10 +1,14 @@ package org.sadtech.autoresponder.entity; +import java.util.Arrays; import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; +/* + Абстрактный класс, предпологающий какие-то действия + */ public abstract class Unit { private Set keyWords; @@ -13,34 +17,40 @@ public abstract class Unit { private Integer priority = 10; private Set nextUnits; - public Unit() { - - } - - public Unit(Unit... nextUnit) { - nextUnits = new HashSet(nextUnits); - } - - public void setKeyWord(String keyWord) { - if (keyWords == null) { - keyWords = new HashSet<>(); + public void setKeyWord(String... keyWords) { + if (this.keyWords == null) { + this.keyWords = new HashSet<>(); } - keyWords.add(keyWord); + this.keyWords.addAll(Arrays.asList(keyWords)); } - public void setNextUnit(Unit unit) { - if (nextUnits == null) { - nextUnits = new HashSet<>(); + public void setKeyWords(Set keyWords) { + if (this.keyWords == null) { + this.keyWords = new HashSet<>(); } - nextUnits.add(unit); + this.keyWords.addAll(keyWords); } public Set getKeyWords() { return keyWords; } - public void setKeyWords(Set keyWords) { - this.keyWords = keyWords; + public void setNextUnit(Unit... units) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } + nextUnits.addAll(Arrays.asList(units)); + } + + public void setNextUnits(Set nextUnits) { + if (nextUnits == null) { + nextUnits = new HashSet<>(); + } + this.nextUnits.addAll(nextUnits); + } + + public Set getNextUnits() { + return nextUnits; } public Integer getMatchThreshold() { @@ -59,14 +69,6 @@ public abstract class Unit { this.priority = priority; } - public Set getNextUnits() { - return nextUnits; - } - - public void setNextUnits(Set nextUnits) { - this.nextUnits = nextUnits; - } - public Pattern getPattern() { return pattern; } diff --git a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java index 37b40b9..84eb190 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java +++ b/src/main/java/org/sadtech/autoresponder/entity/UnitPointer.java @@ -2,6 +2,9 @@ package org.sadtech.autoresponder.entity; import java.util.Objects; +/* + Сохраняет юнит, на котором остановился пользователь. + */ public class UnitPointer { private Integer entityId; diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java index dd40da0..576bf75 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java @@ -8,6 +8,8 @@ public interface UnitPointerRepository { Integer add(UnitPointer unitPointer); + void edit(UnitPointer unitPointer); + void remove(Integer entityId); void addAll(Map unitPointerMap); diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java index 0d2c3f1..5534c55 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java @@ -15,6 +15,11 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository { return unitPointer.getEntityId(); } + @Override + public void edit(UnitPointer unitPointer) { + unitPointerMap.get(unitPointer.getEntityId()).setUnit(unitPointer.getUnit()); + } + @Override public void remove(Integer entityId) { unitPointerMap.remove(entityId); diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java index e819854..b174eea 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -1,5 +1,6 @@ package org.sadtech.autoresponder.service; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; public interface UnitPointerService { @@ -10,4 +11,6 @@ public interface UnitPointerService { UnitPointer getByEntityId(Integer entityId); + void edit(Integer personId, Unit unit); + } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index c9e42f3..05bea40 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -1,5 +1,6 @@ package org.sadtech.autoresponder.service; +import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.UnitPointer; import org.sadtech.autoresponder.repository.UnitPointerRepository; import org.sadtech.autoresponder.repository.UnitPointerRepositoryMap; @@ -25,6 +26,13 @@ public class UnitPointerServiceImpl implements UnitPointerService { return unitPointerRepository.findByEntityId(entityId); } + @Override + public void edit(Integer personId, Unit unit) { + if (check(personId)) { + unitPointerRepository.edit(new UnitPointer(personId, unit)); + } + } + @Override public void add(UnitPointer unitPointer) { unitPointerRepository.add(unitPointer); diff --git a/src/main/java/org/sadtech/autoresponder/util/Parser.java b/src/main/java/org/sadtech/autoresponder/util/Parser.java index 91ffdc1..9fc5d54 100644 --- a/src/main/java/org/sadtech/autoresponder/util/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/util/Parser.java @@ -5,6 +5,9 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +/* + Возвращает Set слов из текста + */ public class Parser { private Parser() {