/* Environment
  -------------
   - intruder
   - yard
   - door		      
   - window	
   - house
   - alarm
 	
   Elements
  ---------
   - glass
   - smoke
   - heat
   - fire
   - water

   Actions 
  ---------
   - at
   - in 
   - opening
   - entering
   - breaking 	   
   - on 
   

   Variables
  -----------
   DI - Door Induction
   DV - Door Vibration
   WI - Window Induction
   WV - Window Vibration
   MI - Motion (Inside)
   MO - Motion (Outside)
   GB - Glass Break
   SM - Smoke
   HT - Heat
   FD - Flood */


/* ----------------------------------------- */
/* Seperate the elements of the input list */

start(Glist) :- get_var(Glist).

get_var([DI, DV, WI, WV, MI, MO, GB, SM, HT, FD|Rest]) :- get_v1(DI), 
     							  get_v2(DV),
							  get_v3(WI),
							  get_v4(WV),
							  get_v5(MI),
							  get_v6(MO),
							  get_v7(GB),
							  get_v8(SM),
							  get_v9(HT),
							  get_v10(FD).
get_v1(DI) :- member(0, [DI]);  
	      member(1, [DI]), act1(S1).

get_v2(DV) :- member(0, [DV]); 
	      member(1, [DV]), act2(S2).

get_v3(WI) :- member(0, [WI]); 
	      member(1, [WI]), act3(S3).

get_v4(WV) :- member(0, [WV]); 
	      member(1, [WV]), act4(S4).

get_v5(MI) :- member(0, [MI]); 
	      member(1, [MI]), act5(S5).    

get_v6(MO) :- member(0, [MO]); 
	      member(1, [MO]), act6(S6).

get_v7(GB) :- member(0, [GB]); 
	      member(1, [GB]), act7(S7).

get_v8(SM) :- member(0, [SM]); 
	      member(1, [SM]), act8(S8).

get_v9(HT) :- member(0, [HT]); 
	      member(1, [HT]), act9(S9).

get_v10(FD) :- member(0, [FD]); 
	       member(1, [FD]), act10(S10).


/* --------------------------------------- */
/* Initial Conditions */

not(at(intruder, door)).
not(at(intruder, window)).
not(in(intruder, yard)).
not(in(intruder, house)).

act1(S1) :- opening(intruder, door). 
act2(S2) :- at(intruder, door), entering(intruder, door). 
act3(S3) :- opening(intruder, window).
act4(S4) :- at(intruder, window), entering(intruder, window). 
act5(S5) :- in(intruder, house).
act6(S6) :- in(intruder, yard), activate(alarm). 
act7(S7) :- breaking(glass), entering(intruder, window).
act8(S8) :- in(smoke, house), in(fire, house), activate(alarm).
act9(s9) :- in(heat, house).
act10(s10) :- in(water, house), activate(alarm). 

opening(intruder, door).
opening(intruder, window).
at(intruder, door).
at(intruder, window).
in(intruder, house).
in(intruder, yard). 
breaking(glass).
in(smoke, house). 
in(heat, house).
in(water, house):- write(['House is flooded']), nl. 


/* ---------------------------------------- */
/* Actions */

:- dynamic at/2.
:- dynamic in/3. 
:- dynamic on/2. 

entering(intruder, door) :- at(intruder, door),
			    opening(intruder, door),
			    in(intruder, yard),
     			    /*retract(at(intruder, door)),
     			    assert(in(intruder, house)),*/
     	    		    write(['Intruder entering house through door']), nl.
			    

entering(intruder, window) :- at(intruder, window),
			      opening(intruder, window),
                              in(intruder, yard),
     			      /*retract(at(intruder, window)),
     			      assert(in(intruder, house)),*/
     	    		      write(['Intruder entering house through window']), nl.


activate(alarm) :- /*retract(not(on(alarm)),
		   assert(on(alarm)),*/
		   write(['Alarm activated']), nl. 		   

in(fire, house) :- in(smoke, house),
		   in(heat, house),
		   /*retract(not(on(alarm)),
		   assert(in(fire, house)),*/
		   write(['Fire in house']). 

deterred(intruder) :- not(in(intruder, house)),
		      /*retract(in(intruder, house)),*/
		      write(['Intruder deterred']), nl.

false(alarm) :- at(intruder, X),
		write(['False Alarm']). 
















		

