:- consult(library(clpfd)).

tasks([      % Start, Duration, End, Consumption, Identifier
         task(1, 1, _, 2, "Mathematische Grundlagen 1"),
         task(2, 1, _, 2, "Mathematische Grundlagen 2"),
         task(2, 1, _, 4, "Logik"),
         task(1, 1, _, 4, "Linguistische Grundlagen"),
         task(ProgCLPython, 1, _, 4, "CL-Prog. mit Python"),
         task(2, 1, _, 4, "AP Prop"),
         task(1, 1, _, 2, "Phonetik"),
         task(1, 1, _, 2, "Morphologie"),
         task(1, 1, _, 2, "Semantik"),
         task(2, 1, _, 2, "Phonologie"),
         task(2, 1, _, 2, "Syntax"),
         task(2, 1, _, 2, "Pragmatik"),
         task(2, 1, _, 4, "AP Linguistik 2 (MoSy)"),
         task(2, 1, _, 4, "AP Linguistik 3 (SemPra)"),
         task(3, 1, _, 10, "Programmierung (Info 1)"),
         task(3, 1, _, 0, "AP Programmierung"),
         task(2, 1, _, 8, "CL-Einführung"),
         task(GramFor, 1, _, 4, gramFor),
         task(2, 1, _, 0, "AP CL1"),
         task(MaschUeb, 1, _, 4, maschUeb),
         task(Corpus, 1, _, 2, "Corpuslinguistik"),
         task(CLProgR, 1, _, 4, "CL-Prog. mit R"),
         task(APCL2, 1, _, 4, "AP CL2"),
         task(FormSpr, 1, _, 2, "Formale Sprachen"),
         task(ThemSem1, 1, _, 4, "Themenseminar 1"),
         task(APCL3, 1, _, 4, "AP CL3"),
         task(CLProgLogFunk, 1, _, 4, "CL-Prog. logisch/funktional"),
         task(CompSem, 1, _, 4, "Computational Semantics"),
         task(ThemSem2, 1, _, 4, "Themenseminar 2"),
         task(APCL4, 1, _, 4, "AP CL4"),
         task(Parsing, 1, _, 2, "Parsing"),
         task(ThemSem3, 1, _, 4, "Themenseminar 3"),
         task(APCL5, 1, _, 4, "AP CL5"),
         task(VertiefungInf, 1, _, 10, "Vertiefung Informatik"),
         task(Frei5, 1, _, 5, "Vertiefung frei (5 CP)"),
         task(Frei6, 1, _, 6, "Vertiefung frei (6 CP)"),
         task(Frei4, 1, _, 4, "Vertiefung frei (4 CP)"),
         task(Frei2, 1, _, 2, "Vertiefung frei (2 CP)"),
         task(APCL6, 1, _, 4, "AP CL6"),
         task(PrakProj, 1, _, 7, "Praktisches Projekt"),
         task(Wahl4, 1, _, 4, "Wahlbereich (4 CP)"),
         task(Wahl6, 1, _, 6, "Wahlbereich (6 CP)"),
         task(Wahl8, 1, _, 8, "Wahlbereich (8 CP)"),
         task(BA, 1, _, 12, "Bachelorarbeit")],
     Starts) :-
    % CPFrei is 180-2-2-4-4-4-2-2-2-2-2-2-10-8-4-4-2-4-2-4-4-4-4-2-4-10-7-18-12,
    Starts = [ProgCLPython, GramFor, MaschUeb, Corpus, CLProgR,
          FormSpr, ThemSem1, CLProgLogFunk, CompSem, ThemSem2,
          Parsing, ThemSem3, VertiefungInf, Wahl8, Wahl6, Wahl4,
          PrakProj, BA, APCL2,APCL3,APCL4,APCL5,APCL6,
          Frei2, Frei4, Frei5, Frei6],
    ThemSem1 #>= FormSpr,
    ThemSem2 #>= CLProgLogFunk,
    ThemSem2 #>= CompSem,
    ThemSem3 #>= Parsing,
    % ProgCLPython #>= 3, % Programmierung
    CLProgR #>= 3,
    CLProgLogFunk #>= 3,
    VertiefungInf #>= 3,
    maplist(#>=(BA), Starts),
    maplist(#>=(APCL2), [MaschUeb,Corpus,CLProgR]),
    APCL2 #= MaschUeb #\/ APCL2 #= Corpus #\/ APCL2 #= CLProgR,
    maplist(#>=(APCL3), [FormSpr,ThemSem1]),
    APCL3 #= FormSpr #\/ APCL3 #= ThemSem1,
    maplist(#>=(APCL4), [CLProgLogFunk, CompSem, ThemSem2]),
    APCL4 #= CLProgLogFunk #\/ APCL4 #= CompSem #\/ APCL4 #= ThemSem2,
    maplist(#>=(APCL5), [Parsing, ThemSem3]),
    APCL5 #= Parsing #\/ APCL5 #= ThemSem3,
    maplist(#>=(APCL6), [VertiefungInf,Frei2,Frei4,
             Frei6, Frei5]),
    APCL6 #= VertiefungInf #\/ APCL6 #= Frei2 #\/ APCL6 #= Frei4
    #\/ APCL6 #= Frei6 #\/ APCL6 #= Frei5,
    1 #>= abs(Corpus - CLProgR),
    1 #>= abs(MaschUeb - CLProgR),
    1 #>= abs(2 - GramFor),
    1 #>= abs(FormSpr - ThemSem1),
    1 #>= abs(Parsing - ThemSem3),
    1 #>= abs(CLProgLogFunk - CompSem),
    1 #>= abs(CompSem - ThemSem2),
    1 #>= abs(CLProgLogFunk - ThemSem2),
    1 #>= abs(VertiefungInf - Frei2),
    1 #>= abs(VertiefungInf - Frei4),
    1 #>= abs(VertiefungInf - Frei5),
    1 #>= abs(VertiefungInf - Frei6),
    1 #>= abs(Frei2 - Frei4),
    1 #>= abs(Frei2 - Frei5),
    1 #>= abs(Frei2 - Frei6),
    1 #>= abs(Frei4 - Frei5),
    1 #>= abs(Frei4 - Frei6),
    1 #>= abs(Frei5 - Frei6).

sched_courses :-
    tasks(Tasks, Starts),
    Starts ins 1..6,
    cumulative(Tasks,[limit(32)]),
    label(Starts),
    printsem(1,Tasks),
    printsem(2,Tasks),
    printsem(3,Tasks),
    printsem(4,Tasks),
    printsem(5,Tasks),
    printsem(6,Tasks).

semester(Sem,task(Sem,_,_,_,_)).
getcp(task(_,_,_,CP,_),CP).
printsem(Sem,Tasks) :-
    include(semester(Sem), Tasks, TasksS),
    write("Semester "), write(Sem), write(": "),
    maplist(getcp,TasksS,CPs),
    sum(CPs, #=, CP),
    writeln(CP),
    maplist(writetask,TasksS).
writetask(task(_,_,_,_,T)) :- write("- "), writeln(T).
