:- discontiguous atRobby/2, freeRobby/1, ordered/2.

:- [golog_swi, planners].

%  Initial State  

atRobby(room(1),s0).
ordered(room(1),s0).
ordered(room(2),s0).
ordered(room(3),s0).
ordered(room(4),s0).
freeRobby(s0).

% static predicates
connected(room(1),hallway).
connected(room(2),hallway).
connected(room(3),hallway).
connected(room(4),hallway).
connected(room(1),room(3)).
connected(room(2),room(4)).

teamachine(room(1)).
cupstack(room(2)).

%  Goal  

goal(S) :- not(ordered(room(1),S)), not(ordered(room(2),S)),
	not(ordered(room(3),S)), not(ordered(room(4),S)).


%  Action Precondition Axioms.

poss(go(X,Y),S) :- (connected(X,Y) ; connected(Y,X)),
	           atRobby(X,S).
poss(getcup(X),S) :- atRobby(X,S), cupstack(X), freeRobby(S).
poss(fillcup(X),S) :- atRobby(X,S), teamachine(X), emptycuploaded(S).
poss(deliver(X),S) :- atRobby(X,S), ordered(X,S), fullcuploaded(S).


%  Successor State Axioms.

atRobby(X,do(A,S)) :- A = go(_,X) ; (atRobby(X,S), \+ A = go(X,_)).
freeRobby(do(A,S)) :- A = deliver(_) ; (freeRobby(S), \+ A=getcup(_)).
emptycuploaded(do(A,S)) :-
	A = getcup(_) ; (emptycuploaded(S), \+ A=fillcup(_)).
fullcuploaded(do(A,S)) :-
	A = fillcup(_) ; (fullcuploaded(S), \+ A=deliver(_)).
ordered(X,do(A,S)) :-
	ordered(X,S), \+ A=deliver(X).

%  Primitive Action Declarations.

primitive_action(getcup(_)).
primitive_action(fillcup(_)).
primitive_action(deliver(_)).
primitive_action(go(_,_)).    


    %  Bad Situations for a Teacup Domain with 1 Robot 


% dont enter a room then exit it immediately */
badSituation(do(go(_,_),do(go(_,room(_)),_))) .

%take a cup as soon as possible, if there are rooms to be served
badSituation(do(go(X,_),S)) :-
	poss(getcup(X),S), ordered(_,S).

%deliver tea as soon as possible
badSituation(do(go(X,_),S)) :-
	poss(deliver(X),S).

