private bool IsValid(OfferedCourse offeredCourse, ReductionStepAction action, OfferedCourseRow offeredCourseRow = null) { if (action == ReductionStepAction.WHITE_ONE_RED_ROW) { return true; } else if (action == ReductionStepAction.WHITE_ALL_COURSES_ROWS) { return true; } else if (action == ReductionStepAction.RED_ALL_COURSES_ROWS) { if (offeredCourse.Color == ReductionStep2ColorStatus.Green) { return false; } else if (offeredCourse.Color == ReductionStep2ColorStatus.WHITE) { return true; } else throw new Exception(); } else if (action == ReductionStepAction.UNCHECK_COURSE_MUST_BE_TAKE) { List<int> lst = new List<int>(); foreach (var o in greenCourses) if (o != offeredCourse) lst.Add(o.Course.Id); //is valid state must be checked return MainCurriculumSateValidator.IsValidState(mainCurriculum, lst); } else if (action == ReductionStepAction.RED_ONE_WHITE_ROW) { if (offeredCourse.Color == ReductionStep2ColorStatus.WHITE) { return true; } else if (offeredCourse.Color == ReductionStep2ColorStatus.Green) { //temorary change the actual current value offeredCourseRow.Color = ReductionStep2ColorStatus.RED; List<Box> boxes = new List<Box> { ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(offeredCourse) }; //restore actual value offeredCourseRow.Color = ReductionStep2ColorStatus.WHITE; foreach (var item in greenCourses) { if (item != offeredCourse) boxes.Add(ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(item)); } List<Box> res = null; var task = Task.Run(() => ReductionStep2ServiceProvider.Validate(boxes, exampCollideChecking)); if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs))) res = task.Result; else { res = null; } if (res == null) return false; return true; } else throw new Exception(); } else if (action == ReductionStepAction.CHECK_COURSE_MUST_BE_TAKE) { if (offeredCourse.Course.Units + currentMustUnits > maxUnits) { return false; } List<int> lst = new List<int>(); foreach (var o in greenCourses) lst.Add(o.Course.Id); lst.Add(offeredCourse.Course.Id); //is valid state must be checked bool output = MainCurriculumSateValidator.IsValidState(mainCurriculum, lst); if (!output) return false; output = false; for (int a = 0; a < offeredCourse.OfferedCourseRows.Count; a++) { if (offeredCourse.OfferedCourseRows[a].Color == ReductionStep2ColorStatus.WHITE) { output = true; break; } } if (!output) return false; Box b1 = ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(offeredCourse); List<Box> boxes = new List<Box> { b1 }; foreach (var item in greenCourses) { boxes.Add(ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(item)); } List<Box> res = null; var task = Task.Run(() => ReductionStep2ServiceProvider.Validate(boxes, exampCollideChecking)); if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs))) res = task.Result; else { res = null; } if (res == null) return false; return true; } throw new Exception(); //return false; }
public async Task RunAfterCreatedAsync() { try { await Task.Run(async () => { await Dispatcher.InvokeAsync(() => { InitializeComponent(); }); //settings if (Properties.Settings.Default.CurrentTermNumber > 0) termNumber = Properties.Settings.Default.CurrentTermNumber; gender = Properties.Settings.Default.Gender == 'm' ? Tataiee.Harif.Infrastructures.GeneralEnums.Gender.MALE : Tataiee.Harif.Infrastructures.GeneralEnums.Gender.FEMALE; if (Properties.Settings.Default.MinUnits > 0) minUnits = Properties.Settings.Default.MinUnits; if (Properties.Settings.Default.MinUnits <= Properties.Settings.Default.MaxUnits && Properties.Settings.Default.MaxUnits > 0) maxUnits = Properties.Settings.Default.MaxUnits; if (Properties.Settings.Default.TimeoutForReductionStep2ms >= 120) timeoutMs = Properties.Settings.Default.TimeoutForReductionStep2ms; if (Properties.Settings.Default.MaxNumberOfSuggestedWeeklyProgram >= 1 && Properties.Settings.Default.MaxNumberOfSuggestedWeeklyProgram <= 50) maxCntSuggestion = Properties.Settings.Default.MaxNumberOfSuggestedWeeklyProgram; capacityFiltering = Properties.Settings.Default.CapacityFiltering; exampCollideChecking = Properties.Settings.Default.ExamCollideChecking; //end settings mainCurriculum = StudentHistoryServiceProvider.CreateNewCurriculmWithSpecificCreditAndFilledBySpecificCourseInforamtion(DirectoryManager.EssentialInforamtion1Directory + DirectoryManager.CreditDeterminerSavedName, DirectoryManager.EssentialInforamtion1Directory + DirectoryManager.CourseInformationSavedName); int CODE_LEN = 7; //load offered course row in memory FileServiceProvider.DeserializeFromXmlFile(DirectoryManager.GoalVersionOfferedCoursesRowDirectory + DirectoryManager.GoalVersionOfferedCoursesRowSavedName, out List<GoalVersionOfferedCoursesRow> goalVersionOfferedCourseRows); //perform reduction step 1 and get courses list after reduced var coursesListAfterReductionStep1 = ReductionStep1.Reduce(DirectoryManager.EssentialInforamtion1Directory + DirectoryManager.CreditDeterminerSavedName, DirectoryManager.EssentialInforamtion1Directory + DirectoryManager.CourseInformationSavedName , termNumber); //assign offeredCourseRows to the output of ReductionStep2 courses List if (capacityFiltering) { goalVersionOfferedCourseRows = ReductionStep2.Reduce(coursesListAfterReductionStep1, goalVersionOfferedCourseRows, gender) .Where(r => r.Capacity - r.Registered > 0).ToList(); } else { goalVersionOfferedCourseRows = ReductionStep2.Reduce(coursesListAfterReductionStep1, goalVersionOfferedCourseRows, gender); } CODE_LEN = coursesListAfterReductionStep1[0].CodeInDesUni.Length; //again group reduced offered course rows var coursesGroups = (from c in goalVersionOfferedCourseRows group c by c.Id.Substring(0, CODE_LEN)).ToList(); //-------- //we can add another reduction step to reduce more if possible but not now. //--------- //fill main offeredCourses and offeredCourseRows lists int i = 0; int j = 0; foreach (var course in coursesGroups) { var c = coursesListAfterReductionStep1.FirstOrDefault(c1 => c1.CodeInDesUni == course.Key); if (c != null) { OfferedCourse oc = new OfferedCourse(c, i++, ReductionStep2ColorStatus.WHITE); foreach (var row in course) { OfferedCourseRow ocr = new OfferedCourseRow(row, ReductionStep2ColorStatus.WHITE, j++, oc); oc.OfferedCourseRows.Add(ocr); cards.Add(null); offeredCourseRows.Add(ocr); } offeredCourses.Add(oc); } } if (Properties.Settings.Default.ReductionStep2MustBeLoad) { FileServiceProvider.DeserializeFromXmlFile(DirectoryManager.ReductionStep2SavedStatus + DirectoryManager.SaveColorOfferedCourses, out List<SaveColorObjectModel> saveColorOfferedCourses); FileServiceProvider.DeserializeFromXmlFile(DirectoryManager.ReductionStep2SavedStatus + DirectoryManager.SaveColorOfferedCourseRows, out List<SaveColorObjectModel> saveColorOfferedCourseRows); saveColorOfferedCourses.ForEach(scoc => { offeredCourses[scoc.CorrespondingIdInSourceList].Color = scoc.Color; }); saveColorOfferedCourseRows.ForEach(scocr => { offeredCourseRows[scocr.CorrespondingIdInSourceList].Color = scocr.Color; }); } //create and fill courses navigation buttons in the right side await Dispatcher.InvokeAsync(() => { stackPanelRightSide.Children.Clear(); Button button1 = null; for (int m = 0; m < offeredCourses.Count; m++) { Badged badged = new Badged { Badge = offeredCourses[m].OfferedCourseCode, Margin = new Thickness(5), Name = "bdg" + offeredCourses[m].CourseId }; Button button = new Button { Content = new TextBlock() { Text = offeredCourses[m].OfferedCourseName, TextAlignment = TextAlignment.Center, TextWrapping = TextWrapping.Wrap }, Name = "btn" + m.ToString(), Width = 250, Height = Double.NaN, Padding = new Thickness(5), Background = Brushes.Transparent }; button.Click += btnCourseNavigation_Click; badged.Content = button; if (m == 0) { button1 = button; } stackPanelRightSide.Children.Add(badged); } selectedButton = button1; allowUserInteraction = true; btnCourseNavigation_Click(button1, null); }); }); } catch { } }
private Task<bool> IsValidAsync(OfferedCourse offeredCourse, ReductionStepAction action, OfferedCourseRow offeredCourseRow = null) { return Task.Run<bool>(() => IsValid(offeredCourse, action, offeredCourseRow)); }