
#const n=4.      

%%%%%%%%%%%%%%%%%%%%  SCENARIO 6 %%%%%%%%%%%%%%%%%
%                                                %
%  John is in Paris. On Dec 1st he packs his     %
%  laptop in the carry-on luggage and takes a    %
%  plane to Baghdad.                             %
%  Was his laptop in Baghdad on Dec 2nd?         %
%                                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This is the first scenario in which we deal
% with time. To do that properly we need
% a Calendar - knowledge base containing
% information about various measures of
% time, e.g. days, months, seasons, etc.
% But since our goal here is to better
% understand the Travel module we ignore
% all these subtleties and simply use
% integers from 1 to 5 to count days.
% Relevant relations in Travel are
% duration(Action,NumOfDays).
% time(T,U,V) - a step T occurred at time V 
% measured in units U. In our case U=d (days).

%           THE TRANSLATION

% John is in Paris. 

    h(at(john,paris),0).

% On Dec 1st he packs his laptop in the carry-on luggage.

    o(pack(john,laptop(john),carry_on(john)),0).

    time(0,d,1).           % Step 0 corresponds to day 1.

% John goes to Baghdad:

    h(trip_by(j(paris,baghdad),plane),1).
    o(go_on(john,j(paris,baghdad)),1).
    time(1,d,1).

% Query: 
% In the previous examples we queried our knolwedge
% base about the values of fluents at the last
% step n of the scenario. 
% The question asked here is of the form "Was fluent
% Fl true at a given time D?". 
% Since we are measuring time in days I reformulate
% this question as "Was Fl true at some point
% during D?".

% To answer this question we need to map steps of 
% the scenario into days. Note that two different steps can be
% mapped into the same day. Step 1 at which John 
% leaves Paris is mapped into Dec 1.
% Step 4 at which he arrives to Baghdad can be 
% associated with the first or second of Dec
% depending on the trip's duration. In the first
% case both steps, 1 and 4, are mapped into Dec 1. 


% The mapping is performed by a module called AboutTime.
% I am just going to copy it here:


%%%%%%%%  AboutTime

1{time(T,d,X) : day(X)}1 :- T < n.

:- time(T1,d,Day1),
   time(T2,d,Day2), 
   T1 < T2, 
   Day2 < Day1.

:- time(T,d,Day1),
   time(T,d,Day2),
   neq(Day1,Day2).

% The following actions are performed
% during a single day.

time(T+1,d,Day) :- 
         o(pack(P,PP,Container),T),
         time(T,d,Day).

time(T+1,d,Day) :- 
         o(embark(P,J),T),
         time(T,d,Day).

time(T+1,d,Day) :- 
         o(stop(J,C),T),
         time(T,d,Day).

time(T+1,d,Day) :- 
         o(disembark(P,J),T),
         time(T,d,Day).

% The new relation true_on(Fl,Day) states that fluent Fl was true 
% sometime during the day Day. Similarly for false_on(Fl,Day).

true_on(Fl,Day) :- 
      time(T,d,Day),
      h(Fl,T).

false_on(Fl,Day) :- 
      time(T,d,Day),
      -h(Fl,T).

% Of course similar relations can be defined for occurrences of actions.


% The NLP translation of the query may look as follows:

answer_true(q) :- 
    true_on(at(laptop(john),baghdad),2).

answer_false(q) :- 
		false_on(at(laptop(john),baghdad),2),
                not answer_true(q).

% Note that the last line is needed since the laptop can 
% be in Paris and in Baghdad at the same day.

type_query(q,boolean).

% Of course this approach will work for an arbitrary fluent
% (or action occurrence)  and arbitrary collection of time
% atoms encoding dates and other time measures. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% As before we'll run the program together with
% the rules and directives below.

       yes :- 
           type_query(Q,boolean),
           answer_true(Q),
           not answer_false(Q).

       no :- 
           type_query(Q,boolean),
           answer_false(Q),
           not answer_true(Q).

       maybe :- 
           type_query(Q,boolean),
           not yes, 
           not no.

hide.

show yes,no,maybe.

% If you run the program you will get two answer sets.
% In the first one all travel is done in one day,
% the second requires two days.
% In the first case John arrives in Baghdad on the 
% first, the program has no information about his 
% whereabouts on the second. Hence the first answer set
% contains the answer "maybe".
% The second answer set assumes that John arrives in
% Baghdad on the second. This time the answer is "yes".

% To see what happens you can uncomment
% show  answer_true(X),answer_false(X),do(A,B),time(A,B,C).

% Note that if we assume that, in the absence of recorded 
% actions, an object stays in one place for at least a day, 
% and write

true_on(at(O,C),Day+1) :- 
      h(at(O,C),n),
      time(n,d,Day).


false_on(at(O,C),Day+1) :- 
      -h(at(O,C),n),
      time(n,d,Day).

