Skip to content

Commit

Permalink
Merge branch 'develop' into unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori authored Apr 5, 2024
2 parents ce834c7 + 62477aa commit 4a5a22f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
5 changes: 0 additions & 5 deletions src/main/java/com/uniovi/WiqEs04bApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.uniovi;

import jakarta.persistence.Persistence;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class WiqEs04bApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
import com.uniovi.services.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
Expand All @@ -18,11 +14,14 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public abstract class AbstractQuestionGenerator implements QuestionGenerator{
private List<Question> questions = new ArrayList<>();
protected final CategoryService categoryService;
private String query;

protected Random random = new Random();

protected String statement;
protected String language;

Expand Down Expand Up @@ -51,7 +50,7 @@ public List<Question> getQuestions() {
try {

String endpointUrl = "https://query.wikidata.org/sparql?query=" +
URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8.toString()) +
URLEncoder.encode(this.getQuery(), StandardCharsets.UTF_8) +
"&format=json";

HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -72,12 +71,14 @@ public List<Question> getQuestions() {

List<String> options = this.generateOptions(resultsNode, result);
String correctAnswer = this.generateCorrectAnswer(result);
String statement = this.getQuestionSubject(result);
questionGenerator(statement, options, correctAnswer, this.getCategory());
String questionStatement = this.getQuestionSubject(result);
questionGenerator(questionStatement, options, correctAnswer, this.getCategory());

}
} catch (InterruptedException e) {
throw new QuestionGeneratorException("Generation of questions was interrupted");
} catch (Exception e) {
throw new RuntimeException(e);
throw new QuestionGeneratorException("An error occurred while generating questions");
}

return questions;
Expand All @@ -87,4 +88,10 @@ public List<Question> getQuestions() {
protected abstract String generateCorrectAnswer(JsonNode result);

protected abstract String getQuestionSubject(JsonNode result);

private static class QuestionGeneratorException extends RuntimeException {
public QuestionGeneratorException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BorderQuestionGenerator extends AbstractGeographyGenerator{
{
put("en", "Which countries share a border with ");
put("es", "¿Con qué países comparte frontera ");
put("fr", "Avec quels pays partage-t-il une frontière ");
}
};
private Set<String> usedCountries = new HashSet<>();
Expand All @@ -33,7 +34,6 @@ private List<String> getAllBorderingCountries(JsonNode resultsNode, String corre

private List<String> selectRandomIncorrectBorderingCountries(List<String> allBorderingCountries, String correctCountry, int count) {
List<String> incorrectBorderingCountries = new ArrayList<>();
Random random = new Random();
while (incorrectBorderingCountries.size() < count && allBorderingCountries.size() > 0) {
int randomIndex = random.nextInt(allBorderingCountries.size());
String selectedBorderingCountry = allBorderingCountries.remove(randomIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CapitalQuestionGenerator extends AbstractGeographyGenerator{
{
put("en", "What is the capital of ");
put("es", "¿Cuál es la capital de ");
put("fr", "Quelle est la capitale de ");
}
};

Expand Down Expand Up @@ -48,7 +49,6 @@ private List<String> getAllCapitals(JsonNode resultsNode, String correctCapital)

private List<String> selectRandomIncorrectCapitals(List<String> allCapitals, String correctCapital, int count) {
List<String> incorrectCapitals = new ArrayList<>();
Random random = new Random();
while (incorrectCapitals.size() < count && allCapitals.size() > 0) {
int randomIndex = random.nextInt(allCapitals.size());
String selectedCapital = allCapitals.remove(randomIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ContinentQuestionGeneration extends AbstractGeographyGenerator{
{
put("en", "In which continent is ");
put("es", "¿En qué continente se encuentra ");
put("fr", "Sur quel continent est-il situé ");
}
};

Expand All @@ -34,7 +35,6 @@ private List<String> getAllContinents(JsonNode resultsNode, String correctContin

private List<String> selectRandomIncorrectContinents(List<String> allContinents, String correctContinent, int count) {
List<String> incorrectContinents = new ArrayList<>();
Random random = new Random();
while (incorrectContinents.size() < count && allContinents.size() > 0) {
int randomIndex = random.nextInt(allContinents.size());
String selectedCapital = allContinents.remove(randomIndex);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/uniovi/entities/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
public class Question implements JsonEntity {
public static final String ENGLISH = "en";
public static final String SPANISH = "es";
public static final String FRENCH = "fr";


@Id
@GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void generateSampleData() {
List<Question> questionsEs = allQuestionGenerator.getQuestions();
questionsEs.forEach(questionService::addNewQuestion);

allQuestionGenerator = new MultipleQuestionGenerator(
new ContinentQuestionGeneration(categoryService, Question.FRENCH),
new CapitalQuestionGenerator(categoryService, Question.FRENCH),
new BorderQuestionGenerator(categoryService, Question.FRENCH)
);
List<Question> questionsFr = allQuestionGenerator.getQuestions();
questionsFr.forEach(questionService::addNewQuestion);

log.info("Sample questions inserted");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@Service
public class GameSessionImpl implements GameSessionService {
public static final Integer NORMAL_GAME_QUESTION_NUM = 20;
public static final Integer NORMAL_GAME_QUESTION_NUM = 4;

private final GameSessionRepository gameSessionRepository;
private final QuestionService questionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Service
public class QuestionServiceImpl implements QuestionService {

private final QuestionRepository questionRepository;
private final Random random = new Random();

public QuestionServiceImpl(QuestionRepository questionRepository) {
this.questionRepository = questionRepository;
Expand Down Expand Up @@ -47,9 +49,9 @@ public List<Question> getRandomQuestions(int num) {
.filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList();
List<Question> res = new ArrayList<>();
for (int i = 0; i < num; i++) {
int idx = (int) (Math.random() * allQuestions.size());
int idx = random.nextInt(allQuestions.size());
while (allQuestions.get(idx).hasEmptyOptions()){
idx = (int) (Math.random() * allQuestions.size());
idx = random.nextInt(allQuestions.size());
}
res.add(allQuestions.get(idx));
}
Expand Down

0 comments on commit 4a5a22f

Please sign in to comment.