diff --git a/questionGenerator/src/main/java/Main.java b/questionGenerator/src/main/java/Main.java index cb196b9e..e5c144aa 100644 --- a/questionGenerator/src/main/java/Main.java +++ b/questionGenerator/src/main/java/Main.java @@ -1,5 +1,6 @@ package main.java; +import java.util.ArrayList; import java.util.List; import main.java.questionGenerator.QuestionGenerator; @@ -8,37 +9,54 @@ import main.java.questionGenerator.repository.QuestionRepository; public class Main { + + private static QuestionGenerator qg = QuestionGenerator.getInstance(); + + private static final long TIME_SKIP = 18000000; //5 hours + // private static final long TIME_SKIP = 43200000; //12 hours + //private static final long TIME_SKIP = 1000; //1 minute + + + private static String[] languages = {"en", "es", "tr"}; + + private static QuestionType[] types = {QuestionType.CAPITAL, QuestionType.LANGUAGE, QuestionType.POPULATION, + QuestionType.SIZE, QuestionType.HEAD_OF_GOVERMENT}; + //, QuestionType.VIDEOGAME_DEVELOPER, QuestionType.VIDEOGAME_PUBLISHER, QuestionType.VIDEOGAME_GENRE, QuestionType.VIDEOGAME_COUNTRY}; - public static void main(String[] args) { - QuestionGenerator qgEn = new QuestionGenerator("en"); - - run(qgEn, QuestionType.CAPITAL, 3); - System.out.println(); - - run(qgEn, QuestionType.LANGUAGE, 3); - System.out.println(); - - run(qgEn, QuestionType.POPULATION, 3); - System.out.println(); - - run(qgEn, QuestionType.SIZE, 3); - - QuestionGenerator qgEs = new QuestionGenerator("es"); - - run(qgEs, QuestionType.CAPITAL, 3); - System.out.println(); - - run(qgEs, QuestionType.LANGUAGE, 3); - System.out.println(); - - run(qgEs, QuestionType.POPULATION, 3); - System.out.println(); - - run(qgEs, QuestionType.SIZE, 3); + //private static final int NUMBER_OF_QUESTIONS = 50; + private static final int NUMBER_OF_QUESTIONS = 100; + //private static final int NUMBER_OF_QUESTIONS = 3; + //private static final int NUMBER_OF_QUESTIONS = 1; + + public static void main(String[] args) { + while(true) { + List questions = generate().stream().map(q -> q.getJSON().toString()).toList(); + QuestionRepository.getInstance().populate(questions); + try { + Thread.sleep(TIME_SKIP); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private static List generate() { + List questions = new ArrayList(); + for(String lang : languages) { + qg.setLanguageCode(lang); + for(QuestionType type: types) + questions.addAll(run(qg, type, NUMBER_OF_QUESTIONS)); + } + return questions; } - private static void run(QuestionGenerator qg, QuestionType type, int numberOfQuestions){ - List questions = qg.generateQuestions(type, numberOfQuestions); - QuestionRepository.getInstance().insert(questions.stream().map(q -> q.getJSON().toString()).toList()); + private static List run(QuestionGenerator qg, QuestionType type, int numberOfQuestions) { + List questions = qg.generateQuestions(type, numberOfQuestions); + for(int i=0; i questions = qg.generateQuestions(type, numberOfQuestions); + for(int i=0; i q.getJSON().toString()).toList()); + } +} diff --git a/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java b/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java index 2c5f81b2..1fd27702 100644 --- a/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/QuestionGenerator.java @@ -6,30 +6,46 @@ import java.util.Random; import main.java.questionGenerator.entityGenerator.EntityGenerator; -import main.java.questionGenerator.generator.AbstractGenerator; -import main.java.questionGenerator.generator.specificGenerators.CapitalGenerator; -import main.java.questionGenerator.generator.specificGenerators.LanguageGenerator; -import main.java.questionGenerator.generator.specificGenerators.PopulationGenerator; -import main.java.questionGenerator.generator.specificGenerators.SizeGenerator; +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.CapitalGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.HeadOfGovernmentGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.LanguageGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameCountryGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameDeveloperGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogameGenreGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames.VideogamePublisherGenerator; +import main.java.questionGenerator.generator.answersAreEntites.withoutSubProperties.DirectorGenerator; +import main.java.questionGenerator.generator.answersAreNotEntites.PopulationGenerator; +import main.java.questionGenerator.generator.answersAreNotEntites.SizeGenerator; import main.java.questionGenerator.question.Question; import main.java.questionGenerator.question.QuestionType; public class QuestionGenerator { - private AbstractGenerator generator; + private Generator generator; private String languageCode; - public QuestionGenerator(String languageCode){ - this.languageCode = languageCode; + private static QuestionGenerator questionGenerator; + + public static QuestionGenerator getInstance() { + if(questionGenerator==null) + questionGenerator = new QuestionGenerator(); + return questionGenerator; + } + + private QuestionGenerator() { + } public List generateQuestions(QuestionType type, int amount){ setGenerator(type); generator.setLocalization(languageCode); + long sampleSize = getSampleSize(amount); + generator.setSampleSize(sampleSize); List questions = new ArrayList<>(); List entites = new ArrayList<>(); try { - entites = EntityGenerator.getEntities(type, 100); + entites = EntityGenerator.getEntities(type, sampleSize, generator.getPropertyId()); } catch (IOException e) { e.printStackTrace(); } @@ -43,6 +59,13 @@ public List generateQuestions(QuestionType type, int amount){ try { q = generator.generate(entity); questions.add(q); + } catch(RuntimeException e) { + /* + * Sometimes not all the parameters for generating can be passed in the query, so this + * acts as failsave to avoid generating undesired questions, while not overloading the + * screen with error messages if not wanted + */ + //System.err.println(e.getMessage()); } catch (Exception e) { //If there's any problem generating the question we jump to the next one System.err.println(e.getMessage()); @@ -55,23 +78,61 @@ public List generateQuestions(QuestionType type, int amount){ private void setGenerator(QuestionType type) { switch (type) { case POPULATION: { - generator = new PopulationGenerator(); + generator = PopulationGenerator.getInstance(); break; } case CAPITAL: { - generator = new CapitalGenerator(); + generator = CapitalGenerator.getInstance(); break; - } case SIZE: { - generator = new SizeGenerator(); + generator = SizeGenerator.getInstance(); break; } case LANGUAGE: { - generator = new LanguageGenerator(); + generator = LanguageGenerator.getInstance(); + break; + } + case HEAD_OF_GOVERMENT: { + generator = HeadOfGovernmentGenerator.getInstance(); + break; + } + case DIRECTOR: { + generator = DirectorGenerator.getInstance(); + break; + } + case VIDEOGAME_DEVELOPER: { + generator = VideogameDeveloperGenerator.getInstance(); + break; + } + case VIDEOGAME_PUBLISHER: { + generator = VideogamePublisherGenerator.getInstance(); + break; + } + case VIDEOGAME_GENRE: { + generator = VideogameGenreGenerator.getInstance(); + break; + } + case VIDEOGAME_COUNTRY: { + generator = VideogameCountryGenerator.getInstance(); break; } } } + public String getLanguageCode() { + return languageCode; + } + + public void setLanguageCode(String languageCode) { + this.languageCode = languageCode; + } + + private long getSampleSize(int amount) { + long value = amount * 10; + if(value<1000) + return 1000; + return value; + } + } diff --git a/questionGenerator/src/main/java/questionGenerator/entityGenerator/EntityGenerator.java b/questionGenerator/src/main/java/questionGenerator/entityGenerator/EntityGenerator.java index 0f4a4335..9e43a4a4 100644 --- a/questionGenerator/src/main/java/questionGenerator/entityGenerator/EntityGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/entityGenerator/EntityGenerator.java @@ -15,15 +15,13 @@ public class EntityGenerator { private static final String PRE_URL = "https://www.wikidata.org/w/api.php?action=query&format=json&" + "list=backlinks&formatversion=2&"; - private static String property = ""; - private static final String PROPERTY_URL = "bltitle=Property%3AP"; + private static final String PROPERTY_URL = "bltitle=Property%3A"; private static final String POST_URL = "&blnamespace=0&"; private static final String LIMIT = "bllimit="; - public static List getEntities(QuestionType type, int size) throws IOException{ - propertyFactory(type); + public static List getEntities(QuestionType type, long size, String property) throws IOException{ URL url = new URL(PRE_URL+PROPERTY_URL+property+POST_URL+LIMIT+size); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); @@ -44,23 +42,6 @@ public static List getEntities(QuestionType type, int size) throws IOExc return result; } - private static void propertyFactory(QuestionType type) { - switch (type) { - case CAPITAL: - property = "36"; - break; - case LANGUAGE: - property = "37"; - break; - case POPULATION: - property = "1082"; - break; - case SIZE: - property = "2046"; - break; - } - } - private static List extractEntities(String json){ List result = new ArrayList<>(); String[] entities = json.split("\"title\":"); diff --git a/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java index 6b788927..ceeff146 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/AbstractGenerator.java @@ -16,7 +16,7 @@ import main.java.questionGenerator.question.Question; import main.java.questionGenerator.question.QuestionType; -public abstract class AbstractGenerator { +public abstract class AbstractGenerator implements Generator { protected static final WikibaseDataFetcher wbdf = WikibaseDataFetcher.getWikidataDataFetcher(); private String language = "en"; @@ -33,6 +33,8 @@ public abstract class AbstractGenerator { private String message; + private long sampleSize = 0L; + public AbstractGenerator(String propertyId, QuestionType type, String message) { this.propertyId = propertyId; this.type = type; @@ -64,21 +66,25 @@ public Question generate(String id) throws Exception { } } + Map labels = idi.getLabels(); + Map> claims = idi.getJsonClaims(); - String name = getName(idi.getLabels()); + String name = getName(labels); //get the question - String question = getQuestion(name); + String question = getQuestion(name, claims); //get the right answer - String rightAnswer = getRightAnswer(idi.getJsonClaims()); + String rightAnswer = getRightAnswer(claims, propertyId); //get the wrong answers List answers = getWrongAnswers(rightAnswer); answers.add(0, rightAnswer); - //create and return the question + answers = decorateAnswers(answers); + + //create and return the question return new Question(question, answers, language, type); } @@ -87,11 +93,7 @@ protected String getName(Map names) { return mtv.getText(); } -// protected abstract String getQuestion(String name); - protected abstract String getRightAnswer(Map> claims) throws Exception; - protected abstract List getWrongAnswers(String rightAnswer) throws Exception; - - protected String getQuestion(String name) { + public String getQuestion(String name, Map> claims) { String q = getMessages().getString(message); return String.format(q, name); } @@ -126,6 +128,10 @@ public void setLocalization(String languageCode) { localize(languageCode); break; } + case "tr":{ + localize(languageCode); + break; + } default:{ localize("en"); break; @@ -139,4 +145,18 @@ private void localize(String languageCode) { this.messages = ResourceBundle.getBundle(MESSAGES_PATH, localization); } + public String getMessage(){ + return message; + } + + @Override + public void setSampleSize(long sampleSize) { + this.sampleSize = sampleSize; + } + + @Override + public long getSampleSize() { + return sampleSize; + } + } diff --git a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreNotEntites.java b/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreNotEntites.java deleted file mode 100644 index 9d5f0017..00000000 --- a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreNotEntites.java +++ /dev/null @@ -1,75 +0,0 @@ -package main.java.questionGenerator.generator; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -import main.java.questionGenerator.question.QuestionType; - -public abstract class AnswersAreNotEntites extends AbstractGenerator{ - - //Open to changes if needed. - public AnswersAreNotEntites(String propertyId, QuestionType type, String message) { - super(propertyId, type, message); - } - - @Override - protected List getWrongAnswers(String rightAnswer) { - int inumber = 0; - float fnumber = 0; - // Check if it is a float - try { - inumber = Integer.parseInt(rightAnswer); - } catch(NumberFormatException e1) { - try { - fnumber = Float.parseFloat(rightAnswer); - } catch (NumberFormatException e2) { - //throw some exception or return null. - } - } - - List wrongAnswers = new ArrayList<>(); - Random rnd = new Random(); - - // Gives values depending on parameter with percentage - // Example: If parameter is 50 value range is number*.5 and number*1.5 - int parameter = 50; - - - if(inumber != 0){ - //for integer values - for(int i = 0; i < 3; i++){ - long wrong = (inumber * (100 + rnd.nextInt(parameter * 2 + 1) - parameter) / 100); - // An extra check for the wrong answer to be same sign. - if((inumber > 0 && wrong < 0) || (inumber < 0 && wrong > 0)) - { - i--; - continue; - } - // Checking if it creates the same answer as any other. - if(wrong == inumber || wrongAnswers.contains(String.valueOf(wrong))) - i--; - else - wrongAnswers.add(String.valueOf(wrong)); - } - } - else{ - //for float values - for(int i = 0; i < 3; i++){ - float wrong = (fnumber * (100 + rnd.nextInt(parameter * 2 + 1) - parameter) / 100); - // An extra check for the wrong answer to be same sign. - if((fnumber > 0 && wrong < 0) || (fnumber < 0 && wrong > 0)) - { - i--; - continue; - } - // Checking if it creates the same answer as any other - if(wrong == fnumber || wrongAnswers.contains(String.valueOf(wrong))) - i--; - else - wrongAnswers.add(String.valueOf(wrong)); - } - } - return wrongAnswers; - } -} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/Generator.java b/questionGenerator/src/main/java/questionGenerator/generator/Generator.java new file mode 100644 index 00000000..e4bce495 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/Generator.java @@ -0,0 +1,30 @@ +package main.java.questionGenerator.generator; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; + +import main.java.questionGenerator.question.Question; + +public interface Generator { + + Question generate(String id) throws Exception; + + String getQuestion(String name, Map> claims); + + String getRightAnswer(Map> claims, String propertyId) throws Exception; + + List getWrongAnswers(String rightAnswer) throws Exception; + + List decorateAnswers(List answers); + + String getPropertyId(); + + void setLocalization(String languageCode); + + void setSampleSize(long sampleSize); + + long getSampleSize(); + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/AbstractAnswersAreEntites.java similarity index 57% rename from questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java rename to questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/AbstractAnswersAreEntites.java index f7bd6713..963104e7 100644 --- a/questionGenerator/src/main/java/questionGenerator/generator/AnswersAreEntities.java +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/AbstractAnswersAreEntites.java @@ -1,76 +1,29 @@ -package main.java.questionGenerator.generator; +package main.java.questionGenerator.generator.answersAreEntites; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Random; import org.wikidata.wdtk.datamodel.implementation.ItemDocumentImpl; -import org.wikidata.wdtk.datamodel.interfaces.Snak; -import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException; import main.java.questionGenerator.entityGenerator.EntityGenerator; +import main.java.questionGenerator.generator.AbstractGenerator; import main.java.questionGenerator.question.QuestionType; -public abstract class AnswersAreEntities extends AbstractGenerator { - - private final String PROPERTY_TO_CHECK; - private QuestionType type; - public AnswersAreEntities(String propertyId, QuestionType type, String propertyToCheck, String message) { - super(propertyId, type, message); - this.PROPERTY_TO_CHECK = propertyToCheck; - this.type = type; - } +public abstract class AbstractAnswersAreEntites extends AbstractGenerator{ - @Override - protected String getRightAnswer(Map> claims) throws Exception { - if(claims.get(super.getPropertyId())==null) { - throw new Exception("Claims does not have the property " + super.getPropertyId()); - } - - for(Statement st : claims.get(super.getPropertyId())) { - boolean valid = true; - for(SnakGroup sg : st.getQualifiers()) { - for(Snak s : sg.getSnaks()) { - String value = getIdFromLink(s.getPropertyId().toString()); - if(value.equals(PROPERTY_TO_CHECK)) { - valid = false; - break; - } - } - if(!valid) - break; - } - if(valid) { - return processRightAnswer(st); - } - } - return null; - } - - private String processRightAnswer(Statement st) { - String entity = getIdFromLink(st.getValue().toString()); - String answer = ""; - try { - ItemDocumentImpl idi = getAlreadyProcessedEntity(entity); - if(idi==null) { - idi = (ItemDocumentImpl) wbdf.getEntityDocument(entity); - answer = getName(idi.getLabels()); - addProcessedEntity(entity, idi); - } - else - answer = getName(idi.getLabels()); - } catch (MediaWikiApiErrorException | IOException e) { - - } - return answer; - } - - private String getAnswer(String id) throws Exception{ + private QuestionType type; + + public AbstractAnswersAreEntites(String propertyId, QuestionType type, String message) { + super(propertyId, type, message); + this.type = type; + } + + private String getAnswer(String id) throws Exception{ ItemDocumentImpl idi = getAlreadyProcessedEntity(id); if(idi==null) { @@ -87,20 +40,14 @@ private String getAnswer(String id) throws Exception{ return null; } } - return getRightAnswer(idi.getJsonClaims()); + return getRightAnswer(idi.getJsonClaims(), getPropertyId()); } - - protected String getIdFromLink(String url) { - String[] split1 = url.split(" "); - String[] split2 = split1[0].split("/"); - return split2[split2.length-1]; - } - + @Override - protected List getWrongAnswers(String rightAnswer) throws Exception { + public List getWrongAnswers(String rightAnswer) throws Exception { List entites = new ArrayList<>(); try { - entites = EntityGenerator.getEntities(type, 100); + entites = EntityGenerator.getEntities(type, getSampleSize(), getPropertyId()); } catch (IOException e) { e.printStackTrace(); } @@ -117,5 +64,38 @@ protected List getWrongAnswers(String rightAnswer) throws Exception { } return result; } + + /** + * Since most of the implementations do not require to decorate the answer, this general one + * simply returns the value passed to it as a parameter + */ + @Override + public List decorateAnswers(List answers) { + return answers; + } + + protected String getIdFromLink(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + + protected String processRightAnswer(Statement st) { + String entity = getIdFromLink(st.getValue().toString()); + String answer = ""; + try { + ItemDocumentImpl idi = getAlreadyProcessedEntity(entity); + if(idi==null) { + idi = (ItemDocumentImpl) wbdf.getEntityDocument(entity); + answer = getName(idi.getLabels()); + addProcessedEntity(entity, idi); + } + else + answer = getName(idi.getLabels()); + } catch (MediaWikiApiErrorException | IOException e) { + + } + return answer; + } } diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/AnswersAreEntitiesWithSubProperties.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/AnswersAreEntitiesWithSubProperties.java new file mode 100644 index 00000000..e1a2badc --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/AnswersAreEntitiesWithSubProperties.java @@ -0,0 +1,48 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Snak; +import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; +import org.wikidata.wdtk.datamodel.interfaces.Statement; + +import main.java.questionGenerator.generator.answersAreEntites.AbstractAnswersAreEntites; +import main.java.questionGenerator.question.QuestionType; + +public abstract class AnswersAreEntitiesWithSubProperties extends AbstractAnswersAreEntites { + + private final String PROPERTY_TO_CHECK; + + public AnswersAreEntitiesWithSubProperties(String propertyId, QuestionType type, String propertyToCheck, String message) { + super(propertyId, type, message); + this.PROPERTY_TO_CHECK = propertyToCheck; + } + + @Override + public String getRightAnswer(Map> claims, String propertyId) throws Exception { + if(claims.get(propertyId)==null) { + throw new Exception("Claims does not have the property " + propertyId); + } + + for(Statement st : claims.get(propertyId)) { + boolean valid = true; + for(SnakGroup sg : st.getQualifiers()) { + for(Snak s : sg.getSnaks()) { + String value = getIdFromLink(s.getPropertyId().toString()); + if(value.equals(PROPERTY_TO_CHECK)) { + valid = false; + break; + } + } + if(!valid) + break; + } + if(valid) { + return processRightAnswer(st); + } + } + return null; + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/CapitalGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/CapitalGenerator.java new file mode 100644 index 00000000..c50a19f8 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/CapitalGenerator.java @@ -0,0 +1,24 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class CapitalGenerator extends AnswersAreEntitiesWithSubProperties { + + private final static String PROPERTY = "P36"; + private final static String PROPERTY_TO_CHECK = "P582"; + private final static String MESSAGE = "question.capital"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new CapitalGenerator(); + return generator; + } + + private CapitalGenerator(){ + super(PROPERTY, QuestionType.CAPITAL, PROPERTY_TO_CHECK, MESSAGE); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/HeadOfGovernmentGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/HeadOfGovernmentGenerator.java new file mode 100644 index 00000000..e0f96b7e --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/HeadOfGovernmentGenerator.java @@ -0,0 +1,24 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class HeadOfGovernmentGenerator extends AnswersAreEntitiesWithSubProperties { + + private final static String PROPERTY = "P6"; + private final static String PROPERTY_TO_CHECK = "P582"; + private final static String MESSAGE = "question.governmentHead"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new HeadOfGovernmentGenerator(); + return generator; + } + + private HeadOfGovernmentGenerator() { + super(PROPERTY, QuestionType.HEAD_OF_GOVERMENT, PROPERTY_TO_CHECK, MESSAGE); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/LanguageGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/LanguageGenerator.java new file mode 100644 index 00000000..8d1df7ef --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/LanguageGenerator.java @@ -0,0 +1,34 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties; + +import java.util.List; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.formatAnswers.CapitalLetersFormater; + +public class LanguageGenerator extends AnswersAreEntitiesWithSubProperties { + + private final static String PROPERTY = "P37"; + private final static String PROPERTY_TO_CHECK = "P518"; + private final static String MESSAGE = "question.language"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new LanguageGenerator(); + return generator; + } + + private LanguageGenerator(){ + super(PROPERTY, QuestionType.LANGUAGE, PROPERTY_TO_CHECK, MESSAGE); + } + + @Override + public List decorateAnswers(List answers) { + AnswerFormater formater = new CapitalLetersFormater(null); + return formater.format(answers); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/AbstractVideogameGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/AbstractVideogameGenerator.java new file mode 100644 index 00000000..cfd8cf83 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/AbstractVideogameGenerator.java @@ -0,0 +1,26 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; + +import main.java.questionGenerator.generator.answersAreEntites.withSubProperties.AnswersAreEntitiesWithSubProperties; +import main.java.questionGenerator.question.QuestionType; + +public abstract class AbstractVideogameGenerator extends AnswersAreEntitiesWithSubProperties{ + + private String realProperty; + + public AbstractVideogameGenerator(String propertyId, QuestionType type, String propertyToCheck, + String message, String realProperty) { + super(propertyId, type, propertyToCheck, message); + this.realProperty = realProperty; + } + + @Override + public String getRightAnswer(Map> claims, String propertyId) throws Exception { + return super.getRightAnswer(claims, realProperty); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameCountryGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameCountryGenerator.java new file mode 100644 index 00000000..beca47d7 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameCountryGenerator.java @@ -0,0 +1,25 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class VideogameCountryGenerator extends AbstractVideogameGenerator { + + private static final String PROPERTY = "P404"; + private static final String PROPERTY_TO_CHECK = "P2868"; + private static final String MESSAGE = "question.videogame.country"; + private static final String REAL_PROPERTY = "P495"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new VideogameCountryGenerator(); + return generator; + } + + private VideogameCountryGenerator() { + super(PROPERTY, QuestionType.VIDEOGAME_COUNTRY, PROPERTY_TO_CHECK, MESSAGE, REAL_PROPERTY); + } + +} \ No newline at end of file diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameDeveloperGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameDeveloperGenerator.java new file mode 100644 index 00000000..4c541ee5 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameDeveloperGenerator.java @@ -0,0 +1,26 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class VideogameDeveloperGenerator extends AbstractVideogameGenerator { + + private static final String PROPERTY = "P404"; + private static final String PROPERTY_TO_CHECK = "P2868"; + private static final String MESSAGE = "question.videogame.developer"; + private static final String REAL_PROPERTY = "P178"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new VideogameDeveloperGenerator(); + return generator; + } + + private VideogameDeveloperGenerator() { + super(PROPERTY, QuestionType.VIDEOGAME_DEVELOPER, PROPERTY_TO_CHECK, MESSAGE, REAL_PROPERTY); + } + + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameGenreGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameGenreGenerator.java new file mode 100644 index 00000000..b9a34210 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogameGenreGenerator.java @@ -0,0 +1,34 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames; + +import java.util.List; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.formatAnswers.CapitalLetersFormater;; + +public class VideogameGenreGenerator extends AbstractVideogameGenerator { + + private static final String PROPERTY = "P404"; + private static final String PROPERTY_TO_CHECK = "P2868"; + private static final String MESSAGE = "question.videogame.genre"; + private static final String REAL_PROPERTY = "P136"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new VideogameGenreGenerator(); + return generator; + } + + private VideogameGenreGenerator() { + super(PROPERTY, QuestionType.VIDEOGAME_DEVELOPER, PROPERTY_TO_CHECK, MESSAGE, REAL_PROPERTY); + } + + @Override + public List decorateAnswers(List answers) { + AnswerFormater formater = new CapitalLetersFormater(null); + return formater.format(answers); + } +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogamePublisherGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogamePublisherGenerator.java new file mode 100644 index 00000000..68689608 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withSubProperties/videogames/VideogamePublisherGenerator.java @@ -0,0 +1,24 @@ +package main.java.questionGenerator.generator.answersAreEntites.withSubProperties.videogames; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class VideogamePublisherGenerator extends AbstractVideogameGenerator { + + private static final String PROPERTY = "P404"; + private static final String PROPERTY_TO_CHECK = "P2868"; + private static final String MESSAGE = "question.videogame.publisher"; + private static final String REAL_PROPERTY = "P123"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new VideogamePublisherGenerator(); + return generator; + } + + private VideogamePublisherGenerator() { + super(PROPERTY, QuestionType.VIDEOGAME_PUBLISHER, PROPERTY_TO_CHECK, MESSAGE, REAL_PROPERTY); + } +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/AnswersAreEntitiesWithoutSubProperties.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/AnswersAreEntitiesWithoutSubProperties.java new file mode 100644 index 00000000..732befe4 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/AnswersAreEntitiesWithoutSubProperties.java @@ -0,0 +1,37 @@ +package main.java.questionGenerator.generator.answersAreEntites.withoutSubProperties; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; + +import main.java.questionGenerator.generator.answersAreEntites.AbstractAnswersAreEntites; +import main.java.questionGenerator.question.QuestionType; + +public abstract class AnswersAreEntitiesWithoutSubProperties extends AbstractAnswersAreEntites { + + private static final String INSTANCE_OF_PROPERTY = "P31"; + + public AnswersAreEntitiesWithoutSubProperties(String propertyId, QuestionType type, String message) { + super(propertyId, type, message); + } + + @Override + public String getRightAnswer(Map> claims, String propertyId) throws Exception { + if(claims.get(propertyId)==null) { + throw new Exception("Claims does not have the property " + propertyId); + } + + List stms = claims.get(propertyId); + Statement stm = stms.get(stms.size()-1); + return processRightAnswer(stm); + } + + @Override + public String getQuestion(String name, Map> claims) { + String value = processRightAnswer(claims.get(INSTANCE_OF_PROPERTY).get(0)); + String q = getMessages().getString(getMessage()); + return String.format(q, value, name); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/DirectorGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/DirectorGenerator.java new file mode 100644 index 00000000..65977fab --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreEntites/withoutSubProperties/DirectorGenerator.java @@ -0,0 +1,23 @@ +package main.java.questionGenerator.generator.answersAreEntites.withoutSubProperties; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; + +public class DirectorGenerator extends AnswersAreEntitiesWithoutSubProperties { + + private final static String PROPERTY = "P57"; + private final static String MESSAGE = "question.director"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new DirectorGenerator(); + return generator; + } + + private DirectorGenerator() { + super(PROPERTY, QuestionType.DIRECTOR, MESSAGE); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/AnswersAreNotEntites.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/AnswersAreNotEntites.java new file mode 100644 index 00000000..70d16c01 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/AnswersAreNotEntites.java @@ -0,0 +1,52 @@ +package main.java.questionGenerator.generator.answersAreNotEntites; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import main.java.questionGenerator.generator.AbstractGenerator; +import main.java.questionGenerator.question.QuestionType; + +public abstract class AnswersAreNotEntites extends AbstractGenerator{ + + public AnswersAreNotEntites(String propertyId, QuestionType type, String message) { + super(propertyId, type, message); + } + + @Override + public List getWrongAnswers(String rightAnswer) { + float fnumber = 0; + // Check if it is a float + try { + fnumber = Float.parseFloat(rightAnswer); + } catch(NumberFormatException e1) { + } + + List wrongAnswers = new ArrayList<>(); + Random rnd = new Random(); + + // Gives values depending on parameter with percentage + // Example: If parameter is 50 value range is number*.5 and number*1.5 + int parameter = 50; + + + + //for float values + for(int i = 0; i < 3; i++){ + float wrong = (fnumber * (100 + rnd.nextInt(parameter * 2 + 1) - parameter) / 100); + // An extra check for the wrong answer to be same sign. + if((fnumber > 0 && wrong < 0) || (fnumber < 0 && wrong > 0)) + { + i--; + continue; + } + // Checking if it creates the same answer as any other + if(wrong == fnumber || wrongAnswers.contains(String.valueOf(wrong))) + i--; + else + wrongAnswers.add(String.valueOf(wrong)); + } + + return wrongAnswers; + } +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/PopulationGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/PopulationGenerator.java new file mode 100644 index 00000000..889aee7f --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/PopulationGenerator.java @@ -0,0 +1,54 @@ +package main.java.questionGenerator.generator.answersAreNotEntites; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.formatAnswers.EmbellishNumbersFormater; + +public class PopulationGenerator extends AnswersAreNotEntites { + + private final static String PROPERTY = "P1082"; + private final static String MESSAGE = "question.population"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new PopulationGenerator(); + return generator; + } + + private PopulationGenerator(){ + super(PROPERTY, QuestionType.POPULATION, MESSAGE); + } + + @Override + public String getRightAnswer(Map> claims, String propertyId) { + List statements = claims.get(PROPERTY); + Value v = statements.get(statements.size()-1).getValue(); + return v.toString(); + } + + @Override + public List decorateAnswers(List answers) { + AnswerFormater formater = new EmbellishNumbersFormater(null); + return formater.format(answers); + } + + @Override + public List getWrongAnswers(String rightAnswer) { + List original = super.getWrongAnswers(rightAnswer); + List result = new ArrayList<>(); + for(String s : original) { + result.add(String.valueOf((int) Float.parseFloat(s))); + } + return result; + } +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java new file mode 100644 index 00000000..3e321036 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/generator/answersAreNotEntites/SizeGenerator.java @@ -0,0 +1,52 @@ +package main.java.questionGenerator.generator.answersAreNotEntites; + +import java.util.List; +import java.util.Map; + +import org.wikidata.wdtk.datamodel.interfaces.Statement; +import org.wikidata.wdtk.datamodel.interfaces.Value; + +import main.java.questionGenerator.generator.Generator; +import main.java.questionGenerator.question.QuestionType; +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.formatAnswers.AddUnitsFormater; +import main.java.questionGenerator.question.answers.formatAnswers.EmbellishNumbersFormater; +import main.java.questionGenerator.question.answers.formatAnswers.RemoveEFromNumber; + +public class SizeGenerator extends AnswersAreNotEntites { + + private final static String PROPERTY = "P2046"; + private final static String MESSAGE = "question.size"; + + private static Generator generator; + + public static Generator getInstance() { + if(generator==null) + generator = new SizeGenerator(); + return generator; + } + + private SizeGenerator() { + super(PROPERTY, QuestionType.SIZE, MESSAGE); + } + + @Override + public String getRightAnswer(Map> claims, String propertyId) { + Value v = claims.get(PROPERTY).get(0).getValue(); + return getRightAnswerEntity(v.toString()); + } + + private String getRightAnswerEntity(String url) { + String[] split1 = url.split(" "); + String[] split2 = split1[0].split("/"); + return split2[split2.length-1]; + } + + @Override + public List decorateAnswers(List answers) { + AnswerFormater formater = new RemoveEFromNumber(new EmbellishNumbersFormater( + new AddUnitsFormater(null, "km^2"))); + return formater.format(answers); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/CapitalGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/CapitalGenerator.java deleted file mode 100644 index 64444133..00000000 --- a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/CapitalGenerator.java +++ /dev/null @@ -1,16 +0,0 @@ -package main.java.questionGenerator.generator.specificGenerators; - -import main.java.questionGenerator.generator.AnswersAreEntities; -import main.java.questionGenerator.question.QuestionType; - -public class CapitalGenerator extends AnswersAreEntities { - - private final static String PROPERTY = "P36"; - private final static String PROPERTY_TO_CHECK = "P582"; - private final static String MESSAGE = "question.capital"; - - public CapitalGenerator(){ - super(PROPERTY, QuestionType.CAPITAL, PROPERTY_TO_CHECK, MESSAGE); - } - -} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/LanguageGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/LanguageGenerator.java deleted file mode 100644 index 5e2b7781..00000000 --- a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/LanguageGenerator.java +++ /dev/null @@ -1,16 +0,0 @@ -package main.java.questionGenerator.generator.specificGenerators; - -import main.java.questionGenerator.generator.AnswersAreEntities; -import main.java.questionGenerator.question.QuestionType; - -public class LanguageGenerator extends AnswersAreEntities { - - private final static String PROPERTY = "P37"; - private final static String PROPERTY_TO_CHECK = "P518"; - private final static String MESSAGE = "question.language"; - - public LanguageGenerator(){ - super(PROPERTY, QuestionType.LANGUAGE, PROPERTY_TO_CHECK, MESSAGE); - } - -} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/PopulationGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/PopulationGenerator.java deleted file mode 100644 index b1feb771..00000000 --- a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/PopulationGenerator.java +++ /dev/null @@ -1,27 +0,0 @@ -package main.java.questionGenerator.generator.specificGenerators; - -import java.util.List; -import java.util.Map; - -import org.wikidata.wdtk.datamodel.interfaces.Statement; -import org.wikidata.wdtk.datamodel.interfaces.Value; - -import main.java.questionGenerator.generator.AnswersAreNotEntites; -import main.java.questionGenerator.question.QuestionType; - -public class PopulationGenerator extends AnswersAreNotEntites { - - private final static String PROPERTY = "P1082"; - private final static String MESSAGE = "question.population"; - - public PopulationGenerator(){ - super(PROPERTY, QuestionType.POPULATION, MESSAGE); - } - - @Override - protected String getRightAnswer(Map> claims) { - List statements = claims.get(PROPERTY); - Value v = statements.get(statements.size()-1).getValue(); - return v.toString(); - } -} diff --git a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/SizeGenerator.java b/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/SizeGenerator.java deleted file mode 100644 index 2259c13e..00000000 --- a/questionGenerator/src/main/java/questionGenerator/generator/specificGenerators/SizeGenerator.java +++ /dev/null @@ -1,33 +0,0 @@ -package main.java.questionGenerator.generator.specificGenerators; - -import java.util.List; -import java.util.Map; - -import org.wikidata.wdtk.datamodel.interfaces.Statement; -import org.wikidata.wdtk.datamodel.interfaces.Value; - -import main.java.questionGenerator.generator.AnswersAreNotEntites; -import main.java.questionGenerator.question.QuestionType; - -public class SizeGenerator extends AnswersAreNotEntites { - - private final static String PROPERTY = "P2046"; - private final static String MESSAGE = "question.size"; - - public SizeGenerator() { - super(PROPERTY, QuestionType.SIZE, MESSAGE); - } - - @Override - protected String getRightAnswer(Map> claims) { - Value v = claims.get(PROPERTY).get(0).getValue(); - return getRightAnswerEntity(v.toString()); - } - - private String getRightAnswerEntity(String url) { - String[] split1 = url.split(" "); - String[] split2 = split1[0].split("/"); - return split2[split2.length-1]; - } - -} diff --git a/questionGenerator/src/main/java/questionGenerator/question/Question.java b/questionGenerator/src/main/java/questionGenerator/question/Question.java index 900e3b49..7068cbd6 100644 --- a/questionGenerator/src/main/java/questionGenerator/question/Question.java +++ b/questionGenerator/src/main/java/questionGenerator/question/Question.java @@ -9,6 +9,7 @@ public class Question { private String question; private List answers; + private int number = -1; private String language; private QuestionType type; @@ -43,12 +44,16 @@ public void addRightAnswer(String answer) { answers.add(0, answer); } + public void setNumber(int number){ + this.number = number; + } public JSONObject getJSON() { JSONObject json = new JSONObject(); json.accumulate("question", question); for(String s : answers) json.accumulate("answers", s); + if(number != -1) json.accumulate("number", number); //Para que los tests pasen json.accumulate("language", language); json.accumulate("type", type); return json; diff --git a/questionGenerator/src/main/java/questionGenerator/question/QuestionType.java b/questionGenerator/src/main/java/questionGenerator/question/QuestionType.java index 8f1ec42c..28bb63f0 100644 --- a/questionGenerator/src/main/java/questionGenerator/question/QuestionType.java +++ b/questionGenerator/src/main/java/questionGenerator/question/QuestionType.java @@ -2,6 +2,7 @@ public enum QuestionType { - POPULATION, CAPITAL, SIZE, LANGUAGE; + POPULATION, CAPITAL, SIZE, LANGUAGE, HEAD_OF_GOVERMENT, DIRECTOR, VIDEOGAME_DEVELOPER, + VIDEOGAME_PUBLISHER, VIDEOGAME_GENRE, VIDEOGAME_COUNTRY; } diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/AbstractFormater.java b/questionGenerator/src/main/java/questionGenerator/question/answers/AbstractFormater.java new file mode 100644 index 00000000..a977174b --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/AbstractFormater.java @@ -0,0 +1,19 @@ +package main.java.questionGenerator.question.answers; + +import java.util.List; + +public abstract class AbstractFormater implements AnswerFormater{ + + private AnswerFormater formater; + + public AbstractFormater(AnswerFormater formater){ + this.formater = formater; + } + + public List end(List answers){ + if(formater!=null) + return formater.format(answers); + return answers; + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/AnswerFormater.java b/questionGenerator/src/main/java/questionGenerator/question/answers/AnswerFormater.java new file mode 100644 index 00000000..f320907d --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/AnswerFormater.java @@ -0,0 +1,11 @@ +package main.java.questionGenerator.question.answers; + +import java.util.List; + +public interface AnswerFormater { + + List format(List answers); + + List end(List answers); + +} diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/AddUnitsFormater.java b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/AddUnitsFormater.java new file mode 100644 index 00000000..899fe9ae --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/AddUnitsFormater.java @@ -0,0 +1,26 @@ +package main.java.questionGenerator.question.answers.formatAnswers; + +import java.util.ArrayList; +import java.util.List; + +import main.java.questionGenerator.question.answers.AbstractFormater; +import main.java.questionGenerator.question.answers.AnswerFormater; + +public class AddUnitsFormater extends AbstractFormater { + + private String unit; + + public AddUnitsFormater(AnswerFormater formater, String unit) { + super(formater); + this.unit = unit; + } + + @Override + public List format(List answers) { + List result = new ArrayList(); + for(String s : answers) + result.add(s +" " + unit); + return end(result); + } + +} diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/CapitalLetersFormater.java b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/CapitalLetersFormater.java new file mode 100644 index 00000000..156c867b --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/CapitalLetersFormater.java @@ -0,0 +1,32 @@ +package main.java.questionGenerator.question.answers.formatAnswers; + +import java.util.ArrayList; +import java.util.List; + +import main.java.questionGenerator.question.answers.AbstractFormater; +import main.java.questionGenerator.question.answers.AnswerFormater; + +public class CapitalLetersFormater extends AbstractFormater { + + public CapitalLetersFormater(AnswerFormater formater) { + super(formater); + } + + @Override + public List format(List answers) { + List result = new ArrayList<>(); + for(String s : answers) { + result.add(toUppercaseFirstCharacter(s)); + } + return end(result); + + } + + private String toUppercaseFirstCharacter(String string) { + char[] chars = string.toCharArray(); + chars[0] = Character.toUpperCase(chars[0]); + String result = String.copyValueOf(chars); + return result; + } + +} \ No newline at end of file diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/EmbellishNumbersFormater.java b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/EmbellishNumbersFormater.java new file mode 100644 index 00000000..d07e30e2 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/EmbellishNumbersFormater.java @@ -0,0 +1,61 @@ +package main.java.questionGenerator.question.answers.formatAnswers; + +import java.util.ArrayList; +import java.util.List; + +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.AbstractFormater; + +public class EmbellishNumbersFormater extends AbstractFormater { + + public EmbellishNumbersFormater(AnswerFormater formater) { + super(formater); + } + + @Override + public List format(List answers) { + List result = new ArrayList<>(); + for(String s : answers) { + result.add(decorateAnswer(s)); + } + return end(result); + } + + private String decorateAnswer(String answer) { + char[] chars = answer.toCharArray(); + chars = invert(chars); + Character splitter = ','; + int splitPoint = getSplitPoint(chars); + String result = ""; + int j=0; + for(int i=0; isplitPoint) { + j++; + if(j%3==0 && i=0; i--) { + result[j++] = chars[i]; + } + return result; + } + +} \ No newline at end of file diff --git a/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/RemoveEFromNumber.java b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/RemoveEFromNumber.java new file mode 100644 index 00000000..70f932f3 --- /dev/null +++ b/questionGenerator/src/main/java/questionGenerator/question/answers/formatAnswers/RemoveEFromNumber.java @@ -0,0 +1,44 @@ +package main.java.questionGenerator.question.answers.formatAnswers; + +import java.util.ArrayList; +import java.util.List; + +import main.java.questionGenerator.question.answers.AnswerFormater; +import main.java.questionGenerator.question.answers.AbstractFormater; + +public class RemoveEFromNumber extends AbstractFormater { + + public RemoveEFromNumber(AnswerFormater formater) { + super(formater); + } + + @Override + public List format(List answers) { + List result = new ArrayList<>(); + for(String s : answers) { + result.add(removeE(s)); + } + return end(result); + } + + private String removeE(String answer) { + String[] number = answer.split("E"); + if(number.length==1) + return answer; + String value = removeDecimalPoint(number[0]); + for(int i=value.length(); i questionJSONList){ return false; } } + + public boolean removeAll() { + try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) { + MongoDatabase database = mongoClient.getDatabase("questions"); + + MongoCollection collection = database.getCollection("questions"); + + collection.deleteMany(Document.parse("{}")); + return true; + } catch (Exception e) { + System.out.println(e); + return false; + } + } + + public boolean populate(List questions) { + try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) { + MongoDatabase database = mongoClient.getDatabase("questions"); + + MongoCollection collection = database.getCollection("questions"); + + collection.deleteMany(Document.parse("{}")); + + List documents = new ArrayList<>(); + for (String questionJSON : questions) { + documents.add(Document.parse(questionJSON)); + } + + collection.insertMany(documents); + + return true; + + } catch (Exception e) { + System.out.println(e); + return false; + } + + } } \ No newline at end of file diff --git a/questionGenerator/src/main/resources/messages.properties b/questionGenerator/src/main/resources/messages.properties index 780a92bd..d9837ebb 100644 --- a/questionGenerator/src/main/resources/messages.properties +++ b/questionGenerator/src/main/resources/messages.properties @@ -1,4 +1,10 @@ question.population = What's the population of %s? question.language = What's the official language of %s? question.capital = What's the capital of %s? -question.size = What's the size of %s? \ No newline at end of file +question.size = What's the size of %s? +question.governmentHead = Who's the current head of the government of %s? +question.director = Who's the director of the %s %s? +question.videogame.developer = Who were the developers of the videogame %s? +question.videogame.publisher = Who was the publisher of the videogame %s? +question.videogame.genre = What's the main genre of the videogame %s? +question.videogame.country = Which country is the videogame %s from? \ No newline at end of file diff --git a/questionGenerator/src/main/resources/messages_en.properties b/questionGenerator/src/main/resources/messages_en.properties index 780a92bd..d9837ebb 100644 --- a/questionGenerator/src/main/resources/messages_en.properties +++ b/questionGenerator/src/main/resources/messages_en.properties @@ -1,4 +1,10 @@ question.population = What's the population of %s? question.language = What's the official language of %s? question.capital = What's the capital of %s? -question.size = What's the size of %s? \ No newline at end of file +question.size = What's the size of %s? +question.governmentHead = Who's the current head of the government of %s? +question.director = Who's the director of the %s %s? +question.videogame.developer = Who were the developers of the videogame %s? +question.videogame.publisher = Who was the publisher of the videogame %s? +question.videogame.genre = What's the main genre of the videogame %s? +question.videogame.country = Which country is the videogame %s from? \ No newline at end of file diff --git a/questionGenerator/src/main/resources/messages_es.properties b/questionGenerator/src/main/resources/messages_es.properties index 839ae10d..d48a4b96 100644 --- a/questionGenerator/src/main/resources/messages_es.properties +++ b/questionGenerator/src/main/resources/messages_es.properties @@ -1,4 +1,10 @@ -question.population = ¿Cuál es la población de %s? -question.language = ¿Cuál es el idioma oficial de %s? -question.capital = ¿Cuál es la capital de %s? -question.size = ¿Cuál es el área (km cuadrados) de %s? \ No newline at end of file +question.population = ¿Cuál es la población de %s? +question.language = ¿Cuál es el idioma oficial de %s? +question.capital = ¿Cuál es la capital de %s? +question.size = ¿Cuál es el área de %s? +question.governmentHead = ¿Quién es el actual jefe de gobierno de %s? +question.director = ¿Quién es el director de la %s %s? +question.videogame.developer = ¿Quién desarolló el videojuego %s? +question.videogame.publisher = ¿Cuál fue la distribuidora del videojuego %s? +question.videogame.genre = ¿Cuál es el género principal del videojuego %s? +question.videogame.country = ¿De que país viene el videojuego %s? \ No newline at end of file diff --git a/questionGenerator/src/main/resources/messages_tr.properties b/questionGenerator/src/main/resources/messages_tr.properties new file mode 100644 index 00000000..7fe7762d --- /dev/null +++ b/questionGenerator/src/main/resources/messages_tr.properties @@ -0,0 +1,10 @@ +question.population = %s nufusu ne kadardir? +question.language = %s resmi dili nedir? +question.capital = %s baskenti neresidir? +question.size = %s buyuklugu ne kadardir? +question.governmentHead = %s baskani kimdir? +question.director = %s %s filminin direktoru kimdir? +question.videogame.developer = %s video oyunun gelistiricileri kimdir? +question.videogame.publisher = %s video oyunun yayimcilari kimdir? +question.videogame.genre = %s video oyunun turu nedir? +question.videogame.country = %s video oyunu hangi ulkeden cikmistir? \ No newline at end of file diff --git a/questionGenerator/src/test/java/questionGenerator/QuestionGeneratorTests.java b/questionGenerator/src/test/java/questionGenerator/QuestionGeneratorTests.java index 67a6aa97..fcceef44 100644 --- a/questionGenerator/src/test/java/questionGenerator/QuestionGeneratorTests.java +++ b/questionGenerator/src/test/java/questionGenerator/QuestionGeneratorTests.java @@ -13,7 +13,6 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import main.java.questionGenerator.QuestionGenerator; @@ -21,25 +20,20 @@ public class QuestionGeneratorTests { - private QuestionGenerator qgEN; - private QuestionGenerator qgES; + private QuestionGenerator qg = QuestionGenerator.getInstance(); private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE, QuestionType.LANGUAGE}; - @BeforeEach - void setUp(){ - qgEN = new QuestionGenerator("en"); - qgES = new QuestionGenerator("es"); - } - @Test void testGenerateQuestionsEnglish() { String question; + qg.setLanguageCode("en"); + for(QuestionType t : types) { for(int i=0; i<3; i++) { - question = qgEN.generateQuestions(t, 1).get(0).getJSON().toString(); + question = qg.generateQuestions(t, 1).get(0).getJSON().toString(); JSONObject json = null; //Check correct format @@ -96,9 +90,11 @@ void testGenerateQuestionsSpanish() { String question; + qg.setLanguageCode("es"); + for(QuestionType t : types) { for(int i=0; i<3; i++) { - question = qgES.generateQuestions(t, 1).get(0).getJSON().toString(); + question = qg.generateQuestions(t, 1).get(0).getJSON().toString(); JSONObject json = null; //Check correct format diff --git a/questionGenerator/src/test/java/questionGenerator/generator/CapitalGeneratorTests.java b/questionGenerator/src/test/java/questionGenerator/generator/CapitalGeneratorTests.java new file mode 100644 index 00000000..9403bed4 --- /dev/null +++ b/questionGenerator/src/test/java/questionGenerator/generator/CapitalGeneratorTests.java @@ -0,0 +1,102 @@ +package test.java.questionGenerator.generator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import main.java.questionGenerator.QuestionGenerator; +import main.java.questionGenerator.question.Question; +import main.java.questionGenerator.question.QuestionType; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class CapitalGeneratorTests { + + private QuestionGenerator qg = QuestionGenerator.getInstance(); + private List questions = qg.generateQuestions(QuestionType.CAPITAL, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("What's the capital of ")); + assertTrue(q.getQuestion().endsWith("?")); + } + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.DIRECTOR, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("Who's the director of the ")); + assertTrue(q.getQuestion().endsWith("?")); + } + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.HEAD_OF_GOVERMENT, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("Who's the current head of the government of ")); + assertTrue(q.getQuestion().endsWith("?")); + } + + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.LANGUAGE, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("What's the official language of ")); + assertTrue(q.getQuestion().endsWith("?")); + } + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.POPULATION, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("What's the population of ")); + assertTrue(q.getQuestion().endsWith("?")); + } + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.SIZE, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("What's the size of ")); + assertTrue(q.getQuestion().endsWith("?")); + } + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.VIDEOGAME_COUNTRY, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("Which country is the videogame ")); + assertTrue(q.getQuestion().endsWith(" from?")); + } + + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.VIDEOGAME_DEVELOPER, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("Who were the developers of the videogame ")); + assertTrue(q.getQuestion().endsWith("?")); + } + + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.VIDEOGAME_GENRE, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("What's the main genre of the videogame ")); + assertTrue(q.getQuestion().endsWith("?")); + } + + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i questions = qg.generateQuestions(QuestionType.VIDEOGAME_PUBLISHER, 3); + + @Test + @Order(1) + public void AmountOfQuestions() { + assertTrue(questions.size()<=3); + } + + @Test + @Order(2) + public void QuestionsAreGeneratedTest() { + assertNotNull(questions); + assertNotEquals(List.of(questions), questions); + for(Question q : questions) { + assertNotNull(q); + } + } + + @Test + @Order(3) + public void AllQuestionsAreDifferentTest() { + List messages = new ArrayList(); + for(Question q : questions) { + String question = q.getQuestion(); + assertFalse(messages.contains(question)); + messages.add(question); + } + + assertEquals(questions.size(), messages.size()); + } + + @Test + @Order(4) + public void AllAnswersInAQuestionAreDifferent() { + for(Question q : questions) { + assertFalse(q.getAnswers().isEmpty()); + List answers = new ArrayList(); + for(String answer : q.getAnswers()) { + assertFalse(answers.contains(answer)); + answers.add(answer); + } + assertEquals(q.getAnswers().size(), answers.size()); + } + } + + @Test + @Order(5) + public void TheQuestionFollowsTheExpectedMessage() { + for(Question q : questions) { + assertTrue(q.getQuestion().startsWith("Who was the publisher of the videogame ")); + assertTrue(q.getQuestion().endsWith("?")); + } + + } + + @Test + @Order(6) + public void QuestionInTheJSONIsTheExpectedOne() { + for(Question q : questions) { + String expectedQuestion = q.getQuestion(); + String actualQuestion = q.getJSON().get("question").toString(); + assertEquals(expectedQuestion, actualQuestion); + } + } + + @Test + @Order(7) + public void AnswersInTheJSONAreTheExpectedOnes() { + for(Question q : questions) { + List answers = q.getAnswers(); + JSONArray actualAnswers = q.getJSON().getJSONArray("answers"); + for(int i=0; i