File size: 4,703 Bytes
d2213a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Фабрика для создания задач"""
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'
            ]
        }