-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint.pl
More file actions
125 lines (111 loc) · 4.08 KB
/
print.pl
File metadata and controls
125 lines (111 loc) · 4.08 KB
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
121
122
123
124
125
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Project Declarative Programming %
% Jens Nevens %
% %
% pretty_print(+S) %
% pretty_print(+SID, +S) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- module(print, [pretty_print/1,
pretty_print/2]).
:- use_module(small_instance).
%:- use_module(large_short_instance).
%:- use_module(large_long_instance).
:- use_module(utils).
% pretty_print(+SID, +Schedule)
% +SID: Student ID
% +Schedule: schedule/1 functor that contains a list
% of event/4 functors.
%
% The events in the given schedule are filtered by
% filter_events/3, based on the given SID. The
% resulting list is passed to print_day/1, via findall,
% so all alternatives are tried.
pretty_print(PID, schedule(Events)) :-
filter_events(Events, PID, FilteredEvents),
findall(_, print_day(FilteredEvents), _).
% filter_events(+Events, +SID, -FilteredEvents)
% +Events: List of event/4 functors
% +SID: Student ID
% -FilteredEvents: +Events filtered by +SID
%
% Loops through the list of given events and add all
% elements of Events to FilteredEvents when the student
% with SID follows the course associated with Event.
filter_events(Events, PID, Filtered) :-
filter_events_acc(Events, PID, [], Filtered).
filter_events_acc([], _, Filtered, Filtered).
filter_events_acc([event(EID,RID,Day,Start)|RestEvents], PID, Filtered0, Filtered) :-
student(PID,_),
has_exam(CID,EID),
findall(SID, follows(SID,CID), Students),
member(PID, Students),
append([event(EID,RID,Day,Start)], Filtered0, NewFiltered),
!,
filter_events_acc(RestEvents, PID, NewFiltered, Filtered).
filter_events_acc([event(EID,RID,Day,Start)|RestEvents], PID, Filtered0, Filtered) :-
lecturer(PID,_),
has_exam(CID,EID),
teaches(PID,CID),
append([event(EID,RID,Day,Start)], Filtered0, NewFiltered),
!,
filter_events_acc(RestEvents, PID, NewFiltered, Filtered).
filter_events_acc([_|RestEvents], PID, Filtered0, Filtered) :-
filter_events_acc(RestEvents, PID, Filtered0, Filtered).
% pretty_print(+Schedule)
% +Schedule: schedule/1 functor that contains a list of
% event/4 functors.
%
% The events are passed to print_day/1
% via findall, so all alternatives are tried. This means
% that the events are grouped together based on all possible
% values for Days.
pretty_print(schedule(Events)) :-
findall(_, print_day(Events), _).
% print_day(+Events)
% +Events: List of event/4 functors
%
% The setof groups the given events together
% based on Day. RID and Start are not bound in the member-predicate,
% but are bound in the template. The Day is printed to output
% and the resulting list is passed to print_event/1. Since this is
% done via findall, all alternatives are tried, i.e. for each day,
% the resulting list of events is grouped on all possible rooms
% that still occur in the list.
print_day(Events) :-
setof(event(EID,RID,_,Start), RID^Start^member(event(EID,RID,Day,Start),Events), Result),
format("~n ~n*** DAY ~d *** ~n ~n", Day),
findall(_, print_event(Result), _).
% print_event(+Events)
% +Events: List of event/4 functors
%
% The setof groups the given events together
% based on RID. Start is not bound in the member-predicate,
% but is bound in the template. Afterwards, the name of
% the room is printed to output, the resulting list is
% sorted based on Start-hour and passed on to print/1.
print_event(Events) :-
setof(event(EID,_,Day,Start), Start^member(event(EID,RID,Day,Start),Events), Result),
room(RID, RoomName),
format("~a:~n", RoomName),
sort_functors_asc(4, Result, Sorted),
print(Sorted).
% print(+Events)
% +Events: List of event/4 functors
%
% Loops through the list of given events
% gets extra information about the event, such as
% exam name, lecturer name, ..., and prints this
% information to output using format.
print([]).
print([event(EID,_,_,Start)|RestEvents]) :-
exam(EID,ExamName),
duration(EID,Duration),
End is Start + Duration,
has_exam(CID,EID),
teaches(LID,CID),
lecturer(LID, LecturerName),
format("~d:00 - ", Start),
format("~d:00: ", End),
format("~a ", ExamName),
format("(~a).~n", LecturerName),
print(RestEvents).