task_generator / core /task_factory.py
reginafeles's picture
Upload 40 files
d2213a5 verified
"""Фабрика для создания задач"""
from typing import List, Dict, Any
from models.settings import GenerationSettings
from models.task import Task
from config import TASK_TYPE_MAPPING, MAX_TASKS_PER_REQUEST
from core.validators import ProbabilityValidator
import random
# Импорт всех генераторов
from generators.classic.exam import ExamTicketsGenerator
from generators.classic.mixed import MixedItemsGenerator
from generators.classic.transport import TransportGenerator
from generators.classic.athletes import AthletesGenerator
from generators.classic.defective import DefectiveItemsGenerator
from generators.classic.dice import DiceGenerator
from generators.classic.coins import CoinsGenerator
from generators.classic.conferences import ConferenceGenerator
from generators.classic.paired_games import PairedGamesGenerator
from generators.classic.ratios import RatiosGenerator
from generators.classic.clock import ClockGenerator
from generators.classic.tv import TVChannelsGenerator
from generators.theorems.complementary import ComplementaryGenerator
from generators.theorems.incompatible import IncompatibleGenerator
from generators.theorems.independent import IndependentGenerator
from generators.theorems.total_prob import TotalProbabilityGenerator
from generators.theorems.sequences import SequencesGenerator
from generators.theorems.exact_count import ExactCountGenerator
from generators.theorems.chess import ChessMastersGenerator
class TaskFactory:
"""Фабрика для создания задач по теории вероятности"""
def __init__(self):
self.generators = {}
self._initialize_generators()
def _initialize_generators(self):
"""Инициализация всех генераторов"""
self.generators = {
'exam_tickets': ExamTicketsGenerator,
'mixed_items': MixedItemsGenerator,
'transport': TransportGenerator,
'athletes': AthletesGenerator,
'defective_items': DefectiveItemsGenerator,
'dice': DiceGenerator,
'coins': CoinsGenerator,
'conference': ConferenceGenerator,
'paired_games': PairedGamesGenerator,
'ratios': RatiosGenerator,
'clock': ClockGenerator,
'tv_channels': TVChannelsGenerator,
'complementary': ComplementaryGenerator,
'incompatible': IncompatibleGenerator,
'independent': IndependentGenerator,
'total_probability': TotalProbabilityGenerator,
'sequences': SequencesGenerator,
'exact_count': ExactCountGenerator,
'chess_masters': ChessMastersGenerator
}
def generate_tasks(self, settings: GenerationSettings) -> List[Dict[str, Any]]:
"""
Генерация списка задач
Аргументы:
settings: настройки генерации
Возвращает:
список задач в формате словаря
"""
tasks = []
count = min(settings.count, MAX_TASKS_PER_REQUEST)
if not settings.task_types:
available_types = list(self.generators.keys())
else:
available_types = [TASK_TYPE_MAPPING[t] for t in settings.task_types if t in TASK_TYPE_MAPPING]
if not available_types:
available_types = list(self.generators.keys())
generated_count = 0
max_attempts = count * 10
attempts = 0
while generated_count < count and attempts < max_attempts:
attempts += 1
task_type = random.choice(available_types)
generator_class = self.generators[task_type]
generator = generator_class(settings.complexity)
task = generator.generate()
task_dict = task.to_dict()
if not ProbabilityValidator.validate_task(task_dict):
continue
tasks.append(task_dict)
generated_count += 1
return tasks
def get_all_task_types(self) -> Dict[str, List[str]]:
"""Получение всех доступных типов задач"""
return {
'classic': [
'exam_tickets', 'mixed_items', 'transport', 'athletes',
'defective_items', 'dice', 'coins', 'conference',
'paired_games', 'ratios', 'clock', 'tv_channels'
],
'theorems': [
'complementary', 'incompatible', 'independent',
'total_probability', 'sequences',
'exact_count', 'chess_masters'
]
}