Skip to content

Commit

Permalink
Get data for more than one stage for the timetable generation (#157)
Browse files Browse the repository at this point in the history
* Added use more than one stage

* Update DatabaseHelper unit tests.

* Final fixes.
  • Loading branch information
karimosman authored and svenvanhal committed Jun 21, 2018
1 parent 8223964 commit 4d96d0a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Implementation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Implementation
{
class Program
{
public static int StageId = 2;
public static List<int> StageIds = new List<int>{{2}};

static void Main(string[] args)
{
Expand All @@ -24,7 +24,7 @@ static void Main(string[] args)
var task = new Program().Start();

// Attach handlers
task.ContinueWith(t => OnSuccess(t, academicYear, StageId, 0), TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(t => OnSuccess(t, academicYear, StageIds[0], 0), TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(OnCanceled, TaskContinuationOptions.OnlyOnCanceled);
task.ContinueWith(OnError, TaskContinuationOptions.OnlyOnFaulted);

Expand All @@ -35,7 +35,7 @@ static void Main(string[] args)
public Task<Timetable> Start()
{
// Create algorithm task
using (var model = new DataModel(StageId))
using (var model = new DataModel(StageIds))
using (var generator = new TimetableGenerator())
{
return generator.RunAlgorithm(new FetAlgorithm(), model);
Expand All @@ -45,7 +45,7 @@ public Task<Timetable> Start()

private static int GetAcademicYear()
{
using (var model = new DataModel(StageId))
using (var model = new DataModel())
{

// Get academic year id, section id and quarter id.
Expand Down
10 changes: 8 additions & 2 deletions Timetabling.Tests/Helper/DatabaseHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public void SaveTimetableTest()

using (var conn = Effort.DbConnectionFactory.CreateTransient())
{
var model = new DataModel(conn);
var model = new DataModel(conn)
{
StageIds = new List<int> { 1 }
};

using (var context = new DatabaseHelper(model))
{
Expand Down Expand Up @@ -87,7 +90,10 @@ public void CreateTimetableTest()
{
using (var conn = Effort.DbConnectionFactory.CreateTransient())
{
var model = new DataModel(conn);
var model = new DataModel(conn)
{
StageIds = new List<int> { 1 }
};

using (var context = new DatabaseHelperExposer(model))
{
Expand Down
2 changes: 1 addition & 1 deletion Timetabling.Tests/Objects/TestDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public TestDataModel()
MockSetStage.As<IQueryable<LookupStageModel>>().Setup(m => m.ElementType).Returns(DataStage.ElementType);
MockSetStage.As<IQueryable<LookupStageModel>>().Setup(m => m.GetEnumerator()).Returns(DataStage.GetEnumerator());

MockDataModel = new Mock<DataModel>(4);
MockDataModel = new Mock<DataModel>(new List<int> { { 4 } });
MockDataModel.Setup(item => item.ActitvityGroups).Returns(MockSetActivityGroup.Object);
MockDataModel.Setup(item => item.ClassesLookup).Returns(MockSetClass.Object);
MockDataModel.Setup(item => item.SubjectGrades).Returns(MockSetSubjectGrade.Object);
Expand Down
7 changes: 4 additions & 3 deletions Timetabling/DB/DataModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;

Expand All @@ -12,15 +13,15 @@ public class DataModel : DbContext
/// <summary>
/// The stage identifier.
/// </summary>
public int StageId;
public List<int> StageIds;

/// <inheritdoc />
/// <summary>
/// Constructs a new DataModel based on the connection string in app.config.
/// </summary>
public DataModel(int stageId) : base("name=DataModel")
public DataModel(List<int> stageIds) : base("name=DataModel")
{
StageId = stageId;
StageIds = stageIds;
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion Timetabling/DB/TimetableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TimetableModel
/// <summary>
/// Section ID.
/// </summary>
[Column("SectionId")]
[Column("sectionId")]
public int SectionId { get; set; }

/// <summary>
Expand Down
13 changes: 2 additions & 11 deletions Timetabling/Helper/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DatabaseHelper : IDisposable
public DataModel Model { get; set; }

/// <inheritdoc />
public DatabaseHelper() : this(new DataModel(0)) { }
public DatabaseHelper() : this(new DataModel()) { }

/// <summary>
/// Constructs a new DatabaseHelper on a DataModel.
Expand Down Expand Up @@ -78,7 +78,7 @@ protected int CreateTimetable(DataModel model, Timetable tt)
Name = $"{tt.AcademicYearId} - {tt.QuarterId} - {tt.SectionId}",
AcademicYearId = tt.AcademicYearId,
QuarterId = tt.QuarterId,
SectionId = model.StageId,
SectionId = model.StageIds?[0] ?? 0,
ConflictWeight = tt.ConflictWeight,
Conflicts = conflictText,
OutputDir = tt.OutputFolder
Expand Down Expand Up @@ -106,8 +106,6 @@ protected void CreateActivities(DataModel model, int ttId, Timetable tt)
foreach (var activity in tt.Activities)
{

Logger.Info("Processing activity " + activity.Id);

// Create new activity
var activityEntry = new TimetableActivityModel
{
Expand All @@ -133,7 +131,6 @@ protected void CreateActivities(DataModel model, int ttId, Timetable tt)
// Create activity - class relations
CreateActivityClassRelations(model, activityEntry.Id, activity.Resource);

Logger.Info("Done processing activity " + activity.Id + "\r\n");
}

}
Expand All @@ -147,8 +144,6 @@ protected void CreateActivities(DataModel model, int ttId, Timetable tt)
protected void CreateActivityTeacherRelations(DataModel model, int activityId, Activity activity)
{

Logger.Info("Creating activity / teacher relations.");

// Iterate over teachers in activity
foreach (var teacherId in activity.Teachers)
{
Expand Down Expand Up @@ -176,14 +171,10 @@ protected void CreateActivityTeacherRelations(DataModel model, int activityId, A
protected void CreateActivityClassRelations(DataModel model, int activityId, Activity activity)
{

Logger.Info("Creating activity / class relations.");

// Iterate over teachers in activity
foreach (var classEntry in activity.Students)
{

Logger.Info($"Creating new activity / class relation: {activityId} - {classEntry.Value}");

// Create new activity - teacher relation
model.TimetableActivityClasses.AddOrUpdate(new TimetableActivityClassModel
{
Expand Down
8 changes: 5 additions & 3 deletions Timetabling/Objects/ActivitiesList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ join s in dB.SubjectGrades on activity.SubjectId equals s.SubjectId
join t in dB.Employees on activity.TeacherId equals t.EmployeeId
join grade in dB.GradesLookup on c.GradeId equals grade.GradeId
join sub in dB.Subjects on activity.SubjectId equals sub.SubjectId
where s.GradeId == c.GradeId && t.IsActive == true && grade.StageId == dB.StageId && s.CollectionId != null
join stage in dB.StageIds on grade.StageId equals stage
where s.GradeId == c.GradeId && t.IsActive == true && s.CollectionId != null
group new { teacherId = activity.TeacherId, grade.GradeId, subjectId = activity.SubjectId, c.ClassName, ClassID = c.ClassId, NumberOfLlessonsPerWeek = s.NumberOfLessonsPerWeek, NumberOfLlessonsPerDay = s.NumberOfLessonsPerDay, CollectionID = s.CollectionId }
by new { s.CollectionId, s.GradeId, activity.ClassId } into g
select g;
Expand Down Expand Up @@ -98,7 +99,8 @@ join c in dB.ClassesLookup on activity.ClassId equals c.ClassId
join s in dB.SubjectGrades on activity.SubjectId equals s.SubjectId
join t in dB.Employees on activity.TeacherId equals t.EmployeeId
join grade in dB.GradesLookup on c.GradeId equals grade.GradeId
where c.GradeId == s.GradeId && grade.StageId == dB.StageId && s.CollectionId == null
join stage in dB.StageIds on grade.StageId equals stage
where c.GradeId == s.GradeId && s.CollectionId == null
select new { TeacherID = activity.TeacherId, GradeID = c.GradeId, SubjectID = activity.SubjectId, c.ClassName, ClassID = c.ClassId, NumberOfLlessonsPerWeek = s.NumberOfLessonsPerWeek, NumberOfLlessonsPerDay = s.NumberOfLessonsPerDay };

// Iterate over single activities
Expand Down Expand Up @@ -135,7 +137,7 @@ private void CollectionMerge()

foreach (var item in query)
{
var list = Activities.Values.Where(x => x.CollectionId != 0 && x.CollectionId.Equals(item) && x.IsCollection==true);
var list = Activities.Values.Where(x => x.CollectionId != 0 && x.CollectionId.Equals(item) && x.IsCollection == true);

// Groups the lessons by the number of lesson of the week.
var group = from a in list
Expand Down

0 comments on commit 4d96d0a

Please sign in to comment.