Merge branch 'release/release-1.1.0'

This commit is contained in:
Mark Struchkov 2019-01-21 00:40:21 +03:00
commit e1165a4a5a
15 changed files with 266 additions and 173 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.autoresponder</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
<version>1.1.0-RELEASE</version>
<build>
<plugins>
<plugin>
@ -41,6 +41,7 @@
<version>${log4j}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>

View File

@ -1,51 +1,57 @@
package org.sadtech.autoresponder;
import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.entity.compare.UnitPriorityComparator;
import org.sadtech.autoresponder.service.PersonService;
import org.sadtech.autoresponder.service.UnitService;
import org.sadtech.autoresponder.submodule.parser.InsertWords;
import org.sadtech.autoresponder.submodule.parser.Parser;
import java.util.Collections;
import java.util.List;
@AllArgsConstructor
public class Autoresponder {
private UnitService unitService;
private PersonService personService;
public String answer(@NotNull Integer idPerson, @NotNull String message) {
Person person = CheckAndAddPerson(idPerson);
public Autoresponder(UnitService unitService, PersonService personService) {
this.unitService = unitService;
this.personService = personService;
}
public Unit answer(Integer idPerson, String message) {
Person person = checkAndAddPerson(idPerson);
Unit unit;
if (person.getUnit() == null) {
unit = unitService.nextUnit(unitService.menuUnit(), message);
unit = nextUnit(unitService.menuUnit(), message);
} else {
unit = unitService.nextUnit(person.getUnit(), message);
unit = nextUnit(person.getUnit().getNextUnits(), message);
}
person.setUnit(unit);
return unit.getAnswer();
return unit;
}
public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List<String> words) {
String answer = answer(idPerson, message);
InsertWords insertWords = new InsertWords();
insertWords.setInText(answer);
insertWords.insert(words);
return insertWords.getOutText();
}
private Person CheckAndAddPerson(Integer idPerson) {
private Person checkAndAddPerson(Integer idPerson) {
Person person;
if (personService.checkPerson(idPerson)) {
person = personService.getPersonById(idPerson);
} else {
person = new Person();
person.setId(idPerson);
person = new Person(idPerson);
personService.addPerson(person);
}
return person;
}
private Unit nextUnit(List<Unit> nextUnits, String message) {
if (nextUnits.size() > 0) {
Parser parser = new Parser();
parser.setText(message);
parser.parse();
return nextUnits.stream().filter(nextUnit -> !Collections.disjoint(nextUnit.getKeyWords(), parser.getWords())).max(new UnitPriorityComparator()).get();
} else {
return null;
}
}
}

View File

@ -1,11 +1,48 @@
package org.sadtech.autoresponder.entity;
import lombok.Data;
import java.util.Objects;
@Data
public class Person {
private Integer id;
private Unit unit;
public Person(Integer id, Unit unit) {
this.id = id;
this.unit = unit;
}
public Person(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Unit getUnit() {
return unit;
}
public void setUnit(Unit unit) {
this.unit = unit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) &&
Objects.equals(unit, person.unit);
}
@Override
public int hashCode() {
return Objects.hash(id, unit);
}
}

View File

@ -1,19 +1,83 @@
package org.sadtech.autoresponder.entity;
import lombok.Data;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Data
public class Unit {
public abstract class Unit {
private Integer idUnit;
private List<Unit> nextUnits;
private String answer;
private Integer priority;
private Set<String> keyWords;
private Integer matchThreshold;
private Boolean level = false;
private Integer priority;
private Boolean level;
private List<Unit> nextUnits;
public Unit() {
level = false;
}
public Unit(Set<String> keyWords, Integer matchThreshold, Integer priority, Boolean level, List<Unit> nextUnits) {
this.keyWords = keyWords;
this.matchThreshold = matchThreshold;
this.priority = priority;
this.level = level;
this.nextUnits = nextUnits;
}
public Set<String> getKeyWords() {
return keyWords;
}
public void setKeyWords(Set<String> keyWords) {
this.keyWords = keyWords;
}
public Integer getMatchThreshold() {
return matchThreshold;
}
public void setMatchThreshold(Integer matchThreshold) {
this.matchThreshold = matchThreshold;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Boolean getLevel() {
return level;
}
public void setLevel(Boolean level) {
this.level = level;
}
public List<Unit> getNextUnits() {
return nextUnits;
}
public void setNextUnits(List<Unit> nextUnits) {
this.nextUnits = nextUnits;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Unit unit = (Unit) o;
return Objects.equals(keyWords, unit.keyWords) &&
Objects.equals(matchThreshold, unit.matchThreshold) &&
Objects.equals(priority, unit.priority) &&
Objects.equals(level, unit.level) &&
Objects.equals(nextUnits, unit.nextUnits);
}
@Override
public int hashCode() {
return Objects.hash(keyWords, matchThreshold, priority, level, nextUnits);
}
}

View File

@ -0,0 +1,19 @@
package org.sadtech.autoresponder.entity.compare;
import org.sadtech.autoresponder.entity.Unit;
import java.util.Comparator;
public class UnitPriorityComparator implements Comparator<Unit> {
@Override
public int compare(Unit o1, Unit o2) {
if (o1.getPriority() < o2.getPriority()) {
return -1;
} else if (o1.getPriority().equals(o2.getPriority())) {
return 0;
}
return 1;
}
}

View File

@ -2,14 +2,16 @@ package org.sadtech.autoresponder.repository;
import org.sadtech.autoresponder.entity.Person;
import java.util.Collection;
import java.util.Map;
public interface PersonRepository {
void addPerson(Person person);
void removePerson(Person person);
void addPersonAll(Map<Integer, Person> personCollection);
Person getPersonById(Integer idPerson);
}

View File

@ -7,8 +7,6 @@ import java.util.List;
public interface UnitRepository {
Unit getUnitById(Integer idUnit);
void addUnit(Unit unit);
void addUnits(Collection<Unit> units);
@ -16,4 +14,5 @@ public interface UnitRepository {
void removeUnit(Unit idUnit);
List<Unit> menuUnits();
}

View File

@ -1,42 +0,0 @@
package org.sadtech.autoresponder.repository.impl;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.*;
public class UnitRepositoryMap implements UnitRepository {
private Map<Integer, Unit> units = new HashMap<>();
@Override
public Unit getUnitById(Integer idUnit) {
return units.get(idUnit);
}
@Override
public void addUnit(Unit unit) {
units.put(unit.getIdUnit(), unit);
}
@Override
public void addUnits(Collection<Unit> units) {
units.addAll(units);
}
@Override
public void removeUnit(Unit idUnit) {
units.remove(idUnit.getIdUnit());
}
@Override
public List<Unit> menuUnits() {
ArrayList<Unit> unitsMenu = new ArrayList<>();
for (Integer integer : units.keySet()) {
if (units.get(integer).getLevel()) {
unitsMenu.add(units.get(integer));
}
}
return unitsMenu;
}
}

View File

@ -1,13 +1,14 @@
package org.sadtech.autoresponder.service;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import java.util.List;
public interface UnitService {
Unit nextUnit(Unit unit, String message);
List<Unit> menuUnit();
Unit getUnitById(Integer idUnit);
Unit menuUnit();
void addUnitRepository(UnitRepository unitRepository);
}

View File

@ -1,15 +1,17 @@
package org.sadtech.autoresponder.service.impl;
import lombok.AllArgsConstructor;
import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.repository.PersonRepository;
import org.sadtech.autoresponder.service.PersonService;
@AllArgsConstructor
public class PersonServiceImpl implements PersonService {
private PersonRepository personRepository;
public PersonServiceImpl(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
public Person getPersonById(Integer integer) {
return personRepository.getPersonById(integer);

View File

@ -1,52 +1,38 @@
package org.sadtech.autoresponder.service.impl;
import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import org.sadtech.autoresponder.service.UnitService;
import org.sadtech.autoresponder.submodule.parser.Parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Log4j
@AllArgsConstructor
public class UnitServiceImpl implements UnitService {
private UnitRepository unitRepository;
private List<UnitRepository> unitRepositories;
public Unit nextUnit(Unit unit, @NotNull String message) {
ArrayList<Unit> nextUnits = (ArrayList<Unit>) unit.getNextUnits();
if (nextUnits.size() > 0) {
Parser parser = new Parser();
parser.setText(message);
parser.parse();
Unit unitReturn = new Unit();
unitReturn.setPriority(0);
for (Unit nextUnit : nextUnits) {
if (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority() > unitReturn.getPriority())) {
unitReturn = nextUnit;
}
}
return unitReturn;
} else {
return null;
public UnitServiceImpl() {
unitRepositories = new ArrayList<>();
}
public UnitServiceImpl(List<UnitRepository> unitRepositories) {
this.unitRepositories = unitRepositories;
}
@Override
public List<Unit> menuUnit() {
List<Unit> units = new ArrayList<>();
for (UnitRepository unitRepository : unitRepositories) {
units.addAll(unitRepository.menuUnits());
}
return units;
}
@Override
public Unit getUnitById(@NotNull Integer idUnit) {
return unitRepository.getUnitById(idUnit);
}
@Override
public Unit menuUnit() {
Unit menuUnit = new Unit();
menuUnit.setNextUnits(unitRepository.menuUnits());
return menuUnit;
public void addUnitRepository(UnitRepository unitRepository) {
unitRepositories.add(unitRepository);
}

View File

@ -1,4 +1,4 @@
package org.sadtech.autoresponder.submodule.parser;
package org.sadtech.autoresponder.submodule.insertwords;
import lombok.Getter;
import lombok.Setter;

View File

@ -1,8 +1,5 @@
package org.sadtech.autoresponder.submodule.parser;
import lombok.Getter;
import lombok.Setter;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
@ -10,12 +7,17 @@ import java.util.regex.Pattern;
public class Parser {
@Getter
private Set<String> words = new HashSet<>();
@Setter
private String text;
public Set<String> getWords() {
return words;
}
public void setText(String text) {
this.text = text;
}
public void parse() {
Pattern p = Pattern.compile("[а-яА-Я0-9]+");
Matcher m = p.matcher(text);

View File

@ -5,38 +5,38 @@ import org.junit.Before;
import org.junit.Test;
import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository;
import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap;
import org.sadtech.autoresponder.repository.impl.UnitRepositoryMap;
import org.sadtech.autoresponder.service.impl.PersonServiceImpl;
import org.sadtech.autoresponder.service.impl.UnitServiceImpl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.*;
public class AutoresponderTest {
private Person person = new Person();
private Unit unit = new Unit();
private Unit unit2 = new Unit();
private Unit unit3 = new Unit();
private Person person = new Person(1);
private TextUnit unit = new TextUnit();
private TextUnit unit2 = new TextUnit();
private TextUnit unit3 = new TextUnit();
private ArrayList<Unit> units = new ArrayList<>();
private UnitRepositoryMap unitRepository = new UnitRepositoryMap();
private TextUnitRepositoryList unitRepository = new TextUnitRepositoryList();
private PersonRepositoryMap personRepository = new PersonRepositoryMap();
private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository);
private UnitServiceImpl unitService = new UnitServiceImpl();
private PersonServiceImpl personService = new PersonServiceImpl(personRepository);
private Autoresponder autoresponder = new Autoresponder(unitService, personService);
@Before
public void before() {
unitService.addUnitRepository(unitRepository);
HashSet<String> words = new HashSet<>();
HashSet<String> words2 = new HashSet<>();
words.add("тест");
words2.add("тест");
words2.add("привет");
unit.setIdUnit(1);
unit.setLevel(true);
unit.setPriority(50);
unit.setKeyWords(words);
@ -48,20 +48,17 @@ public class AutoresponderTest {
unit.setNextUnits(units);
unit2.setIdUnit(2);
unit2.setAnswer("Ответ с {0} параметрами!");
unit2.setPriority(60);
unit2.setKeyWords(words);
unit2.setMatchThreshold(100);
unit3.setIdUnit(3);
unit3.setAnswer("Второй Ответ с {0} параметрами!");
unit3.setPriority(50);
unit3.setKeyWords(words2);
unit3.setMatchThreshold(100);
person.setUnit(unit);
person.setId(1);
unitRepository.addUnit(unit);
unitRepository.addUnit(unit2);
@ -70,49 +67,17 @@ public class AutoresponderTest {
@Test
public void usualAnswer() {
Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!");
Unit unit = autoresponder.answer(person.getId(), "Привет это тест срабатывания");
Assert.assertEquals(((TextUnit) unit).getAnswer(), "Ответ с {0} параметрами!");
}
@Test
public void answerOneParameter() {
ArrayList<String> words = new ArrayList<>();
words.add("одним");
Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!");
}
@Test
public void NoAnswer() {
person.setUnit(null);
String test = autoresponder.answer(person.getId(), "Привет это срабатывания");
Assert.assertNull(test);
autoresponder.answer(person.getId(), "Привет это срабатывания");
}
@Test
public void answerTwoParameter() {
ArrayList<String> words = new ArrayList<>();
words.add("одним");
words.add("двумя");
unit2.setAnswer("Ответ с {0} и {1}");
Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним и двумя");
}
@Test
public void incorrectSettingsWords() {
ArrayList<String> words = new ArrayList<>();
words.add("одним");
words.add("двумя");
unit2.setAnswer("Ответ с {1} и {3}");
Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и {3}");
}
@Test
public void answerRepeatSingleParameter() {
ArrayList<String> words = new ArrayList<>();
words.add("одним");
words.add("двумя");
unit2.setAnswer("Ответ с {1} и {1}");
Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и двумя");
}
@Test
public void answerByPriority() {
@ -121,7 +86,57 @@ public class AutoresponderTest {
@Test
public void answerNoPerson() {
Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!");
Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!");
TextUnit textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания");
Assert.assertEquals(textUnit.getAnswer(), "Здравствуйте, вы написали в нашу компанию!");
textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания");
Assert.assertEquals(textUnit.getAnswer(), "Ответ с {0} параметрами!");
}
private class TextUnit extends Unit {
private String answer;
public TextUnit() {
super();
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
private class TextUnitRepositoryList implements UnitRepository {
List<Unit> textUnits = new ArrayList<>();
@Override
public void addUnit(Unit unit) {
textUnits.add(unit);
}
@Override
public void addUnits(Collection<Unit> units) {
textUnits.addAll(units);
}
@Override
public void removeUnit(Unit idUnit) {
textUnits.remove(idUnit);
}
@Override
public List<Unit> menuUnits() {
List<Unit> units = new ArrayList<>();
for (Unit textUnit : textUnits) {
if (textUnit.getLevel()) {
units.add(textUnit);
}
}
return units;
}
}
}

View File

@ -1,8 +1,9 @@
package org.sadtech.autoresponder.submodule.parser;
package org.sadtech.autoresponder.submodule.insertwords;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.sadtech.autoresponder.submodule.insertwords.InsertWords;
import java.util.ArrayList;