From c6a0418933ac744a5800074828661c1f5acccc71 Mon Sep 17 00:00:00 2001 From: vovademar Date: Fri, 23 Jun 2023 02:08:27 +0700 Subject: [PATCH] impl --- .../kotlin/nsu/algos/entities/TimetableOut.kt | 37 ++- .../kotlin/nsu/algos/service/AlgoMaker.kt | 4 +- .../nsu/algos/service/impl/AlgoMakerImpl.kt | 287 ++++++------------ 3 files changed, 125 insertions(+), 203 deletions(-) diff --git a/src/main/kotlin/nsu/algos/entities/TimetableOut.kt b/src/main/kotlin/nsu/algos/entities/TimetableOut.kt index cdda763..957824e 100644 --- a/src/main/kotlin/nsu/algos/entities/TimetableOut.kt +++ b/src/main/kotlin/nsu/algos/entities/TimetableOut.kt @@ -1,6 +1,35 @@ package nsu.algos.entities -data class TimetableOut( - val day: Int, val hour: Int, val roomId: Int, val roomNum: String, - val courseId: Int, val courseName: String, val teacherName: String, val groupNum: String -) +import jakarta.persistence.* + +@Entity +class TimetableOut( + + @Id + @Column(name = "TIMETABLE_ID") + @GeneratedValue(strategy = GenerationType.IDENTITY) + val timetableId: Int = 0, + + @Column(name = "day") + val day: Int, + + @Column(name = "hour") + val hour: Int, + + @Column(name = "roomNum") + val roomNum: String, + + @Column(name = "courseId") + val courseId: Int, + + @Column(name = "courseName") + val courseName: String, + + @Column(name = "teacherName") + val teacherName: String, + + @Column(name = "groupNum") + val groupNum: String +) { + constructor() : this(0, 0, 0, "", 0, "", "", "") +} diff --git a/src/main/kotlin/nsu/algos/service/AlgoMaker.kt b/src/main/kotlin/nsu/algos/service/AlgoMaker.kt index 8dba58a..b728312 100644 --- a/src/main/kotlin/nsu/algos/service/AlgoMaker.kt +++ b/src/main/kotlin/nsu/algos/service/AlgoMaker.kt @@ -1,7 +1,7 @@ package nsu.algos.service -import nsu.algos.entities.Period +import nsu.algos.entities.TimetableOut interface AlgoMaker { - fun runAlgo():List + fun runAlgo():List } \ No newline at end of file diff --git a/src/main/kotlin/nsu/algos/service/impl/AlgoMakerImpl.kt b/src/main/kotlin/nsu/algos/service/impl/AlgoMakerImpl.kt index c13b5ce..d6cd7b1 100644 --- a/src/main/kotlin/nsu/algos/service/impl/AlgoMakerImpl.kt +++ b/src/main/kotlin/nsu/algos/service/impl/AlgoMakerImpl.kt @@ -8,134 +8,89 @@ import org.springframework.stereotype.Service class AlgoMakerImpl( private val groupAlgoService: GroupAlgoService, private val courseService: CourseService, - private val periodService: PeriodService, private val roomAlgoService: RoomAlgoService, private val teacherAlgoService: TeacherAlgoService ) : AlgoMaker { -// val -// val a:Map> + val groups: List = groupAlgoService.findAll() + val teachers: List = teacherAlgoService.findAll() + val rooms: List = roomAlgoService.findAll() + val courses: List = courseService.findAll() - override fun runAlgo(): List { - TODO("Not yet implemented") - } - -} - -fun main() { - //мапа по дням недели - //>> - val daysMap: MutableMap>> = mutableMapOf( - 1 to mutableMapOf( - 1 to mutableListOf(true, false, false), - 2 to mutableListOf(true, false, false), - 3 to mutableListOf(true, false, false) - ), - 2 to mutableMapOf( - 1 to mutableListOf(false, false, false), - 2 to mutableListOf(false, false, false), - 3 to mutableListOf(false, false, false) - ), - 3 to mutableMapOf( - 1 to mutableListOf(false, false, false), - 2 to mutableListOf(false, false, false), - 3 to mutableListOf(false, false, false) - ) - ) -// testMap[1]!![1]!![1] = -// daysMap[1]!![1]!![0] = false -// println(daysMap[1]!![1]!![0]) - - // мапа по группе и свободному времени - // day groupId freeTime - - - val groupMap: MutableMap>> = mutableMapOf( - 1 to mutableMapOf( - 1 to mutableListOf(true, true, true), - 2 to mutableListOf(true, true, true), - 3 to mutableListOf(true, true, true) - ), - 2 to mutableMapOf( - 1 to mutableListOf(false, true, true), - 2 to mutableListOf(true, true, true), - 3 to mutableListOf(true, true, true) - ), - 3 to mutableMapOf( - 1 to mutableListOf(true, true, true), - 2 to mutableListOf(true, true, true), - 3 to mutableListOf(true, true, true) - ) - ) + override fun runAlgo(): List { + val availableTimeForCourse: MutableMap>> = mutableMapOf() + courses.forEach { + availableTimeForCourse[it.id] = mutableSetOf(Pair(it.day, it.hour)) + } - val teacherCourseMap: MutableMap = mutableMapOf( - 2 to 2, - 1 to 3, - 3 to 1 - ) + val roomSet:MutableSet = mutableSetOf() + for (room in rooms){ + roomSet.add(room.id.toInt()) + } + // day room courseId? + val timetable: MutableMap>> = mutableMapOf() - // day, courseId, freeTime - val courseMap: MutableMap>> = mutableMapOf( - 1 to mutableMapOf( - 1 to mutableListOf(true, true, true), - 2 to mutableListOf(true, true, true), - 3 to mutableListOf(true, true, true) - ), - 2 to mutableMapOf( - 1 to mutableListOf(true, true, false), - 2 to mutableListOf(false, true, false), - 3 to mutableListOf(true, false, true) - ), - 3 to mutableMapOf( - 1 to mutableListOf(false, true, true), - 2 to mutableListOf(false, true, true), - 3 to mutableListOf(true, true, false) - ) - ) + // groupId + val groupCourseMap: MutableMap>> = mutableMapOf() + for (i in 0 until 6) { + timetable[i] = mutableMapOf() + roomSet.forEach { timetable[i]!![it] = arrayOfNulls(5).toMutableList() } + } + for (j in availableTimeForCourse.keys.sortedBy { availableTimeForCourse[it]!!.size }) { + + val dayList: MutableList = mutableListOf() + availableTimeForCourse[j]!!.forEach { dayList.add(it.first - 1) } + val hourList: MutableList = mutableListOf() + availableTimeForCourse[j]!!.forEach { hourList.add(it.second - 1) } + + dayList.forEach { dayFromList -> + timetable[dayFromList]!!.forEach { tt -> + var t: Int? = null + var lim = 0 + hourList.forEach { hourFromList -> + if (tt.value[hourFromList] == null && lim != 1) { + timetable[dayFromList]!![tt.key]!![hourFromList] = j + t = hourFromList + + val groupTemp = courses.filter { it.id == j }.first().groupId + if (!groupCourseMap.keys.contains(groupTemp)) { + groupCourseMap[groupTemp] = mutableSetOf(Pair(dayFromList + 1, hourFromList + 1)) + lim = 1 + } + } + } + (t?.let { hourList.remove(it) }) + } + } - val intersectionMap: MutableMap = mutableMapOf() + } - for ((day, dayData) in daysMap) { - for ((roomId, freeTimeList) in dayData) { - for ((courseId, courseData) in courseMap) { - for (i in 0 until 3) { - if (courseData[roomId]!![i] == freeTimeList[i] && courseData[roomId]!![i]) { - intersectionMap[courseId] = intersectionMap.getOrDefault(courseId, 0) + 1 + var count = 0 + val timetableOut: MutableList = mutableListOf() + for ((day, timetableData) in timetable) { + for ((roomId, lesson) in timetableData) { + for(( lessonNum, courseId ) in lesson.withIndex()){ + if (courseId != null){ + val roomNum = rooms.filter { it.id.toInt() == roomId }.first().name + val courseName = courses.filter { it.id == courseId }.first().name + val teacherId = courses.filter { it.id == courseId }.first().teacherId.teacherId + val teacherName = teachers.filter { it.teacherId == teacherId }.first().name + val groupId = courses.filter { it.id == courseId }.first().groupId + val groupName = groups.filter { it.id.toInt() == groupId }.first().name + timetableOut.add(TimetableOut(count ,day, lessonNum, roomNum, courseId, courseName, teacherName, groupName)) + count += 1 } } } } + return timetableOut } -// for((day, courseData) in courseMap){ -// val freeTimeSet: MutableSet> = HashSet() -// courseData.forEach { (t, u) -> freeTimeSet.add(Pair(day, ))} -// -// } - //course day room - //println(courseMap) - //courseid, windows - - -// val availableTimeForCourseSorted = availableTimeForCourse -// -// availableTimeForCourse.keys.sortedBy { availableTimeForCourse[it]!!.size } - - // courseid - свободные окна -// val mapOfIntersection: MutableMap = mutableMapOf() -// for (i in availableTimeForCourse.keys.sortedBy { availableTimeForCourse[it]!!.size }) { -// var counter = 0 -// for (j in availableTimeForRoom.values) { -// counter += (availableTimeForCourse[i]!! intersect j).size -// } -// mapOfIntersection[i] = counter -// //println("$counter $i - counter") -// } - - //roomId +} +fun main() { val groups: MutableList = mutableListOf() groups.add(GroupAlgo(1, "20214", 13)) @@ -148,59 +103,44 @@ fun main() { teachers.add(TeacherAlgo(1, "Vlasov")) teachers.add(TeacherAlgo(2, "Vaskevich")) teachers.add(TeacherAlgo(3, "Dmitrievski")) - teachers.add(TeacherAlgo(4, "Zlyga")) + teachers.add(TeacherAlgo(4, "Zlygostev")) teachers.add(TeacherAlgo(5, "Postovalov")) val rooms: MutableList = mutableListOf() rooms.add(RoomAlgo(12, "2128", 50)) rooms.add(RoomAlgo(21, "3343", 30)) - rooms.add(RoomAlgo(31, "2128", 200)) + rooms.add(RoomAlgo(31, "3107", 200)) rooms.add(RoomAlgo(41, "1234", 24)) val courses: MutableList = mutableListOf() courses.add(Course(1, "OOP", "Lecture", teachers[0], 1, 1, 2)) - courses.add(Course(2, "Math", "Lecture", teachers[1], 2, 1, 3)) + courses.add(Course(2, "Math", "Lecture", teachers[1], 2, 1, 2)) courses.add(Course(3, "Law", "Seminar", teachers[2], 3, 2, 2)) courses.add(Course(4, "DAO", "Lecture", teachers[3], 4, 2, 3)) - courses.add(Course(5, "Aboba", "Lecture", teachers[4], 5, 2, 5)) - - val availableTimeForCourse: MutableMap>> = mutableMapOf( - 1 to mutableSetOf(Pair(1, 1), Pair(1, 2)), - 2 to mutableSetOf(Pair(3, 1), Pair(3, 3)), - 3 to mutableSetOf(Pair(2, 4), Pair(2, 3)), - 4 to mutableSetOf(Pair(4, 4), Pair(4, 3)), - 5 to mutableSetOf(Pair(5, 4)), - ) - - //generate mutable set of Pairs - val pairs: MutableSet> = mutableSetOf() -// for (i in 1 until 7) { -// for(j in 1 until 6 ){ -// pairs.add(Pair (i, j)) -// } -// } + courses.add(Course(5, "Test1", "Lecture", teachers[4], 5, 2, 3)) + courses.add(Course(6, "Test2", "Lecture", teachers[0], 5, 3, 5)) + courses.add(Course(7, "Test3", "Lecture", teachers[1], 5, 3, 5)) + courses.add(Course(8, "Test4", "Lecture", teachers[2], 5, 3, 4)) + courses.add(Course(9, "Test5", "Lecture", teachers[3], 5, 4, 5)) + courses.add(Course(10, "Test6", "Lecture", teachers[2], 5, 4, 1)) + courses.add(Course(11, "Test7", "Lecture", teachers[1], 5, 4, 1)) + courses.add(Course(12, "Test8", "Lecture", teachers[3], 5, 3, 5)) + courses.add(Course(13, "Test9", "Lecture", teachers[2], 5, 1, 5)) + courses.add(Course(14, "Test10", "Lecture", teachers[1], 5, 5, 5)) + courses.add(Course(15, "Test10", "Lecture", teachers[1], 5, 5, 5)) + + val availableTimeForCourse: MutableMap>> = mutableMapOf() - println(pairs) - // from courses to availableTimeForCourse courses.forEach { availableTimeForCourse[it.id] = mutableSetOf(Pair(it.day, it.hour)) } - println("$availableTimeForCourse - time") - - - //roomId, availableTime - val availableTimeForRoom: MutableMap>> = mutableMapOf( - 1 to mutableSetOf(Pair(1, 2), Pair(1, 2)), - 2 to mutableSetOf(Pair(3, 1), Pair(3, 3)), - 3 to mutableSetOf(Pair(2, 4), Pair(2, 3)), - 4 to mutableSetOf(Pair(4, 4), Pair(4, 3)), - 5 to mutableSetOf(Pair(5, 4)), - ) - - var roomSet = mutableSetOf(12, 21, 31, 41, 51) + val roomSet:MutableSet = mutableSetOf() + for (room in rooms){ + roomSet.add(room.id.toInt()) + } // day room courseId? - var timetable: MutableMap>> = mutableMapOf() + val timetable: MutableMap>> = mutableMapOf() // groupId val groupCourseMap: MutableMap>> = mutableMapOf() @@ -237,7 +177,7 @@ fun main() { } } - + var count = 0 val timetableOut: MutableList = mutableListOf() for ((day, timetableData) in timetable) { for ((roomId, lesson) in timetableData) { @@ -249,59 +189,12 @@ fun main() { val teacherName = teachers.filter { it.teacherId == teacherId }.first().name val groupId = courses.filter { it.id == courseId }.first().groupId val groupName = groups.filter { it.id.toInt() == groupId }.first().name - timetableOut.add(TimetableOut(day, lessonNum, roomId, roomNum, courseId, courseName, teacherName, groupName)) + timetableOut.add(TimetableOut(count ,day, lessonNum, roomNum, courseId, courseName, teacherName, groupName)) + count += 1 } } } } - println("$timetableOut - timetableOut") - timetable.forEach { println(it) } - - - //println(timetable) - - println("$groupCourseMap - group") - -// -// for (i in mapOfIntersection.keys.sortedBy { mapOfIntersection[it]!! }){ -// for(j in ) -// } -// -// println(mapOfIntersection) -// println(mapOfIntersection.keys.sortedBy { mapOfIntersection[it]!! }) -// - - println(availableTimeForCourse) - - - println(availableTimeForRoom) - -// Вывод карты с количеством пересечений для каждого курса -// for ((courseId, intersectionCount) in intersectionMap) { -// println("Курс $courseId: $intersectionCount пересечений") -// } - - -// val intersectionMap: MutableMap = mutableMapOf() -// -// for ((courseId, courseData) in courseMap) { -// for ((day, dayData) in daysMap) { -// for ((roomId, freeTimeList) in dayData) { -// val courseFreeTimeList = courseData[day] -// if (courseFreeTimeList != null) { -// val intersectionCount = freeTimeList.zip(courseFreeTimeList) -// .count { (d1, d2) -> d1 && d2 } -// intersectionMap[courseId] = intersectionMap.getOrDefault(courseId, 0) + intersectionCount -// } -// } -// } -// } -// -//// Вывод карты с количеством пересечений для каждого курса -// for ((courseId, intersectionCount) in intersectionMap) { -// println("Курс $courseId: $intersectionCount пересечений") -// } - - + timetableOut.forEach { println(it.roomNum) } } \ No newline at end of file